diff --git a/nhc.conf b/nhc.conf index 10b407f..ca5761b 100644 --- a/nhc.conf +++ b/nhc.conf @@ -211,7 +211,8 @@ ### ### Check that the time has not drifted too much. ### Only actually checks if the env var is defined (in /etc/sysconfig/nhc) -* || [ ! -z "${NHC_PTPD_STATUS+x}" ] && check_ptpd_drift +* || if [ ! -z "${NHC_PTPD_STATUS+x}" ]; then check_ptpd_drift; fi +* || if [ ! -z "${NHC_CHRONYC_MAX_DRIFT+x}" ]; then check_chrony_drift; fi ####################################################################### ### diff --git a/scripts/lbnl_time.nhc b/scripts/lbnl_time.nhc index db526a1..4c9ce12 100644 --- a/scripts/lbnl_time.nhc +++ b/scripts/lbnl_time.nhc @@ -37,3 +37,35 @@ function check_ptpd_drift() { return 0 } +function check_chrony_drift() { + local DRIFT=0 + + DRIFT=$(chronyc tracking | grep "Last offset *:" | tr -s " " | cut -d" " -f5 | sed 's/^[-+]//') + dbg "$FUNCNAME: drift is $DRIFT" + + if [ -z "${DRIFT}" ]; then + dbg "Could not find drift line, sleeping 5 seconds" + sleep 5 + + DRIFT=$(chronyc tracking | grep "Last offset *:" | tr -s " " | cut -d" " -f5 | sed 's/^[-+]//') + dbg "$FUNCNAME: drift is $DRIFT" + + if [ -z "${DRIFT}" ]; then + die 1 "$FUNCNAME: No drift line found in chronyc output after 5 second sleep" + return 1 + fi + fi + + if (( $(echo "${DRIFT#-} > $NHC_CHRONYC_MAX_DRIFT " | bc -l) )); then + die 1 "$FUNCNAME: CHRONY drift too high: ${DRIFT} with threshold $NHC_CHRONYC_MAX_DRIFT" + return 1 + fi + + return 0 + + +} + + + + diff --git a/test/test_lbnl_time.nhc b/test/test_lbnl_time.nhc index 2a4dd94..26853ec 100644 --- a/test/test_lbnl_time.nhc +++ b/test/test_lbnl_time.nhc @@ -1,7 +1,8 @@ # Tests for lbnl_time.nhc -plan $((1)) "lbnl_fs.nhc" && { +plan 2 "lbnl_time.nhc" && { is "`type -t check_ptpd_drift 2>&1`" 'function' 'check_ptpd_drift() loaded properly' + is "`type -t check_chrony_drift 2>&1`" 'function' 'check_chrony_drift() loaded properly' local OLD_SHELL="$SHELL" SHELL=: