#!/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:
# [['1.1.1', '4', '0'],
#  ['1.1.15', '1', '0'],
#  ['1.2.1', '4', '0'],
#  ['1.2.15', '1', '0'],
#  ['1.3.1', '4', '0'],
#  ['1.3.15', '1', '0'],
#  ['1.4.1', '4', '0'],
#  ['1.4.15', '1', '0'],
#  ['1.5.1', '4', '0'],
#  ['1.5.15', '1', '0'],
#  ['1.6.1', '4', '0'],
#  ['1.6.15', '1', '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):
    parsed = {}
    for end_oid, state, value in 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) / 10
    return parsed


def inventory_raritan_pdu_ocprot(parsed):
    for protector_id in parsed:
        yield protector_id, "raritan_pdu_ocprot_current_default_levels"


def check_raritan_pdu_ocprot(item, params, parsed):
    states = {
        "-1" : (3, "Overcurrent protector information is unavailable"),
        "0"  : (2, "Overcurrent protector is open"),
        "1"  : (0, "Overcurrent protector is closed"),
    }

    if item in parsed:
        yield states[parsed[item]["state"]]

        current = parsed[item]["current"]
        warn, crit = params

        infotext = "Current: %.1f A" % current
        levelstext = " (warn/crit at %.1f/%.1f A)" % (warn, crit)

        if current >= crit:
            status = 2
            infotext += levelstext
        elif current >= warn:
            status = 1
            infotext += levelstext
        else:
            status = 0

        perfdata = [ ("current", current, warn, crit) ]
        yield status, infotext, perfdata


check_info["raritan_pdu_ocprot"] = {
    "parse_function"            : parse_raritan_pdu_ocprot,
    "inventory_function"        : inventory_raritan_pdu_ocprot,
    "check_function"            : check_raritan_pdu_ocprot,
    "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",
                                  ]),
    "snmp_scan_function"        : lambda oid: "13742" in oid(".1.3.6.1.2.1.1.2.0"),
}
