Xymon Mailing List Archive search

contchk.sh (submission)

list Charles Jones
Wed, 30 Jan 2008 11:22:05 -0700
Message-Id: <user-dd07d87342fc@xymon.invalid>

The past week I've been trying to share any of the hobbit ext scripts that I wrote.  Here's another, contchk.sh. I initially created it because Hobbits content check does not follow "302 Redirect" response codes, and I also had a need to check a URL which was expecting a certain referrer.  I remembered I was using this script when I saw Gary Baluha's urlplus.pl.

Incidentally, I cannot use urlplus, as it makes use of bbhostgrep, which I discovered fails if your bb-host entries are too long.

contchk.sh is a fairly short bash script which makes use of curl. You can optionally specify a referrer to spoof. For most folks, Garys script is probably best to use, as it has more enhanced error messages and such. If anything, it's interesting to see different ways multiple people tackle a problem :)

Note: besides the script itself (which should reside in $BBHOME/ext), you will need to add this to your $BBHOME/etc/hobbitlaunch.cfg:
[contchk]
        ENVFILE /home/monitor/server/etc/hobbitserver.cfg
        NEEDS hobbitd
        CMD $BBHOME/ext/contchk.sh
        LOGFILE $BBSERVERLOGS/contchk.log
        INTERVAL 5m

-Charles

#!/bin/bash
# contchk.sh written by Charles Jones (user-02bccbb1bbb5@xymon.invalid) 6/6/2007
# This script is designed to perform a content check on a URL and report the
# status to a Hobbit server.
#
# This script was created because Hobbits built-in content check functionality
# does not follow 302 redirects.
#
# The script parses out a "contchk" tag in the bb-hosts file. The proper
# syntax is: contchk;URL;REFERRER;CHECKSTRING
#
# Note that CHECKSTRING cannot contain spaces so you must use regular
# expression metacharacters, so use something like string.with.spaces
BBHTAG=contchk     # Name of the tag in bb-hosts
COLUMN=cont        # Column display name in Hobbit
CURL=/usr/bin/curl # Location of curl binary
CURLOPTS="--connect-timeout 30 -m 30 -s -L -b cookiejar" # Curl options
# Note: using grep because bbhostgrep fails on long lines
grep $BBHTAG $BBHOME/etc/bb-hosts | while read L
   do
      set $L    # To get one line of output from bbhostgrep
      HOSTIP="$1"
      MACHINEDOTS="$2"
      MACHINE=`echo $2 | $SED -e's/\./,/g'`
      CHECKURL=`echo $4 | awk -F";" '{print $2}'`    # Parse out the check URL
      REFERRER=`echo $4 | awk -F";" '{print $3}'`    # Parse out the referrer string
      if [ "" != "$REFERRER" ];
         then
           REFERRER="-e $REFERRER"
      fi
      CHECKSTRING=`echo $4 | awk -F";" '{print $4}'` # Parse out the check string
      $CURL $CURLOPTS $REFERRER $CHECKURL |grep -q "$CHECKSTRING"
      status=$? # Save greps return status
      if [ 0 -eq $status ]; then # grep returns 0 if it found something
        COLOR=green
        MSG="String <b>\"$CHECKSTRING\"</b> was found in <a href=$CHECKURL>$CHECKURL</a>"
        $BB $BBDISP "status $MACHINE.$COLUMN $COLOR `date` Content Check OK
        ${MSG}
        "
      else # grep didn't find anything
        COLOR=red
        MSG="String <b>\"$CHECKSTRING\"</b> was NOT FOUND in <a href=$CHECKURL>$CHECKURL</a>"
        $BB $BBDISP "status $MACHINE.$COLUMN $COLOR `date` Content Check FAILED
        ${MSG}
        "
      fi
   done
exit 0