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

Source Code for Module linspector.lib.config.hosts

  1  import re 
  2   
  3   
4 -class Host:
5 - def __init__(self, name="", host="", parent="", services=None, comment=""):
6 self.name = name 7 self.host = host 8 self.parent = parent 9 self.services = services 10 self.comment = comment
11
12 - def __str__(self):
13 ret = "Host('Name: " + self.name + "', 'Access: " + self.host + "', " 14 if self.parent != "": 15 ret += "'Parent: " + self.parent + "', " 16 ret += "'HostServices: {" 17 for s in self.services: 18 ret += str(s) + "\n" 19 ret += "}" 20 return ret
21 22
23 -class HostService:
24 - def __init__(self, service, warning="", critical=""):
25 self.service = service 26 self.warning = warning 27 self.critical = critical
28
29 - def setCommand(self, command):
30 self.service.command = command
31
32 - def getCommand(self):
33 return self.service.command
34
35 - def __str__(self):
36 ret = str(self.service) 37 if self.warning: 38 ret += "warning: " + str(self.warning) 39 if self.critical: 40 ret += "critical: " + str(self.critical) 41 return ret;
42 43
44 -def parseHostList(hosts, services, log):
45 """ 46 parse the HostList and replace any command as nessesary 47 """ 48 #precompiled regexPattern which finds replacements in service strings 49 pattern = re.compile("@(\w+)") 50 #get a List of Host Objects and leave services unparsed for this moment 51 parsedHosts = [Host(name, **values) for name, values in hosts.items()] 52 #predefined dict to cache service replacements by name 53 serviceReplacements = {} 54 for host in parsedHosts: 55 #list to store HostService Objects 56 hostServices = [] 57 #lets start to parse the Service Dict. 58 for servicename, serviceParams in host.services.items(): 59 #bool to check if the service is defined. 60 found = False 61 #to a real iteration. just pick the right service 62 for service in services: 63 if service.name != servicename: 64 continue 65 #indicate we found a service 66 found = True 67 #check to see if we already regexed our service command 68 if service.name not in serviceReplacements: 69 serviceReplacements[service.name] = pattern.findall(service.command) 70 for params in serviceParams: 71 #copy replacements from service command 72 replacements = serviceReplacements[service.name][:] 73 #for every Host.service.parameter we need a new HostService Object 74 hostService = HostService(service.clone()) 75 #check if the ServiceParameter contain warnings or critical values 76 if 'warning' in params: 77 hostService.warning = params['warning'] 78 del params['warning'] 79 if 'critical' in params: 80 hostService.critical = params['critical'] 81 del params['critical'] 82 #any remainig parm should be a replacement 83 for parm in params: 84 if parm not in replacements: 85 log.w("undefined parameter: " + parm + " in host " + host.name + " from service " + service.name) 86 continue 87 #replace our ServiceCommand with the parameter_value (search, replacement, string) 88 hostService.setCommand(re.sub('@' + parm, params[parm], hostService.getCommand())) 89 replacements.remove(parm) 90 #host will not be inside ServiceParameters, so check this also 91 if 'host' in replacements: 92 hostService.setCommand(re.sub('@host', host.host, hostService.getCommand())) 93 replacements.remove('host') 94 #replacements should be empty now. 95 #If not we cannot use this command as some values are missing 96 if replacements: 97 log.w("Hostservice " + servicename + " from host " + host.name + " is ignored because of missing replacements: " + str( 98 replacements)) 99 else: 100 #anything ok! add to our valid hostServices 101 hostServices.append(hostService) 102 #we could't find the service defined in this host. Service ignored! 103 if not found: 104 log.w("Service " + servicename + " not defined in host " + host.name) 105 #replace host.service member by parsed HostService Objects 106 host.services = hostServices 107 return parsedHosts
108