Skip to content

Commit be70803

Browse files
authored
Nagios: Update Check_Ntp_Plugin with chronyd support (adoptium#4315)
* Nagios: Update Check_Ntp_Plugin with chronyd support * Nagios: Small Tweaks To Script
1 parent 6a26579 commit be70803

1 file changed

Lines changed: 96 additions & 26 deletions

File tree

  • ansible/playbooks/AdoptOpenJDK_Unix_Playbook/roles/Nagios_Plugins/tasks/additional_plugins
Lines changed: 96 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env bash
12
# Copyright 2020 The Original Author(s)
23
#
34
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -12,32 +13,101 @@
1213
# See the License for the specific language governing permissions and
1314
# limitations under the License.
1415

15-
#! /usr/bin/env bash
16+
# Combined NTP/Chronyd Time Synchronization Check Plugin
1617

17-
/sbin/service ntpd status >/dev/null 2>&1
18-
if [[ $? != 0 ]]; then
19-
echo "WARNING - Check NTPD Service"
20-
exit 1
18+
# Nagios exit codes:
19+
# 0 = OK
20+
# 1 = WARNING
21+
# 2 = CRITICAL
22+
# 3 = UNKNOWN
23+
24+
is_ok() {
25+
[[ "$1" == "active" || "$1" == "yes" ]]
26+
}
27+
28+
check_timedatectl() {
29+
local STATE SERVICE_STATE SYNC_STATE
30+
31+
STATE="$(timedatectl 2>/dev/null)"
32+
local RC=$?
33+
34+
if [[ $RC -ne 0 || -z "$STATE" ]]; then
35+
return 1 # timedatectl not available
36+
fi
37+
38+
SERVICE_STATE="$(printf '%s\n' "$STATE" | sed -n 's/^[[:space:]]*NTP service:[[:space:]]*//p')"
39+
SYNC_STATE="$(printf '%s\n' "$STATE" | sed -n 's/^[[:space:]]*System clock synchronized:[[:space:]]*//p')"
40+
41+
if [[ -z "$SERVICE_STATE" && -z "$SYNC_STATE" ]]; then
42+
return 1 # Could not parse timedatectl output
43+
fi
44+
45+
if ! is_ok "$SERVICE_STATE" && ! is_ok "$SYNC_STATE"; then
46+
echo "CRITICAL - NTP service is not active and clock is not synchronized"
47+
exit 2
48+
fi
49+
50+
if ! is_ok "$SERVICE_STATE"; then
51+
echo "WARNING - NTP service is not active"
52+
exit 1
53+
fi
54+
55+
if ! is_ok "$SYNC_STATE"; then
56+
echo "WARNING - Time not synchronized"
57+
exit 1
58+
fi
59+
60+
echo "OK - Time synchronized (via timedatectl)"
61+
exit 0
62+
}
63+
64+
check_ntpd() {
65+
# Check if ntpd service is running
66+
if ! service ntpd status >/dev/null 2>&1; then
67+
echo "WARNING - Check NTPD Service"
68+
exit 1
69+
fi
70+
71+
# Check synchronization status using ntpstat
72+
if ! command -v ntpstat >/dev/null 2>&1; then
73+
echo "UNKNOWN - ntpstat command not found"
74+
exit 3
75+
fi
76+
77+
ntpstat >/dev/null 2>&1
78+
is_agent_syncd=$?
79+
80+
case $is_agent_syncd in
81+
0)
82+
echo "OK - Time synchronized (via ntpd)"
83+
exit 0
84+
;;
85+
1)
86+
echo "CRITICAL - Time not synchronized"
87+
exit 2
88+
;;
89+
2)
90+
echo "UNKNOWN - Clock state indeterminate"
91+
exit 3
92+
;;
93+
*)
94+
echo "UNKNOWN - Unexpected RC from ntpstat"
95+
exit 3
96+
;;
97+
esac
98+
}
99+
100+
# Main execution
101+
# Try modern timedatectl first (systemd-based systems)
102+
if command -v timedatectl >/dev/null 2>&1; then
103+
check_timedatectl
104+
fi
105+
106+
# Fall back to traditional ntpd check
107+
if command -v service >/dev/null 2>&1; then
108+
check_ntpd
21109
fi
22110

23-
ntpstat >/dev/null 2>&1
24-
is_agent_syncd=$?
25-
26-
case $is_agent_syncd in
27-
0)
28-
echo "OK - Time synchronized"
29-
exit 0
30-
;;
31-
1)
32-
echo "CRITICAL - Time not synchronized"
33-
exit 2
34-
;;
35-
2)
36-
echo "UNKNOWN - Clock state indeterminant"
37-
exit 3
38-
;;
39-
*)
40-
echo "UNKNOWN - Unexpected RC"
41-
exit 3
42-
;;
43-
esac
111+
# If we get here, neither method was available
112+
echo "UNKNOWN - No time synchronization method available (tried timedatectl and ntpd)"
113+
exit 3

0 commit comments

Comments
 (0)