def discover_nrpe(host_name, command, args): # Check if we have already checked this if discovery_cache['nrpe'].has_key("%s,%s,%s" % (host_name, command,args)): return discovery_cache['nrpe']["%s,%s,%s" % (host_name, command, args)] ret = network_scan.runCommand("check_nrpe -H %s -c %s %s" % (host_name, command, args)) if ret[0] == 0: discovery_cache['nrpe']["%s,%s,%s" % (host_name, command, args)] = True return True discovery_cache['nrpe']["%s,%s,%s" % (host_name, command, args)] = False return False
def run_discovery(host_name, packs, **kwargs): # Reset cache discovery_cache['inet'] = {} discovery_cache['nrpe'] = {} discovered = {} # Go through all the packs for p in packs: discovered[p] = [] # And loop through the discovery methods for d in packs[p]['discovery']: # We have a builtin method if d[0]: method, args = d[0].split(' ', 1) # Inet scanner invoked if method == 'inet': # Port open if discover_inet(host_name, args): # Append affected services for package in d[2:]: discovered[p].append(package) elif method == 'plugin': command = args for old_string,new_string in kwargs.items(): command = command.replace(old_string,new_string) ret, stdout, stderr = network_scan.runCommand(command) if ret == 0: for package in d[2:]: discovered[p].append(package) elif method == 'nrpe': for old_string,new_string in kwargs.items(): args = args.replace(old_string,new_string) try: command, nrpe_args = args.split(None, 1) except ValueError: command = args nrpe_args = '' if discover_nrpe(host_name, command, nrpe_args): for package in d[2:]: discovered[p].append(package) else: raise Exception('Unsupported discovery method "%s"' % method) elif d[1]: raise Exception('shell discovery not implemented') else: raise Exception('No discovery defined') return discovered