Skip to content

Instantly share code, notes, and snippets.

@ekristen
Last active February 22, 2023 14:26
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ekristen/41dfd50f639996afff98ed33dfcc4b2b to your computer and use it in GitHub Desktop.
Save ekristen/41dfd50f639996afff98ed33dfcc4b2b to your computer and use it in GitHub Desktop.
Chrony Nagios Plugin
#!/bin/bash
# Changes by Erik Kristensen
#
# Changelog (2018/01/31)
# - Improved debugging, just set DEBUG=true in shell env
# - Can set critical, warning and service name via ENV vars or command line opts
# - Improved if statements for checking warning, critical conditions
# - Errors unknown if `bc` command line tool is not installed
#
# Original Source: https://exchange.nagios.org/directory/Plugins/Network-Protocols/NTP-and-Time/check_ntp(chrony)/details
# Original Author
# Author: Abd Alhameed Ghaith
# License: GPL
# Date:06/04/2016
# Purpose: check the chrony service if its running or not ,if running them it will check if the Leap status is in normal status or not.if not this means that you have to check the connectivity between your server and NTP server.if all the previous is working well,the script will complare between local machine time and NTP time and give the nagios status accordingly.
if [ "x${DEBUG}" != "x" ]; then
set -e
set -x
fi
if [ "x`which bc`" == "x" ]; then
echo "bc command is required for this plugin"
exit 3
fi
SERVICE_NAME=${SERVICE_NAME:-chrony}
WARNING_VALUE=${WARNING_VALUE:-500}
CRITICAL_VALUE=${CRITICAL_VALUE:-1000}
NUMBER_VALIDATION='^[0-9]+$'
OPTS=`getopt -o w:c:s: --long warning:,critical:,service-name: -n 'parse-iotions' -- "$@"`
if [ $? != 0 ]; then
echo "Failed parsing options." >&2
exit 3
fi
eval set -- "$OPTS"
while true; do
case "$1" in
-w | --warning ) WARNING_VALUE="$2"; shift 2;;
-c | --critical ) CRITICAL_VALUE="$2"; shift 2;;
-s | --service-name ) SERVICE_NAME="$2"; shift 2;;
-- ) shift; break ;;
* ) break ;;
esac
done
if ! [[ $WARNING_VALUE =~ $NUMBER_VALIDATION ]] || ! [[ $CRITICAL_VALUE =~ $NUMBER_VALIDATION ]]; then
echo 'Please provide only number in the warning and critical values'
exit 3
fi
if [[ $WARNING_VALUE -gt $CRITICAL_VALUE ]] || [[ $WARNING_VALUE -eq $CRITICAL_VALUE ]] ; then
echo "The Critical Value (${CRITICAL_VALUE}) must be larger than the Warning Value (${WARNING_VALUE})"
exit 3
fi
CHECK_SERVICE=`systemctl status ${SERVICE_NAME}.service | grep Active | awk -F '(' '{ print $2 }' | awk -F ')' '{ print $1 }'`
if [[ $CHECK_SERVICE == 'dead' ]]; then
echo "CRITICAL - The service ${SERVICE_NAME} is not running"
exit 2
fi
CHECK_SERVER_SYNC=`chronyc tracking | grep 'Leap status' | awk -F ':' '{ print $2 }' | sed -e 's/^ //'`
if [[ $CHECK_SERVER_SYNC == 'Not synchronised' ]]; then
echo "CRITICAL - Server is not synchronised with the ntp server"
exit 2
fi
CHECK_TIME_DIFF=`chronyc tracking | grep 'System time' | awk -F ':' '{ print $2 }' | awk '{ print $1 }'| sed -e 's/^ //'`
CHECK_TIME_DIFF_INT=`chronyc tracking | grep 'System time' | awk -F ':' '{ print $2 }' | awk '{ print $1 }'| sed -e 's/^ //' | awk -F '.' '{ print $1 }'`
DIFF_IN_SECOND=`echo "(($CHECK_TIME_DIFF * 1000))" | bc`
FAST_SLOW_VALUE=`chronyc tracking | grep 'System time' | awk -F ':' '{ print $2 }' | awk '{ print $3 }'| sed -e 's/^ //'`
if [[ $CHECK_TIME_DIFF_INT -ge $CRITICAL_VALUE ]]; then
echo "CRITICAL time is $CHECK_TIME_DIFF $FAST_SLOW_VALUE of NTP Time"
echo "|Time Differences in=$CHECK_TIME_DIFF"
exit 2
fi
if [[ $CHECK_TIME_DIFF_INT -gt $WARNING_VALUE ]]; then
echo "WARNING time is $CHECK_TIME_DIFF_INT $FAST_SLOW_VALUE of NTP Time"
echo "|Time Differences in=$CHECK_TIME_DIFF"
exit 1
fi
echo "OK - time is $CHECK_TIME_DIFF $FAST_SLOW_VALUE of NTP Time"
echo "|Time Differences in=$CHECK_TIME_DIFF"
exit 0
@codydiehl
Copy link

Very useful, thank you!

@Back-N-Black
Copy link

Back-N-Black commented Jul 15, 2021

Line 76 has a typo, should be 'print' not 'rint'. Thanks for your updates, seems to work for me.

@ekristen
Copy link
Author

Fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment