%PDF- %PDF-
Direktori : /lib64/nagios/plugins/nccustom/ |
Current File : //lib64/nagios/plugins/nccustom/check_http_full_stack.py |
#!/usr/libexec/platform-python # Version 2019/07/12 # Created by Bogdan Kukharskiy, NameCheap # Version 2019/08/01 # Added perfdata output for nagios # Version 2019/08/05 # Added 301/302 redirect recognition # ConfigParser - python2 # configparser - python3 import requests, argparse, re, sys, configparser, os def print_result(status, astring, domain, url, responseTime): rtms = round(responseTime * 1000, 3) # convert to milliseconds print(str(status) + ": " + astring + domain + url + " | ResponseTime=" + str(rtms) +"ms;800;1000") if __name__ == "__main__": #parsing arguments parser = argparse.ArgumentParser() parser.add_argument("-v", "--verbose", default=False, action="store_true", help="Extra output") parser.add_argument("-d", "--domain", type=str, help="Domain name to test", required=True) parser.add_argument("-u", "--url", type=str, default="/monitoring/nc-test-htfs.php", help="link to test, started with / (by default: /monitoring/nc-test-htfs.php)") parser.add_argument("-c", "--configfile", type=str, default="", help="Config file with patterns, full path") args = parser.parse_args() #parsing config file config = configparser.ConfigParser() # - python 3 #config = ConfigParser.ConfigParser() if args.configfile == "": configFileName = os.path.join(os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(sys.argv[0]))), 'check_http_full_stack.conf') else: configFileName = args.configfile config.read(configFileName) OKPattern = re.compile(config.get('Pattern', 'OK')) MysqlConnectionFailedPattern = re.compile(config.get('Pattern', 'MysqlConnectionFailed')) # main routine try: r = requests.get('http://' + args.domain + args.url, allow_redirects=False) if args.verbose == True: print("Status code: " + str(r.status_code)) print("Header: \n" + str(r.headers)) print("Body: \n" + str(r.text)) if r.status_code == 200: # 200 we got correct response, checking if connection to Mysql is OK if OKPattern.match(r.text): print_result(r.status_code, "All OK at http://", args.domain, args.url, r.elapsed.total_seconds()) sys.exit(0) elif MysqlConnectionFailedPattern.match(r.text): print_result(r.status_code, "Connection to MySQL failed: " + r.text.split("<br>")[1], "", "", r.elapsed.total_seconds()) sys.exit(2) else: print_result(r.status_code, "Connection via HTTP is ok, but got unknown response pattern in body", "", "", r.elapsed.total_seconds()) sys.exit(2) elif r.status_code == 301 or r.status_code == 302: print_result(r.status_code, "HTTP connection OK, but got too many redirects (wrong .htaccess?) - http://", args.domain, args.url, r.elapsed.total_seconds()) sys.exit(2) elif r.status_code == 401: print_result(r.status_code, "Unauthorized Request - http://", args.domain, args.url, r.elapsed.total_seconds()) sys.exit(2) elif r.status_code == 403: print_result(r.status_code, "Requested URL is forbidden - http://", args.domain, args.url, r.elapsed.total_seconds()) sys.exit(2) elif r.status_code == 404: print_result(r.status_code, "Requested URL not found - http://", args.domain, args.url, r.elapsed.total_seconds()) sys.exit(2) elif r.status_code == 500: print_result(r.status_code, "Internal Server Error - http://", args.domain, args.url, r.elapsed.total_seconds()) sys.exit(2) elif r.status_code == 502: print_result(r.status_code, "Bad gateway - http://", args.domain, args.url, r.elapsed.total_seconds()) sys.exit(2) elif r.status_code == 503: print_result(r.status_code, "Service Unavailable (Apache down?) - http://", args.domain, args.url, r.elapsed.total_seconds()) sys.exit(2) else: print_result(r.status_code, "Undescribed response for HTTP request at http://", args.domain, args.url, r.elapsed.total_seconds()) sys.exit(2) except requests.ConnectionError: print("Failed to connect to http://" + args.domain + args.url + ". HAProxy down? Wrong DNS response?") sys.exit(2)