#!/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.5.2.1.3.1.1 "FAN1 SYS"
#.1.3.6.1.4.1.231.2.10.2.2.10.5.2.1.3.1.2 "FAN2 SYS"
#.1.3.6.1.4.1.231.2.10.2.2.10.5.2.1.3.1.3 "FAN3 SYS"
#.1.3.6.1.4.1.231.2.10.2.2.10.5.2.1.3.1.4 "FAN4 SYS"
#.1.3.6.1.4.1.231.2.10.2.2.10.5.2.1.3.1.5 "FAN5 SYS"
#.1.3.6.1.4.1.231.2.10.2.2.10.5.2.1.3.1.6 "FAN PSU1"
#.1.3.6.1.4.1.231.2.10.2.2.10.5.2.1.3.1.7 "FAN PSU2"
#.1.3.6.1.4.1.231.2.10.2.2.10.5.2.1.5.1.1 3
#.1.3.6.1.4.1.231.2.10.2.2.10.5.2.1.5.1.2 3
#.1.3.6.1.4.1.231.2.10.2.2.10.5.2.1.5.1.3 3
#.1.3.6.1.4.1.231.2.10.2.2.10.5.2.1.5.1.4 3
#.1.3.6.1.4.1.231.2.10.2.2.10.5.2.1.5.1.5 3
#.1.3.6.1.4.1.231.2.10.2.2.10.5.2.1.5.1.6 3
#.1.3.6.1.4.1.231.2.10.2.2.10.5.2.1.5.1.7 3
#.1.3.6.1.4.1.231.2.10.2.2.10.5.2.1.6.1.1 5820
#.1.3.6.1.4.1.231.2.10.2.2.10.5.2.1.6.1.2 6000
#.1.3.6.1.4.1.231.2.10.2.2.10.5.2.1.6.1.3 6000
#.1.3.6.1.4.1.231.2.10.2.2.10.5.2.1.6.1.4 6000
#.1.3.6.1.4.1.231.2.10.2.2.10.5.2.1.6.1.5 6120
#.1.3.6.1.4.1.231.2.10.2.2.10.5.2.1.6.1.6 2400
#.1.3.6.1.4.1.231.2.10.2.2.10.5.2.1.6.1.7 2400

factory_settings['fan_fsc_sc2_levels'] = {
    'lower' : ( 1500, 2000),
}

def inventory_fsc_sc2_fans(info):
    for line in info:
        yield line [0], {}


def check_fsc_sc2_fans(item, params, info):
    status_map = {
        '1': (3, 'Status is unknown'),
        '2': (0, 'Status is disabled'),
        '3': (0, 'Status is ok'),
        '4': (2, 'Status is failed'),
        '5': (1, 'Status is prefailure-predicted'),
        '6': (1, 'Status is redundant-fan-failed'),
        '7': (3, 'Status is not-manageable'),
        '8': (0, 'Status is not-present'),
    }

    if type(params) == tuple:
        params = { 'lower' : params }

    for designation, status, rpm in info:
        if designation == item:
            # in this case booth values are important
            if rpm:
                yield check_fan(int(rpm), params)
            else:
                yield 3, "Device did not deliver rpm values"

            yield status_map.get(status, (3, 'Status is unknown'))


check_info["fsc_sc2_fans"] = {
    'inventory_function'        : inventory_fsc_sc2_fans,
    'check_function'            : check_fsc_sc2_fans,
    'service_description'       : 'FSC %s',
    'snmp_info'                 : ('.1.3.6.1.4.1.231.2.10.2.2.10.5.2.1', [
                                        '3',      #sc2fanDesignation
                                        '5',      #sc2fanStatus
                                        '6',      #sc2fanCurrentSpeed
                                  ]),
    'snmp_scan_function'        : lambda oid: oid(".1.3.6.1.4.1.231.2.10.2.2.10.1.1.0"),
    'has_perfdata'              : True,
    'default_levels_variable'   : 'fan_fsc_sc2_levels',
    'group'                     : "hw_fans",
    'includes'                  : [ 'fan.include' ],
}
