check_ntp-simple

Intro

I made an NTP joke once. The timing was perfect.

This plugin checks the status of ntp on a server. For remote hosts, this should be run over ssh with check_by_ssh plugin. It uses ntpq to look for valid candidates (+) or a locked server (*) and will alert if the status is anything other than locked.

Command definition

#run "check_ntp-simple.sh" over check_by_ssh
define command {
        command_name    check_by_ssh-check_ntp-simple
        command_line    /usr/lib/nagios/plugins/check_by_ssh -H $ARG1$ -C "/home/nagios/checks/check_ntp-simple.sh $HOSTADDRESS$"
}

Check definition

define service{
        use                             generic-service
        host_name                       host1
        service_description             ntp
        check_command                   check_by_ssh-check_ntp-simple!sshrelayhostname
        contact_groups                  admin
        notification_interval           0
        }

check_apc.sh code

#!/bin/bash
# Written Andrew Stringer, 25/11/2013
# contact me on:- nagios atsymbol rainsbrook dot co dot uk
 
# Purpose is to check NTP synchronisation.
# Unlike other ntp checks, it only looks to see if there is a locked rference source
# or viable peer, it does not check delay or jitter.
# The reason for this is that my nagios server is a virtual machine and it seems to
# produce spurious warnings and criticals even though the targets are keeping time satisfactorily.
 
 
#Print out command line options
usage() { echo "Usage:- check_ntp-simple.sh <hostname>"  1>&2; exit 4; }
 
#Check we have some arguments passed
if [ -z "$1" ] ; then
        usage
        exit 4
fi
 
HOSTNAME=$1
 
# Fedora /Centos
#NTPQ=/usr/sbin/ntpq
 
# Ubuntu
NTPQ=/usr/bin/ntpq
 
NTPQREPLY=`${NTPQ} -p ${HOSTNAME}`
 
if [ -z "${NTPQREPLY}" ]; then
        echo "UNKNOWN - No reply received from ntp."
        exit 3
fi
 
# Example ntpq -p output:-
#ntpq -p
#     remote           refid      st t when poll reach   delay   offset  jitter
#==============================================================================
#+0.fedora.pool.n 212.83.133.52    3 u   34   64    7   36.366   13.499  42.658
#+1.fedora.pool.n 193.190.230.65   2 u   36   64    7  108.403    1.778  34.216
#*2.fedora.pool.n 40.33.41.76      2 u   33   64    7   38.729   23.144  53.312
#+3.fedora.pool.n 161.143.24.141   2 u   33   64    7   44.494   20.569  51.089
 
 
VIABLECLOCKS=`echo ${NTPQREPLY} | grep '+'`
VIABLECLOCKSRES=$?
 
LOCKEDCLOCK=`echo  ${NTPQREPLY} | grep '*'`
LOCKEDCLOCKRES=$?
 
if [[ $LOCKEDCLOCKRES -eq 0 ]]; then
        echo "OK - ntp is locked."
        exit 0
elif [[ $VIABLECLOCKSRES -eq 0 ]]; then
        echo "WARNING - ntp is not locked but candidates are available."
        exit 1
elif [[ $VIABLECLOCKSRES -ne 0 ]] && [[ $VIABLECLOCKSRES -ne 0 ]]; then
        echo "CRITICAL - ntp is not locked and candidates aren't available."
        exit 2
else
        echo "UNKNOWN - No reply received from ntp."
        exit 3
fi
 
exit 0
 
nagios/check_ntp-simple.txt · Last modified: 26/09/2023 09:22 by andrew