How to add Zabbix time synchronization ntp userparameter check script to Monitor Linux servers?
We needed to set on some servers at my work an elementary check with Zabbix monitoring to check whether servers time is correctly synchronized with ntpd time service as well report if the ntp daemon is correctly running on the machine. For that a userparameter script was developed called userparameter_ntp.conf the script is simplistic and few a lines of bash shell scripting
stuff is based on gresping information required from ntpq and ntpstat common ntp client commands to get information about the status of time synchronization on the servers.
[root@linuxserver ]# ntpstat
synchronised to NTP server (10.80.200.30) at stratum 3
time correct to within 47 ms
polling server every 1024 s
[root@linuxserver ]# ntpq -c peers
remote refid st t when poll reach delay offset jitter
==============================================================================
+timeserver1 10.26.239.41 2 u 319 1024 377 15.864 1.270 0.262
+timeserver2 10.82.239.41 2 u 591 1024 377 16.287 -0.334 1.748
*timeserver3 10.82.239.43 2 u 47 1024 377 15.613 -0.553 0.251
timeserver4 .INIT. 16 u – 1024 0 0.000 0.000 0.000
Below is Zabbix UserParameter script that does report us 3 important values we monitor to make sure time server synchronization works as expected the zabbix keys we set are ntp.offset, ntp.sync, ntp.exact in attempt to describe what we're fetching from ntp client:
[root@linuxserver ]# cat /etc/zabbix/zabbix-agent.d/userparameter_ntp.conf
UserParameter=ntp.offset,(/usr/sbin/ntpq -pn | /usr/bin/awk 'BEGIN { offset=1000 } $1 ~ /\*/ { offset=$9 } END { print offset }')
#UserParameter=ntp.offset,(/usr/sbin/ntpq -pn | /usr/bin/awk 'FNR==4{print $9}')
UserParameter=ntp.sync,(/usr/bin/ntpstat | cut -f 1 -d " " | tr -d ' \t\n\r\f')
UserParameter=ntp.exact,(/usr/bin/ntpstat | /usr/bin/awk 'FNR==2{print $5,$6}')
In Zabbix the monitored ntpd parameters set-upped looks like this:
!Note that in above userparameter example, the commented userparameter script is a just another way to do an ntpd offset returned value which was developed before the more sophisticated with more regular expression checks from the /usr/sbin/ntpd via ntpq, perhaps if you want to extend it you can also use another script to report more verbose information to Zabbix if that is required like ouput from ntpq -c peers command:
UserParameter=ntp.verbose,(/usr/sbin/ntpq -c peers)
Of course to make the Zabbix fetch necessery data from monitored hosts, we need to set-up further new Zabbix Template with the respective Trigger and Items.
Below are few screenshots including the triggers used.
- ntpd.trigger
{NTP:net.udp.service[ntp].last(0)}<1
- NTP Synchronization trigger
{NTP:ntp.sync.iregexp(unsynchronised)}=1
As you can see from history we have setup our items to Store history of reported data to Zabbix from parameter script for 90 days and update our monitor check, every 30 seconds from the monitored hosts to which Tempate is applied.
Well that's all folks, time synchronization issues we'll be promptly triggering a new Alarm in Zabbix !