MySQL Plugin Error handling

This commit is contained in:
Bastian Schroll 2015-05-20 21:51:13 +02:00
parent 5a405e1ddb
commit 463bcefa81

View file

@ -15,33 +15,37 @@ def run(typ,freq,data):
logging.debug(" - %s = %s", key, val)
except:
logging.exception("cannot read config file")
#open DB-Connection
try:
logging.debug("connect to MySQL")
connection = mysql.connector.connect(host = globals.config.get("MySQL","dbserver"), user = globals.config.get("MySQL","dbuser"), passwd = globals.config.get("MySQL","dbpassword"), db = globals.config.get("MySQL","database"))
cursor = connection.cursor()
if typ == "FMS":
#data = {"fms":fms_id[0:8], "status":fms_status, "direction":fms_direction, "tsi":fms_tsi}
cursor.execute("INSERT INTO "+globals.config.get("MySQL","tableFMS")+" (time,fms,status,direction,tsi) VALUES (NOW(),%s,%s,%s,%s)",(data["fms"],data["status"],data["direction"],data["tsi"]))
elif typ == "ZVEI":
#data = {"zvei":zvei_id}
cursor.execute("INSERT INTO "+globals.config.get("MySQL","tableZVEI")+" (time,zvei) VALUES (NOW(),"+data["zvei"]+")")
elif typ == "POC":
#data = {"ric":poc_id, "function":poc_sub, "msg":poc_text}
cursor.execute("INSERT INTO "+globals.config.get("MySQL","tablePOC")+" (time,ric,funktion,text) VALUES (NOW(),%s,%s,%s)",(data["ric"],data["function"],data["msg"]))
else:
logging.warning(typ + " not supportet")
return
cursor.close()
connection.commit()
except:
logging.exception("%s to MySQL failed", typ)
logging.exception("cannot connect to MySQL")
else:
try:
logging.debug("Insert %s", typ)
if typ == "FMS":
#data = {"fms":fms_id[0:8], "status":fms_status, "direction":fms_direction, "tsi":fms_tsi}
cursor.execute("INSERT INTO "+globals.config.get("MySQL","tableFMS")+" (time,fms,status,direction,tsi) VALUES (NOW(),%s,%s,%s,%s)",(data["fms"],data["status"],data["direction"],data["tsi"]))
elif typ == "ZVEI":
#data = {"zvei":zvei_id}
cursor.execute("INSERT INTO "+globals.config.get("MySQL","tableZVEI")+" (time,zvei) VALUES (NOW(),"+data["zvei"]+")")
elif typ == "POC":
#data = {"ric":poc_id, "function":poc_sub, "msg":poc_text}
cursor.execute("INSERT INTO "+globals.config.get("MySQL","tablePOC")+" (time,ric,funktion,text) VALUES (NOW(),%s,%s,%s)",(data["ric"],data["function"],data["msg"]))
else:
logging.warning(typ + " not supportet")
except:
logging.exception("cannot Insert %s", typ)
finally:
cursor.close()
connection.close() #Close connection in every case
except:
logging.exception("unknown error")