BOSWatch/plugins/MySQL/MySQL.py

120 lines
3.6 KiB
Python
Raw Normal View History

2015-05-20 10:38:21 +02:00
#!/usr/bin/python
# -*- coding: cp1252 -*-
2015-05-31 12:40:43 +02:00
"""
MySQL-Plugin to dispatch FMS-, ZVEI- and POCSAG - messages to a MySQL database
@author: Jens Herrmann
@author: Bastian Schroll
@requires: MySQL-Configuration has to be set in the config.ini
@requires: Created Database/Tables, see boswatch.sql
"""
2015-05-20 10:38:21 +02:00
import logging # Global logger
2015-05-20 21:03:58 +02:00
import mysql
import mysql.connector
2015-05-20 10:38:21 +02:00
from includes import globals # Global variables
2015-06-29 12:19:44 +02:00
##
#
# onLoad (init) function of plugin
# will be called one time by the pluginLoader on start
2015-06-29 12:19:44 +02:00
#
def onLoad():
"""
While loading the plugins by pluginLoader.loadPlugins()
this onLoad() routine is called one time for initialize the plugin
2015-06-29 12:19:44 +02:00
@requires: nothing
@return: nothing
"""
# nothing to do for this plugin
return
2015-05-31 12:40:43 +02:00
##
#
# Main function of MySQL-plugin
# will be called by the alarmHandler
#
2015-05-20 10:38:21 +02:00
def run(typ,freq,data):
2015-05-31 12:40:43 +02:00
"""
This function is the implementation of the MySQL-Plugin.
It will store the data to an MySQL database
The configuration for the MySQL-Connection is set in the config.ini.
For DB- and tablestructure see boswatch.sql
@type typ: string (FMS|ZVEI|POC)
@param typ: Typ of the dataset for sending to BosMon
@type data: map of data (structure see interface.txt)
@param data: Contains the parameter for dispatch to BosMon.
@type freq: string
@keyword freq: frequency is not used in this plugin
@requires: MySQL-Configuration has to be set in the config.ini
@requires: Created Database/Tables, see boswatch.sql
@return: nothing
"""
2015-05-20 10:38:21 +02:00
try:
#
2015-05-20 10:38:21 +02:00
#ConfigParser
#
2015-05-20 10:38:21 +02:00
logging.debug("reading config file")
try:
2015-05-20 21:03:58 +02:00
for key,val in globals.config.items("MySQL"):
2015-05-20 10:38:21 +02:00
logging.debug(" - %s = %s", key, val)
except:
logging.error("cannot read config file")
logging.debug("cannot read config file", exc_info=True)
else: # Without config, plugin couldn't work
2015-05-20 21:51:13 +02:00
try:
#
# Connect to MySQL
2015-05-31 12:40:43 +02:00
#
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()
2015-05-20 21:51:13 +02:00
except:
logging.error("cannot connect to MySQL")
logging.debug("cannot connect to MySQL", exc_info=True)
else: # Without connection, plugin couldn't work
try:
#
# Create and execute SQL-statement
#
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"]))
elif typ == "ZVEI":
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,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)
except:
logging.error("cannot Insert %s", typ)
logging.debug("cannot Insert %s", typ, exc_info=True)
return
finally:
logging.debug("close MySQL")
try:
cursor.close()
connection.close() #Close connection in every case
except:
pass
2015-05-20 10:38:21 +02:00
except:
logging.error("unknown error")
logging.debug("unknown error", exc_info=True)