#!/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_plugs_default = '"on"'  # only to be downwards compatible and can be removed any decade now.


def inventory_raritan_pdu_plugs(parsed):
    for key, value in parsed.iteritems():
        yield key, value["state_int"]


def parse_raritan_pdu_plugs(info):
    parsed = {}

    for outlet_label, outlet_name, outlet_state in info:
        try:
            state_int = int(outlet_state)
        except ValueError:
            state_int = None
        parsed[outlet_label] = {
            "state": raritan_map_state.get(outlet_state, (None, None)),
            "state_int": state_int,
            "outlet_name": outlet_name,
        }
    return parsed


@get_parsed_item_data
def check_raritan_pdu_plugs(_no_item, params, data):
    if data.get("outlet_name"):
        yield 0, data["outlet_name"]
    required_state = params
    state, state_info = data["state"]
    outlet_state = data["state_int"]
    state = 0
    if isinstance(required_state, int):
        if outlet_state != required_state:
            state = 2
    elif state_info != required_state:
        state = 2
    yield state, "Status: %s" % state_info


check_info['raritan_pdu_plugs'] = {
    "inventory_function" : inventory_raritan_pdu_plugs,
    "parse_function": parse_raritan_pdu_plugs,
    "check_function" : check_raritan_pdu_plugs,
    "service_description" : "Plug %s",
    "group" : "plugs",
    "snmp_info"  : ( ".1.3.6.1.4.1.13742.6", [
                                "3.5.3.1.2", # Outlet-Label (Index) (PDU identifier of the outlet)
                                "3.5.3.1.3", # OutletName (eigene vergebene Bezeichnung für den Ausgang)
                                "4.1.2.1.3", # Outlet state at present (sh.o. outlet_state_info im check)
                 ]),
    "snmp_scan_function" : lambda oid: oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.13742.6") and \
                                      (oid(".1.3.6.1.4.1.13742.6.3.2.1.1.3.1").startswith("PX2-2") or \
                                      oid(".1.3.6.1.4.1.13742.6.3.2.1.1.3.1").startswith("PX3")),
    "includes" : [ "raritan.include" ],
}
