Xymon Mailing List Archive search

contchk.sh (submission)

3 messages in this thread

list Charles Jones · Wed, 30 Jan 2008 11:22:05 -0700 ·
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
list Gary Baluha · Thu, 31 Jan 2008 11:35:51 -0500 ·
quoted from Charles Jones
On Jan 30, 2008 1:22 PM, Charles Jones <user-e86b4aeade4e@xymon.invalid> wrote:
...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.
Hmm, I haven't run into bb-host entries that long.  Can you give an example
of one of them? I'd be interested in seeing if my script can be tweaked to
work around that issue.

Incidentally, I'm working on another release of urlplus that adds form
submission checking, as well as being able to handle multiple-step form
submissions (submit something to one webpage, check the result and submit on
that new webpage, etc).  I'm also cleaning up the commenting and rearranging
a few things in the code to make it easier to modify (including a hard-coded
value that I moved to a user-modifiable constant.
list Charles Jones · Fri, 01 Feb 2008 09:44:26 -0700 ·
quoted from Gary Baluha
Gary Baluha wrote:
On Jan 30, 2008 1:22 PM, Charles Jones <user-e86b4aeade4e@xymon.invalid <mailto:user-e86b4aeade4e@xymon.invalid>> wrote:

    ...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.


Hmm, I haven't run into bb-host entries that long.  Can you give an example of one of them? I'd be interested in seeing if my script can be tweaked to work around that issue.
Upon re-testing, it looks like the actual reason for the failure was that by default bbhostgrep only finds space-delimited tags, and so to find a tag that is tagname=text or tagname;text;text, you must use a wildcard - "bbhostgrep tagname*". This is actually in the man page and I guess I just didn't read it thoroughly before :)
Incidentally, I'm working on another release of urlplus that adds form submission checking, as well as being able to handle multiple-step form submissions (submit something to one webpage, check the result and submit on that new webpage, etc).  I'm also cleaning up the commenting and rearranging a few things in the code to make it easier to modify (including a hard-coded value that I moved to a user-modifiable constant.
I think Henrik has plans to implement something similar, you basically need to be able to dynamically and temporarily store cookies and possibly spoof referrer.