#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2017             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.


# example output


def inventory_dell_idrac_power(info):
    for index, status, count in info[0]:
        yield index, None


def check_dell_idrac_power(item, _no_params, info):
    translate_status = {
        "1" : (3, "other"),
        "2" : (3, "unknown"),
        "3" : (0, "full"),
        "4" : (1, "degraded"),
        "5" : (2, "lost"),
        "6" : (0, "not redundant"),
        "7" : (1, "redundancy offline"),
    }

    for index, status, count in info[0]:
        if index == item:
            state, state_readable = translate_status[status]
            yield state, "Status: %s" % state_readable


check_info['dell_idrac_power'] = {
    'inventory_function'    : inventory_dell_idrac_power,
    'check_function'        : check_dell_idrac_power,
    'service_description'   : 'Power Supply Redundancy %s',
    'snmp_info'             : [ ('.1.3.6.1.4.1.674.10892.5.4.600.10.1', [
                                        "2", # IDRAC-MIB::powerUnitIndex
                                        "5", # IDRAC-MIB::powerUnitRedundancyStatus
                                        "6", # IDRAC-MIB::powerSupplyCountForRedundancy
                                ]),
                                ('.1.3.6.1.4.1.674.10892.5.4.600.12.1', [
                                        "2", # IDRAC-MIB::powerSupplyIndex
                                        "5", # IDRAC-MIB::powerSupplyStatus
                                        "7", # IDRAC-MIB::powerSupplyType
                                        "8", # IDRAC-MIB::powerSupplyLocationName
                                ]),
                              ],
    'snmp_scan_function'    : lambda oid: oid('.1.3.6.1.2.1.1.2.0').startswith(".1.3.6.1.4.1.674.10892.5"),
}


def inventory_dell_idrac_power_unit(info):
    for index, status, psu_type, location in info[1]:
        yield index, None


def check_dell_idrac_power_unit(item, _no_params, info):
    translate_status = {
        "1" : (3, "OTHER"),
        "2" : (3, "UNKNOWN"),
        "3" : (0, "OK"),
        "4" : (1, "NONCRITICAL"),
        "5" : (2, "CRITICAL"),
        "6" : (2, "NONRECOVERABLE"),
    }

    translate_type = {
        "1" : "OTHER",
        "2" : "UNKNOWN",
        "3" : "LINEAR",
        "4" : "SWITCHING",
        "5" : "BATTERY",
        "6" : "UPS",
        "7" : "CONVERTER",
        "8" : "REGULATOR",
        "9" : "AC",
        "10" : "DC",
        "11" : "VRM",
    }

    for index, status, psu_type, location in info[1]:
        if index == item:
            state, state_readable = translate_status[status]
            psu_type_readable = translate_type[psu_type]
            yield state, "Status: %s, Type: %s, Name: %s" % \
                            ( state_readable, psu_type_readable, location )

check_info['dell_idrac_power.unit'] = {
    'inventory_function'    : inventory_dell_idrac_power_unit,
    'check_function'        : check_dell_idrac_power_unit,
    'service_description'   : 'Power Supply %s',
}
