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

def inventory_raritan_pdu_plugs(info):
    inventory = []
    for line in info:
        if saveint(line[0]) != 0:
            inventory.append((line[0], int(line[2])) )
    return inventory

def check_raritan_pdu_plugs(item, params, info):
    required_state = params
    states = {
        -1 :  "unavailable",
         0 :  "open",
         1 :  "closed",
         2 :  "belowLowerCritical",
         3 :  "belowLowerWarning",
         4 :  "normal",
         5 :  "aboveUpperWarning",
         6 :  "aboveUpperCritical",
         7 :  "on",
         8 :  "off",
         9 :  "detected",
         10:  "notDetected",
         11:  "alarmed",
    }
    state = 3
    message = "Sensor not found"

    for line in info:
        if line[0] == item:
            outletLabel = line[0] # Outlet-Label (Index) (PDU identifier of the outlet)
            outletName = line[1] # OutletName (vergebene Bezeichnung für den Ausgang)
            outletState = int(line[2]) # The Outlet state at present (sh. outlet_state_info)
            state = 0
            if type(required_state) == int:
                if outletState != required_state:
                    state = 2
            elif states[outletState] != required_state:
                state = 2

            message = outletName + "Status is: " + states[outletState]

    return (state, message)

check_info['raritan_pdu_plugs'] = {
  "inventory_function"  : inventory_raritan_pdu_plugs,
  "check_function"      : check_raritan_pdu_plugs,
  "service_description" : "Plug %s",
  "group"               : "plugs",
  "has_perfdata"        : False,
  "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.4.1.13742.6.3.2.1.1.3.1").startswith("PX2-2")

}
