#!/usr/bin/python
# -*- coding: utf-8; py-indent-offset: 4 -*-
#  _______ __   _ ____     __
# |       |  \ | |___ \   / /
# |       |   \| | __) | / /-,_
# |       | |\   |/ __/ /__   _|
# |_______|_| \__|_____|   |_|
#
# @author Markus Birth <markus.birth@weltn24.de>

def inventory_selenio_mcp_power(info):
    inventory = []
    if len(info[0]) > 0 and info[0][1] > 0:
        inventory.append( ("Power", None) )
    return inventory

def check_selenio_mcp_power(item, _no_params, info):
    E12enum = [ "Yes", "No" ]
    E13enum = [ "not present", "unknown", "AC", "DC" ]
    E18enum = [ "degraded", "faulty", "not present", "OK", "N/A" ]
    E46enum = [ "degraded", "faulty", "OK" ]
    E66enum = [ "Active", "Standby", "Synchronizing", "not present" ]
    E71enum = [ "PS1 (left)", "PS2 (right)", "Both (load sharing)" ]

    if len(info[0]) < 3:
        return 3, "%s not found in SNMP data." % item

    # remove OID part from array
    info[0].pop(0)

    systemVoltStatus = E46enum[int(info[0][0])]
    psuStatus = [ E18enum[int(info[0][1])], E18enum[int(info[0][2])] ]
    psuExpected = [ E12enum[int(info[0][3])], E12enum[int(info[0][4])] ]
    ctrlVoltStatus = [ E18enum[int(info[0][5])], E18enum[int(info[0][6])] ]
    psuType = [ E13enum[int(info[0][7])], E13enum[int(info[0][8])] ]
    activePSU = E71enum[int(info[0][9])]
    ctrlStatus = [ E66enum[int(info[0][10])], E66enum[int(info[0][11])] ]

    status = 0
    message = "Active PSU: %s." % activePSU
    details  = "\\nActive Power Supply: %s" % activePSU
    details += "\\nPSU1: %s (Expected: %s) - %s" % (psuStatus[0], psuExpected[0], psuType[0])
    details += "\\nPSU2: %s (Expected: %s) - %s" % (psuStatus[1], psuExpected[1], psuType[1])
    details += "\\nSystem Voltage Status: %s" % systemVoltStatus
    details += "\\nController 1 Status: %s" % ctrlStatus[0]
    details += "\\nController 1 Voltage Status: %s" % ctrlVoltStatus[0]
    details += "\\nController 2 Status: %s" % ctrlStatus[1]
    details += "\\nController 2 Voltage Status: %s" % ctrlVoltStatus[1]

    if systemVoltStatus != "OK":
        message += " System voltage is %s." % systemVoltStatus
        if systemVoltStatus == "degraded":
            message += " (!)"
            if status < 1:
                status = 1
        elif systemVoltStatus == "faulty":
            message += " (!!)"
            if status < 2:
                status = 2

    for i in range(0, 2):
        if psuExpected[i] == "Yes" and psuStatus[i] != "OK":
            message += " PSU%i is %s" % ((i+1), psuStatus[i])
            if psuStatus[i] in [ "degraded", "N/A" ]:
                if status < 1:
                    status = 1
            else:
                if status < 2:
                    status = 2

        # if a controller is in Standby, the corresponding PSU will be "degraded"
        if ctrlVoltStatus[i] not in [ "OK", "N/A" ] and not ( ctrlVoltStatus[i] == "degraded" and ctrlStatus[i] == "Standby" ):
            message += " Ctrl%i voltage is %s." % ((i+1), ctrlVoltStatus[i])
            if ctrlVoltStatus[i] in [ "degraded", "not present" ]:
                message += " (!)"
                if status < 1:
                    status = 1
            else:
                message += " (!!)"
                if status < 2:
                    status = 2

    if status == 0:
        message += " All ok."

    message += details

    return status, message

check_info["selenio_mcp_power"] = {
    "check_function"        : check_selenio_mcp_power,
    "inventory_function"    : inventory_selenio_mcp_power,
    "service_description"   : "%s",
    "snmp_info"             : (".1.3.6.1.4.1.290.9.3.3.21", ["1.1", "25.1"],
                                   [ OID_END,  # "1.1.1.1.0" (MCP3) or "25.1.1.1.0" (MCP1)
                                         385,  # 0 systemVoltStatus (E46enum)
                                         390,  # 1 primaryPSUStatus (E18enum)
                                         391,  # 2 secondaryPSUStatus
                                         473,  # 3 PSU1 expected (E12enum)
                                         474,  # 4 PSU2 expected
                                        1173,  # 5 Ctrl1VoltageStatus (E18enum)
                                        1186,  # 6 Ctrl2VoltageStatus
                                        1375,  # 7 PSU1Type (E13enum)
                                        1376,  # 8 PSU2Type (E13enum)
                                        1404,  # 9 activePSU (E71enum)
                                         393,  # 10 Ctrl1Status (E66enum)
                                         228,  # 11 Ctrl2Status
                              ]),
    "snmp_scan_function"    : lambda oid: oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.290.9.3.3.21"),
    "has_perfdata"          : False
}
