2019-03-01 12:09:12 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""!
|
|
|
|
|
____ ____ ______ __ __ __ _____
|
|
|
|
|
/ __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ /
|
|
|
|
|
/ __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ <
|
|
|
|
|
/ /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ /
|
|
|
|
|
/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/
|
|
|
|
|
German BOS Information Script
|
|
|
|
|
by Bastian Schroll
|
|
|
|
|
|
2019-10-28 21:20:05 +01:00
|
|
|
@file: moduleBase.py
|
2019-03-01 12:09:12 +01:00
|
|
|
@date: 01.03.2019
|
|
|
|
|
@author: Bastian Schroll
|
|
|
|
|
@description: Module main class to inherit
|
|
|
|
|
"""
|
|
|
|
|
import logging
|
|
|
|
|
import time
|
2019-10-30 11:08:26 +01:00
|
|
|
from abc import ABC, abstractmethod
|
2019-03-01 12:09:12 +01:00
|
|
|
|
2019-10-25 21:58:35 +02:00
|
|
|
from boswatch import wildcard
|
|
|
|
|
|
2019-03-01 12:09:12 +01:00
|
|
|
logging.debug("- %s loaded", __name__)
|
|
|
|
|
|
|
|
|
|
|
2019-10-30 11:08:26 +01:00
|
|
|
class ModuleBase(ABC):
|
2019-03-01 12:09:12 +01:00
|
|
|
"""!Main module class"""
|
|
|
|
|
|
2019-10-24 23:12:20 +02:00
|
|
|
_modulesActive = []
|
2019-03-01 12:09:12 +01:00
|
|
|
|
|
|
|
|
def __init__(self, moduleName, config):
|
|
|
|
|
"""!init preload some needed locals and then call onLoad() directly"""
|
|
|
|
|
self._moduleName = moduleName
|
|
|
|
|
self.config = config
|
2019-10-24 23:12:20 +02:00
|
|
|
self._modulesActive.append(self)
|
2019-03-01 12:09:12 +01:00
|
|
|
|
|
|
|
|
# for time counting
|
|
|
|
|
self._cumTime = 0
|
|
|
|
|
self._moduleTime = 0
|
|
|
|
|
|
|
|
|
|
# for statistics
|
|
|
|
|
self._runCount = 0
|
|
|
|
|
self._moduleErrorCount = 0
|
|
|
|
|
|
|
|
|
|
logging.debug("[%s] onLoad()", moduleName)
|
|
|
|
|
self.onLoad()
|
|
|
|
|
|
2019-10-25 15:36:10 +02:00
|
|
|
def _cleanup(self):
|
|
|
|
|
"""!Cleanup routine calls onUnload() directly"""
|
2019-03-01 12:09:12 +01:00
|
|
|
logging.debug("[%s] onUnload()", self._moduleName)
|
2019-10-24 23:12:20 +02:00
|
|
|
self._modulesActive.remove(self)
|
2019-03-01 12:09:12 +01:00
|
|
|
self.onUnload()
|
|
|
|
|
|
|
|
|
|
def _run(self, bwPacket):
|
2019-10-24 23:12:20 +02:00
|
|
|
"""!start an run of the module.
|
2019-03-01 12:09:12 +01:00
|
|
|
|
|
|
|
|
@param bwPacket: A BOSWatch packet instance
|
|
|
|
|
@return bwPacket or False"""
|
|
|
|
|
self._runCount += 1
|
|
|
|
|
logging.debug("[%s] run #%d", self._moduleName, self._runCount)
|
|
|
|
|
|
2019-10-24 23:42:52 +02:00
|
|
|
tmpTime = time.time()
|
2019-03-01 12:09:12 +01:00
|
|
|
try:
|
|
|
|
|
logging.debug("[%s] doWork()", self._moduleName)
|
|
|
|
|
bwPacket = self.doWork(bwPacket)
|
|
|
|
|
except:
|
|
|
|
|
self._moduleErrorCount += 1
|
|
|
|
|
logging.exception("[%s] alarm error", self._moduleName)
|
2019-10-24 23:42:52 +02:00
|
|
|
self._moduleTime = time.time() - tmpTime
|
2019-03-01 12:09:12 +01:00
|
|
|
|
|
|
|
|
self._cumTime += self._moduleTime
|
|
|
|
|
|
|
|
|
|
logging.debug("[%s] took %0.3f seconds", self._moduleName, self._moduleTime)
|
|
|
|
|
|
|
|
|
|
return bwPacket
|
|
|
|
|
|
|
|
|
|
def _getStatistics(self):
|
|
|
|
|
"""!Returns statistical information's from last module run
|
|
|
|
|
|
|
|
|
|
@return Statistics as pyton dict"""
|
2019-10-25 14:32:24 +02:00
|
|
|
stats = {"type": "module",
|
2019-10-25 13:25:56 +02:00
|
|
|
"runCount": self._runCount,
|
2019-03-01 12:09:12 +01:00
|
|
|
"cumTime": self._cumTime,
|
|
|
|
|
"moduleTime": self._moduleTime,
|
|
|
|
|
"moduleErrorCount": self._moduleErrorCount}
|
|
|
|
|
return stats
|
|
|
|
|
|
|
|
|
|
def onLoad(self):
|
|
|
|
|
"""!Called by import of the module
|
2019-10-24 23:12:20 +02:00
|
|
|
can be inherited"""
|
2019-03-01 12:09:12 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def doWork(self, bwPacket):
|
|
|
|
|
"""!Called module run
|
2019-10-24 23:12:20 +02:00
|
|
|
can be inherited
|
2019-03-01 12:09:12 +01:00
|
|
|
|
|
|
|
|
@param bwPacket: bwPacket instance"""
|
|
|
|
|
logging.warning("no functionality in module %s", self._moduleName)
|
|
|
|
|
|
|
|
|
|
def onUnload(self):
|
2019-10-24 23:12:20 +02:00
|
|
|
"""!Called on shutdown of boswatch
|
|
|
|
|
can be inherited"""
|
2019-03-01 12:09:12 +01:00
|
|
|
pass
|
2019-10-25 21:58:35 +02:00
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def registerWildcard(newWildcard, bwPacketField):
|
|
|
|
|
"""!Register a new wildcard
|
|
|
|
|
|
|
|
|
|
@param newWildcard: wildcard where parser searching for
|
|
|
|
|
@param bwPacketField: field from bwPacket where holds replacement data"""
|
|
|
|
|
if not newWildcard.startswith("{") or not newWildcard.endswith("}"):
|
|
|
|
|
logging.error("wildcard not registered - false format: %s", newWildcard)
|
|
|
|
|
return
|
|
|
|
|
if bwPacketField == "":
|
|
|
|
|
logging.error("wildcard not registered - bwPacket field is empty")
|
|
|
|
|
return
|
|
|
|
|
wildcard.registerWildcard(newWildcard, bwPacketField)
|