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

#.1.3.6.1.4.1.231.2.10.2.2.10.6.2.1.3.1.1 "PSU1"
#.1.3.6.1.4.1.231.2.10.2.2.10.6.2.1.3.1.2 "PSU2"
#.1.3.6.1.4.1.231.2.10.2.2.10.6.2.1.5.1.1 3
#.1.3.6.1.4.1.231.2.10.2.2.10.6.2.1.5.1.2 3
#.1.3.6.1.4.1.231.2.10.2.2.10.6.2.1.6.1.1 52
#.1.3.6.1.4.1.231.2.10.2.2.10.6.2.1.6.1.2 40
#.1.3.6.1.4.1.231.2.10.2.2.10.6.2.1.7.1.1 448
#.1.3.6.1.4.1.231.2.10.2.2.10.6.2.1.7.1.2 448

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


def check_fsc_sc2_psu(item, _no_params, info):
    psu_status = {
        '1' :   (3, 'Status is unknown'),
        '2' :   (0, 'Status is not-present'),
        '3' :   (0, 'Status is ok'),
        '4' :   (2, 'Status is failed'),
        '5' :   (2, 'Status is ac-fail'),
        '6' :   (2, 'Status is dc-fail'),
        '7' :   (2, 'Status is critical-temperature'),
        '8' :   (0, 'Status is not-manageable'),
        '9' :   (1, 'Status is fan-failure-predicted'),
        '10':   (2, 'Status is fan-failure'),
        '11':   (1, 'Status is power-safe-mode'),
        '12':   (1, 'Status is non-redundant-dc-fail'),
        '13':   (1, 'Status is non-redundant-ac-fail')
    }

    for designation, status, load, nominal in info:
        if designation == item:
            yield psu_status.get(status, (3, 'Status is unknown'))

            if nominal and load:
                yield 0, 'Nominal load: {0} W, Actual load: {1} W'.format(nominal, load), [('power', int(load))]
            else:
                yield 1, 'Did not receive load data'


check_info["fsc_sc2_psu"] = {
    'inventory_function'        : inventory_fsc_sc2_psu,
    'check_function'            : check_fsc_sc2_psu,
    'service_description'       : 'FSC %s',
    'snmp_info'                 : ('.1.3.6.1.4.1.231.2.10.2.2.10.6.2.1', [
                                        '3',    #sc2PowerSupplyDesignation
                                        '5',    #sc2PowerSupplyStatus
                                        '6',    #sc2psPowerSupplyLoad
                                        '7',    #sc2psPowerSupplyNominal
                                  ]),
    'snmp_scan_function'        : lambda oid: oid(".1.3.6.1.4.1.231.2.10.2.2.10.1.1.0"),
    'has_perfdata'              : True,
}
