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.
 

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 r"""!Create new router"""
34 self._routerDict = {}
35 self._startTime = int(time.time())
36

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

◆ 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
105 def runRouters(self, routerRunList, bwPacket):
106 r"""!Run given Routers
107
108 @param routerRunList: string or list of router names in string form
109 @param bwPacket: instance of Packet class"""
110 if type(routerRunList) is str: # convert single string name to list
111 routerRunList = [routerRunList]
112
113 for routerName in routerRunList:
114 if routerName in self._routerDict:
115 self._routerDict[routerName].runRouter(bwPacket)
116 else:
117 logging.warning("unknown router: %s", routerName)
118
119 self._saveStats() # write stats to stats file
120

◆ cleanup()

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

Run cleanup routines for all loaded route points.

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

◆ _showRouterRoute()

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

Show the routes of all routers.

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

◆ _saveStats()

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

Save current statistics to file.

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

Field Documentation

◆ _routerDict

boswatch.router.routerManager.RouterManager._routerDict
protected

◆ _startTime

boswatch.router.routerManager.RouterManager._startTime
protected