BOSWatch 3
Python Script to receive and decode German BOS Information with rtl_fm and multimon-NG
 
Loading...
Searching...
No Matches
boswatch.router.routerManager.RouterManager Class Reference

Class to manage all routers. More...

Public Member Functions

def __init__ (self)
 Create new router.
 
def buildRouters (self, config)
 Initialize Routers from given config file.
 
def runRouters (self, routerRunList, bwPacket)
 Run given Routers.
 
def cleanup (self)
 Run cleanup routines for all loaded route points.
 

Data Fields

 config
 

Protected Member Functions

def _showRouterRoute (self)
 Show the routes of all routers.
 
def _saveStats (self)
 Save current statistics to file.
 

Protected Attributes

 _routerDict
 
 _startTime
 

Detailed Description

Class to manage all routers.

Constructor & Destructor Documentation

◆ __init__()

def boswatch.router.routerManager.RouterManager.__init__ (   self)

Create new router.

32 def __init__(self):
33 """!Create new router"""
34 self.config = None
35 self._routerDict = {}
36 self._startTime = int(time.time())
37

Member Function Documentation

◆ buildRouters()

def boswatch.router.routerManager.RouterManager.buildRouters (   self,
  config 
)

Initialize Routers from given config file.

    @param config: instance of ConfigYaml class
    @return True or False
39 def buildRouters(self, config):
40 r"""!Initialize Routers from given config file
41
42 @param config: instance of ConfigYaml class
43 @return True or False"""
44 self.config = config
45 self._routerDict = {} # all routers and instances of modules/plugins would be destroyed
46 routerDict_tmp = {}
47 logging.debug("build routers")
48
49 # first we have to init all routers
50 # because a router can be a valid target and we need his reference
51 for router in config.get("router"):
52 if router.get("name") is None or router.get("route") is None:
53 logging.error("name or route not found in router: %s", router)
54 return False
55 if router.get("name") in self._routerDict:
56 logging.error("duplicated router name: %s", router.get("name"))
57 return False
58 routerDict_tmp[router.get("name")] = Router(router.get("name"))
59
60 for router in config.get("router"):
61 routerName = router.get("name")
62
63 for route in router.get("route"):
64 routeType = route.get("type")
65 routeRes = route.get("res")
66 routeName = route.get("name", default=routeRes)
67
68 routeConfig = route.get("config", default=ConfigYAML()) # if no config - build a empty
69
70 if routeType is None or routeRes is None:
71 logging.error("type or name not found in route: %s", route)
72 return False
73
74 try:
75 if routeType == "plugin":
76 importedFile = importlib.import_module(routeType + "." + routeRes)
77 loadedClass = importedFile.BoswatchPlugin(routeConfig)
78 routerDict_tmp[routerName].addRoute(Route(routeName,
79 loadedClass._run,
80 loadedClass._getStatistics,
81 loadedClass._cleanup))
82
83 elif routeType == "module":
84 importedFile = importlib.import_module(routeType + "." + routeRes)
85 loadedClass = importedFile.BoswatchModule(routeConfig)
86 routerDict_tmp[routerName].addRoute(Route(routeName,
87 loadedClass._run,
88 loadedClass._getStatistics,
89 loadedClass._cleanup))
90
91 elif routeType == "router":
92 routerDict_tmp[routerName].addRoute(Route(routeName, routerDict_tmp[routeRes].runRouter))
93
94 else:
95 logging.error("unknown type '%s' in %s", routeType, route)
96 return False
97
98 except ModuleNotFoundError:
99 logging.exception("%s not found: %s", route.get("type"), route.get("res"))
100 return False
101
102 logging.debug("finished building routers")
103 self._routerDict = routerDict_tmp
104 self._showRouterRoute()
105 return True
106

◆ runRouters()

def boswatch.router.routerManager.RouterManager.runRouters (   self,
  routerRunList,
  bwPacket 
)

Run given Routers.

    @param routerRunList: string or list of router names in string form
    @param bwPacket: instance of Packet class
107 def runRouters(self, routerRunList, bwPacket):
108 r"""!Run given Routers
109
110 @param routerRunList: string or list of router names in string form
111 @param bwPacket: instance of Packet class"""
112 if type(routerRunList) is str: # convert single string name to list
113 routerRunList = [routerRunList]
114
115 for routerName in routerRunList:
116 if routerName in self._routerDict:
117 self._routerDict[routerName].runRouter(bwPacket)
118 else:
119 logging.warning("unknown router: %s", routerName)
120
121 if self.config.get("server", "logging", default=False):
122 self._saveStats() # write stats to stats file
123

◆ cleanup()

def boswatch.router.routerManager.RouterManager.cleanup (   self)

Run cleanup routines for all loaded route points.

124 def cleanup(self):
125 r"""!Run cleanup routines for all loaded route points"""
126 for name, routerObject in self._routerDict.items():
127 logging.debug("Start cleanup for %s", name)
128 for routePoint in routerObject.routeList:
129 if routePoint.cleanup:
130 routePoint.cleanup()
131

◆ _showRouterRoute()

def boswatch.router.routerManager.RouterManager._showRouterRoute (   self)
protected

Show the routes of all routers.

132 def _showRouterRoute(self):
133 r"""!Show the routes of all routers"""
134 for name, routerObject in self._routerDict.items():
135 logging.debug("Route for %s", name)
136 counter = 0
137 for routePoint in routerObject.routeList:
138 counter += 1
139 logging.debug(" %d. %s", counter, routePoint.name)
140

◆ _saveStats()

def boswatch.router.routerManager.RouterManager._saveStats (   self)
protected

Save current statistics to file.

141 def _saveStats(self):
142 r"""!Save current statistics to file"""
143 lines = []
144 for name, routerObject in self._routerDict.items():
145 lines.append("[" + name + "]")
146 lines.append(" - Route points: " + str(len(routerObject.routeList)))
147 lines.append(" - Runs: " + str(routerObject._getStatistics()['runCount']))
148 for routePoint in routerObject.routeList:
149 lines.append("[+] " + routePoint.name)
150 if routePoint.statistics:
151 if routePoint.statistics()['type'] == "module":
152 lines.append(" - Runs: " + str(routePoint.statistics()['runCount']))
153 lines.append(" - Run errors: " + str(routePoint.statistics()['moduleErrorCount']))
154 elif routePoint.statistics()['type'] == "plugin":
155 lines.append(" - Runs: " + str(routePoint.statistics()['runCount']))
156 lines.append(" - Setup errors: " + str(routePoint.statistics()['setupErrorCount']))
157 lines.append(" - Alarm errors: " + str(routePoint.statistics()['alarmErrorCount']))
158 lines.append(" - Teardown errors: " + str(routePoint.statistics()['teardownErrorCount']))
159 lines.append("")
160
161 with open("stats_" + str(self._startTime) + ".txt", "w") as stats:
162 for line in lines:
163 stats.write(line + "\n")

Field Documentation

◆ config

boswatch.router.routerManager.RouterManager.config

◆ _routerDict

boswatch.router.routerManager.RouterManager._routerDict
protected

◆ _startTime

boswatch.router.routerManager.RouterManager._startTime
protected