Skip to content
Snippets Groups Projects
check_timesync 3.15 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
# ======================================================================

. `dirname $0`/inc_pluginfunctions
myos=`ph.getOS`
tmpfile=/tmp/check_timesync.tmp

# ----------------------------------------------------------------------
# pre check if timedatectl is available
# ----------------------------------------------------------------------
which timedatectl >/dev/null
if [ $? -ne 0 ]; then
  ph.abort "timesync UNKNOWN: timedatectl was not found ... maybe your OS ${myos} is not supported"
fi


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

# ph.execIfReady "timedatectl status | grep '^NTP synchronized'" >/dev/null
# echo ----- output of timedatectl status >$tmpfile
# timedatectl status >>$tmpfile
ph.execIfReady "timedatectl show >$tmpfile; grep '^NTPSynchronized' $tmpfile >/dev/null"

cat $tmpfile | grep "^NTPSynchronized" >/dev/null
if [ $? -ne 0 ]; 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
  cat $tmpfile | grep "^NTPSynchronized=yes" >/dev/null
  if [ $? -eq 0 ]; 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=
if [ -f /etc/systemd/timesyncd.conf ]; then
  sSyncService="systemd-timesyncd"
fi
if [ -f /etc/chrony.conf ]; then
  sSyncService="chronyd"
fi
if [ -f /etc/chrony/chrony.conf ]; then
  sSyncService="chronyd"
fi
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

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