From 7aa9165ba2f8a5cc5a0c2eb2d3276fc12fd23496 Mon Sep 17 00:00:00 2001 From: Bastian Schroll Date: Wed, 8 Apr 2015 21:21:50 +0200 Subject: [PATCH] check and log the HTTP response --- boswatch.py | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/boswatch.py b/boswatch.py index 42326cc..8f1a9bf 100644 --- a/boswatch.py +++ b/boswatch.py @@ -20,6 +20,7 @@ import argparse #for parse the args import ConfigParser #for parse the config file import re #Regex for validation + # Functions def curtime(format="%Y-%m-%d %H:%M:%S"): return time.strftime(format) @@ -238,7 +239,7 @@ try: fms_time_old = timestamp #save last time if useMySQL: #only if MySQL is active - log("insert FMS into MySQL") + log("FMS to MySQL") try: connection = mysql.connector.connect(host = str(dbserver), user = str(dbuser), passwd = str(dbpassword), db = str(database)) cursor = connection.cursor() @@ -246,19 +247,22 @@ try: cursor.close() connection.commit() except: - log("FMS cannot insert into MySQL","error") + log("FMS to MySQL failed","error") finally: connection.close() #Close connection in every case if useHTTPrequest: #only if HTTPrequest is active - log("FMS to HTTP request") + log("FMS to HTTP") try: httprequest = httplib.HTTPConnection(url) httprequest.request("HEAD", "/") httpresponse = httprequest.getresponse() - #if args.verbose: print httpresponse.status, httpresponse.reason + if str(httpresponse.status) == "200": #Check HTTP Response an print a Log or Error + log("HTTP response: "+str(httpresponse.status)+" - "+str(httpresponse.reason)) + else: + log("HTTP response: "+str(httpresponse.status)+" - "+str(httpresponse.reason),"error") except: - log("FMS HTTP request failed","error") + log("FMS to HTTP failed","error") else: log("No valid FMS: "+fms_id) else: @@ -281,7 +285,7 @@ try: zvei_time_old = timestamp #save last time if useMySQL: #only if MySQL is active - log("insert ZVEI into MySQL") + log("ZVEI to MySQL") try: connection = mysql.connector.connect(host = str(dbserver), user = str(dbuser), passwd = str(dbpassword), db = str(database)) cursor = connection.cursor() @@ -289,22 +293,28 @@ try: cursor.close() connection.commit() except: - log("ZVEI cannot insert into MySQL","error") + log("ZVEI to MySQL failed","error") finally: connection.close() #Close connection in every case if useHTTPrequest: #only if HTTPrequest is active - log("ZVEI to HTTP request") + log("ZVEI to HTTP") try: httprequest = httplib.HTTPConnection(url) httprequest.request("HEAD", "/") httpresponse = httprequest.getresponse() - #if args.verbose: print httpresponse.status, httpresponse.reason + if str(httpresponse.status) == "200": #Check HTTP Response an print a Log or Error + log("HTTP response: "+str(httpresponse.status)+" - "+str(httpresponse.reason)) + else: + log("HTTP response: "+str(httpresponse.status)+" - "+str(httpresponse.reason),"error") + except: + log("ZVEI to HTTP failed","error") except: - log("ZVEI HTTP request failed","error") + log("ZVEI to HTTP failed","error") else: log("No valid ZVEI: "+zvei_id) + except KeyboardInterrupt: log("Keyboard Interrupt","error") except: