Esempio n. 1
0
def test_inv_docker_container_labels():
    info = [
        line.split("\0") if "\0" in line else line.split(" ") for line in AGENT_OUTPUT.split("\n")
    ]
    plugin = agent_based_register.get_inventory_plugin(
        InventoryPluginName('docker_container_labels'))
    assert plugin
    assert list(plugin.inventory_function(info)) == [
        Attributes(path=['software', 'applications', 'docker', 'container'],
                   inventory_attributes={
                       'labels': ('com.docker.swarm.node.id: x2my5tv8bqg0yh5jq98gzodr2, '
                                  'com.docker.swarm.service.id: nrgxet23d204ywz1rjl8fbtff, '
                                  'com.docker.swarm.service.name: redis, '
                                  'com.docker.swarm.task: , '
                                  'com.docker.swarm.task.id: jjp7380fb51n4figvv4zxl350, '
                                  'com.docker.swarm.task.name: redis.1.jjp7380fb51n4figvv4zxl350')
                   },
                   status_attributes={})
    ]
def test_migrated_inventory_plugin(fix_plugin_legacy, fix_register):
    # pick an automigrated plugin
    test_plugin_name = "perle_chassis"
    assert test_plugin_name in fix_plugin_legacy.inv_info

    plugin = fix_register.inventory_plugins.get(
        InventoryPluginName(test_plugin_name))
    assert plugin is not None

    # think of a model, and remember it:
    result = list(
        plugin.inventory_function([["42.23", "", "", "", "", "", ""]]))

    assert len(result) == 1

    attr = result[0]
    assert isinstance(attr, Attributes)
    assert attr.inventory_attributes[
        "model"] == "42.23"  # abracadabra! Is this your model?
Esempio n. 3
0
def create_inventory_plugin(
    *,
    name: str,
    sections: Optional[List[str]] = None,
    inventory_function: Callable,
    inventory_default_parameters: Optional[Dict] = None,
    inventory_ruleset_name: Optional[str] = None,
    module: Optional[str] = None,
) -> InventoryPlugin:
    """Return an InventoryPlugin object after validating and converting the arguments one by one

    For a detailed description of the parameters please refer to the exposed function in the
    'register' namespace of the API.
    """
    plugin_name = InventoryPluginName(name)

    subscribed_sections = create_subscribed_sections(sections, plugin_name)

    validate_function_arguments(
        type_label="inventory",
        function=inventory_function,
        has_item=False,
        default_params=inventory_default_parameters,
        sections=subscribed_sections,
    )

    # validate check arguments
    validate_default_parameters(
        "inventory",
        inventory_ruleset_name,
        inventory_default_parameters,
    )

    return InventoryPlugin(
        name=plugin_name,
        sections=subscribed_sections,
        inventory_function=_filter_inventory(inventory_function),
        inventory_default_parameters=inventory_default_parameters or {},
        inventory_ruleset_name=(RuleSetName(inventory_ruleset_name)
                                if inventory_ruleset_name else None),
        module=module,
    )
Esempio n. 4
0
def test_inv_docker_node_images():
    parsed = [
        line.split("\0") if "\0" in line else line.split(" ")
        for line in AGENT_OUTPUT.split("\n")
    ]
    plugin = agent_based_register.get_inventory_plugin(
        InventoryPluginName('docker_node_images'))
    assert plugin
    assert list(plugin.inventory_function(parsed)) == [
        TableRow(
            path=['software', 'applications', 'docker', 'containers'],
            key_columns={
                'id': '891a6f6a1c28',
                'image': 'b2bf42ca5d8f',
                'name': '/relaxed_shaw',
                'creation': '2021-02-12T12:15:28.230110819Z',
                'labels':
                'another_container_label: 2, container: label, image_label_command_line: 1, image_label_dockerfile: 2',
                'status': 'running'
            },
            inventory_columns={},
            status_columns={}),
        TableRow(path=['software', 'applications', 'docker', 'images'],
                 key_columns={'id': 'b2bf42ca5d8f'},
                 inventory_columns={
                     'repotags':
                     'hello:world',
                     'repodigests':
                     '',
                     'creation':
                     '2021-02-12T11:29:33.063968737Z',
                     'size':
                     1231733,
                     'labels':
                     'image_label_command_line: 1, image_label_dockerfile: 2'
                 },
                 status_columns={}),
        TableRow(path=['software', 'applications', 'docker', 'images'],
                 key_columns={'id': 'b2bf42ca5d8f'},
                 inventory_columns={},
                 status_columns={'amount_containers': 1}),
    ]
def test_inv_docker_node_images_labels_null():
    plugin = agent_based_register.get_inventory_plugin(
        InventoryPluginName('docker_node_images'))
    assert plugin
    assert list(plugin.inventory_function(AGENT_OUTPUT_NULL_LABELS_ST)) == [
        TableRow(path=['software', 'applications', 'docker', 'images'],
                 key_columns={'id': '666620a54926'},
                 inventory_columns={
                     'repotags': 'plantuml:latest',
                     'repodigests': '',
                     'creation': '2021-02-25T08:42:22.47977742Z',
                     'size': 389770065,
                     'labels': ''
                 },
                 status_columns={}),
        TableRow(path=['software', 'applications', 'docker', 'images'],
                 key_columns={'id': '666620a54926'},
                 inventory_columns={},
                 status_columns={'amount_containers': 0}),
    ]
Esempio n. 6
0
def test_migrated_inventory_plugin(inv_info):  # pylint: disable=unused-argument
    # pick a plugin, any plugin
    plugin = agent_based_register.get_inventory_plugin(InventoryPluginName("aix_baselevel"))
    assert plugin is not None

    # think of a version, and remember it:
    result = list(plugin.inventory_function([["42.23"]]))

    assert len(result) == 1

    attr = result[0]
    assert isinstance(attr, Attributes)
    assert attr.path == ["software", "os"]
    assert attr.status_attributes == {}
    assert attr.inventory_attributes == {
        "version": "42.23",  # abracadabra! Is this your version?
        "vendor": "IBM",
        "type": "aix",
        "name": "IBM AIX 42.23"
    }
def test_inv_docker_container_network():
    info = [
        line.split("\0") if "\0" in line else line.split(" ") for line in AGENT_OUTPUT.split("\n")
    ]
    plugin = agent_based_register.get_inventory_plugin(
        InventoryPluginName('docker_container_network'))
    assert plugin
    assert list(plugin.inventory_function(info)) == [
        TableRow(
            path=['software', 'applications', 'docker', 'container', 'networks'],
            key_columns={
                'name': 'bridge',
                'network_id': '8f2baa1938b7957f876c1f5aa6767b86395b6971f349793bd7fae12eae6b83f0',
                'ip_address': '172.17.0.2',
                'ip_prefixlen': 16,
                'gateway': '172.17.0.1',
                'mac_address': '02:42:ac:11:00:02'
            },
            inventory_columns={},
            status_columns={})
    ]
Esempio n. 8
0
def _get_relevant_raw_sections(services: List[Service],
                               host_config: config.HostConfig):
    # see if we can remove this function, once inventory plugins have been migrated to
    # the new API. In particular, we schould be able to get rid of the imports.

    if host_config.do_status_data_inventory:
        # This is called during checking, but the inventory plugins are not loaded yet
        import cmk.base.inventory_plugins as inventory_plugins  # pylint: disable=import-outside-toplevel
        from cmk.base.check_api import get_check_api_context  # pylint: disable=import-outside-toplevel
        inventory_plugins.load_plugins(get_check_api_context,
                                       inventory.get_inventory_context)
        inventory_plugin_names: Iterable[InventoryPluginName] = (
            InventoryPluginName(name.split('.')[0])
            for name in inventory_plugins.inv_info)
    else:
        inventory_plugin_names = ()

    return agent_based_register.get_relevant_raw_sections(
        check_plugin_names=(s.check_plugin_name for s in services),
        inventory_plugin_names=inventory_plugin_names,
    )
Esempio n. 9
0
def test_inv_docker_node_images(fix_register):
    parsed = [line.split("\0") for line in AGENT_OUTPUT.split("\n")]
    plugin = fix_register.inventory_plugins[InventoryPluginName(
        "docker_node_images")]
    assert list(plugin.inventory_function(parsed)) == [
        TableRow(
            path=["software", "applications", "docker", "containers"],
            key_columns={
                "id": "891a6f6a1c28",
                "image": "b2bf42ca5d8f",
                "name": "/relaxed_shaw",
                "creation": "2021-02-12T12:15:28.230110819Z",
                "labels":
                "another_container_label: 2, container: label, image_label_command_line: 1, image_label_dockerfile: 2",
                "status": "running",
            },
            inventory_columns={},
            status_columns={},
        ),
        TableRow(
            path=["software", "applications", "docker", "images"],
            key_columns={"id": "b2bf42ca5d8f"},
            inventory_columns={
                "repotags": "hello:world",
                "repodigests": "",
                "creation": "2021-02-12T11:29:33.063968737Z",
                "size": 1231733,
                "labels":
                "image_label_command_line: 1, image_label_dockerfile: 2",
            },
            status_columns={},
        ),
        TableRow(
            path=["software", "applications", "docker", "images"],
            key_columns={"id": "b2bf42ca5d8f"},
            inventory_columns={},
            status_columns={"amount_containers": 1},
        ),
    ]
Esempio n. 10
0
def test_inv_docker_node_images_labels_null(fix_register):
    plugin = fix_register.inventory_plugins[InventoryPluginName(
        "docker_node_images")]
    assert list(plugin.inventory_function(AGENT_OUTPUT_NULL_LABELS_ST)) == [
        TableRow(
            path=["software", "applications", "docker", "images"],
            key_columns={"id": "666620a54926"},
            inventory_columns={
                "repotags": "plantuml:latest",
                "repodigests": "",
                "creation": "2021-02-25T08:42:22.47977742Z",
                "size": 389770065,
                "labels": "",
            },
            status_columns={},
        ),
        TableRow(
            path=["software", "applications", "docker", "images"],
            key_columns={"id": "666620a54926"},
            inventory_columns={},
            status_columns={"amount_containers": 0},
        ),
    ]
def test_migrated_inventory_plugin(fix_plugin_legacy, fix_register):
    # pick an automigrated plugin
    test_plugin_name = "aix_baselevel"
    assert test_plugin_name in fix_plugin_legacy.inv_info

    plugin = fix_register.inventory_plugins.get(
        InventoryPluginName(test_plugin_name))
    assert plugin is not None

    # think of a version, and remember it:
    result = list(plugin.inventory_function([["42.23"]]))

    assert len(result) == 1

    attr = result[0]
    assert isinstance(attr, Attributes)
    assert attr.path == ["software", "os"]
    assert attr.status_attributes == {}
    assert attr.inventory_attributes == {
        "version": "42.23",  # abracadabra! Is this your version?
        "vendor": "IBM",
        "type": "aix",
        "name": "IBM AIX 42.23"
    }
Esempio n. 12
0
def test_inv_prtconf(fix_register):
    plugin = fix_register.inventory_plugins[InventoryPluginName("prtconf")]
    assert list(plugin.inventory_function(INFO)) == EXPECTED
Esempio n. 13
0
def test_inv_prtconf():
    plugin = agent_based_register.get_inventory_plugin(
        InventoryPluginName('prtconf'))
    assert plugin
    assert list(plugin.inventory_function(INFO)) == EXPECTED
def test_inv_lnx_block_devices(fix_register, info, inventory_data):
    plugin = fix_register.inventory_plugins[InventoryPluginName("lnx_block_devices")]
    assert list(plugin.inventory_function(info)) == inventory_data
Esempio n. 15
0
def test_inv_perle_chassis(fix_register):
    plugin = fix_register.inventory_plugins[InventoryPluginName(
        "perle_chassis")]
    assert sort_inventory_result(
        plugin.inventory_function(INFO)) == sort_inventory_result(EXPECTED)
Esempio n. 16
0
def test_inv_docker_container_node_name_legacy_agent_output(fix_register):
    plugin = fix_register.inventory_plugins[InventoryPluginName(
        "docker_container_node_name")]
    with pytest.raises(AgentOutputMalformatted):
        list(plugin.inventory_function([["node_name"]]))
Esempio n. 17
0
def test_inv_solaris_pkginfo(fix_register) -> None:
    plugin = fix_register.inventory_plugins[InventoryPluginName(
        "solaris_pkginfo")]
    with set_timezone("ETC-2"):
        result = list(plugin.inventory_function(INFO))
    assert result == RESULT
Esempio n. 18
0
def test_inv_docker_container_node_name_legacy_agent_output():
    plugin = agent_based_register.get_inventory_plugin(
        InventoryPluginName('docker_container_node_name'))
    assert plugin
    with pytest.raises(AgentOutputMalformatted):
        list(plugin.inventory_function([['node_name']]))
def test_inv_oracle_systemparameter(fix_register, info, expected):
    plugin = fix_register.inventory_plugins[InventoryPluginName(
        "oracle_systemparameter")]
    assert list(plugin.inventory_function(info)) == expected
Esempio n. 20
0
def test_inv_oracle_systemparameter(info, params, inventory_data):
    plugin = agent_based_register.get_inventory_plugin(
        InventoryPluginName('lnx_sysctl'))
    assert plugin
    assert list(plugin.inventory_function(params, info)) == inventory_data
Esempio n. 21
0
def test_inv_oracle_systemparameter(fix_register, info, params, inventory_data):
    plugin = fix_register.inventory_plugins[InventoryPluginName("lnx_sysctl")]
    assert list(plugin.inventory_function(params, info)) == inventory_data
Esempio n. 22
0
def test_inv_lnx_block_devices(info, inventory_data):
    plugin = agent_based_register.get_inventory_plugin(
        InventoryPluginName('lnx_block_devices'))
    assert plugin
    assert list(plugin.inventory_function(info)) == inventory_data
Esempio n. 23
0
def test_inv_oracle_instance(fix_register, line, expected_data):
    section = fix_register.agent_sections[SectionName("oracle_instance")]
    parsed = section.parse_function([line])
    inv_plugin = fix_register.inventory_plugins[InventoryPluginName(
        "oracle_instance")]
    assert list(inv_plugin.inventory_function(parsed)) == expected_data
Esempio n. 24
0
def test_inv_oracle_systemparameter(info, expected):
    plugin = agent_based_register.get_inventory_plugin(
        InventoryPluginName('oracle_systemparameter'))
    assert plugin
    assert list(plugin.inventory_function(info)) == expected
Esempio n. 25
0
def test_inv_oracle_instance_multiline(fix_register):
    lines = [
        [
            "SID",
            "VERSION",
            "OPENMODE",
            "LOGINS",
            "_ARCHIVER",
            "123",
            "_DBID",
            "LOGMODE",
            "_DATABASE_ROLE",
            "_FORCE_LOGGING",
            "_NAME",
            "080220151025",
            "_PLUGGABLE",
            "_CON_ID",
            "",
            "_PDBID",
            "_POPENMODE",
            "_PRESTRICTED",
            "_PTOTAL_SIZE",
            "_PRECOVERY_STATUS",
            "_PUP_SECONDS",
            "_PBLOCK_SIZE",
        ],
        [
            "SID",
            "VERSION",
            "_OPENMODE",
            "LOGINS",
            "_ARCHIVER",
            "_RAW_UP_SECONDS",
            "_DBID",
            "LOGMODE",
            "_DATABASE_ROLE",
            "_FORCE_LOGGING",
            "_NAME",
            "080220151026",
            "TRUE",
            "_CON_ID",
            "PNAME",
            "_PDBID",
            "POPENMODE",
            "_PRESTRICTED",
            "_PTOTAL_SIZE",
            "_PRECOVERY_STATUS",
            "456",
            "_PBLOCK_SIZE",
        ],
    ]

    section = fix_register.agent_sections[SectionName("oracle_instance")]
    parsed = section.parse_function(lines)  # type: ignore[arg-type]
    inv_plugin = fix_register.inventory_plugins[InventoryPluginName(
        "oracle_instance")]

    expected_data = [
        TableRow(
            path=["software", "applications", "oracle", "instance"],
            key_columns={
                "sid": "SID",
            },
            inventory_columns={
                "pname": "",
                "version": "VERSION",
                "openmode": "OPENMODE",
                "logmode": "LOGMODE",
                "logins": "LOGINS",
                "db_creation_time": "2015-02-08 10:25",
            },
            status_columns={
                "db_uptime": 123,
            },
        ),
        TableRow(
            path=["software", "applications", "oracle", "instance"],
            key_columns={
                "sid": "SID",
            },
            inventory_columns={
                "pname": "PNAME",
                "version": "VERSION",
                "openmode": "POPENMODE",
                "logmode": "LOGMODE",
                "logins": "ALLOWED",
                "db_creation_time": "2015-02-08 10:26",
            },
            status_columns={
                "db_uptime": 456,
            },
        ),
    ]

    assert list(inv_plugin.inventory_function(
        parsed)) == expected_data  # type: ignore[union-attr]
Esempio n. 26
0
def test_inv_docker_node_info(parsed, expected):
    plugin = agent_based_register.get_inventory_plugin(
        InventoryPluginName('docker_node_info'))
    assert plugin
    assert list(plugin.inventory_function(parsed)) == expected
def test_inv_docker_node_info(fix_register, parsed, expected):
    plugin = fix_register.inventory_plugins[InventoryPluginName(
        "docker_node_info")]
    assert list(plugin.inventory_function(parsed)) == expected
Esempio n. 28
0
def test_inv_oracle_instance_multiline():
    lines = [
        [
            'SID',
            'VERSION',
            'OPENMODE',
            'LOGINS',
            '_ARCHIVER',
            '123',
            '_DBID',
            'LOGMODE',
            '_DATABASE_ROLE',
            '_FORCE_LOGGING',
            '_NAME',
            '080220151025',
            '_PLUGGABLE',
            '_CON_ID',
            '',
            '_PDBID',
            '_POPENMODE',
            '_PRESTRICTED',
            '_PTOTAL_SIZE',
            '_PRECOVERY_STATUS',
            '_PUP_SECONDS',
            '_PBLOCK_SIZE',
        ],
        [
            'SID',
            'VERSION',
            '_OPENMODE',
            'LOGINS',
            '_ARCHIVER',
            '_RAW_UP_SECONDS',
            '_DBID',
            'LOGMODE',
            '_DATABASE_ROLE',
            '_FORCE_LOGGING',
            '_NAME',
            '080220151026',
            'TRUE',
            '_CON_ID',
            'PNAME',
            '_PDBID',
            'POPENMODE',
            '_PRESTRICTED',
            '_PTOTAL_SIZE',
            '_PRECOVERY_STATUS',
            '456',
            '_PBLOCK_SIZE',
        ],
    ]

    section = agent_based_register.get_section_plugin(
        SectionName('oracle_instance'))
    parsed = section.parse_function(lines)  # type: ignore[arg-type]
    inv_plugin = agent_based_register.get_inventory_plugin(
        InventoryPluginName('oracle_instance'))

    expected_data = [
        TableRow(
            path=['software', 'applications', 'oracle', 'instance'],
            key_columns={
                "sid": "SID",
                "pname": "",
            },
            inventory_columns={
                "version": "VERSION",
                "openmode": "OPENMODE",
                "logmode": 'LOGMODE',
                "logins": "LOGINS",
                "db_creation_time": "2015-02-08 10:25",
            },
        ),
        TableRow(
            path=['software', 'applications', 'oracle', 'instance'],
            key_columns={
                "sid": "SID",
                "pname": "PNAME",
            },
            inventory_columns={
                "version": "VERSION",
                "openmode": "POPENMODE",
                "logmode": 'LOGMODE',
                "logins": "ALLOWED",
                "db_creation_time": "2015-02-08 10:26",
            },
        ),
        TableRow(
            path=['software', 'applications', 'oracle', 'instance'],
            key_columns={
                "sid": "SID",
                "pname": "",
            },
            status_columns={
                "db_uptime": 123,
            },
        ),
        TableRow(
            path=['software', 'applications', 'oracle', 'instance'],
            key_columns={
                "sid": "SID",
                "pname": "PNAME",
            },
            status_columns={
                "db_uptime": 456,
            },
        ),
    ]

    assert list(inv_plugin.inventory_function(
        parsed)) == expected_data  # type: ignore[union-attr]