BW3-Core/boswatch/plugin/plugin.py

121 lines
3.6 KiB
Python
Raw Normal View History

2018-01-08 19:57:48 +01:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""!
____ ____ ______ __ __ __ _____
/ __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ /
/ __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ <
/ /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ /
/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/
German BOS Information Script
by Bastian Schroll
@file: plugin.py
@date: 08.01.2018
@author: Bastian Schroll
@description: Plugin main class to inherit
"""
import logging
import time
logging.debug("- %s loaded", __name__)
class Plugin:
"""!Main plugin class"""
_pluginsActive = 0
def __init__(self, pluginName):
"""!init preload some needed locals
and then call onLoad() directly"""
self._pluginName = pluginName
logging.debug("load %s", self._pluginName)
self._pluginsActive += 1
# for time counting
self._setupTime = 0
self._alarmTime = 0
self._teardownTime = 0
self._endTime = 0
# for statistics
self._runCount = 0
self._setupErrorCount = 0
self._alarmErrorCount = 0
self._teardownErrorCount = 0
self.onLoad()
def __del__(self):
"""!Destructor calls onUnload() directly"""
logging.debug("unload %s", self._pluginName)
self._pluginsActive -= 1
self.onUnload()
2018-01-08 23:42:26 +01:00
logging.debug("[%s] statistics:", self._pluginName)
2018-01-08 19:57:48 +01:00
logging.debug("- runs %d", self._runCount)
logging.debug("- setup errors %d", self._setupErrorCount)
logging.debug("- alarm errors %d", self._alarmErrorCount)
logging.debug("- teardown errors %d", self._teardownErrorCount)
2018-01-08 23:42:26 +01:00
def _run(self, bwPacket):
2018-01-08 19:57:48 +01:00
self._runCount += 1
2018-01-08 23:42:26 +01:00
logging.debug("[%s] run #%s", self._pluginName, self._runCount)
2018-01-08 19:57:48 +01:00
self._setupTime = time.time()
try:
2018-01-08 23:42:26 +01:00
logging.debug("[%s] setup", self._pluginName)
2018-01-08 19:57:48 +01:00
self.setup()
except:
self._setupErrorCount += 1
2018-01-08 23:42:26 +01:00
logging.exception("[%s] error in setup", self._pluginName)
2018-01-08 19:57:48 +01:00
self._alarmTime = time.time()
try:
2018-01-08 23:42:26 +01:00
logging.debug("[%s] alarm", self._pluginName)
2018-01-08 19:57:48 +01:00
self.alarm(bwPacket)
except:
self._alarmErrorCount += 1
2018-01-08 23:42:26 +01:00
logging.exception("[%s] error in alarm", self._pluginName)
2018-01-08 19:57:48 +01:00
self._teardownTime = time.time()
try:
2018-01-08 23:42:26 +01:00
logging.debug("[%s] teardown", self._pluginName)
2018-01-08 19:57:48 +01:00
self.teardown()
except:
self._teardownErrorCount += 1
2018-01-08 23:42:26 +01:00
logging.exception("[%s] error in teardown", self._pluginName)
2018-01-08 19:57:48 +01:00
self._endTime = time.time()
2018-01-08 23:42:26 +01:00
logging.debug("[%s] took %0.2f seconds", self._pluginName, self._endTime - self._setupTime)
2018-01-08 19:57:48 +01:00
logging.debug("- setup: %0.2f sec.", self._alarmTime - self._setupTime)
logging.debug("- alarm: %0.2f sec.", self._teardownTime - self._alarmTime)
logging.debug("- teardown: %0.2f sec.", self._endTime - self._teardownTime)
def onLoad(self):
"""!Called by import of the plugin
Must be inherit"""
pass
def setup(self):
"""!Called before alarm
Must be inherit"""
pass
def alarm(self, bwPacket):
"""!Called on alarm
Must be inherit
@param bwPacket: bwPacket instance"""
pass
def teardown(self):
"""!Called after alarm
Must be inherit"""
pass
def onUnload(self):
"""!Called by destruction of the plugin
Must be inherit"""
pass