-
Notifications
You must be signed in to change notification settings - Fork 9
/
check_service_status.sh
65 lines (59 loc) · 1.6 KB
/
check_service_status.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
#!/bin/bash
#
# Nagios plugin check_service_status
#
# Version: 1.0, released on 01/11/2013, tested on Debian GNU/Linux 7.0 (wheezy).
#
# This plugin checks status of /etc/init.d scripts.
#
# Copyright (c) 2013 by Rutger Spiertz <[email protected]> for Kumina bv.
#
# This work is licensed under the Creative Commons Attribution-Share Alike 3.0
# Unported license. In short: you are free to share and to make derivatives of
# this work under the conditions that you appropriately attribute it, and that
# you only distribute it under the same, similar or a compatible license. Any
# of the above conditions can be waived if you get permission from the copyright
# holder.
# Default exit codes.
OK=0;
WARNING=1;
CRITICAL=2;
UNKNOWN=3;
# Not enough arguments makes no sense.
if [ $# -lt 1 ]; then
echo "No service to check."
exit $UNKNOWN
fi
if [ -x "/usr/sbin/service" ]; then
# Use 'service' command, as it's forward-compatible with other init systems
CHECK_CMD="/usr/sbin/service ${1}"
elif [ -x "/etc/init.d/$1" ]; then
# Fail-back if there is no 'service' command.
# Check if script exists and is executable.
CHECK_CMD="/etc/init.d/${1}"
else
echo "Service does not exist or its initscript is not executable."
exit $CRITICAL
fi
# Not being root makes no sense.
if ([ `id -u` != "0" ]); then
echo "We're a non-privileged user. Not all scripts support that."
exit $UNKNOWN
fi
# Ask script for status.
RETTEXT=`${CHECK_CMD} status 2>&1`
RETVAL=$?
echo $RETTEXT
# Act on certain return values (LSB).
case $RETVAL in
0)
exit $OK
;;
1|2|3)
exit $CRITICAL
;;
*)
echo "Service $1 is unknown."
exit $UNKNOWN
;;
esac