Package linspector :: Package lib :: Package config :: Module parser
[hide private]
[frames] | no frames]

Source Code for Module linspector.lib.config.parser

  1  from os.path import isfile 
  2  import json 
  3  import sys 
  4   
  5  from layouts import Layout 
  6  from hostgroups import HostGroup 
  7  from members import Member 
  8  from periods import CronPeriod, DatePeriod, IntervalPeriod 
  9   
 10  from lib.services.service import Service 
 11  from lib.processors.processor import Processor 
 12  from lib.parsers.parser import Parser 
 13  from lib.tasks.task import  Task 
 14   
 15  MOD_SERVICES     = "services" 
 16  MOD_PROCESSORS   = "processors" 
 17  MOD_PARSERS      = "parsers" 
 18  MOD_TASKS        = "tasks" 
 19   
 20  sys.path.append("lib/" + MOD_SERVICES) 
 21  sys.path.append("lib/" + MOD_PROCESSORS) 
 22  sys.path.append("lib/" + MOD_PARSERS) 
 23  sys.path.append("lib/" + MOD_TASKS) 
 24   
 25  KEY_LAYOUTS      = "layouts" 
 26  KEY_HOSTGROUPS   = "hostgroups" 
 27  KEY_MEMBERS      = "members" 
 28  KEY_PERIODS      = "periods" 
 29  KEY_CORE         = "core" 
 30   
31 -class ConfigurationException(Exception):
32 - def __init__(self, msg, log):
33 log.e(msg) 34 self.msg = msg
35
36 - def __str__(self):
37 return repr(self.msg)
38 39
40 -class ConfigParser:
41 - def __init__(self, log):
42 """ 43 initializes a new ConfigParser Object 44 45 params: 46 log: pre configured logger Object to post messages while parsing" 47 """ 48 self.log = log 49 self.hostgroups = {} 50 self.members = {} 51 self.periods = {} 52 self.layouts = {} 53 self._loadedMods={MOD_SERVICES:{}, MOD_PROCESSORS:{}, MOD_TASKS:{}}
54
55 - def _create_new_config_dict(self):
56 return {"members": {}, "periods":{}, "hostgroups":{}, "layouts":{}, "core":{}}
57
58 - def create_config(self, config):
59 configDict = self._create_new_config_dict() 60 for layout in config.get_layouts(): 61 layout._to_config_dict(configDict)
62
63 - def _read_json_config(self, configFilename):
64 """ 65 reads the config File and returns a dictionary, while lowering the first keys 66 67 params: 68 configFilename: the path under which the configuration file should be found 69 """ 70 if not isfile(configFilename): 71 msg = "config file not found at " + str(configFilename) 72 raise ConfigurationException(msg, self.log) 73 74 self.configfilename = configFilename 75 76 with open(configFilename) as cfgFile: 77 config = cfgFile.read() 78 79 self.log.i("reading Config: " + configFilename) 80 return json.loads(config)
81
82 - def _get_as_list(self, configValue):
83 """ 84 In some cases the config permits to define a list or a single value. 85 86 returns the value as list 87 """ 88 return configValue if isinstance(configValue, list) else [configValue]
89
90 - def _create_raw_Object(self, jsonDict, msgName, creator):
91 items = [] 92 for key, val in jsonDict.items(): 93 try: 94 item = creator(key, val) 95 items.append(item) 96 except Exception: 97 self.log.w("ignoring " + msgName + ": " + key + "! reason:") 98 self.log.w(str(Exception)) 99 return items
100
101 - def create_layouts_from_json(self, jsonLayouts):
102 layouts = [] 103 for lName, lValues in jsonLayouts.items(): 104 try: 105 layout = Layout(lName, **lValues) 106 layouts.append(layout) 107 except ConfigurationException: 108 self.log.w("ignoring Layout " + lName + "! reason:") 109 self.log.w(str(Exception)) 110 return layouts
111
112 - def create_hostgroups_from_json(self, jsonHostGroups):
113 """ 114 creates Hostgroups from the jsonConfig 115 """ 116 hostgroups = [] 117 for hgName, hgValues in jsonHostGroups.items(): 118 try: 119 hostgroup = HostGroup(hgName, **hgValues) 120 hostgroups.append(hostgroup) 121 except ConfigurationException: 122 self.log.w("ignoring hostgroup: " + hgName + "!") 123 self.log.w("reason: " + str(Exception)) 124 return hostgroups
125
126 - def create_members_from_json(self, jsonMembers):
127 """ 128 creates Members from the jsonConfig 129 """ 130 members = [] 131 for memberName, memberValues in jsonMembers.items(): 132 try: 133 member = memberName(memberName, **memberValues) 134 members.append(member) 135 except ConfigurationException: 136 self.log.w("ignoring member: " + memberName + "!") 137 self.log.w("reason: " + str(Exception)) 138 return members
139
140 - def _load_module(self, clazz, modPart):
141 mods = self._loadedMods[modPart] 142 if clazz in mods: 143 return mods["class"] 144 else: 145 mod = __import__(clazz) 146 mods[clazz] = mod 147 return mod
148
149 - def replace_with_import(self, objList, modPart, items_func, class_check):
150 for obj in objList: 151 repl = [] 152 items = items_func(obj) 153 for clazzItem in items: 154 try: 155 clazz = clazzItem["class"] 156 mod = self._load_module(clazz, modPart) 157 item = mod.create(**clazzItem) 158 if class_check(item): 159 repl.append(item) 160 else: 161 self.log.w(" ignoring class " + clazzItem["class"] + "! It does not pass the class check!") 162 except ImportError, err: 163 self.log.w("could not import " + clazz + ": " + str(clazzItem) + "! reason") 164 self.log.w(str(err)) 165 except KeyError: 166 self.log.w("Key 'class' not in classItem " + str(clazzItem)) 167 except Exception: 168 self.log.w("Error while replace: " + str(Exception)) 169 del items[:] 170 items.extend(repl)
171
172 - def replace_pointer(self, objectList, replObjectList, id_list_func, id_get_func):
173 for obj in objectList: 174 replacements = [] 175 idList = id_list_func(obj) 176 for id in idList: 177 repl = [o for o in replObjectList if id == id_get_func(o)] 178 if len(repl) == 1: 179 replacements.append(repl[0]) 180 181 del idList[:] 182 idList.extend(replacements)
183
184 - def parse_config(self, configFilename):
185 """ 186 parses the json configuration and returns a list of layouts, 187 which contains all nessesary information of the config file. 188 It will only parse nessesary Objects. 189 Parsing will be done in 3 steps: 190 1. get raw Config Objects by just passing the values defined inside the config 191 2. replace references by objects 192 3. do sanity checks 193 194 params: 195 configFilename: indicates which configuration file to parse 196 """ 197 198 self.jsonDict = self._read_json_config(configFilename) 199 200 jsonLayouts = self.jsonDict[KEY_LAYOUTS] 201 #layouts = self._create_raw_Object(jsonLayouts, "Layouts", lambda name, vals: Layout(name, **vals)) 202 layouts = self.create_layouts_from_json(jsonLayouts) 203 204 hostgroupNames = set() 205 for layout in layouts: 206 for hgName in layout.get_hostgroups(): 207 hostgroupNames.add(hgName) 208 209 jsonHostgroups = {} 210 for hgName in hostgroupNames: 211 if not hgName in self.jsonDict[KEY_HOSTGROUPS]: 212 self.log.w("Hostgroup " + hgName + " not found!") 213 for layout in layouts: 214 if hgName in layout.hostgroups: 215 del layout.hostgroups[layout.hostgroups.index(hgName)] 216 jsonHostgroups[hgName] = self.jsonDict[KEY_HOSTGROUPS][hgName] 217 218 self.hostgroups = self.create_hostgroups_from_json(jsonHostgroups) 219 220 memberNames = set() 221 for layout in layouts: 222 for memberName in layout.get_members(): 223 memberNames.add(memberName) 224 225 jsonMembers = {} 226 for memberName in memberNames: 227 if not memberName in self.jsonDict[KEY_MEMBERS]: 228 self.log.w("Member " + memberName + " not found!") 229 for hostgroup in self.hostgroups: 230 if memberName in hostgroup.members: 231 del hostgroup.members[hostgroup.members.index(memberName)] 232 jsonMembers[memberName] = self.jsonDict[KEY_HOSTGROUPS][memberName] 233 234 self.members = self.create_members_from_json(jsonMembers)
235 236
237 -def parsePeriodList(name, values):
238 if "date" in values: 239 return DatePeriod(name, **values) 240 241 comp = ["weeks", "days", "hours", "minutes", "seconds", "start_date"] 242 if len([i for i in comp if i in values]) > 0 : 243 return IntervalPeriod(name, **values) 244 245 comp = ["year", "month", "day", "week", "day_of_week", "hour", "minute", "second"] 246 if len([i for i in comp if i in values]) > 0 : 247 return CronPeriod(name, **values) 248 else: 249 raise ConfigurationException("could not determine correct Period(" + repr(values)+").")
250 251
252 -class FullConfigParser(ConfigParser):
253 - def parse_config(self, configFilename):
254 """ 255 parses the json configuration and returns a list of layouts, 256 which contains all nessesary information of the config file. 257 parses the full config 258 Parsing will be done in 3 steps: 259 1. get raw Config Objects by just passing the values defined inside the config 260 2. replace references by objects, import services, tasks, parsers and processors 261 3. do sanity checks 262 263 params: 264 configFilename: indicates which configuration file to parse 265 """ 266 self.jsonDict = self._read_json_config(configFilename) 267 268 # first step 269 creator = lambda name, values: Layout(name,**values) 270 layouts = self._create_raw_Object(self.jsonDict[KEY_LAYOUTS], "Layout", creator) 271 272 creator = lambda name, values: Member(name, **values) 273 members = self._create_raw_Object(self.jsonDict[KEY_MEMBERS], "Member", creator) 274 275 creator = lambda name, values: HostGroup(name, **values) 276 hostgroups = self._create_raw_Object(self.jsonDict[KEY_HOSTGROUPS], "Hostgroup", creator) 277 278 creator = parsePeriodList 279 periods = self._create_raw_Object(self.jsonDict[KEY_PERIODS], "Period", creator) 280 281 #2. import and replace 282 items_func = lambda hostgroup: hostgroup.get_services() 283 class_check = lambda service: isinstance(service, Service) 284 self.replace_with_import(hostgroups, MOD_SERVICES, items_func, class_check) 285 286 items_func = lambda hostgroup: hostgroup.get_processors() 287 class_check = lambda processor: isinstance(processor, Processor) 288 self.replace_with_import(hostgroups, MOD_PROCESSORS, items_func, class_check) 289 290 items_func = lambda service: service.get_parser() 291 class_check = lambda parser: isinstance(parser, Parser) 292 self.replace_with_import(hostgroups.services, MOD_PARSERS, items_func, class_check) 293 294 items_func = lambda member: member.get_tasks() 295 class_check = lambda task: isinstance(task, Task) 296 self.replace_with_import(members, MOD_TASKS, items_func, class_check) 297 298 #replace object pointer 299 id_list_func = lambda hostgroup: hostgroup.get_members() 300 id_get_func = lambda member: member.id 301 self.replace_pointer(hostgroups, members, id_list_func, id_get_func) 302 303 services = [] 304 for hg in hostgroups: 305 services.extend(hg.get_services()) 306 id_list_func = lambda service: service.get_periods() 307 id_get_func = lambda period: period.get_name() 308 self.replace_pointer(services, periods, id_list_func, id_get_func) 309 310 id_list_func = lambda layout: layout.get_hostgroups() 311 id_get_func = lambda hostgroup: hostgroup.get_name() 312 self.replace_pointer(layouts, hostgroups, id_list_func, id_get_func) 313 314 return layouts
315