Detailed NTP Server Monitoring script for Monit / MMonit

All kinds of 'OS' scripts

Moderator: leecollings

Post Reply
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Detailed NTP Server Monitoring script for Monit / MMonit

Post 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 2747 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
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Detailed NTP Server Monitoring script for Monit / MMonit

Post by waaren »

Nice one. Thx
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
marmachine
Posts: 133
Joined: Saturday 26 March 2016 10:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Location: Netherlands
Contact:

Re: Detailed NTP Server Monitoring script for Monit / MMonit

Post 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 ")
Kedi
Posts: 561
Joined: Monday 20 March 2023 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Somewhere in NL
Contact:

Re: Detailed NTP Server Monitoring script for Monit / MMonit

Post 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.
Logic will get you from A to B. Imagination will take you everywhere.
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: Detailed NTP Server Monitoring script for Monit / MMonit

Post by ben53252642 »

I setup a dedicated physical server thats only job was to operate as a ntp-server.
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
Kedi
Posts: 561
Joined: Monday 20 March 2023 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Somewhere in NL
Contact:

Re: Detailed NTP Server Monitoring script for Monit / MMonit

Post 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.
Logic will get you from A to B. Imagination will take you everywhere.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests