BOSWatch/plugins/MySQL/MySQL.py

127 lines
3.7 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 function of plugin
# will be called by the pluginLoader
#
def onLoad():
"""
While loading the plugins by pluginLoader.loadPlugins()
this onLoad() routine are called
@requires: nothing
@return: nothing
"""
try:
2015-06-29 17:06:11 +02:00
# we have to do nothing here...
pass
2015-06-29 12:19:44 +02:00
except:
logging.error("unknown error")
logging.debug("unknown error", exc_info=True)
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":
#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}
#Don't use %s here (bug in mysql-lib with one parameter)
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("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)