diff --git a/boswatch/plugin/__init__.py b/boswatch/plugin/__init__.py new file mode 100644 index 0000000..9bad579 --- /dev/null +++ b/boswatch/plugin/__init__.py @@ -0,0 +1 @@ +# coding=utf-8 diff --git a/boswatch/plugin/plugin.py b/boswatch/plugin/plugin.py new file mode 100644 index 0000000..21ba3be --- /dev/null +++ b/boswatch/plugin/plugin.py @@ -0,0 +1,121 @@ +#!/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 + +# from boswatch.module import file + +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() + + logging.debug("%s statistics:", self._pluginName) + 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) + + def run(self, bwPacket): + self._runCount += 1 + + self._setupTime = time.time() + try: + logging.debug("setup %s", self._pluginName) + self.setup() + except: + self._setupErrorCount += 1 + logging.exception("error in setup %s", self._pluginName) + + self._alarmTime = time.time() + try: + logging.debug("alarm %s", self._pluginName) + self.alarm(bwPacket) + except: + self._alarmErrorCount += 1 + logging.exception("error in alarm %s", self._pluginName) + + self._teardownTime = time.time() + try: + logging.debug("teardown %s", self._pluginName) + self.teardown() + except: + self._teardownErrorCount += 1 + logging.exception("error in teardown %s", self._pluginName) + + self._endTime = time.time() + logging.debug("%s took %0.2f seconds", self._pluginName, self._endTime - self._setupTime) + 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