#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-

# (c) 2013 Heinlein Support GmbH
#          Robert Sander <r.sander@heinlein-support.de>

#
# This 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.


# We fetch the following columns from SNMP:
# 2: name of the outlet (used as item)
# 3: operationalState
# 4: Current
# 6: Voltage
# 7: ActivePower
# 8: ApparentPower
# 9: PowerFactor
# 20: CurrentUpperWarning
# 21: CurrentUpperCritical

from pprint import pprint

def inventory_raritan_pdu_outlets(info):
    # Ignore non-connected sensors
    return [ ( "%s %s" % (line[1], line[0]), None) for line in info ]

def check_raritan_pdu_outlets(item, _no_params, info):
    state_info = { -1: "Error",
                   0: "Off",
                   1: "On",
                   2: "Cycling" }

    for label, number, status, current, voltage, activepower, apparentpower, powerfactor, currentwarn, currentcrit in info:
        if "%s %s" % (number, label) != item: continue

        status = int(status)
        current = int(current) / 1000.0
        voltage = int(voltage) / 1000.0
        activepower = int(activepower)
        apparentpower = int(apparentpower)
        powerfactor = int(powerfactor)
        currentwarn = int(currentwarn) / 1000.0
        currentcrit = int(currentcrit) / 1000.0

        infotext = "Status: %s, Current: %0.3fA, Voltage: %0.1fV, Active Power: %dW, Apparant Power: %dVA, Power Factor: %d%%" % ( state_info[status], current, voltage, activepower, apparentpower, powerfactor )
        perfdata = [ ("current", "%fA" % current, currentwarn, currentcrit, 0 ),
                     ("voltage", "%fV" % voltage),
                     ("activepower", "%dW" % activepower),
                     ("apparentpower", "%dVA" % apparentpower),
                     ("powerfactor", "%d%%" % powerfactor, None, None, 0, 100) ]

        rc = 0
        if status == -1:
            rc = 2
        elif status == 0 or status > 1:
            rc = 1
        elif current >= currentcrit and currentcrit > 0:
            rc = 2
        elif current >= currentwarn and currentwarn > 0:
            rc = 1

        return (rc, infotext, perfdata)

    return (3, "Outlet %s not found in SNMP data" % item)

check_info['raritan_pdu_outlets'] = {
    "inventory_function"    : inventory_raritan_pdu_outlets,
    "check_function"        : check_raritan_pdu_outlets,
    "service_description"   : "Outlet %s",
    "has_perfdata"          : True,
    "snmp_info"             : ( ".1.3.6.1.4.1.13742.4.1.2.2.1", [ 2, 1, 3, 4, 6, 7, 8, 9, 20, 21 ] ),
    "snmp_scan_function"    : lambda oid: oid(".1.3.6.1.4.1.13742.4.1.2.1.0"),
}
