Skip to content

Instantly share code, notes, and snippets.

@nicolasmarengo
Last active August 23, 2021 08:25
Show Gist options
  • Save nicolasmarengo/5955734 to your computer and use it in GitHub Desktop.
Save nicolasmarengo/5955734 to your computer and use it in GitHub Desktop.
Nagios Plugin to check if a cronjob is enabled/disabled through NRPE, by checking if the line on crontab has been commented out or not.
#!/bin/bash
#
# Program : check_cronjob
#
PROGNAME=`basename $0`
REVISION=`echo '$Revision: 0.1 $' | sed -e 's/[^0-9.]//g'`
. /usr/lib/nagios/plugins/utils.sh
print_usage() {
echo "Usage: $PROGNAME [-C cronjob] [-s status]"
echo " -C Cronjob name"
echo " -s Status to check (enabled/disabled)"
echo ""
echo "Usage: $PROGNAME --help"
echo "Usage: $PROGNAME --version"
}
print_help() {
print_revision $PROGNAME $REVISION
echo ""
echo "Nagios Plugin to check if a cronjob is running through NRPE"
echo ""
print_usage
echo ""
echo "Cronjob Status Check. - Nico Marengo 2013"
echo ""
exit 0
# support
}
exitstatus=$STATE_WARNING #default
MSG="Missing options. Run -h for help"
while test -n "$1"; do
case "$1" in
--help)
print_help
exit $STATE_OK
;;
-h)
print_help
exit $STATE_OK
;;
--version)
print_revision $PROGNAME $REVISION
exit $STATE_OK
;;
-V)
print_revision $PROGNAME $REVISION
exit $STATE_OK
;;
-C)
CRONJOB=$2;
shift;
;;
-s) STATUS=$2;
shift;
;;
*)
echo "Unknown argument: $1"
print_usage
exit $STATE_UNKNOWN
;;
esac
shift
done
if [ -z "$CRONJOB" ]; then
exitstatus=$STATE_CRITICAL
MSG="What Cronjob?"
fi
JOB=`crontab -l | grep "${CRONJOB}" | head -c 1`
DISABLEDSTATUS='#'
if [ -z "$JOB" ]; then
exitstatus=$STATE_CRITICAL
MSG="Cronjob not found"
else
if [ -z "$STATUS" ]; then
exitstatus=$STATE_CRITICAL
MSG="What Status?"
elif [ "$STATUS" = "enabled" ]; then
if [ "$JOB" != $DISABLEDSTATUS ];then
exitstatus=$STATE_OK
MSG="OK: enabled"
elif [ "$JOB" = $DISABLEDSTATUS ];then
exitstatus=$STATE_CRITICAL
MSG="CRITICAL: disabled"
fi
elif [ "$STATUS" = "disabled" ]; then
if [ "$JOB" != $DISABLEDSTATUS ];then
exitstatus=$STATE_CRITICAL
MSG="CRITICAL: enabled"
elif [ "$JOB" = $DISABLEDSTATUS ];then
exitstatus=$STATE_OK
MSG="OK: disabled"
fi
fi
fi
echo $MSG
exit $exitstatus
define service{
use generic-service ; Name of service template to use
host_name SERVER-HOST-NAME
service_description My Cron Check
check_command check_nrpe!check_NAME_cron
}
command[check_NAME_cron]=/usr/lib/nagios/plugins/check_cronjob -C NAME -s enabled
@HafizNaveed
Copy link

Hi,

I have followed this article and run this command "command[check_NAME_cron]=/usr/lib/nagios/plugins/check_cronjob -C NAME -s enabled"

How I can get the name of cronjob Name?

Thanks

@re-schneider
Copy link

Hi,

thanks for sharing this Nagios probe. I was thinking about maybe extending it to cover some form of last run status too (I have not started tinkering with it yet).
However, as you did not attach any license to your code yet, I do not know if the extended version I want to create would be compatible with the MIT license (see https://github.com/AmadeusITGroup/monitoring-plugins repo on GitHub).

Thanks in advance for your help clarifying if I can build upon your work (with credits where they are due) or if it would be better to separate the additional checks in a separate probe to avoid licensing conflicts :)

@nicolasmarengo
Copy link
Author

@re-schneider . Please feel free to re-use, extend, etc. Happy for it to go on MIT license. Thanks for getting in touch.

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