Don McMorris commented on the check_null script as being a bit of a hack that was pounded on until it worked in the Massachusetts Library Network Cooperative session. I took a look afterward and decided to give a bit of a rewrite a try.

I have attached a patch file, but have not been able to test it in full due to our test system not having any significant logging set up yet. Still, some partial tests make me think it works fine.

My rewrite tries to avoid some of the looping over the same logfile multiple times, adds in a pile of checking of options and such, and should fix the problem of not getting proper counts across midnight mentioned in the comments on the original.

Thomas Berezansky
Merrimack Valley Library Consortium




Index: ESI-Examples/sys/lib/nagios-plugins/check_null
===================================================================
--- ESI-Examples/sys/lib/nagios-plugins/check_null	(revision 879)
+++ ESI-Examples/sys/lib/nagios-plugins/check_null	(working copy)
@@ -2,6 +2,7 @@
 #
 # Copyright (C) 2008-2009  Equinox Software, Inc.
 # Written by Don McMorris <[email protected]>
+# Partially Re-written by Thomas Berezansky <[email protected]>
 #
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License
@@ -15,78 +16,59 @@
 #
 #
 
-
+typeset -i PERIOD
+typeset -i WARNLIMIT
+typeset -i CRITLIMIT
+typeset -i NULLTOT
+PERIOD=$1
 WARNLIMIT=$2
 CRITLIMIT=$3
-PERIOD=$1
-# Note: These should really be checked to ensure they are defined and within range...
+if [ ! $PERIOD -gt 0 -o ! $PERIOD -le 60 ]; then
+	echo "WARN: Period must be defined and greater than 0 but less than or equal to 60"
+	exit 1
+elif [ ! $WARNLIMIT -gt 0 ]; then
+	echo "WARN: Warn limit must be defined and greater than 0"
+	exit 1
+elif [ ! $CRITLIMIT -gt $WARNLIMIT ]; then
+	echo "WARN: Crit limit must be defined and greater than warn limit"
+	exit 1
+fi
 
-PREVTOT=0
-LOGFILE="/var/log/remote/prod/$(date +%Y/%m/%d)/gateway.$(date +%H).log"
+NULLTOT=0
+CURLOGFILE="/var/log/remote/prod/$(date +%Y/%m/%d/gateway.%H).log"
+PREVLOGFILE="/var/log/remote/prod/$(date --date="$PERIOD minutes ago" +%Y/%m/%d/gateway.%H).log"
+STARTMIN=$(date --date="$PERIOD minutes ago" +%M)
 
-if [ $(date +%H | cut -b1) = 0 ]; then
-	CURRHOUR=$(date +%H | cut -b2)
-else
-	CURRHOUR=$(date +%H)
+if [ ! -f $CURLOGFILE ]; then
+	echo "WARN: Current log file missing"
+	exit 1
 fi
 
-if [ $(date +%M | cut -b1) = 0 ]; then
-        CURRMIN=$(date +%M | cut -b2 )
-else
-        CURRMIN=$(date +%M)
+if [ $CURLOGFILE != $PREVLOGFILE ]; then
+	[ -f $PREVLOGFILE ] && NULLTOT=$(grep "Returning NULL" $PREVLOGFILE | cut -d":" -f2 | grep -c "^$(seq -s"\|" -f"%02.0f" $STARTMIN 59)$")
+	STARTMIN=0
 fi
 
-if [ $CURRMIN -lt $PERIOD ]; then
-	# How many minutes of the last hour do we need to check?
-        TMPDIFFM2=$((60 - $(($PERIOD - $CURRMIN))))
+NULLTOT=$(($NULLTOT + $(grep "Returning NULL" $CURLOGFILE | cut -d":" -f2 | grep -c "^$(seq -s"\|" -f"%02.0f" $STARTMIN $(date +%M))$")))
 
-	# This logic will mean that "Returning NULL"'s logged at the late 2300 hour will not be counted during the early Midnight hour check.
-	# This is acceptable for now.
-        if [ $CURRHOUR -gt 0 ]; then
-		# define LOGFILE2 (last hours' log)
-		if [ $CURRHOUR -gt 11 ]; then
-			LOGFILE2="/var/log/remote/prod/$(date +%Y/%m/%d)/gateway.$(($CURRHOUR - 1)).log"
-		else
-			LOGFILE2="/var/log/remote/prod/$(date +%Y/%m/%d)/gateway.0$(($CURRHOUR - 1)).log"
-		fi
+TOPSERVER=$(grep "Returning NULL" $CURLOGFILE | cut -d" " -f3 | sort | uniq -c | sort -nr | head -1)
 
-		while [ $TMPDIFFM2 -lt 60 ]; do
-			PREVTOT=$(($PREVTOT + $(grep "Returning NULL" $LOGFILE2 | cut -d":" -f2 | grep -c $TMPDIFFM2)))
-			TMPDIFFM2=$(($TMPDIFFM2 + 1))
-		done
-        fi
-	while [ $TMPDIFF1 -le $CURRMIN ]; do
-		PREVTOT=$(($PREVTOT + $(grep "Returning NULL" $LOGFILE | cut -d":" -f2 | grep -c $TMPDIFF1)))
-	        TMPDIFF1=$(($TMPDIFF1 + 1))
-	done
-else
-	TMPDIFF1=$(($CURRMIN-$PERIOD))
-	while [ $TMPDIFF1 -le $CURRMIN ]; do
-		PREVTOT=$(($PREVTOT + $(grep "Returning NULL" $LOGFILE | cut -d":" -f2 | grep -c $TMPDIFF1)))
-	        TMPDIFF1=$(($TMPDIFF1 + 1))
-	done
-
-fi
-
-
-TOPSERVER=$(grep "Returning NULL" $LOGFILE | cut -d" " -f3 | sort | uniq -c | sort -nr | head -1)
-
 if [ "$TOPSERVER" != null ]; then
 	SVRMSG=" (Top server this hour: $TOPSERVER)"
 else
 	SVRMSG="."
 fi
 
-if [ $PREVTOT -ge $CRITLIMIT ]; then
-        echo "CRIT: $PREVTOT NULLs returned in past $PERIOD minutes$SVRMSG"
+if [ $NULLTOT -ge $CRITLIMIT ]; then
+        echo "CRIT: $NULLTOT NULLs returned in past $PERIOD minutes$SVRMSG"
         exit 2
-elif [ $PREVTOT -ge $WARNLIMIT ]; then
-        echo "WARN: $PREVTOT NULLs returned in the past $PERIOD minutes$SVRMSG"
+elif [ $NULLTOT -ge $WARNLIMIT ]; then
+        echo "WARN: $NULLTOT NULLs returned in the past $PERIOD minutes$SVRMSG"
         exit 1
-elif [ $PREVTOT -lt $WARNLIMIT ]; then
-        echo "OK: $PREVTOT NULLs returned in the past $PERIOD minutes$SVRMSG"
+elif [ $NULLTOT -lt $WARNLIMIT ]; then
+        echo "OK: $NULLTOT NULLs returned in the past $PERIOD minutes$SVRMSG"
         exit 0
 else
-        echo "WARN: An error has occurred $PREVTOT $PERIOD"
+        echo "WARN: An error has occurred $NULLTOT $PERIOD"
         exit 1
 fi
Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.

Signed-off-by: Thomas Berezansky <[email protected]>

Reply via email to