#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2014             mk@mathias-kettner.de |
# +------------------------------------------------------------------+
#
# This file is part of Check_MK.
# The official homepage is at http://mathias-kettner.de/check_mk.
#
# check_mk is free software;  you can redistribute it and/or modify it
# under the  terms of the  GNU General Public License  as published by
# the Free Software Foundation in version 2.  check_mk is  distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY;  with-
# out even the implied warranty of  MERCHANTABILITY  or  FITNESS FOR A
# PARTICULAR PURPOSE. See the  GNU General Public License for more de-
# tails. You should have  received  a copy of the  GNU  General Public
# License along with GNU Make; see the file  COPYING.  If  not,  write
# to the Free Software Foundation, Inc., 51 Franklin St,  Fifth Floor,
# Boston, MA 02110-1301 USA.

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"),
}
