From 9000b773493f4283437a85b5ea156a822a7f477f Mon Sep 17 00:00:00 2001 From: JHCD Date: Wed, 20 May 2015 21:03:58 +0200 Subject: [PATCH] implement MySQL-plugin --- boswatch.py | 12 +++++++----- plugins/MySQL/MySQL.py | 43 +++++++++++++++++++++++++++++------------- plugins/interface.txt | 2 +- 3 files changed, 38 insertions(+), 19 deletions(-) mode change 100644 => 100755 boswatch.py diff --git a/boswatch.py b/boswatch.py old mode 100644 new mode 100755 index 7af76b6..f1685ed --- a/boswatch.py +++ b/boswatch.py @@ -203,8 +203,10 @@ try: #decoded = str(multimon_ng.stdout.readline()) #Get line data from multimon stdout #only for develop - decoded = "ZVEI2: 25832" + #decoded = "ZVEI2: 25832" #decoded = "FMS: 43f314170000 (9=Rotkreuz 3=Bayern 1 Ort 0x25=037FZG 7141Status 3=Einsatz Ab 0=FZG->LST 2=III(mit NA,ohneSIGNAL)) CRC correct\n'" + #decoded = "POCSAG512: Address: 1234567 Function: 0 Alpha: Hello World" + #decoded = "POCSAG512: Address: 1234567 Function: 1 Alpha: XXMSG MEfeweffsjh" time.sleep(1) @@ -233,7 +235,7 @@ try: fms_time_old = timestamp #in case of double alarm, fms_double_ignore_time set new else: logging.info("FMS:%s Status:%s Richtung:%s TKI:%s", fms_id[0:8], fms_status, fms_direction, fms_tsi) - data = {"fms":fms_id[0:8], "status":fms_status, "direction":fms_direction, "tki":fms_tsi} + data = {"fms":fms_id[0:8], "status":fms_status, "direction":fms_direction, "tsi":fms_tsi} throwAlarm("FMS",data) fms_id_old = fms_id #save last id @@ -280,7 +282,7 @@ try: if re.search("[0-9]{7}", poc_id): #if POC is valid if poc_id >= globals.config.getint("BOSWatch", "poc_filter_range_start"): - if poc_id <= globals.config.getint("BOSWatch", "poc_filter_range_end"): + if poc_id <= globals.config.getint("BOSWatch", "poc_filter_range_end"): if poc_id == poc_id_old and timestamp < poc_time_old + globals.config.getint("BOSWatch", "poc_double_ignore_time"): #check for double alarm logging.warning("POC512 double alarm: %s within %s second(s)", poc_id_old, timestamp-poc_time_old) poc_time_old = timestamp #in case of double alarm, poc_double_ignore_time set new @@ -292,9 +294,9 @@ try: poc_id_old = poc_id #save last id poc_time_old = timestamp #save last time else: - logging.warning("POCSAG512: %s out of filter range", poc_id) + logging.warning("POCSAG512: %s out of filter range (high)", poc_id) else: - logging.warning("POCSAG512: %s out of filter range", poc_id) + logging.warning("POCSAG512: %s out of filter range (low)", poc_id) else: logging.warning("No valid POCSAG512: %s", poc_id) diff --git a/plugins/MySQL/MySQL.py b/plugins/MySQL/MySQL.py index 3d8a5d9..61536b1 100644 --- a/plugins/MySQL/MySQL.py +++ b/plugins/MySQL/MySQL.py @@ -3,28 +3,45 @@ import logging # Global logger import globals # Global variables +import mysql +import mysql.connector def run(typ,freq,data): try: #ConfigParser logging.debug("reading config file") try: - for key,val in globals.config.items("BOSWatch"): + for key,val in globals.config.items("MySQL"): logging.debug(" - %s = %s", key, val) except: logging.exception("cannot read config file") - - if typ == "FMS": - #logging.debug("FMS: %s Status: %s Dir: %s", data["fms"], data["status"], data["direction"]) - logging.debug("FMS") - elif typ == "ZVEI": - #logging.debug("ZVEI: %s", data["zvei"]) - logging.debug("ZVEI") - elif typ == "POC": - #logging.debug("POC: %s/%s - %s", data["ric"], data["function"], data["msg"]) - logging.debug("POC") - else: - logging.warning(typ + " not supportet") + #open DB-Connection + try: + 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) + finally: + connection.close() #Close connection in every case except: logging.exception("unknown error") \ No newline at end of file diff --git a/plugins/interface.txt b/plugins/interface.txt index cf0887e..73d9c5a 100644 --- a/plugins/interface.txt +++ b/plugins/interface.txt @@ -14,7 +14,7 @@ FMS: - fms - status - direction -- tki +- tsi POCSAG: - ric