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