-
Notifications
You must be signed in to change notification settings - Fork 118
/
check_status_code.sh
executable file
·109 lines (91 loc) · 2.12 KB
/
check_status_code.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
105
106
107
108
109
#!/usr/bin/env bash
# Author: Jon Schipp
########
# Examples:
# 1.) Check status code for uptime using the defaults
# $ ./check_status_code.sh -r /usr/bin/uptime
#
# 2.) Custom service does it backwards and exits 1 when running and 0 when stopped.
# $ ./check_status_code.sh -r "/usr/sbin/service custom-server status" -o 1 -c 0
# Nagios Exit Codes
NAGIOS_OK=0
NAGIOS_WARNING=1
NAGIOS_CRITICAL=2
NAGIOS_UNKNOWN=3
# Mutable Nagios Exit Codes
OK=0
WARNING=1
CRITICAL=2
UNKNOWN=3
usage()
{
cat <<EOF
Checks the status, or exit, code of another program and returns a
Nagios status code based on the result.
Options:
-r <cmd> Absolute path of program to run, use quotes for options
-o <int> Status to expect for OK state (def: 0)
-w <int> Status to expect for WARNING state (def: 1)
-c <int> Status to expect for CRITICAL state (def: 2)
-u <int> Status to expect for UNKNOWN state (def: 3)
Usage: $0 -r "/usr/sbin/service sshd status"
EOF
}
argcheck() {
# if less than n argument
if [ $ARGC -lt $1 ]; then
echo "Missing arguments! Use \`\`-h'' for help."
exit 1
fi
}
ARGC=$#
argcheck 1
while getopts "hc:o:r:w:u:" OPTION
do
case $OPTION in
h)
usage
exit 0
;;
r) if ! [ -z $RUN ]; then
echo "Error: Argument \`\`-r'' is required''."
exit 1
else
RUN="$OPTARG"
fi
;;
o)
OK=$OPTARG
;;
w)
WARNING=$OPTARG
;;
c)
CRITICAL=$OPTARG
;;
u)
UNKNOWN=$OPTARG
;;
\?)
exit 1
;;
esac
done
COMMAND=$(echo $RUN | sed 's/ .*//')
if ! [ -x $COMMAND ]; then
echo "Error: $COMMAND does not exist, is not an absolute path, or is not executable."
exit 1
fi
$RUN
CODE=$?
if [ $CODE -eq $OK ]; then
exit $NAGIOS_OK
elif [ $CODE -eq $WARNING ]; then
exit $NAGIOS_WARNING
elif [ $CODE -eq $CRITICAL ]; then
exit $CRITICAL
elif [ $CODE -eq $UNKNOWN ]; then
exit $NAGIOS_UNKNOWN
else
echo "Exit code not understood: $CODE"
fi