-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathcheck_pid.sh
executable file
·96 lines (81 loc) · 1.68 KB
/
check_pid.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
#!/usr/bin/env bash
# Author: Jon Schipp
# Date: 04-15-2014
########
# Examples:
# 1.) Check if sshd has restarted since last check
# $ ./check_pid.sh -f /var/run/sshd.pid
# Nagios Exit Codes
OK=0
WARNING=1
CRITICAL=2
UNKNOWN=3
usage()
{
cat <<EOF
Check if a service has been restarted by comparing its PID to each previous run.
Options:
-f Specify PID filename as full path
-o Temporary PID file storage directory (def: /tmp)
Usage: $0 -f /var/run/sshd.pid -o /tmp/pids/
EOF
}
if [ $# -lt 1 ];
then
usage
exit 1
fi
# Define now to prevent expected number errors
DIR=/tmp
CHECK=0
COUNT=0
while getopts "hf:o:" OPTION
do
case $OPTION in
h)
usage
;;
f)
FILEPATH="$OPTARG"
CHECK=1
;;
o)
DIR="$OPTARG"
;;
\?)
exit 1
;;
esac
done
FILE=$(echo $FILEPATH | sed 's/^.*\///')
if [ ! -f $FILEPATH ]; then
echo "File doesn't exist or is not a regular file!"
exit $UNKNOWN
fi
if [ $CHECK -eq 1 ]; then
if [ ! -f $DIR/$FILE ]; then
echo "First run for PID or can't access file in temporary storage location: $DIR/$FILE"
cp -f $FILEPATH $DIR
exit $UNKNOWN
fi
# In case the PID is temporarily locked by another program
until [ ! -z $NEWPID ] && [ ! -z $OLDPID ];
do
NEWPID=$(cat $FILEPATH)
OLDPID=$(cat $DIR/$FILE)
COUNT=$((COUNT+1))
if [ $COUNT -ge 10 ]
then
break
fi
done
if [ $NEWPID -eq $OLDPID ]; then
echo "Service is still running with the same PID: $(echo $NEWPID)"
cp -f $FILEPATH $DIR
exit $OK
else
echo "Service restarted. OLDPID: $(echo $OLDPID) NEWPID: $(echo $NEWPID)"
cp -f $FILEPATH $DIR
exit $CRITICAL
fi
fi