#!/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.


def inventory_dell_eql_storage(info):
    for line in info:
        yield line[0], {}


def check_dell_eql_storage(item, _no_params, info):
    for name, desc, health_state, raid_state, total_storage, \
            repl_storage, snap_storage, used_storage in info:
        if name == item:
            if desc:
                yield 0, desc

            # Health Status:
            health_states = {
                "0": "Unknown",
                "1": "Normal",
                "2": "Warning",
                "3": "Critical",
            }
            if health_state == "1":
                state = 0
            elif health_state in ["2", "0"]:
                state = 1
            else:
                state = 2
            yield state, "Health State: %s" % health_states[health_state]

            # Raid Status
            raid_states = {
                "1": "Ok",
                "2": "Degraded",
                "3": "Verifying",
                "4": "Reconstructing",
                "5": "Failed",
                "6": "CatastrophicLoss",
                "7": "Expanding",
                "8": "Mirroring",
            }

            if raid_state == "1":
                state = 0
            elif raid_state in ["3", "4", "7", "8"]:
                state = 1
            else:
                state = 2
            yield state, "Raid State: %s" % raid_states[raid_state]

            # Storage
            total_bytes = int(total_storage) * 1048576
            used_bytes = int(used_storage) * 1048576
            repl_bytes = int(repl_storage) * 1048576
            snap_bytes = int(snap_storage) * 1048576
            perfdata = [("fs_used", used_bytes), ("fs_size", total_bytes)]
            yield 0, "Used: %s/%s (Snapshots: %s, Replication: %s)" % \
                    (get_bytes_human_readable(used_bytes), \
                     get_bytes_human_readable(total_bytes),\
                     get_bytes_human_readable(snap_bytes),\
                     get_bytes_human_readable(repl_bytes)),\
                     perfdata



check_info["dell_eql_storage"] = {
    "check_function"        : check_dell_eql_storage,
    "inventory_function"    : inventory_dell_eql_storage,
    "service_description"   : "Storage %s",
    "has_perfdata"          : True,
    "snmp_scan_function"    : lambda oid: "EQL-SUP" in oid(".1.3.6.1.2.1.1.1.0") or \
                                          oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.12740.17"),
    "snmp_info"             : (".1.3.6.1.4.1.12740.2.1", [
                                            "1.1.9.1",  #eqlMemberName
                                            "1.1.7.1",  #eqlMemberDescription
                                            "5.1.1.1",  #eqlMemberHealthStatus
                                            "13.1.1.1", #eqlMemberRaidStatus
                                            "10.1.1.1", #eqlMemberTotalStorage
                                            "10.1.4.1", #eqlMemberReplStorage
                                            "10.1.3.1", #eqlMemberSnapStorage
                                            "10.1.2.1", #eqlMemberUsedStorage
                                        ]),
}
