#! /usr/bin/python # Kirk Hammond / 20130318 / kirkdhammond@gmail.com # This script performs a non case-sensitive string match for content on a web page. It's designed to detect when httpd is up, but website content is not displayed properly. # v1.0 # Imports import sys, re, urllib2 ############################ ## Start Global Variables ## # Nagios Return Codes UNKNOWN = -1 OK = 0 WARNING = 1 CRITICAL = 2 usage = 'usage: ./check_http_content.py ' # ensure there is a value for url and string, if there is not print usage and exit. No data validity check is done, we only check that there is a value for each sys.argv needed by the script. if len(sys.argv) >= 2: url = sys.argv[1] if len(sys.argv) == 3: string = sys.argv[2] else: print usage sys.exit(1) ## End Global Variables ## ########################## # Read the website into a variable def get_content(url): sitetext = urllib2.urlopen(url) return sitetext # Process sitetext for string match def string_match(sitetext): # set status to not found, each time a match is found status is changed to 'match found'. status = 'match not found' for line in sitetext.readlines(): if re.search(string, line, re.IGNORECASE): status = 'match found' return status # Determine state to pass to Nagios def nag_state(status): if status == 'match found': print 'String','"',string,'"','found on page: OK' sys.exit(OK) elif status == 'match not found': print 'String', string, 'not found on page: CRITICAL' sys.exit(CRITICAL) # Main function controls the flow and order of other functions. def main(): sitetext = get_content(url) status = string_match(sitetext) nag_state(status) # call main function if __name__ == '__main__': main()