def init(pid, plugin_alias, script_alias): global plugin_id, icinga_url, icinga_auth, path plugin_id = pid core = Core.get(plugin_id) path = core.expand_path("${module-path}") conf = Settings.get(plugin_id) conf.register_path( '/settings/remote-modules', "Remote module", "Keys for the remote-modules module which handles downloading remote modules on demand" ) conf.register_path( '/settings/remote-modules/modules', "Remote modules", "A list of remote modules to fetch the key is not used.") reg = Registry.get(plugin_id) reg.simple_function('remote_module_update', update, 'Update all or one remote module') for mod in conf.get_section('/settings/remote-modules/modules'): url = conf.get_string('/settings/remote-modules/modules', mod, '') log("Adding module %s as %s" % (mod, url)) conf.register_key('/settings/remote-modules/modules', mod, 'string', "A remote module to fetch", "A remote module to fetch", "") if not update_modules(): log_error("Failed to update modules") else: enable_modules()
def run(self, arguments=[]): cases = [] try: parser = ThrowingArgumentParser(prog='nscp') parser.add_argument("--script", help="The script to run (sort of ignored)", action='store') parser.add_argument("--case", help="Which test case to run", action='append') args = parser.parse_args(arguments) cases = args.case except Exception as e: log_error('Failed to parse command line: %s' % e) result = TestResult('Test result for %d suites' % len(self.suites)) for suite in self.suites: instance = suite.getInstance() instance.setup(self.plugin_id, self.prefix) suite_result = TestResult('Running suite: %s' % instance.title()) if cases: suite_result.append(instance.run_test(cases)) else: suite_result.append(instance.run_test()) result.append(suite_result) result.add_message(suite_result.is_ok(), 'Result from suite: %s' % instance.title()) instance.teardown() return result
def do_one_test(self, script, expected=status.OK, message="Foo Bar", args=[], cleanup=True): result = TestResult('%s (%s)' % (script, args)) (ret, msg, perf) = self.core.simple_query(script, args) if cleanup and os.name != 'nt': message = message.replace('"', '') message = message.replace('$ARG1$', '$') message = message.replace('$ARG2$', '$') message = message.replace('$ARG3$', '$') message = message.replace('\r', '\n') message = message.replace('\n\n', '\n') msg = msg.replace('\r', '\n') msg = msg.replace('\n\n', '\n') result.assert_equals(ret, expected, 'Validate return code for %s' % script) result.assert_equals(msg, message, 'Validate return message for %s' % script) if msg != message: diff = difflib.ndiff(msg.splitlines(1), message.splitlines(1)) for l in diff: log_error(l) return result
def log(self, show_all = False, prefix = '', indent = 0): if self.status: if show_all: log('%s%s%s'%(prefix, ''.rjust(indent, ' '), self)) log_debug('%s%s%s'%(prefix, ''.rjust(indent, ' '), self)) else: log_error('%s%s%s'%(prefix, ''.rjust(indent, ' '), self))
def log(self, prefix = '', indent = 0): start = '%s%s'%(prefix, ''.rjust(indent, ' ')) if self.status: log_debug('%s%s'%(start, self)) else: log_error('%s%s'%(start, self)) for c in self.children: c.log(prefix, indent+1)
def log(self, prefix="", indent=0): start = "%s%s" % (prefix, "".rjust(indent, " ")) if self.status: log_debug("%s%s" % (start, self)) else: log_error("%s%s" % (start, self)) for c in self.children: c.log(prefix, indent + 1)
def append(self, entry): if not entry: log_error('Attempting to add invalid entry (None)') elif entry == self: log_error('Attempting to add self to self') else: if self.status and not entry.is_ok(): self.status = False self.children.append(entry)
def append(self, entry): if not entry: log_error("Attempting to add invalid entry (None)") elif entry == self: log_error("Attempting to add self to self") else: if self.status and not entry.is_ok(): self.status = False self.children.append(entry)
def get_plugins(self): (code, data) = self.registry.query(self.build_command_request(4)) if code == 1: message = plugin_pb2.RegistryResponseMessage() message.ParseFromString(data) for payload in message.payload: if payload.inventory: log_debug('Found %d plugins' % len(payload.inventory)) return payload.inventory log_error('No plugins') return []
def get_plugins(self): (code, data) = self.registry.query(self.build_command_request(4)) if code == 1: message = plugin_pb2.RegistryResponseMessage() message.ParseFromString(data) for payload in message.payload: if payload.inventory: log_debug('Found %d plugins'%len(payload.inventory)) return payload.inventory log_error('No plugins') return []
def get_query_aliases(self): log_debug('Fetching aliases...') (code, data) = self.registry.query(self.build_command_request(5)) if code == 1: message = plugin_pb2.RegistryResponseMessage() message.ParseFromString(data) for payload in message.payload: if payload.inventory: log_debug('Found %d aliases' % len(payload.inventory)) return payload.inventory log_error('No aliases found') return []
def handler_wrapped(self, channel, request): log_error('DISCARDING message on %s' % (channel)) message = plugin_pb2.SubmitRequestMessage() message.ParseFromString(request) command = message.payload[0].command log('Got message %s on %s' % (command, channel)) msg = self.get_response(command) msg.got_response = True self.set_response(msg) return None
def get_query_aliases(self): log_debug('Fetching aliases...') (code, data) = self.registry.query(self.build_command_request(5)) if code == 1: message = plugin_pb2.RegistryResponseMessage() message.ParseFromString(data) for payload in message.payload: if payload.inventory: log_debug('Found %d aliases'%len(payload.inventory)) return payload.inventory log_error('No aliases found') return []
def handler_wrapped(self, channel, request): log_error('DISCARDING message on %s'%(channel)) message = plugin_pb2.SubmitRequestMessage() message.ParseFromString(request) command = message.payload[0].command log('Got message %s on %s'%(command, channel)) msg = self.get_response(command) msg.got_response = True self.set_response(msg) return None
def inbox_handler_wrapped(self, channel, request): message = plugin_pb2.SubmitRequestMessage() message.ParseFromString(request) if len(message.payload) != 1: log_error("Got invalid message on channel: %s"%channel) return None command = message.payload[0].command log_debug('Got message %s on %s'%(command, channel)) msg = NSCAMessage(command) msg.got_response = True self.set_response(msg) return None
def inbox_handler_wrapped(self, channel, request): message = plugin_pb2.SubmitRequestMessage() message.ParseFromString(request) if len(message.payload) != 1: log_error("Got invalid message on channel: %s" % channel) return None command = message.payload[0].command log('Got message %s on %s' % (command, channel)) msg = NSCAMessage(command) msg.got_response = True self.set_response(msg) return None
def __main__(args): global plugin_id # List all namespaces recursivly core = Core.get(plugin_id) (ret, ns_msgs) = core.simple_exec('CheckWMI', 'wmi', ['--list-all-ns']) if len(ns_msgs) == 0: log_error("Failed to execute WMI command is CheckWMI enabled?") else: for ns in ns_msgs[0].splitlines(): # List all classes in each namespace (ret, cls_msgs) = core.simple_exec('any', 'wmi', ['--list-classes', '--simple', '--namespace', ns]) for cls in cls_msgs[0].splitlines(): log( '%s : %s'%(ns, cls))
def extend(self, lst): if isinstance(lst, list): if self.status: for c in lst: if not c.is_ok(): self.status = False for c in lst: if c.contains(self): log_error('Attempting to add a list with me in it') return self.children.extend(lst) else: self.append(lst)
def extend(self, lst): if isinstance(lst, list): if self.status: for c in lst: if not c.is_ok(): self.status = False for c in lst: if c.contains(self): log_error("Attempting to add a list with me in it") return self.children.extend(lst) else: self.append(lst)
def update_modules(module=None): ret = False conf = Settings.get(plugin_id) for mod in conf.get_section('/settings/remote-modules/modules'): if module and not mod == module: continue url = conf.get_string('/settings/remote-modules/modules', mod, '') log("Fetching module %s as %s" % (mod, url)) file = get_target_file(url) if not download_module(url, file): log_error("Failed to fetch module: %s" % mod) continue ret = True return ret
def __main__(args): global plugin_id # List all namespaces recursivly core = Core.get(plugin_id) (ret, ns_msgs) = core.simple_exec('CheckWMI', 'wmi', ['--list-all-ns']) if len(ns_msgs) == 0: log_error("Failed to execute WMI command is CheckWMI enabled?") else: for ns in ns_msgs[0].splitlines(): # List all classes in each namespace (ret, cls_msgs) = core.simple_exec( 'any', 'wmi', ['--list-classes', '--simple', '--namespace', ns]) for cls in cls_msgs[0].splitlines(): log('%s : %s' % (ns, cls))
def do_one_test(self, script, expected = status.OK, message = "Foo Bar", args=[], cleanup=True): result = TestResult('%s (%s)'%(script, args)) (ret, msg, perf) = self.core.simple_query(script, args) if cleanup and os.name != 'nt': message = message.replace('"', '') message = message.replace('$ARG1$', '$') message = message.replace('$ARG2$', '$') message = message.replace('$ARG3$', '$') message = message.replace('\r', '\n') message = message.replace('\n\n', '\n') msg = msg.replace('\r', '\n') msg = msg.replace('\n\n', '\n') result.assert_equals(ret, expected, 'Validate return code for %s'%script) result.assert_equals(msg, message, 'Validate return message for %s'%script) if msg != message: diff = difflib.ndiff(msg.splitlines(1), message.splitlines(1)) for l in diff: log_error(l) return result
def run(self, arguments = []): cases = [] try: parser = ThrowingArgumentParser(prog='nscp') parser.add_argument("--script", help="The script to run (sort of ignored)", action='store') parser.add_argument("--case", help="Which test case to run", action='append') args = parser.parse_args(arguments) cases = args.case except Exception as e: log_error('Failed to parse command line: %s'%e) result = TestResult('Test result for %d suites'%len(self.suites)) for suite in self.suites: instance = suite.getInstance() instance.setup(self.plugin_id, self.prefix) suite_result = TestResult('Running suite: %s'%instance.title()) if cases: suite_result.append(instance.run_test(cases)) else: suite_result.append(instance.run_test()) result.append(suite_result) result.add_message(suite_result.is_ok(), 'Result from suite: %s'%instance.title()) instance.teardown() return result
def log(self, prefix="", indent=0): if self.status: log_debug("%s%s%s" % (prefix, "".rjust(indent, " "), self)) else: log_error("%s%s%s" % (prefix, "".rjust(indent, " "), self))