Monitoring web apps using html forms for logging in

Several times I’ve wanted to monitor if login actually works on webapplications that uses html forms for user validation. Today I made a simple bash nagios plugin to do that. It uses curl and checks it’s output. Curl supports POST variables.
The plugin also checks the unixtime before and after the curl command is being run, then does and expr to find the diff and makes nagios performance data of it.

Feel free to copy paste the code and use it for your own purpose.

#!/bin/sh
#my %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4);
#This is what we get after a successful login
MATCH="bladiblah successful login bladiblah"
#delete the tmp file before writing a new one
rm /tmp/login_check
#Grab the unixtime before the command runs
BEFORE=`date +%s`
#Login to the app
/usr/bin/curl -H "host: example.com" -F mail="mail@example.com" -F userPassword="fail" http://111.111.111.111 -o /tmp/login_check > /dev/null 2>&1
#Grab the unixtime after the command have been run
AFTER=`date +%s`
DIFF=`/usr/bin/expr $AFTER - $BEFORE`
#Check tmp file if it matches our successful login
CHECK=`grep "$MATCH" /tmp/login_check`
if [ "$CHECK" = "$MATCH" ]; then
#WIN!!
echo "OK. Login successful. | response=$DIFF"
exit 0
else
#FAIL!!
echo "CRITICAL. Login failed."
exit 2

And as always, sorry about the wordpress formating of the code.

Tags: , , , , , , ,