From e4e3de1abdc2bf2334cfd359333d691aa67c87ed Mon Sep 17 00:00:00 2001 From: Ricardo Krippner Date: Thu, 29 Sep 2016 14:22:23 +0200 Subject: [PATCH 1/4] updated MySQL Plugin --- plugins/MySQL/MySQL.py | 35 ++++++++-- plugins/MySQL/boswatch.sql | 136 +++++++++++++++++++++++++++++-------- 2 files changed, 139 insertions(+), 32 deletions(-) diff --git a/plugins/MySQL/MySQL.py b/plugins/MySQL/MySQL.py index 80ddadc..c9f2b27 100644 --- a/plugins/MySQL/MySQL.py +++ b/plugins/MySQL/MySQL.py @@ -1,5 +1,5 @@ #!/usr/bin/python -# -*- coding: UTF-8 -*- +# -*- coding: cp1252 -*- """ MySQL-Plugin to dispatch FMS-, ZVEI- and POCSAG - messages to a MySQL database @@ -20,6 +20,26 @@ from includes import globals # Global variables from includes.helper import configHandler +def isSignal(poc_id): + """ + @type poc_id: string + @param poc_id: POCSAG Ric + + @requires: Configuration has to be set in the config.ini + + @return: True if the Ric is Signal, other False + @exception: none + """ + # If RIC is Signal return True, else False + if globals.config.get("MySQL", "signal_ric"): + if poc_id in globals.config.get("MySQL", "signal_ric"): + logging.info("RIC %s is signal", poc_id) + return True + else: + logging.info("RIC %s is not signal", poc_id) + return False + + ## # # onLoad (init) function of plugin @@ -71,7 +91,7 @@ def run(typ,freq,data): # Connect to MySQL # 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"), charset='utf8') + 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() except: logging.error("cannot connect to MySQL") @@ -84,13 +104,18 @@ def run(typ,freq,data): logging.debug("Insert %s", typ) if typ == "FMS": - cursor.execute("INSERT INTO "+globals.config.get("MySQL","tableFMS")+" (time, fms, status, direction, directionText, tsi, description) VALUES (FROM_UNIXTIME(%s),%s,%s,%s,%s,%s,%s)", (data["timestamp"], data["fms"], data["status"], data["direction"], data["directionText"], data["tsi"], data["description"])) + cursor.execute("INSERT INTO "+globals.config.get("MySQL","tableFMS")+" (time,fms,status,direction,directionText,tsi,description) VALUES (NOW(),%s,%s,%s,%s,%s,%s)",(data["fms"],data["status"],data["direction"],data["directionText"],data["tsi"],data["description"])) elif typ == "ZVEI": - cursor.execute("INSERT INTO "+globals.config.get("MySQL","tableZVEI")+" (time, zvei, description) VALUES (FROM_UNIXTIME(%s),%s,%s)", (data["timestamp"], data["zvei"], data["description"])) + cursor.execute("INSERT INTO "+globals.config.get("MySQL","tableZVEI")+" (time,zvei,description) VALUES (NOW(),%s,%s)",(data["zvei"],data["description"])) elif typ == "POC": - cursor.execute("INSERT INTO "+globals.config.get("MySQL","tablePOC")+" (time, ric, function, functionChar, msg, bitrate, description) VALUES (FROM_UNIXTIME(%s),%s,%s,%s,%s,%s,%s)", (data["timestamp"], data["ric"], data["function"], data["functionChar"], data["msg"], data["bitrate"], data["description"])) + if isSignal(data["ric"]): + cursor.execute("UPDATE "+globals.config.get("MySQL","tableSIG")+" SET time = NOW() WHERE ric = "+data["ric"]) + if cursor.rowcount == 0: + cursor.execute("INSERT INTO "+globals.config.get("MySQL","tableSIG")+" (time,ric) VALUES (NOW(),"+data["ric"]+")") + else: + cursor.execute("INSERT INTO "+globals.config.get("MySQL","tablePOC")+" (time,ric,funktion,funktionChar,msg,bitrate,description) VALUES (NOW(),%s,%s,%s,%s,%s,%s)",(data["ric"],data["function"],data["functionChar"],data["msg"],data["bitrate"],data["description"])) else: logging.warning("Invalid Typ: %s", typ) diff --git a/plugins/MySQL/boswatch.sql b/plugins/MySQL/boswatch.sql index 955b1b7..405f52b 100644 --- a/plugins/MySQL/boswatch.sql +++ b/plugins/MySQL/boswatch.sql @@ -19,10 +19,24 @@ SET time_zone = "+00:00"; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; +-- -------------------------------------------------------- + -- --- Datenbank: `boswatch` +-- Datenbank anlegen `boswatch` -- +CREATE DATABASE IF NOT EXISTS boswatch; +USE boswatch; + +-- -------------------------------------------------------- + +-- +-- Benutzer erstellen für Datenbank `boswatch` +-- + +GRANT ALL ON * to 'boswatch'@'localhost' identified by 'boswatch1.mysql'; +FLUSH PRIVILEGES; + -- -------------------------------------------------------- -- @@ -30,16 +44,16 @@ SET time_zone = "+00:00"; -- CREATE TABLE IF NOT EXISTS `bos_fms` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `time` datetime NOT NULL, - `fms` char(8) NOT NULL, - `status` char(1) NOT NULL, - `direction` char(1) NOT NULL, - `directionText` char(10) NOT NULL, - `tsi` varchar(3) NOT NULL, - `description` text, - PRIMARY KEY (`ID`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; + `id` INT(11) NOT NULL AUTO_INCREMENT, + `time` DATETIME NOT NULL, + `fms` VARCHAR(8) NOT NULL, + `status` VARCHAR(1) NOT NULL, + `direction` VARCHAR(1) NOT NULL, + `directionText` TEXT(10) NOT NULL, + `tsi` VARCHAR(3) NOT NULL, + `description` TEXT NOT NULL, + PRIMARY KEY (`ID`) +) ENGINE=MYISAM DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1; -- -------------------------------------------------------- @@ -48,16 +62,16 @@ CREATE TABLE IF NOT EXISTS `bos_fms` ( -- CREATE TABLE IF NOT EXISTS `bos_pocsag` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `time` datetime NOT NULL, - `ric` char(7) NOT NULL, - `function` int(1) NOT NULL, - `functionChar` char(1), - `bitrate` int(4), - `msg` text, - `description` text, - PRIMARY KEY (`ID`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; + `id` INT(11) NOT NULL AUTO_INCREMENT, + `time` DATETIME NOT NULL, + `ric` VARCHAR(7) NOT NULL DEFAULT '0', + `funktion` INT(1) NOT NULL, + `funktionChar` TEXT(1) NOT NULL, + `msg` TEXT NOT NULL, + `bitrate` INT(4) NOT NULL, + `description` TEXT NOT NULL, + PRIMARY KEY (`ID`) +) ENGINE=MYISAM DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1; -- -------------------------------------------------------- @@ -66,13 +80,81 @@ CREATE TABLE IF NOT EXISTS `bos_pocsag` ( -- CREATE TABLE IF NOT EXISTS `bos_zvei` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `time` datetime NOT NULL, - `zvei` char(5) NOT NULL, - `description` text, - PRIMARY KEY (`ID`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; + `id` INT(11) NOT NULL AUTO_INCREMENT, + `time` DATETIME NOT NULL, + `zvei` VARCHAR(5) NOT NULL DEFAULT '0', + `description` TEXT NOT NULL, + PRIMARY KEY (`ID`) +) ENGINE=MYISAM DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1; +-- -------------------------------------------------------- + +-- +-- Tabellenstruktur für Tabelle `bos_signal` +-- + +CREATE TABLE IF NOT EXISTS `bos_signal` ( + `id` INT(11) NOT NULL AUTO_INCREMENT, + `time` DATETIME NOT NULL, + `ric` VARCHAR(7) NOT NULL DEFAULT '0', + PRIMARY KEY (`ID`) +) ENGINE=MYISAM DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1; + +-- -------------------------------------------------------- + +-- +-- Tabellenstruktur für Tabelle `bos_weblogin` +-- + +CREATE TABLE IF NOT EXISTS `bos_weblogin` ( + `id` INT(11) NOT NULL AUTO_INCREMENT, + `user` VARCHAR(150) DEFAULT NULL, + `password` VARCHAR(32) DEFAULT NULL, + `isadmin` BOOLEAN DEFAULT FALSE, + PRIMARY KEY (`id`) +) ENGINE=MYISAM DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1; + +INSERT INTO `bos_weblogin` (`id`, `user`, `password`, `isadmin`) VALUES (NULL, 'admin', '21232f297a57a5a743894a0e4a801fc3', '1'); + +-- -------------------------------------------------------- + +-- +-- Schedule für Tabelle `bos_pocsag` +-- +CREATE EVENT IF NOT EXISTS `Delete POCSAG Entries > 3 Months` + ON SCHEDULE EVERY 1 DAY + STARTS '2016-01-01 00:00:00' + ON COMPLETION PRESERVE ENABLE + DO + DELETE FROM bos_pocsag WHERE time < DATE_SUB(NOW(),INTERVAL 3 MONTH); + +-- -------------------------------------------------------- + +-- +-- Schedule für Tabelle `bos_fms` +-- + +CREATE EVENT IF NOT EXISTS `Delete FMS Entries > 3 Months` + ON SCHEDULE EVERY 1 DAY + STARTS '2016-01-01 00:00:00' + ON COMPLETION PRESERVE ENABLE + DO + DELETE FROM bos_fms WHERE time < DATE_SUB(NOW(),INTERVAL 3 MONTH); + +-- -------------------------------------------------------- + +-- +-- Schedule für Tabelle `bos_zvei` +-- + +CREATE EVENT IF NOT EXISTS `Delete ZVEI Entries > 3 Months` + ON SCHEDULE EVERY 1 DAY + STARTS '2016-01-01 00:00:00' + ON COMPLETION PRESERVE ENABLE + DO + DELETE FROM bos_zvei WHERE time < DATE_SUB(NOW(),INTERVAL 3 MONTH); + + /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; From 8d21a5b9e0a58a8f9439c580342fc49c84914d72 Mon Sep 17 00:00:00 2001 From: Ricardo Krippner Date: Thu, 29 Sep 2016 14:28:55 +0200 Subject: [PATCH 2/4] updated MySQL config --- config/config.template.ini | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/config/config.template.ini b/config/config.template.ini index 3be9c60..24f0c20 100644 --- a/config/config.template.ini +++ b/config/config.template.ini @@ -134,7 +134,7 @@ template = 0 [MySQL] # MySQL configuration dbserver = localhost -dbuser = root +dbuser = boswatch dbpassword = root database = boswatch @@ -142,6 +142,11 @@ database = boswatch tableFMS = bos_fms tableZVEI = bos_zvei tablePOC = bos_pocsag +tableSIG = bos_signal + +# Signal RICs (empty: none set, separator ",") +# f.e.: signal_ric = 1234566,1234567,1234568 +signal_ric = ## SIGNAL-RICS ## [httpRequest] From 1f41b0a8ca1c95edcd6f0b0362d21a87d6a02411 Mon Sep 17 00:00:00 2001 From: Ricardo Krippner Date: Thu, 29 Sep 2016 14:32:43 +0200 Subject: [PATCH 3/4] update Timestamp-Data --- plugins/MySQL/MySQL.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/plugins/MySQL/MySQL.py b/plugins/MySQL/MySQL.py index c9f2b27..7ba29c3 100644 --- a/plugins/MySQL/MySQL.py +++ b/plugins/MySQL/MySQL.py @@ -1,5 +1,5 @@ #!/usr/bin/python -# -*- coding: cp1252 -*- +# -*- coding: UTF-8 -*- """ MySQL-Plugin to dispatch FMS-, ZVEI- and POCSAG - messages to a MySQL database @@ -91,7 +91,7 @@ def run(typ,freq,data): # Connect to MySQL # 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")) + 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"), charset='utf8') cursor = connection.cursor() except: logging.error("cannot connect to MySQL") @@ -104,10 +104,10 @@ def run(typ,freq,data): logging.debug("Insert %s", typ) if typ == "FMS": - cursor.execute("INSERT INTO "+globals.config.get("MySQL","tableFMS")+" (time,fms,status,direction,directionText,tsi,description) VALUES (NOW(),%s,%s,%s,%s,%s,%s)",(data["fms"],data["status"],data["direction"],data["directionText"],data["tsi"],data["description"])) + cursor.execute("INSERT INTO "+globals.config.get("MySQL","tableFMS")+" (time, fms, status, direction, directionText, tsi, description) VALUES (FROM_UNIXTIME(%s),%s,%s,%s,%s,%s,%s)", (data["timestamp"], data["fms"], data["status"], data["direction"], data["directionText"], data["tsi"], data["description"])) elif typ == "ZVEI": - cursor.execute("INSERT INTO "+globals.config.get("MySQL","tableZVEI")+" (time,zvei,description) VALUES (NOW(),%s,%s)",(data["zvei"],data["description"])) + cursor.execute("INSERT INTO "+globals.config.get("MySQL","tableZVEI")+" (time, zvei, description) VALUES (FROM_UNIXTIME(%s),%s,%s)", (data["timestamp"], data["zvei"], data["description"])) elif typ == "POC": if isSignal(data["ric"]): @@ -115,7 +115,7 @@ def run(typ,freq,data): if cursor.rowcount == 0: cursor.execute("INSERT INTO "+globals.config.get("MySQL","tableSIG")+" (time,ric) VALUES (NOW(),"+data["ric"]+")") else: - cursor.execute("INSERT INTO "+globals.config.get("MySQL","tablePOC")+" (time,ric,funktion,funktionChar,msg,bitrate,description) VALUES (NOW(),%s,%s,%s,%s,%s,%s)",(data["ric"],data["function"],data["functionChar"],data["msg"],data["bitrate"],data["description"])) + cursor.execute("INSERT INTO "+globals.config.get("MySQL","tablePOC")+" (time, ric, function, functionChar, msg, bitrate, description) VALUES (FROM_UNIXTIME(%s),%s,%s,%s,%s,%s,%s)", (data["timestamp"], data["ric"], data["function"], data["functionChar"], data["msg"], data["bitrate"], data["description"])) else: logging.warning("Invalid Typ: %s", typ) From 83eebea87f47e2300174ab2a0fcd3520a758b948 Mon Sep 17 00:00:00 2001 From: Ricardo Krippner Date: Thu, 29 Sep 2016 14:48:50 +0200 Subject: [PATCH 4/4] no message --- plugins/MySQL/boswatch.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/MySQL/boswatch.sql b/plugins/MySQL/boswatch.sql index 405f52b..8f584fb 100644 --- a/plugins/MySQL/boswatch.sql +++ b/plugins/MySQL/boswatch.sql @@ -34,7 +34,7 @@ USE boswatch; -- Benutzer erstellen für Datenbank `boswatch` -- -GRANT ALL ON * to 'boswatch'@'localhost' identified by 'boswatch1.mysql'; +GRANT ALL ON * to 'boswatch'@'localhost' identified by 'root'; FLUSH PRIVILEGES; -- --------------------------------------------------------