mirror of
https://github.com/kubernetes/node-problem-detector.git
synced 2026-08-28 01:47:20 +00:00
Improve systemctl check, style + cleanup
- Use `systemctl is-active` to check if service is running
- Cleaner that `grep` on `systemctl status` output
- Return success means service is running/active
- Return failure means not running which could be due to
stopped/failed service or that service does not exist
- Use `command -v` instead of `which`
Ref: https://github.com/koalaman/shellcheck/wiki/SC2230
- Follow Google "Shell Style Guide": indent, use "readonly"
- Minor: Rephrase comment, avoid all caps
This commit is contained in:
+19
-15
@@ -1,23 +1,27 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# NOTE: THIS NTP SERVICE CHECK SCRIPT ASSUME THAT NTP SERVICE IS RUNNING UNDER SYSTEMD.
|
# This plugin checks if the ntp service is running under systemd.
|
||||||
# THIS IS JUST AN EXAMPLE. YOU CAN WRITE YOUR OWN NODE PROBLEM PLUGIN ON DEMAND.
|
# NOTE: This is only an example for systemd services.
|
||||||
|
|
||||||
OK=0
|
readonly OK=0
|
||||||
NONOK=1
|
readonly NONOK=1
|
||||||
UNKNOWN=2
|
readonly UNKNOWN=2
|
||||||
|
|
||||||
which systemctl >/dev/null
|
readonly SERVICE='ntp.service'
|
||||||
if [ $? -ne 0 ]; then
|
|
||||||
echo "Systemd is not supported"
|
# Check systemd cmd present
|
||||||
exit $UNKNOWN
|
if ! command -v systemctl >/dev/null; then
|
||||||
|
echo "Could not find 'systemctl' - require systemd"
|
||||||
|
exit $UNKNOWN
|
||||||
fi
|
fi
|
||||||
|
|
||||||
systemctl status ntp.service | grep 'Active:' | grep -q running
|
# Return success if service active (i.e. running)
|
||||||
if [ $? -ne 0 ]; then
|
if systemctl -q is-active "$SERVICE"; then
|
||||||
echo "NTP service is not running"
|
echo "$SERVICE is running"
|
||||||
exit $NONOK
|
exit $OK
|
||||||
|
else
|
||||||
|
# Does not differenciate stopped/failed service from non-existent
|
||||||
|
echo "$SERVICE is not running"
|
||||||
|
exit $NONOK
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "NTP service is running"
|
|
||||||
exit $OK
|
|
||||||
|
|||||||
Reference in New Issue
Block a user