#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

factory_settings["emc_datadomain_mtree_default_levels"] = {
    "deleted": 2,
    "read-only": 1,
    "read-write": 0,
    "replication destination": 0,
    "retention lock disabled": 0,
    "retention lock enabled": 0,
    "unknown": 3,
}


def parse_emc_datadomain_mtree(info):
    return {
        line[0]: {"precompiled": int(float(line[1]) * 1024**3), "status_code": line[2]}
        for line in info
    }


@get_parsed_item_data
def check_emc_datadomain_mtree(_item, params, mtree_data):
    state_table = {
        "0": "unknown",
        "1": "deleted",
        "2": "read-only",
        "3": "read-write",
        "4": "replication destination",
        "5": "retention lock enabled",
        "6": "retention lock disabled",
    }
    dev_state_str = state_table.get(
        mtree_data["status_code"], "invalid code %s" % mtree_data["status_code"]
    )
    yield (
        params.get(dev_state_str, 3),
        "Status: %s, Precompiled: %s"
        % (dev_state_str, get_bytes_human_readable(mtree_data["precompiled"])),
        [("precompiled", mtree_data["precompiled"])],
    )


check_info["emc_datadomain_mtree"] = {
    "parse_function": parse_emc_datadomain_mtree,
    "check_function": check_emc_datadomain_mtree,
    "inventory_function": discover(),
    "service_description": "MTree %s",
    "has_perfdata": True,
    "snmp_info": (
        ".1.3.6.1.4.1.19746.1.15.2.1.1",
        [
            2,  # MtreeListName
            3,  # MtreeListPreComGi
            4,  # MtreeListStatus
        ],
    ),
    "snmp_scan_function": lambda oid: oid(".1.3.6.1.2.1.1.1.0").startswith("Data Domain OS"),
    "group": "emc_datadomain_mtree",
    "default_levels_variable": "emc_datadomain_mtree_default_levels",
}
