From: Peter Palfrader Date: Mon, 23 Jan 2017 12:47:34 +0000 (+0100) Subject: dsa-check-timedatectl: support sync check only X-Git-Url: https://git.adam-barratt.org.uk/?p=mirror%2Fdsa-nagios.git;a=commitdiff_plain;h=0787b1bcf4334829dbfee2330c90dff38a11dadb dsa-check-timedatectl: support sync check only --- diff --git a/dsa-nagios-checks/checks/dsa-check-timedatectl b/dsa-nagios-checks/checks/dsa-check-timedatectl index 13956d2..4294ed8 100755 --- a/dsa-nagios-checks/checks/dsa-check-timedatectl +++ b/dsa-nagios-checks/checks/dsa-check-timedatectl @@ -26,6 +26,37 @@ set -e set -u MAX=2 +SYNSTATUSONLY="0" + +usage(){ + ret=$1 + + cat < + + Check NTP sync status (per timedatectl's output) and offset to RTC clock. + The latter is particularly interesting for VMs. + + -o Maximum offset to tolerate (Default: $MAX) + -s Check sync status only, do not diff against RTC. +EOF + + exit $ret +} + +while getopts o:sh opt ; do + case "$opt" in + o) MAX="$OPTARG" ;; + s) SYNSTATUSONLY="1";; + h) usage 0 + esac +done +shift $(($OPTIND - 1)) +if [ "$#" -gt 0 ]; then + usage 1 >&2 +fi + temp="$(mktemp)" trap "rm -f '$temp'" EXIT @@ -33,29 +64,34 @@ trap "rm -f '$temp'" EXIT 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\):/ { s/^[^:]*: *//; p}; d' "$temp") +ntpsynced=$(sed '/NTP synchronized:/ { s/^[^:]*: *//; p}; d' "$temp") uts=$(TZ=UTC date -d "$ut" +%s) rtcs=$(TZ=UTC date -d "$rtc" +%s) +delta=$((uts - rtcs)) -d=$((uts - rtcs)) - -if [ "$d" -lt "-$MAX" ] || - [ "$d" -gt "$MAX" ]; then - echo "Warning: time desync $d: RTC vs. system time: $rtc vs. $ut" - exit 1 -fi - +if [ "$SYNSTATUSONLY" -ge 1 ]; then + if [ "$ntpsynced" != "yes" ]; then + echo "Warning: not synced with NTP." + exit 1 + fi +else + if [ "$delta" -lt "-$MAX" ] || + [ "$delta" -gt "$MAX" ]; then + echo "Warning: time desync $delta: RTC vs. system time: $rtc vs. $ut" + exit 1 + fi -bool=$(sed '/\(NTP enabled\|Network time on\):/ { s/^[^:]*: *//; p}; d' "$temp") -if [ "$bool" != "yes" ]; then - echo "Warning: NTP not enabled!" - exit 1 -fi + if [ "$ntpenabled" != "yes" ]; then + echo "Warning: NTP not enabled!" + exit 1 + fi -bool=$(sed '/NTP synchronized:/ { s/^[^:]*: *//; p}; d' "$temp") -if [ "$bool" != "yes" ]; then - echo "Warning: not synced with NTP (but clock is OK for now)." - exit 1 + if [ "$ntpsynced" != "yes" ]; then + echo "Warning: not synced with NTP (but clock is OK for now)." + exit 1 + fi fi echo "OK: synced at $ut."