Ejemplo n.º 1
0
def load_legacy_inventory_plugins(
    get_check_api_context: config.GetCheckApiContext,
    get_inventory_context: GetInventoryApiContext,
) -> None:
    loaded_files: Set[str] = set()
    filelist = config.get_plugin_paths(
        str(cmk.utils.paths.local_inventory_dir),
        cmk.utils.paths.inventory_dir)

    for f in filelist:
        if f[0] == "." or f[-1] == "~":
            continue  # ignore editor backup / temp files

        file_name = os.path.basename(f)
        if file_name in loaded_files:
            continue  # skip already loaded files (e.g. from local)

        try:
            plugin_context = _new_inv_context(get_check_api_context,
                                              get_inventory_context)

            _load_plugin_includes(f, plugin_context)

            exec(open(f).read(), plugin_context)  # yapf: disable
            loaded_files.add(file_name)
        except Exception as e:
            console.error("Error in inventory plugin file %s: %s\n", f, e)
            if cmk.utils.debug.enabled():
                raise
            continue

        # Now store the check context for all plugins found in this file
        for check_plugin_name in inv_info:
            _plugin_contexts.setdefault(check_plugin_name, plugin_context)
            _plugin_file_lookup.setdefault(check_plugin_name, f)

    _extract_snmp_sections(inv_info, _plugin_file_lookup)
    _extract_inventory_plugins(inv_info)
Ejemplo n.º 2
0
    def execute(self, cmd, args):
        # type: (str, List[str]) -> Any
        self._handle_generic_arguments(args)

        try:
            try:
                automation = self._automations[cmd]
            except KeyError:
                raise MKAutomationError(
                    "Automation command '%s' is not implemented." % cmd)

            if automation.needs_checks:
                config.load_all_checks(check_api.get_check_api_context)

            if automation.needs_config:
                config.load(validate_hosts=False)

            result = automation.execute(args)

        except (MKAutomationError, MKTimeout) as e:
            console.error("%s\n" % e)
            if cmk.utils.debug.enabled():
                raise
            return 1

        except Exception as e:
            if cmk.utils.debug.enabled():
                raise
            console.error("%s\n" % e)
            return 2

        finally:
            profiling.output_profile()

        out.output(python_printer.pformat(result))
        out.output('\n')

        return 0
Ejemplo n.º 3
0
def do_snmptranslate(walk_filename: str) -> None:
    if not walk_filename:
        raise MKGeneralException("Please provide the name of a SNMP walk file")

    walk_path = "%s/%s" % (cmk.utils.paths.snmpwalks_dir, walk_filename)
    if not os.path.exists(walk_path):
        raise MKGeneralException("The walk '%s' does not exist" % walk_path)

    def translate(lines: List[bytes]) -> List[Tuple[bytes, bytes]]:
        result_lines = []
        try:
            oids_for_command = []
            for line in lines:
                oids_for_command.append(line.split(b" ")[0])

            command = [
                b"snmptranslate", b"-m", b"ALL",
                b"-M+%s" % cmk.utils.paths.local_mib_dir
            ] + oids_for_command
            p = subprocess.Popen(command,
                                 stdout=subprocess.PIPE,
                                 stderr=open(os.devnull, "w"),
                                 close_fds=True)
            p.wait()
            if p.stdout is None:
                raise RuntimeError()
            output = p.stdout.read()
            result = output.split(b"\n")[0::2]
            for idx, line in enumerate(result):
                result_lines.append((line.strip(), lines[idx].strip()))

        except Exception as e:
            console.error("%s\n" % e)

        return result_lines

    # Translate n-oid's per cycle
    entries_per_cycle = 500
    translated_lines: List[Tuple[bytes, bytes]] = []

    walk_lines = open(walk_path).readlines()
    console.error("Processing %d lines.\n" % len(walk_lines))

    i = 0
    while i < len(walk_lines):
        console.error("\r%d to go...    " % (len(walk_lines) - i))
        process_lines = walk_lines[i:i + entries_per_cycle]
        # FIXME: This encoding ping-pong os horrible...
        translated = translate([ensure_binary(pl) for pl in process_lines])
        i += len(translated)
        translated_lines += translated
    console.error("\rfinished.                \n")

    with suppress(IOError):
        sys.stdout.write("\n".join("%s --> %s" %
                                   (ensure_str(line), ensure_str(translation))
                                   for translation, line in translated_lines) +
                         "\n")
Ejemplo n.º 4
0
def gather_available_raw_section_names(
    sections: Iterable[SNMPScanSection],
    on_error: str,
    do_snmp_scan: bool,
    *,
    binary_host: bool,
    backend: ABCSNMPBackend,
) -> Set[SectionName]:
    try:
        return _snmp_scan(
            sections,
            on_error=on_error,
            do_snmp_scan=do_snmp_scan,
            binary_host=binary_host,
            backend=backend,
        )
    except Exception as e:
        if on_error == "raise":
            raise
        if on_error == "warn":
            console.error("SNMP scan failed: %s\n" % e)

    return set()
Ejemplo n.º 5
0
def gather_available_raw_section_names(
    sections: Collection[SNMPScanSection],
    on_error: str,
    *,
    missing_sys_description: bool,
    backend: ABCSNMPBackend,
) -> Set[SectionName]:
    if not sections:
        return set()

    try:
        return _snmp_scan(
            sections,
            on_error=on_error,
            missing_sys_description=missing_sys_description,
            backend=backend,
        )
    except Exception as e:
        if on_error == "raise":
            raise
        if on_error == "warn":
            console.error("SNMP scan failed: %s\n" % e)

    return set()
Ejemplo n.º 6
0
def _enriched_discovered_services(
    host_name: HostName,
    check_plugin_name: CheckPluginName,
    plugins_services: checking_classes.DiscoveryResult,
) -> Generator[Service, None, None]:
    for service in plugins_services:
        description = config.service_description(host_name, check_plugin_name,
                                                 service.item)
        # make sanity check
        if not description:
            console.error(
                f"{host_name}: {check_plugin_name} returned empty service description - ignoring it.\n"
            )
            continue

        yield Service(
            check_plugin_name=check_plugin_name,
            item=service.item,
            description=description,
            parameters=unwrap_parameters(service.parameters),
            # Convert from APIs ServiceLabel to internal ServiceLabel
            service_labels=DiscoveredServiceLabels(*(ServiceLabel(*l)
                                                     for l in service.labels)),
        )
Ejemplo n.º 7
0
def gather_available_raw_section_names(
    sections: Collection[SNMPScanSection],
    *,
    on_error: OnError = OnError.RAISE,
    missing_sys_description: bool,
    backend: SNMPBackend,
) -> FrozenSet[SectionName]:
    if not sections:
        return frozenset()

    try:
        return _snmp_scan(
            sections,
            on_error=on_error,
            missing_sys_description=missing_sys_description,
            backend=backend,
        )
    except Exception as e:
        if on_error is OnError.RAISE:
            raise
        if on_error is OnError.WARN:
            console.error("SNMP scan failed: %s\n" % e)

    return frozenset()
Ejemplo n.º 8
0
def do_snmpwalk(options, hostnames):
    # type: (SNMPWalkOptions, List[HostName]) -> None
    if "oids" in options and "extraoids" in options:
        raise MKGeneralException(
            "You cannot specify --oid and --extraoid at the same time.")

    if not hostnames:
        raise MKBailOut("Please specify host names to walk on.")

    if not os.path.exists(cmk.utils.paths.snmpwalks_dir):
        os.makedirs(cmk.utils.paths.snmpwalks_dir)

    for hostname in hostnames:
        #TODO: What about SNMP management boards?
        snmp_config = create_snmp_host_config(hostname)

        try:
            _do_snmpwalk_on(snmp_config, options,
                            cmk.utils.paths.snmpwalks_dir + "/" + hostname)
        except Exception as e:
            console.error("Error walking %s: %s\n" % (hostname, e))
            if cmk.utils.debug.enabled():
                raise
        cmk.base.cleanup.cleanup_globals()
Ejemplo n.º 9
0
def scan_parents_of(config_cache: config.ConfigCache,
                    hosts: List[HostName],
                    silent: bool = False,
                    settings: Optional[Dict[str, int]] = None) -> Gateways:
    if settings is None:
        settings = {}

    if config.monitoring_host:
        host_config = config_cache.get_host_config(config.monitoring_host)
        nagios_ip = ip_lookup.lookup_ipv4_address(host_config)
    else:
        nagios_ip = None

    os.putenv("LANG", "")
    os.putenv("LC_ALL", "")

    # Start processes in parallel
    procs: List[Tuple[HostName, Optional[HostAddress],
                      Union[str, subprocess.Popen]]] = []
    for host in hosts:
        console.verbose("%s " % host)
        host_config = config_cache.get_host_config(host)
        try:
            ip = ip_lookup.lookup_ipv4_address(host_config)
            if ip is None:
                raise RuntimeError()
            command = [
                "traceroute", "-w",
                "%d" % settings.get("timeout", 8), "-q",
                "%d" % settings.get("probes", 2), "-m",
                "%d" % settings.get("max_ttl", 10), "-n", ip
            ]
            console.vverbose("Running '%s'\n" %
                             subprocess.list2cmdline(command))

            procs.append((host, ip,
                          subprocess.Popen(command,
                                           stdout=subprocess.PIPE,
                                           stderr=subprocess.STDOUT,
                                           close_fds=True,
                                           encoding="utf-8")))
        except Exception as e:
            if cmk.utils.debug.enabled():
                raise
            procs.append((host, None, "ERROR: %s" % e))

    # Output marks with status of each single scan
    def dot(color: str, dot: str = 'o') -> None:
        if not silent:
            out.output(tty.bold + color + dot + tty.normal)

    # Now all run and we begin to read the answers. For each host
    # we add a triple to gateways: the gateway, a scan state  and a diagnostic output
    gateways: Gateways = []
    for host, ip, proc_or_error in procs:
        if isinstance(proc_or_error, str):
            lines = [proc_or_error]
            exitstatus = 1
        else:
            exitstatus = proc_or_error.wait()
            if proc_or_error.stdout is None:
                raise RuntimeError()
            lines = [l.strip() for l in proc_or_error.stdout.readlines()]

        if exitstatus:
            dot(tty.red, '*')
            gateways.append(
                (None, "failed", 0,
                 "Traceroute failed with exit code %d" % (exitstatus & 255)))
            continue

        if len(lines) == 1 and lines[0].startswith("ERROR:"):
            message = lines[0][6:].strip()
            console.verbose("%s: %s\n", host, message, stream=sys.stderr)
            dot(tty.red, "D")
            gateways.append((None, "dnserror", 0, message))
            continue

        if len(lines) == 0:
            if cmk.utils.debug.enabled():
                raise MKGeneralException(
                    "Cannot execute %s. Is traceroute installed? Are you root?"
                    % command)
            dot(tty.red, '!')
            continue

        if len(lines) < 2:
            if not silent:
                console.error("%s: %s\n" % (host, ' '.join(lines)))
            gateways.append((None, "garbled", 0,
                             "The output of traceroute seem truncated:\n%s" %
                             ("".join(lines))))
            dot(tty.blue)
            continue

        # Parse output of traceroute:
        # traceroute to 8.8.8.8 (8.8.8.8), 30 hops max, 40 byte packets
        #  1  * * *
        #  2  10.0.0.254  0.417 ms  0.459 ms  0.670 ms
        #  3  172.16.0.254  0.967 ms  1.031 ms  1.544 ms
        #  4  217.0.116.201  23.118 ms  25.153 ms  26.959 ms
        #  5  217.0.76.134  32.103 ms  32.491 ms  32.337 ms
        #  6  217.239.41.106  32.856 ms  35.279 ms  36.170 ms
        #  7  74.125.50.149  45.068 ms  44.991 ms *
        #  8  * 66.249.94.86  41.052 ms 66.249.94.88  40.795 ms
        #  9  209.85.248.59  43.739 ms  41.106 ms 216.239.46.240  43.208 ms
        # 10  216.239.48.53  45.608 ms  47.121 ms 64.233.174.29  43.126 ms
        # 11  209.85.255.245  49.265 ms  40.470 ms  39.870 ms
        # 12  8.8.8.8  28.339 ms  28.566 ms  28.791 ms
        routes: List[Optional[str]] = []
        for line in lines[1:]:
            parts = line.split()
            route = parts[1]
            if route.count('.') == 3:
                routes.append(route)
            elif route == '*':
                routes.append(None)  # No answer from this router
            else:
                if not silent:
                    console.error(
                        "%s: invalid output line from traceroute: '%s'\n" %
                        (host, line))

        if len(routes) == 0:
            error = "incomplete output from traceroute. No routes found."
            console.error("%s: %s\n" % (host, error))
            gateways.append((None, "garbled", 0, error))
            dot(tty.red)
            continue

        # Only one entry -> host is directly reachable and gets nagios as parent -
        # if nagios is not the parent itself. Problem here: How can we determine
        # if the host in question is the monitoring host? The user must configure
        # this in monitoring_host.
        if len(routes) == 1:
            if ip == nagios_ip:
                gateways.append(
                    (None, "root", 0, ""))  # We are the root-monitoring host
                dot(tty.white, 'N')
            elif config.monitoring_host:
                gateways.append(((config.monitoring_host, nagios_ip, None),
                                 "direct", 0, ""))
                dot(tty.cyan, 'L')
            else:
                gateways.append((None, "direct", 0, ""))
            continue

        # Try far most route which is not identical with host itself
        ping_probes = settings.get("ping_probes", 5)
        skipped_gateways = 0
        this_route: Optional[HostAddress] = None
        for r in routes[::-1]:
            if not r or (r == ip):
                continue
            # Do (optional) PING check in order to determine if that
            # gateway can be monitored via the standard host check
            if ping_probes:
                if not gateway_reachable_via_ping(r, ping_probes):
                    console.verbose("(not using %s, not reachable)\n",
                                    r,
                                    stream=sys.stderr)
                    skipped_gateways += 1
                    continue
            this_route = r
            break
        if not this_route:
            error = "No usable routing information"
            if not silent:
                console.error("%s: %s\n" % (host, error))
            gateways.append((None, "notfound", 0, error))
            dot(tty.blue)
            continue

        # TTLs already have been filtered out)
        gateway_ip = this_route
        gateway = _ip_to_hostname(config_cache, this_route)
        if gateway:
            console.verbose("%s(%s) ", gateway, gateway_ip)
        else:
            console.verbose("%s ", gateway_ip)

        # Try to find DNS name of host via reverse DNS lookup
        dns_name = _ip_to_dnsname(gateway_ip)
        gateways.append(
            ((gateway, gateway_ip, dns_name), "gateway", skipped_gateways, ""))
        dot(tty.green, 'G')
    return gateways
Ejemplo n.º 10
0
def _discover_host_labels_for_source_type(
    *,
    host_key: HostKey,
    parsed_sections_broker: ParsedSectionsBroker,
    discovery_parameters: DiscoveryParameters,
) -> Mapping[str, HostLabel]:

    try:
        host_data = parsed_sections_broker[host_key]
    except KeyError:
        return {}

    host_labels = {}
    try:
        # We do *not* process all available raw sections. Instead we see which *parsed*
        # sections would result from them, and then process those.
        parse_sections = {
            agent_based_register.get_section_plugin(rs).parsed_section_name
            for rs in host_data.sections
        }
        applicable_sections = parsed_sections_broker.determine_applicable_sections(
            parse_sections,
            host_key.source_type,
        )

        console.vverbose("Trying host label discovery with: %s\n" %
                         ", ".join(str(s.name) for s in applicable_sections))
        for section_plugin in _sort_sections_by_label_priority(
                applicable_sections):

            kwargs = {
                'section':
                parsed_sections_broker.get_parsed_section(
                    host_key, section_plugin.parsed_section_name),
            }

            host_label_params = config.get_host_label_parameters(
                host_key.hostname, section_plugin)
            if host_label_params is not None:
                kwargs["params"] = host_label_params

            try:
                for label in section_plugin.host_label_function(**kwargs):
                    console.vverbose(
                        f"  {label.name}: {label.value} ({section_plugin.name})\n"
                    )
                    host_labels[label.name] = HostLabel(
                        label.name,
                        label.value,
                        section_plugin.name,
                    )
            except (KeyboardInterrupt, MKTimeout):
                raise
            except Exception as exc:
                if cmk.utils.debug.enabled(
                ) or discovery_parameters.on_error == "raise":
                    raise
                if discovery_parameters.on_error == "warn":
                    console.error("Host label discovery of '%s' failed: %s\n" %
                                  (section_plugin.name, exc))

    except KeyboardInterrupt:
        raise MKGeneralException("Interrupted by Ctrl-C.")

    return host_labels
Ejemplo n.º 11
0
def load_all_plugins():
    for plugin, exception in load_plugins_with_exceptions("cmk.base.plugins.agent_based"):
        console.error("Error in agent based plugin %s: %s\n", plugin, exception)
        if cmk.utils.debug.enabled():
            raise exception
Ejemplo n.º 12
0
def do_restart(core, only_reload=False):
    # type: (MonitoringCore, bool) -> None
    try:
        backup_path = None

        if try_get_activation_lock():
            # TODO: Replace by MKBailOut()/MKTerminate()?
            console.error("Other restart currently in progress. Aborting.\n")
            sys.exit(1)

        # Save current configuration
        if os.path.exists(cmk.utils.paths.nagios_objects_file):
            backup_path = cmk.utils.paths.nagios_objects_file + ".save"
            console.verbose("Renaming %s to %s\n",
                            cmk.utils.paths.nagios_objects_file,
                            backup_path,
                            stream=sys.stderr)
            os.rename(cmk.utils.paths.nagios_objects_file, backup_path)
        else:
            backup_path = None

        try:
            core_config.do_create_config(core, with_agents=True)
        except Exception as e:
            # TODO: Replace by MKBailOut()/MKTerminate()?
            console.error("Error creating configuration: %s\n" % e)
            if backup_path:
                os.rename(backup_path, cmk.utils.paths.nagios_objects_file)
            if cmk.utils.debug.enabled():
                raise
            sys.exit(1)

        if config.monitoring_core == "cmc" or cmk.base.nagios_utils.do_check_nagiosconfig():
            if backup_path:
                os.remove(backup_path)

            core.precompile()

            do_core_action(only_reload and "reload" or "restart")
        else:
            # TODO: Replace by MKBailOut()/MKTerminate()?
            console.error("Configuration for monitoring core is invalid. Rolling back.\n")

            broken_config_path = "%s/check_mk_objects.cfg.broken" % cmk.utils.paths.tmp_dir
            open(broken_config_path, "w").write(open(cmk.utils.paths.nagios_objects_file).read())
            console.error("The broken file has been copied to \"%s\" for analysis.\n" %
                          broken_config_path)

            if backup_path:
                os.rename(backup_path, cmk.utils.paths.nagios_objects_file)
            else:
                os.remove(cmk.utils.paths.nagios_objects_file)
            sys.exit(1)

    except Exception as e:
        if backup_path:
            try:
                os.remove(backup_path)
            except OSError as oe:
                if oe.errno != errno.ENOENT:
                    raise
        if cmk.utils.debug.enabled():
            raise
        # TODO: Replace by MKBailOut()/MKTerminate()?
        console.error("An error occurred: %s\n" % e)
        sys.exit(1)