4

Looking for a script to (1) check status of ntp and (2) if it is within +/- 1 second of a given time server (eg 123.456.789.10). (3) In addition, system time should be checked to see if the timezone is set correctly (eg PST)

Here's what I have so far, checking the status of ntp:

#!/bin/bash

if [[ ! -x /usr/bin/ntpstat ]]
then
  echo "ntpstat is NOT installed: please install it"
  exit 3

res=$(/usr/bin/ntpstat)
rc=$?

case $rc in
  0 )
    echo "clocks are synchronized"
    ;;
  1 )
    echo "WARNING: clocks are NOT synchronized"
    ;;
  2 )
    echo "CRITICAL: NTPD IS DOWN -- NO STATUS"
    ;;
esac
2
  • When I check, I use ntpdate -q <known ntp server name/IP> and parse the output to get the actual offset versus that server. This disconnects the "is it good" logic from the script doing the actual data collection so the script doesn't need to be updated or have a parameter changed if we decide to change how tightly a time sync must be in order to be "good" Commented Feb 17, 2016 at 7:09
  • Is it for Nagios? Commented Feb 17, 2016 at 9:19

1 Answer 1

6

I use ntpq for that.

Here's some snippets and pseudo code.

First, calculate the offset and store it in a var:

ntp_offset=$(ntpq -pn | \
     /usr/bin/awk 'BEGIN { offset=1000 } $1 ~ /\*/ { offset=$9 } END { print offset }')
  1. Server OK when: ntp_offset < 1000

  2. Server unsynchronised when: ntp_offset >= 1000

  3. the check for ntpd is down may be done with different approaches, depending on your OS. For example, use service ntpd status on red hat, centos and similar, and then check the result status on the $? variable.

5
  • 1
    The 0, 1, 2 levels suggest it is a Nagios script, and there is a plug-in for that. Commented Feb 17, 2016 at 9:19
  • @RuiFRibeiro, actually the command that I execute is remove from our zabbix configuration. Commented Feb 17, 2016 at 18:08
  • Thank you, @NunoPereira .isn't there a correlation between ntpstat and ntpq? When I try ntpstat the output looks like this: synchronised to NTP server (10.248.181.16) at stratum 3 time correct to within 1010 ms polling server every 64 s However, at the exact same time, executing ntpq -pn looks like this: remote refid st t when poll reach delay offset jitter ======================================================= *10.248.181.16 24.56.178.140 2 u 15 64 1 0.261 0.300 0.006 offset = 0.300 (Sorry I'm unable to format the comment right)
    – Cartwig
    Commented Feb 18, 2016 at 1:42
  • 1
    Not really, @Cartwig. Short story: ntpstat gives the maximum error that your local clock has, while ntpq -pn gives what the local peer estimates as the offset to each of the remote peers. I prefer the second, and the values are very different (offset should be much smaller than the "time correct to within" value). Check serverfault.com/questions/184257/… for more info about both. Commented Feb 18, 2016 at 2:19
  • NOTE: This may not work without telling ntpq to explicitly use IPv4. - unix.stackexchange.com/questions/345778/…
    – slm
    Commented Mar 11, 2018 at 4:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .