Nagios Custom Plugin: Syslog Directory Increment Check
So at my job, I need a generic script for Nagios to help me make sure the syslog from other servers are coming in.
After some research and banging my head because I haven't code bash script for years, here is my solution:
#!/bin/bash
# Default Check Path
DEFAULT_PATH=/var/log/syslogs/
if [ -z "$1" ]; then
CHECK_PATH=$DEFAULT_PATH
else
CHECK_PATH=$1
fi
LOG_FILE=$CHECK_PATH'.LOG_SIZE'
# Set Directory size and logged size
dirSIZE=$(du -s $CHECK_PATH | cut -d'/' -f1)
logSIZE=$(tail -1 $LOG_FILE | cut -d' ' -f2)
# Set timestamp var.
CT=$(date +%s)
LT=$(tail -1 $LOG_FILE | cut -d':' -f1)
TD=$(($CT - $LT | bc))
if [ "$dirSIZE" -eq "$logSIZE" ]; then
if [ $(($TD / 60)) -lt 10 ]; then
echo "OK - No change for $(($TD / 60)) minutes"
exit 0
elif [ $(($TD / 60)) -le 15 ]; then
echo "WARNING - No change for $(($TD / 60)) minutes"
exit 1
else
echo "CRITICAL - No change for $(($TD / 60)) minutes"
exit 2
fi
elif [ "$dirSIZE" -ne "$logSIZE" ]; then
echo "OK - $CHECK_PATH: $dirSIZE"
echo "$CT: $dirSIZE" > $CHECK_PATH.LOG_SIZE
exit 0
else
echo "UNKOWN - $0 failed"
exit 4
fi
Reference: How To Create Nagios Plugins With Bash On Ubuntu 12.10