-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_systemctl
executable file
·153 lines (126 loc) · 3.53 KB
/
check_systemctl
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/bin/bash
# 1.0.1: Removed double quote in service status, double quote might be bad interpreted by scripts such as custom notification script.
# 1.0: Initial script
SCRIPT_VERSION=1.0.1
MAINTAINER_NAME="Mickael Ange"
SCRIPT_HOSTNAME=$(hostname)
SCRIPT_DIR=$(pwd)
WHOAREU=$(whoami)
SCRIPT_NAME=$0
PARAM=$@
VERSION_TO_DISPLAY="Script Version: $SCRIPT_VERSION\nMaintainer: $MAINTAINER_NAME <$MAINTAINER_EMAIL_ADDRESS>"
GET_VERSION=false
RUNNING_STATUS="active"
STOPPED_STATUS="inactive"
FAILED_STATUS="failed"
DEFAULT_EXPECTED_STATUS=$RUNNING_STATUS
# Nagios Exit Codes
EXIT_OK=0
EXIT_WARNING=1
EXIT_CRITICAL=2
EXIT_UNKNOWN=3
f_display_usage() {
cat << EOF
Check service status with systemctl.
USAGE: $0 -s <service_name> [OPTIONS]
OPTIONS:
-e | --expected-status
The expected service status: $RUNNING_STATUS | $STOPPED_STATUS.
Defaults: $DEFAULT_EXPECTED_STATUS
-h | --help
Display this help.
-s | --service
The name of the service to check, e.g. nginx or nginx.service.
-v | --version
Display script version.
EOF
}
f_log() {
msg="$1"
level=$2
msg_to_log_in_console="$level - $msg"
echo -e "$msg_to_log_in_console"
}
f_log_unknown() {
f_log "$1" "UNKNOWN"
f_display_usage
exit $EXIT_UNKNOWN
}
f_log_ok() {
f_log "$1" "OK"
exit $EXIT_OK
}
f_log_warning() {
f_log "$1" "WARNING"
exit $EXIT_WARNING
}
f_log_critical() {
f_log "$1" "CRITICAL"
exit $EXIT_CRITICAL
}
f_parse_parameters() {
# Define long options
long_opts="expected-status:,help,service:,version"
# read the options
params=`getopt -o e:hs:v --long $long_opts -n $SCRIPT_NAME -- $PARAM`
eval set -- "$params"
while true ; do
case "$1" in
-e|--expected-status)
EXPECTED_STATUS="$2"
shift
;;
-h|--help)
f_log_unknown "Display help"
;;
-s|--service)
SERVICE_NAME="$2"
shift
;;
-v|--version) # Get the version of the script
GET_VERSION=true
;;
--) break
;;
# Handle unknown option
*) f_log_unknown "Unknown option: $1, exited.."
;;
esac
shift
done
# Validate parsed parameters
f_validate_parameters
}
f_diplay_script_version(){
f_log_unknown "$VERSION_TO_DISPLAY"
}
f_validate_parameters() {
# Display script version
if $GET_VERSION; then
f_diplay_script_version
exit $EXIT_UNKNOWN
fi
# Check service name
if [ -z "$SERVICE_NAME" ]; then
f_log_unknown "Service name is mandatory, you must provide one in command line with option -s or --service."
fi
if [ -z "$EXPECTED_STATUS" ]; then
EXPECTED_STATUS=$DEFAULT_EXPECTED_STATUS
fi
if [ "$EXPECTED_STATUS" != "$RUNNING_STATUS" ] && [ "$EXPECTED_STATUS" != "$STOPPED_STATUS" ]; then
f_log_unknown "Expected status -e should be $RUNNING_STATUS or $STOPPED_STATUS (actual: $EXPECTED_STATUS)"
fi
}
f_check_service_status() {
SERVICE_STATUS=`systemctl is-active $SERVICE_NAME`
STATUS_MSG="Systemctl - Service $SERVICE_NAME is in state $SERVICE_STATUS (expected status is $EXPECTED_STATUS)"
if [ "$SERVICE_STATUS" = "$EXPECTED_STATUS" ]; then
f_log_ok "$STATUS_MSG"
else
f_log_critical "$STATUS_MSG"
fi
}
f_parse_parameters
f_check_service_status
_