Esempio n. 1
0
def _add_scanned_hosts_to_folder(folder: "CREFolder",
                                 found: NetworkScanFoundHosts) -> None:
    network_scan_properties = folder.attribute("network_scan")

    translation = network_scan_properties.get("translate_names", {})

    entries = []
    for host_name, ipaddr in found:
        host_name = translate_hostname(translation, host_name)

        attrs = update_metadata({}, created_by=_("Network scan"))

        if "tag_criticality" in network_scan_properties:
            attrs["tag_criticality"] = network_scan_properties.get(
                "tag_criticality", "offline")

        if network_scan_properties.get("set_ipaddress", True):
            attrs["ipaddress"] = ipaddr

        if not Host.host_exists(host_name):
            entries.append((host_name, attrs, None))

    with store.lock_checkmk_configuration():
        folder.create_hosts(entries)
        folder.save()
Esempio n. 2
0
    def from_headerline(
        cls,
        line: bytes,
        translation: TranslationOptions,
        *,
        encoding_fallback: str,
    ) -> "PiggybackMarker":
        raw_host_name = ensure_str_with_fallback(
            line.strip()[4:-4],
            encoding='utf-8',
            fallback=encoding_fallback,
        )
        assert raw_host_name
        hostname = translate_hostname(translation, raw_host_name)

        # Protect Checkmk against unallowed host names. Normally source scripts
        # like agent plugins should care about cleaning their provided host names
        # up, but we need to be sure here to prevent bugs in Checkmk code.
        return cls(regex("[^%s]" % REGEX_HOST_NAME_CHARS).sub("_", hostname))
Esempio n. 3
0
    def from_headerline(
        cls,
        line: bytes,
        translation: TranslationOptions,
        *,
        encoding_fallback: str,
    ) -> "PiggybackMarker":
        # ? ensure_str called on a bytes object with different possible encodings
        raw_host_name = ensure_str_with_fallback(
            line.strip()[4:-4],
            encoding="utf-8",
            fallback=encoding_fallback,
        )
        assert raw_host_name
        hostname = translate_hostname(translation, raw_host_name)

        # Protect Checkmk against unallowed host names. Normally source scripts
        # like agent plugins should care about cleaning their provided host names
        # up, but we need to be sure here to prevent bugs in Checkmk code.
        # TODO: this should be moved into the HostName class, if it is ever created.
        return cls(
            HostName(
                regex("[^%s]" % REGEX_HOST_NAME_CHARS).sub("_", hostname)))
def test_translate_hostname(hostname, translation, result):
    assert translations.translate_hostname(translation, hostname) == result