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
|
|
|
|
|
|
2018-01-14 16:28:11 +01:00
|
|
|
from boswatch.utils import paths
|
|
|
|
|
from boswatch.config import Config
|
|
|
|
|
|
2018-01-08 19:57:48 +01:00
|
|
|
logging.debug("- %s loaded", __name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Plugin:
|
|
|
|
|
"""!Main plugin class"""
|
|
|
|
|
|
|
|
|
|
_pluginsActive = 0
|
|
|
|
|
|
|
|
|
|
def __init__(self, pluginName):
|
2018-01-09 13:35:54 +01:00
|
|
|
"""!init preload some needed locals and then call onLoad() directly"""
|
2018-01-08 19:57:48 +01:00
|
|
|
self._pluginName = pluginName
|
|
|
|
|
self._pluginsActive += 1
|
|
|
|
|
|
|
|
|
|
# for time counting
|
2018-01-09 13:35:54 +01:00
|
|
|
self._sumTime = 0
|
2018-01-08 19:57:48 +01:00
|
|
|
self._setupTime = 0
|
|
|
|
|
self._alarmTime = 0
|
|
|
|
|
self._teardownTime = 0
|
2018-01-09 13:35:54 +01:00
|
|
|
self._tmpTime = 0
|
2018-01-08 19:57:48 +01:00
|
|
|
|
|
|
|
|
# for statistics
|
|
|
|
|
self._runCount = 0
|
|
|
|
|
self._setupErrorCount = 0
|
|
|
|
|
self._alarmErrorCount = 0
|
|
|
|
|
self._teardownErrorCount = 0
|
|
|
|
|
|
2018-01-14 16:28:11 +01:00
|
|
|
if paths.FileExist(paths.PLUGIN_PATH + pluginName + "/" + pluginName + ".ini"):
|
|
|
|
|
self.config = Config()
|
|
|
|
|
self.config.loadConfigFile(paths.PLUGIN_PATH + pluginName + "/" + pluginName + ".ini", pluginName)
|
|
|
|
|
|
2018-01-11 13:02:04 +01:00
|
|
|
logging.debug("[%s] onLoad()", pluginName)
|
2018-01-08 19:57:48 +01:00
|
|
|
self.onLoad()
|
|
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
|
"""!Destructor calls onUnload() directly"""
|
2018-01-11 13:02:04 +01:00
|
|
|
logging.debug("[%s] onUnload()", self._pluginName)
|
2018-01-08 19:57:48 +01:00
|
|
|
self._pluginsActive -= 1
|
|
|
|
|
self.onUnload()
|
|
|
|
|
|
2018-01-09 13:35:54 +01:00
|
|
|
def _loadConfig(self):
|
|
|
|
|
pass
|
|
|
|
|
|
2018-01-08 23:42:26 +01:00
|
|
|
def _run(self, bwPacket):
|
2018-01-11 13:02:04 +01:00
|
|
|
"""!start an complete running turn of an plugin.
|
|
|
|
|
Calls setup(), alarm() and teardown() in this order.
|
|
|
|
|
The alarm() method serves the BOSWatch packet to the plugin.
|
|
|
|
|
|
|
|
|
|
@param bwPacket: A BOSWatch packet instance"""
|
2018-01-08 19:57:48 +01:00
|
|
|
self._runCount += 1
|
2018-01-11 13:02:04 +01:00
|
|
|
logging.debug("[%s] run #%d", self._pluginName, self._runCount)
|
2018-01-08 19:57:48 +01:00
|
|
|
|
2018-01-09 13:35:54 +01:00
|
|
|
self._tmpTime = time.time()
|
2018-01-08 19:57:48 +01:00
|
|
|
try:
|
2018-01-11 13:02:04 +01:00
|
|
|
logging.debug("[%s] setup()", self._pluginName)
|
2018-01-08 19:57:48 +01:00
|
|
|
self.setup()
|
|
|
|
|
except:
|
|
|
|
|
self._setupErrorCount += 1
|
2018-01-11 13:02:04 +01:00
|
|
|
logging.exception("[%s] error in setup()", self._pluginName)
|
2018-01-08 19:57:48 +01:00
|
|
|
|
2018-01-09 13:35:54 +01:00
|
|
|
self._setupTime = time.time() - self._tmpTime
|
|
|
|
|
self._tmpTime = time.time()
|
2018-01-08 19:57:48 +01:00
|
|
|
try:
|
2018-01-11 13:02:04 +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-11 13:02:04 +01:00
|
|
|
logging.exception("[%s] error in alarm()", self._pluginName)
|
2018-01-08 19:57:48 +01:00
|
|
|
|
2018-01-09 13:35:54 +01:00
|
|
|
self._alarmTime = time.time() - self._tmpTime
|
|
|
|
|
self._tmpTime = time.time()
|
2018-01-08 19:57:48 +01:00
|
|
|
try:
|
2018-01-11 13:02:04 +01:00
|
|
|
logging.debug("[%s] teardown()", self._pluginName)
|
2018-01-08 19:57:48 +01:00
|
|
|
self.teardown()
|
|
|
|
|
except:
|
|
|
|
|
self._teardownErrorCount += 1
|
2018-01-11 13:02:04 +01:00
|
|
|
logging.exception("[%s] error in teardown()", self._pluginName)
|
2018-01-08 19:57:48 +01:00
|
|
|
|
2018-01-09 13:35:54 +01:00
|
|
|
self._teardownTime = time.time() - self._tmpTime
|
|
|
|
|
self._sumTime = self._setupTime + self._alarmTime + self._teardownTime
|
|
|
|
|
|
2018-01-08 19:57:48 +01:00
|
|
|
self._endTime = time.time()
|
2018-01-09 13:35:54 +01:00
|
|
|
logging.debug("[%s] took %0.3f seconds", self._pluginName, self._sumTime)
|
2018-01-10 12:57:20 +01:00
|
|
|
# logging.debug("- setup: %0.2f sec.", self._setupTime)
|
|
|
|
|
# logging.debug("- alarm: %0.2f sec.", self._alarmTime)
|
|
|
|
|
# logging.debug("- teardown: %0.2f sec.", self._teardownTime)
|
2018-01-09 13:35:54 +01:00
|
|
|
|
|
|
|
|
def _getStatistics(self):
|
|
|
|
|
"""!Returns statistical information's from last plugin run
|
|
|
|
|
|
2018-01-12 12:33:41 +01:00
|
|
|
@return Statistics as pyton dict"""
|
2018-01-13 16:31:20 +01:00
|
|
|
stats = {"runCount": self._runCount, "sumTime": self._sumTime, "cumTime": self._runCount * self._sumTime,
|
2018-01-12 12:33:41 +01:00
|
|
|
"setupTime": self._setupTime, "alarmTime": self._alarmTime, "teardownTime": self._teardownTime,
|
|
|
|
|
"setupErrorCount": self._setupErrorCount, "alarmErrorCount": self._alarmErrorCount, "teardownErrorCount": self._teardownErrorCount}
|
|
|
|
|
return stats
|
2018-01-08 19:57:48 +01:00
|
|
|
|
|
|
|
|
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
|