예제 #1
0
파일: __init__.py 프로젝트: PLUTEX/checkmk
def do_inventory_actions_during_checking_for(
    config_cache: config.ConfigCache,
    host_config: config.HostConfig,
    ipaddress: Optional[HostAddress],
    *,
    parsed_sections_broker: ParsedSectionsBroker,
) -> None:

    status_data_store = StructuredDataStore(
        Path(cmk.utils.paths.status_data_dir))

    if not host_config.do_status_data_inventory:
        # includes cluster case
        status_data_store.remove_files(host_name=host_config.hostname)
        return  # nothing to do here

    trees = _do_inv_for_realhost(
        host_config,
        ipaddress,
        parsed_sections_broker=parsed_sections_broker,
        run_plugin_names=EVERYTHING,
        retentions_tracker=RetentionsTracker([]),
    )
    if trees.status_data and not trees.status_data.is_empty():
        status_data_store.save(host_name=host_config.hostname,
                               tree=trees.status_data)
예제 #2
0
def test_real_is_equal_save_and_load(tree, tmp_path):
    store = StructuredDataStore(tmp_path)
    try:
        store.save(host_name=HostName("foo"), tree=tree)
        loaded_tree = store.load(host_name=HostName("foo"))
        assert tree.is_equal(loaded_tree)
    finally:
        shutil.rmtree(str(tmp_path))
예제 #3
0
def test_real_save_gzip(tmp_path):
    host_name = HostName("heute")
    target = tmp_path / str(host_name)
    raw_tree = {
        "node": {
            "foo": 1,
            "bär": 2,
        },
    }
    tree = StructuredDataNode.deserialize(raw_tree)
    store = StructuredDataStore(tmp_path)
    store.save(host_name=host_name, tree=tree)

    assert target.exists()

    gzip_filepath = target.with_suffix(".gz")
    assert gzip_filepath.exists()

    with gzip.open(str(gzip_filepath), "rb") as f:
        f.read()
예제 #4
0
def _save_inventory_tree(
    hostname: HostName,
    inventory_tree: StructuredDataNode,
    retentions: Retentions,
) -> Optional[StructuredDataNode]:

    inventory_store = StructuredDataStore(cmk.utils.paths.inventory_output_dir)

    if inventory_tree.is_empty():
        # Remove empty inventory files. Important for host inventory icon
        inventory_store.remove_files(host_name=hostname)
        return None

    old_tree = inventory_store.load(host_name=hostname)
    update_result = retentions.may_update(int(time.time()), old_tree)

    if old_tree.is_empty():
        console.verbose("New inventory tree.\n")

    elif not old_tree.is_equal(inventory_tree):
        console.verbose("Inventory tree has changed. Add history entry.\n")
        inventory_store.archive(
            host_name=hostname,
            archive_dir=cmk.utils.paths.inventory_archive_dir,
        )

    elif update_result.save_tree:
        console.verbose(
            "Update inventory tree%s.\n"
            % (" (%s)" % update_result.reason if update_result.reason else "")
        )
    else:
        console.verbose(
            "Inventory tree not updated%s.\n"
            % (" (%s)" % update_result.reason if update_result.reason else "")
        )
        return None

    inventory_store.save(host_name=hostname, tree=inventory_tree)
    return old_tree