Skip to content
Snippets Groups Projects
check_timesync 3.57 KiB
#!/bin/bash
# ======================================================================
#
# NAGIOS CLIENT CHECK :: check if timesync with ntpd or timesyncd
# is active
#
# ----------------------------------------------------------------------
# script checks output of "timedatectl status" and shows status of
# the sync service (systemd-timesyncd or chronyd)
# - centos7
# - debian8
# - ubuntu16
# ----------------------------------------------------------------------
# 2018-10-26  v1.0  <axel.hahn@iml.unibe.ch>
# 2020-03-05  v1.1  <axel.hahn@iml.unibe.ch> switch to ph.* helper functions
# 2020-05-13  v1.2  <axel.hahn@iml.unibe.ch> update pre detect output of timedatectl
# 2021-08-19  v1.3  <martin.gasser@iml.unibe.ch> update for chrony with timedatectl 
# 2025-02-19  v1.4  <axel.hahn@unibe.ch>         update help page
# ======================================================================

. $(dirname $0)/inc_pluginfunctions
self_APPVERSION=1.4

myos="$( ph.getOS )"
tmpfile=/tmp/check_timesync.tmp

# ----------------------------------------------------------------------
# functions
# ----------------------------------------------------------------------

# show help text
function showHelp(){
    local _self; _self=$(basename $0)
cat <<EOF
$( ph.showImlHelpHeader )

Check if time snychronisation is active.
It uses timedatectl and detects systemd timesyncd and chronyd.

SYNTAX:

  $_self [-h]

OPTIONS:

  -h or --help   show this help.

PARAMETERS:

  None.

EOF
}

# ----------------------------------------------------------------------
# check params
# ----------------------------------------------------------------------

if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
  showHelp
  exit 0;
fi

if [ -n "$*" ]; then
  echo "ERROR: no parameter expected."
  exit 1
fi

# ----------------------------------------------------------------------
# check output of timedatectl command
# ----------------------------------------------------------------------

ph.require "timedatectl"

ph.execIfReady "timedatectl show >$tmpfile; grep '^NTPSynchronized' $tmpfile >/dev/null"

if ! cat $tmpfile | grep "^NTPSynchronized" >/dev/null
then
  ph.setStatus "unknown"
  # ph.status "timesync: timedatectl has no line NTP synchronized ... maybe your OS ${myos} is not supported"
  ph.status "timesync: timedatectl has no line NTP synchronized"
  cat $tmpfile
else
  if cat $tmpfile | grep "^NTPSynchronized=yes" >/dev/null
  then
    ph.status "timesync: a timesync service is active on this ${myos} host"
  else
    ph.setStatus "critical"
    ph.status "timesync: there is no active timesync - you need to activate ntpd or timesyncd on this ${myos} host"
  fi
fi
# /timedatectl

# ----------------------------------------------------------------------
# detect sync service and show its output
# ----------------------------------------------------------------------

echo >>$tmpfile
sSyncService=

test -f /etc/systemd/timesyncd.conf && sSyncService="systemd-timesyncd"
test -f /etc/chrony.conf && sSyncService="chronyd"
test -f /etc/chrony/chrony.conf && sSyncService="chronyd"

if [ -z $sSyncService ]; then
  echo "REMARK: no sync service detected ... or this sensu check does not support it" >>$tmpfile
else
  echo ----- output of systemctl status $sSyncService >>$tmpfile
  systemctl status $sSyncService >>$tmpfile
fi


# ----------------------------------------------------------------------
# output & exit
# ----------------------------------------------------------------------

cat $tmpfile | grep "^NTPSynchronized"
echo
cat $tmpfile

rm -f $tmpfile

ph.exit

# ----------------------------------------------------------------------