#!/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
# .1.3.6.1.4.1.674.10892.1.600.10.1.2.1.1 1
# .1.3.6.1.4.1.674.10892.1.600.10.1.5.1.1 3
# .1.3.6.1.4.1.674.10892.1.600.10.1.6.1.1 0
# .1.3.6.1.4.1.674.10892.1.600.12.1.2.1.1 1
# .1.3.6.1.4.1.674.10892.1.600.12.1.2.1.2 2
# .1.3.6.1.4.1.674.10892.1.600.12.1.5.1.1 3
# .1.3.6.1.4.1.674.10892.1.600.12.1.5.1.2 3
# .1.3.6.1.4.1.674.10892.1.600.12.1.7.1.1 9
# .1.3.6.1.4.1.674.10892.1.600.12.1.7.1.2 9
# .1.3.6.1.4.1.674.10892.1.600.12.1.8.1.1 PS1 Status
# .1.3.6.1.4.1.674.10892.1.600.12.1.8.1.2 PS2 Status


def inventory_dell_om_power(info):
    for index, _status, _count in info[0]:
        yield (index, None)


def check_dell_om_power(item, 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, "Redundancy status: %s" % state_readable


check_info['dell_om_power'] = {
    'inventory_function': inventory_dell_om_power,
    'check_function': check_dell_om_power,
    'service_description': 'Power Supply Redundancy %s',
    'snmp_info': [
        (
            '.1.3.6.1.4.1.674.10892.1.600.10.1',
            [
                "2",  # MIB-Dell-10892::powerUnitIndex
                "5",  # MIB-Dell-10892::powerUnitRedundancyStatus
                "6",  # MIB-Dell-10892::powerSupplyCountForRedundancy
            ]),
        (
            '.1.3.6.1.4.1.674.10892.1.600.12.1',
            [
                "2",  # MIB-Dell-10892::powerSupplyIndex
                "5",  # MIB-Dell-10892::powerSupplyStatus
                "7",  # MIB-Dell-10892::powerSupplyType
                "8",  # MIB-Dell-10892::powerSupplyLocationName
            ]),
    ],
    'snmp_scan_function': scan_dell_om,
    'includes': ["dell_om.include"],
}


def inventory_dell_om_power_unit(info):
    for line in info[1]:
        yield (line[0], None)


def check_dell_om_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_om_power.unit'] = {
    'inventory_function': inventory_dell_om_power_unit,
    'check_function': check_dell_om_power_unit,
    'service_description': 'Power Supply %s',
}
