-
Notifications
You must be signed in to change notification settings - Fork 11
/
check_php_sessions.sh
executable file
·104 lines (90 loc) · 2.52 KB
/
check_php_sessions.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/bash
CACHEFILE=/var/tmp/nagios/check_php_sessions
ERRFILE=/var/tmp/nagios/check_php_sessions.err
CACHE=1 # days
NAGIOS_USER=${SUDO_USER:-$(whoami)}
if ! [ -d "$(dirname "$CACHEFILE")" ]; then
install -g "$NAGIOS_USER" -o "$NAGIOS_USER" -m 750 -d "$(dirname "$CACHEFILE")"
fi
if [ -d /usr/local/ispconfig ] ; then
THRESHOLD=30000
else
THRESHOLD=10000
fi
AGE=16
LIST=false
EXCLUDES="/var/cache /var/lib /usr/share /lost+found /proc /sys /dev /run"
while getopts "e:n:a:Lf" option; do
case $option in
e)
EXCLUDES="${EXCLUDES} ${OPTARG}"
;;
n)
THRESHOLD=${OPTARG}
;;
a)
AGE=${OPTARG}
;;
L)
LIST=true
;;
f)
rm -f "$CACHEFILE"
;;
*)
esac
done
if [ -f "$CACHEFILE" ] && [ ! -O "$CACHEFILE" ]; then
echo "UNKNOWN: $CACHEFILE is not owned by $USER"
exit 3
fi
if [ -f "$ERRFILE" ] && [ ! -O "$ERRFILE" ]; then
echo "UNKNOWN: $ERRFILE is not owned by $USER"
exit 3
fi
FIND_EXCLUDES=""
for EXCLUDE in ${EXCLUDES}; do
FIND_EXCLUDES="${FIND_EXCLUDES} -path ${EXCLUDE} -prune -o"
done
FIND_OPTS="-regextype posix-egrep -regex '.*/(ci_session|sess_).*' -ctime +${AGE} -print"
if [ -z "$(find $CACHEFILE -mtime -${CACHE} -print)" ]; then
if ! LC_ALL=C eval "nice -n 10 find / ${FIND_EXCLUDES} ${FIND_OPTS}" > $CACHEFILE 2>$ERRFILE; then
if grep -v 'No such device' "$ERRFILE"; then
rm -f $CACHEFILE
echo "UNKNOWN: error during find"
exit 3
fi
fi
if [ -d /var/lib/php/sessions ]; then
if ! LC_ALL=C eval "nice -n 10 find /var/lib/php/sessions ${FIND_OPTS}" >> $CACHEFILE 2>$ERRFILE; then
if grep -v 'No such device' "$ERRFILE"; then
rm $CACHEFILE
echo "UNKNOWN: error during second find"
exit 3
fi
fi
fi
else
if grep -q '^/' $CACHEFILE; then
# shellcheck disable=SC2013
files=$(cat $CACHEFILE | xargs ls -d 2>/dev/null)
if [ -n "$files" ]; then
echo -e "$files" > $CACHEFILE
else
truncate -s 0 $CACHEFILE
fi
fi
fi
if $LIST; then
cat $CACHEFILE
else
total=$(grep -c '^/' $CACHEFILE) # grep avoids to count empty lines
msg="$total PHP old session files found | total=$total;;;;;"
if [ "$total" -lt "${THRESHOLD}" ] ; then
echo "OK: $msg"
exit 0
else
echo "WARNING: $msg"
exit 1
fi
fi