Skip to content

Commit 3a41fc2

Browse files
authored
Merge pull request #392 from arekkusu/origin/patch-2
Improve systemctl check, style + cleanup
2 parents f535592 + 4df720c commit 3a41fc2

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

config/plugin/check_ntp.sh

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
#!/bin/bash
22

3-
# NOTE: THIS NTP SERVICE CHECK SCRIPT ASSUME THAT NTP SERVICE IS RUNNING UNDER SYSTEMD.
4-
# THIS IS JUST AN EXAMPLE. YOU CAN WRITE YOUR OWN NODE PROBLEM PLUGIN ON DEMAND.
3+
# This plugin checks if the ntp service is running under systemd.
4+
# NOTE: This is only an example for systemd services.
55

6-
OK=0
7-
NONOK=1
8-
UNKNOWN=2
6+
readonly OK=0
7+
readonly NONOK=1
8+
readonly UNKNOWN=2
99

10-
which systemctl >/dev/null
11-
if [ $? -ne 0 ]; then
12-
echo "Systemd is not supported"
13-
exit $UNKNOWN
10+
readonly SERVICE='ntp.service'
11+
12+
# Check systemd cmd present
13+
if ! command -v systemctl >/dev/null; then
14+
echo "Could not find 'systemctl' - require systemd"
15+
exit $UNKNOWN
1416
fi
1517

16-
systemctl status ntp.service | grep 'Active:' | grep -q running
17-
if [ $? -ne 0 ]; then
18-
echo "NTP service is not running"
19-
exit $NONOK
18+
# Return success if service active (i.e. running)
19+
if systemctl -q is-active "$SERVICE"; then
20+
echo "$SERVICE is running"
21+
exit $OK
22+
else
23+
# Does not differenciate stopped/failed service from non-existent
24+
echo "$SERVICE is not running"
25+
exit $NONOK
2026
fi
2127

22-
echo "NTP service is running"
23-
exit $OK

0 commit comments

Comments
 (0)