Page 1 of 1

Detailed NTP Server Monitoring script for Monit / MMonit

Posted: Wednesday 11 December 2019 12:40
by ben53252642
This is a script I wrote for detailed monitoring of an NTP server (ntpd) via Monit / MMonit:

Checks:
1) If NTP server is synchronised or unsynchronised
2) If NTP server is synchronised to an acceptable time level in ms

Below is an image showing example output into mmonit for status coming from the NTP server via the bash script:

mmonit-ntp.JPG
mmonit-ntp.JPG (215.24 KiB) Viewed 3059 times

ntpsync.sh

Code: Select all

#!/bin/bash

# Configuration
timeaccuracy="200" # Level of accuracy in ms before alert

# Get output
output=$(/usr/bin/ntpstat)
outputrawms=$(echo "$output" | grep -Eo '\w+ ms' | awk '{ print $1 }')

# If NTP server is unsynchronised exit and display error
if [[ $output == *"unsynchronised"* ]]; then
  echo "$output"
  exit 1
fi

# If NTP server is synchronised within acceptable ms display data and exit
if (( $outputrawms < "$timeaccuracy" )); then
  echo "$output"
  exit 0
fi

# If NTP server is synchronised output acceptable ms display message and exit
if (( $outputrawms >= "$timeaccuracy" )); then
  echo -e "Time accuracy is outside acceptable range of less than $timeaccuracy ms, output:\n$output" >&2
  exit 1
fi
Here is the section that goes in /etc/monit/monitrc:

Code: Select all

# Check if NTP is synchronised
check program ntp_synchronisation with path "/etc/monit/customscripts/ntpsync.sh"
 with timeout 5 seconds
 if status != 0 then alert

Re: Detailed NTP Server Monitoring script for Monit / MMonit

Posted: Wednesday 11 December 2019 15:17
by waaren
Nice one. Thx

Re: Detailed NTP Server Monitoring script for Monit / MMonit

Posted: Tuesday 13 June 2023 23:38
by marmachine
Nice, but needs some work...

Code: Select all

Last output:
/etc/monit/ntpsync.sh: regel 7: /usr/bin/ntpstat: Bestand of map bestaat niet
/etc/monit/ntpsync.sh: regel 17: ((: < 200 : syntaxfout: operator verwacht (het onjuiste symbool is "< 200 ")
/etc/monit/ntpsync.sh: regel 23: ((: >= 200 : syntaxfout: operator verwacht (het onjuiste symbool is ">= 200 ")

Re: Detailed NTP Server Monitoring script for Monit / MMonit

Posted: Wednesday 14 June 2023 9:05
by Kedi
A lot of systems don't have ntpstat installed.
A lot of systems don't have ntpd running.
A lot of systems have time synchronized by systemd.
And then this monit solution won't work.

Re: Detailed NTP Server Monitoring script for Monit / MMonit

Posted: Wednesday 14 June 2023 10:01
by ben53252642
I setup a dedicated physical server thats only job was to operate as a ntp-server.

Re: Detailed NTP Server Monitoring script for Monit / MMonit

Posted: Wednesday 14 June 2023 12:15
by Kedi
When you use this command in a running Raspberry

Code: Select all

sudo ntpdate nl.pool.ntp.org
Then I get a respons

Code: Select all

14 Jun 12:11:27 ntpdate[11647]: adjust time server 164.92.216.152 offset 0.001751 sec
So my system (using systemd) is after 38 days of the last boot accurate on 1.751 milliseconds.
That's enough for me, no need for a separate time-server and more accurate than the limit of 200 ms in the script.