def __init__(self, datamgr, query_cache, db, pnp_path, return_queue): self.datamgr = datamgr self.query_cache = query_cache self.db = db self.pnp_path = pnp_path self.return_queue = return_queue self.counters = LiveStatusCounters()
def __init__(self, configs, hosts, services, contacts, hostgroups, servicegroups, contactgroups, timeperiods, commands, schedulers, pollers, reactionners, brokers, db, pnp_path, return_queue): self.configs = configs self.hosts = hosts self.services = services self.contacts = contacts self.hostgroups = hostgroups self.servicegroups = servicegroups self.contactgroups = contactgroups self.timeperiods = timeperiods self.commands = commands self.schedulers = schedulers self.pollers = pollers self.reactionners = reactionners self.brokers = brokers self.db = db LiveStatus.pnp_path = pnp_path self.debuglevel = 2 self.return_queue = return_queue self.create_out_map_delegates() self.create_out_map_hooks() # add Host attributes to Hostsbygroup etc. for attribute in LiveStatus.out_map['Host']: LiveStatus.out_map['Hostsbygroup'][attribute] = LiveStatus.out_map['Host'][attribute] for attribute in self.out_map['Service']: LiveStatus.out_map['Servicesbygroup'][attribute] = LiveStatus.out_map['Service'][attribute] for attribute in self.out_map['Service']: LiveStatus.out_map['Servicesbyhostgroup'][attribute] = LiveStatus.out_map['Service'][attribute] self.counters = LiveStatusCounters()
class LiveStatus(object): """A class that represents the status of all objects in the broker """ def __init__(self, datamgr, query_cache, db, pnp_path, return_queue): self.datamgr = datamgr self.query_cache = query_cache self.db = db self.pnp_path = pnp_path self.return_queue = return_queue self.counters = LiveStatusCounters() def handle_request(self, data): """Execute the livestatus request. This function creates a LiveStatusRequest method, calls the parser, handles the execution of the request and formatting of the result. """ request = LiveStatusRequest(data, self.datamgr, self.query_cache, self.db, self.pnp_path, self.return_queue, self.counters) request.parse_input(data) if sorted([q.my_type for q in request.queries]) == ['command', 'query', 'wait']: # The Multisite way for query in [q for q in request.queries if q.my_type == 'command']: result = query.launch_query() response = query.response response.format_live_data(result, query.columns, query.aliases) output, keepalive = response.respond() output = [q for q in request.queries if q.my_type == 'wait'] + [q for q in request.queries if q.my_type == 'query'] elif sorted([q.my_type for q in request.queries]) == ['query', 'wait']: # The Thruk way output = [q for q in request.queries if q.my_type == 'wait'] + [q for q in request.queries if q.my_type == 'query'] keepalive = True elif sorted([q.my_type for q in request.queries]) == ['command', 'query']: for query in [q for q in request.queries if q.my_type == 'command']: result = query.launch_query() response = query.response response.format_live_data(result, query.columns, query.aliases) output, keepalive = response.respond() for query in [q for q in request.queries if q.my_type == 'query']: # This was a simple query, respond immediately result = query.launch_query() # Now bring the retrieved information to a form which can be sent back to the client response = query.response response.format_live_data(result, query.columns, query.aliases) output, keepalive = response.respond() elif sorted([q.my_type for q in request.queries]) == ['query']: for query in [q for q in request.queries if q.my_type == 'query']: # This was a simple query, respond immediately result = query.launch_query() # Now bring the retrieved information to a form which can be sent back to the client response = query.response response.format_live_data(result, query.columns, query.aliases) output, keepalive = response.respond() elif sorted([q.my_type for q in request.queries]) == ['command']: for query in [q for q in request.queries if q.my_type == 'command']: result = query.launch_query() response = query.response response.format_live_data(result, query.columns, query.aliases) output, keepalive = response.respond() elif [q.my_type for q in request.queries if q.my_type != 'command'] == []: # Only external commands. Thruk uses it when it sends multiple # objects into a downtime. for query in [q for q in request.queries if q.my_type == 'command']: result = query.launch_query() response = query.response response.format_live_data(result, query.columns, query.aliases) output, keepalive = response.respond() else: # We currently do not handle this kind of composed request output = "" print "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" print "We currently do not handle this kind of composed request" print sorted([q.my_type for q in request.queries]) print "DURATION %.4fs" % (time.time() - request.tic) return output, keepalive def count_event(self, counter): self.counters.increment(counter)
class LiveStatus(object, Hooker): """A class that represents the status of all objects in the broker """ # Use out_map from the mapping.py file out_map = out_map def __init__(self, configs, hosts, services, contacts, hostgroups, servicegroups, contactgroups, timeperiods, commands, schedulers, pollers, reactionners, brokers, db, pnp_path, return_queue): self.configs = configs self.hosts = hosts self.services = services self.contacts = contacts self.hostgroups = hostgroups self.servicegroups = servicegroups self.contactgroups = contactgroups self.timeperiods = timeperiods self.commands = commands self.schedulers = schedulers self.pollers = pollers self.reactionners = reactionners self.brokers = brokers self.db = db LiveStatus.pnp_path = pnp_path self.debuglevel = 2 self.return_queue = return_queue self.create_out_map_delegates() self.create_out_map_hooks() # add Host attributes to Hostsbygroup etc. for attribute in LiveStatus.out_map['Host']: LiveStatus.out_map['Hostsbygroup'][attribute] = LiveStatus.out_map['Host'][attribute] for attribute in self.out_map['Service']: LiveStatus.out_map['Servicesbygroup'][attribute] = LiveStatus.out_map['Service'][attribute] for attribute in self.out_map['Service']: LiveStatus.out_map['Servicesbyhostgroup'][attribute] = LiveStatus.out_map['Service'][attribute] self.counters = LiveStatusCounters() def handle_request(self, data): """Execute the livestatus request. This function creates a LiveStatusRequest method, calls the parser, handles the execution of the request and formatting of the result. """ request = LiveStatusRequest(data, self.configs, self.hosts, self.services, self.contacts, self.hostgroups, self.servicegroups, self.contactgroups, self.timeperiods, self.commands, self.schedulers, self.pollers, self.reactionners, self.brokers, self.db, self.pnp_path, self.return_queue, self.counters) request.parse_input(data) #print "REQUEST\n%s\n" % data to_del = [] if sorted([q.my_type for q in request.queries]) == ['command', 'query', 'wait']: # The Multisite way for query in [q for q in request.queries if q.my_type == 'command']: result = query.launch_query() response = query.response response.format_live_data(result, query.columns, query.aliases) output, keepalive = response.respond() output = [q for q in request.queries if q.my_type == 'wait'] + [q for q in request.queries if q.my_type == 'query'] elif sorted([q.my_type for q in request.queries]) == ['query', 'wait']: # The Thruk way output = [q for q in request.queries if q.my_type == 'wait'] + [q for q in request.queries if q.my_type == 'query'] keepalive = True elif sorted([q.my_type for q in request.queries]) == ['command', 'query']: for query in [q for q in request.queries if q.my_type == 'command']: result = query.launch_query() response = query.response response.format_live_data(result, query.columns, query.aliases) output, keepalive = response.respond() for query in [q for q in request.queries if q.my_type == 'query']: # This was a simple query, respond immediately result = query.launch_query() # Now bring the retrieved information to a form which can be sent back to the client response = query.response response.format_live_data(result, query.columns, query.aliases) output, keepalive = response.respond() elif sorted([q.my_type for q in request.queries]) == ['query']: for query in [q for q in request.queries if q.my_type == 'query']: # This was a simple query, respond immediately result = query.launch_query() # Now bring the retrieved information to a form which can be sent back to the client response = query.response response.format_live_data(result, query.columns, query.aliases) output, keepalive = response.respond() elif sorted([q.my_type for q in request.queries]) == ['command']: for query in [q for q in request.queries if q.my_type == 'command']: result = query.launch_query() response = query.response response.format_live_data(result, query.columns, query.aliases) output, keepalive = response.respond() elif [q.my_type for q in request.queries if q.my_type != 'command'] == []: # Only external commands. Thruk uses it when it sends multiple # objects into a downtime. for query in [q for q in request.queries if q.my_type == 'command']: result = query.launch_query() response = query.response response.format_live_data(result, query.columns, query.aliases) output, keepalive = response.respond() else: # We currently do not handle this kind of composed request output = "" print "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" print "We currently do not handle this kind of composed request" print sorted([q.my_type for q in request.queries]) #print "RESPONSE\n%s\n" % output print "DURATION %.4fs" % (time.time() - request.tic) return output, keepalive def create_out_map_delegates(self): """Add delegate keys for certain attributes. Some attributes are not directly reachable via prop or need a complicated depythonize function. Example: Logline (the objects created for a "GET log" request have the column current_host_state. The Logline object does not have an attribute of this name, but a log_host attribute. The Host object represented by log_host has an attribute state which is the desired current_host_state. Because it's the same for all columns starting with current_host, a rule can be applied that automatically redirects the resolving to the corresponding object. Instead of creating a complicated depythonize handler which gets log_host and then state, two new keys for Logline/current_host_state are added: delegate = log_host as = state This instructs the hook function to first get attribute state of the object represented by log_host. """ delegate_map = { 'Logline' : { 'current_service_' : 'log_service', 'current_host_' : 'log_host', }, 'Service' : { 'host_' : 'host', }, 'Comment' : { 'service_' : 'ref', 'host_' : 'ref', }, 'Downtime' : { 'service_' : 'ref', 'host_' : 'ref', } } for objtype in LiveStatus.out_map: for attribute in LiveStatus.out_map[objtype]: entry = LiveStatus.out_map[objtype][attribute] if objtype in delegate_map: for prefix in delegate_map[objtype]: if attribute.startswith(prefix): if 'delegate' not in entry: entry['delegate'] = delegate_map[objtype][prefix] entry['as'] = attribute.replace(prefix, '') def count_event(self, counter): self.counters.increment(counter)