X-Git-Url: https://git.adam-barratt.org.uk/?a=blobdiff_plain;f=dsa-nagios-checks%2Fchecks%2Fdsa-check-timedatectl;fp=dsa-nagios-checks%2Fchecks%2Fdsa-check-timedatectl;h=bc978621023197833816f3bce2df76fc0b40d5bf;hb=eca24ed5ed13af0724c8e6f9482bc446cf0b950b;hp=4bb624728f4f2a608e96ce6e9de7c75af0b49c05;hpb=921d5435e35bb7a8ffbfdb2ab2e751f17e6384b9;p=mirror%2Fdsa-nagios.git diff --git a/dsa-nagios-checks/checks/dsa-check-timedatectl b/dsa-nagios-checks/checks/dsa-check-timedatectl index 4bb6247..bc97862 100755 --- a/dsa-nagios-checks/checks/dsa-check-timedatectl +++ b/dsa-nagios-checks/checks/dsa-check-timedatectl @@ -61,11 +61,32 @@ fi temp="$(mktemp)" trap "rm -f '$temp'" EXIT -LC_ALL=C timedatectl > "$temp" -ut=$(sed '/Universal time:/ { s/^[^:]*: *//; p}; d' "$temp") -rtc=$(sed '/RTC time:/ { s/^[^:]*: *//; p}; d' "$temp") -ntpenabled=$(sed '/\(NTP enabled\|Network time on\|NTP service\):/ { s/^[^:]*: *//; p}; d' "$temp") -ntpsynced=$(sed '/\(NTP synchronized\|System clock synchronized\):/ { s/^[^:]*: *//; p}; d' "$temp") +systemdversion="$(timedatectl --version | head -n1 | awk '{print $2}')" +if [ -z "$systemdversion" ]; then + echo "Unknown: Cannot get systemd version" + exit 3 +fi +if [ "$systemdversion" -lt 241 ] ; then # before buster (Debian 10) + LC_ALL=C timedatectl > "$temp" + ut=$(sed '/Universal time:/ { s/^[^:]*: *//; p}; d' "$temp") + rtc=$(sed '/RTC time:/ { s/^[^:]*: *//; p}; d' "$temp") + ntpenabled=$(sed '/\(NTP enabled\|Network time on\|NTP service\):/ { s/^[^:]*: *//; p}; d' "$temp") + ntpsynced=$(sed '/\(NTP synchronized\|System clock synchronized\):/ { s/^[^:]*: *//; p}; d' "$temp") +else + LC_ALL=C timedatectl show > "$temp" + ut=$(sed '/^TimeUSec=/ { s/^[^=]*=//; p}; d' "$temp") + rtc=$(sed '/^RTCTimeUSec=/ { s/^[^=]*=//; p}; d' "$temp") + ntpenabled=$(sed '/^NTP=/ { s/^[^=]*=//; p}; d' "$temp") + ntpsynced=$(sed '/^NTPSynchronized=/ { s/^[^=]*=//; p}; d' "$temp") + if [ "$ntpenabled" = "no" ]; then # in buster (Debian 10) ntpenabled no longer also considers the ntp service + ntp_status=$(systemctl is-enabled 'ntp.service' 2>/dev/null) && rc=$? || rc=$? + if [ "$rc" = 0 ] && [ "$ntp_status" = "enabled" ] ; then + if systemctl --quiet is-active ntp.service; then + ntpenabled=yes + fi + fi + fi +fi uts=$(TZ=UTC date -d "$ut" +%s) rtcs=$(TZ=UTC date -d "$rtc" +%s 2>/dev/null || echo "N/A")