#!/usr/bin/python #=================================================================================================================== # # FILE: check_form_mech.py # # USAGE: # # DESCRIPTION: Checks a form by pretenting to be a browser # # OPTIONS: --- # REQUIREMENTS: # BUGS: # NOTES: --- # AUTHOR: Tim Pretlove # VERSION: 0.1 # CREATED: 07-09-2010 # REVISION: --- # LICENCE: GNU # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # #=================================================================================================================== import re import mechanize import argparse import time import sys parser = argparse.ArgumentParser() parser.add_argument('-u', '--url', required=True, help='url string e.g -u http://foobar.com') parser.add_argument('-f', '--formname', required=True, help='name of the form e.g -f login') parser.add_argument('-n', '--values', required=True, help='values for input e.g -n "login=foo&password=bar"') parser.add_argument('-w', '--warning', type=float, default=False, help='time in seconds before warning e.g -w 5') parser.add_argument('-c', '--critical', type=float, default=False, help='time in seconds before critical e.g -c 10') parser.add_argument('-v', '--verbose', action='store_true', help='print out nagios timing information') parser.add_argument('-e', '--expect', required=True, help='string to find on returned web page e.g -e "Hello Sweetie"') args = parser.parse_args(); if args.verbose: if args.critical == False or args.warning == False: print "error: -v specified but no -w or -c" print parser.print_help() sys.exit(4) if args.critical < args.warning: print "error: warning greater than critical" print parser.print_help() sys.exit(4) br = mechanize.Browser(factory=mechanize.RobustFactory()) br.addheaders = [('User-agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.6')] starttime = time.time() br.open(args.url) br.select_form(name=args.formname) sep = args.values.split('&') dict1 = {} for vals in sep: l, m = vals.split('=') dict1[l] = m for n, o in dict1.iteritems(): br[n] = o response = br.submit() res = response.read() elapsed = time.time() - starttime expectstr = re.search(args.expect, res) def checks(): nagname = "HTTPFORM" status = ["OK", "WARNING", "CRITICAL","CRITICAL"] message = ["Recieved expect string","host slow responding","Failed to retrieve expect string","host critical response time"] if args.critical: if elapsed >= args.critical: print "%s %s %s" % (nagname, status[3], message[3]), return(2) if args.warning: if elapsed >= args.warning: print "%s %s %s" % (nagname, status[1], message[1]), return(1) if expectstr: print "%s %s: %s" % (nagname, status[0], message[0]), return(0) else: print "%s %s: %s" % (nagname, status[2], message[2]), return(2) retcode = checks() if args.verbose: strr = "|time=" + str(elapsed) + ";" + str(args.warning) + ";0;" + str(args.critical) sys.stdout.write(strr) sys.exit(retcode)