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

raritan_pdu_ocprot_current_default_levels = (14.0, 15.0)

# Example for info:
# [[[u'1.1.1', u'4', u'0'],
#   [u'1.1.15', u'1', u'0'],
#   [u'1.2.1', u'4', u'0'],
#   [u'1.2.15', u'1', u'0'],
#   [u'1.3.1', u'4', u'70'],
#   [u'1.3.15', u'1', u'0'],
#   [u'1.4.1', u'4', u'0'],
#   [u'1.4.15', u'1', u'0'],
#   [u'1.5.1', u'4', u'0'],
#   [u'1.5.15', u'1', u'0'],
#   [u'1.6.1', u'4', u'0'],
#   [u'1.6.15', u'1', u'0']],
#  [[u'1'],
#   [u'0'],
#   [u'1'],
#   [u'0'],
#   [u'1'],
#   [u'0'],
#   [u'1'],
#   [u'0'],
#   [u'1'],
#   [u'0'],
#   [u'1'],
#   [u'0']]]
# Raritan implements a strange way of indexing here. The two last components
# of the OID should really be swapped!


def parse_raritan_pdu_ocprot(info):
    flattened_info = [
        [end_oid, state, value, scale]
        for (end_oid, state, value), (scale,) in zip(info[0], info[1])
    ]
    parsed = {}
    for end_oid, state, value, scale in flattened_info:
        protector_id = "C" + end_oid.split(".")[1]  # 1.5.1 --> Item will be "C5"

        if end_oid.endswith(".15"):
            parsed.setdefault(protector_id, {})["state"] = state
        elif end_oid.endswith(".1"):
            parsed.setdefault(protector_id, {})["current"] = float(value) / pow(10, int(scale))
    return parsed


@get_parsed_item_data
def check_raritan_pdu_ocprot(item, params, data):
    states = {
        "-1": (3, "Overcurrent protector information is unavailable"),
        "0": (2, "Overcurrent protector is open"),
        "1": (0, "Overcurrent protector is closed"),
    }
    if "state" in data:
        yield states[data["state"]]

    if "current" in data:
        yield check_levels(data["current"], "current", params, unit="A", infoname="Current")


check_info["raritan_pdu_ocprot"] = {
    "parse_function": parse_raritan_pdu_ocprot,
    "inventory_function": discover(
        lambda key, value: True, default_params="raritan_pdu_ocprot_current_default_levels"
    ),
    "check_function": check_raritan_pdu_ocprot,
    "has_perfdata": True,
    "service_description": "Overcurrent Protector %s",
    "group": "ocprot_current",
    "snmp_info": [
        (
            ".1.3.6.1.4.1.13742.6.5.3.3.1",
            [
                OID_END,
                "3",
                "4",
            ],
        ),
        (".1.3.6.1.4.1.13742.6.3.4.4.1", ["7"]),
    ],
    "snmp_scan_function": lambda oid: "13742" in oid(".1.3.6.1.2.1.1.2.0"),
}
