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

# .1.3.6.1.4.1.4526.10.43.1.6.1.3.1.0 2 --> FASTPATH-BOXSERVICES-PRIVATE-MIB::boxServicesFanItemState.1.0
# .1.3.6.1.4.1.4526.10.43.1.6.1.3.1.1 2 --> FASTPATH-BOXSERVICES-PRIVATE-MIB::boxServicesFanItemState.1.1
# .1.3.6.1.4.1.4526.10.43.1.6.1.3.1.2 2 --> FASTPATH-BOXSERVICES-PRIVATE-MIB::boxServicesFanItemState.1.2
# .1.3.6.1.4.1.4526.10.43.1.6.1.3.1.3 2 --> FASTPATH-BOXSERVICES-PRIVATE-MIB::boxServicesFanItemState.1.3
# .1.3.6.1.4.1.4526.10.43.1.6.1.3.1.4 2 --> FASTPATH-BOXSERVICES-PRIVATE-MIB::boxServicesFanItemState.1.4
# .1.3.6.1.4.1.4526.10.43.1.6.1.3.1.5 1 --> FASTPATH-BOXSERVICES-PRIVATE-MIB::boxServicesFanItemState.1.5
# .1.3.6.1.4.1.4526.10.43.1.6.1.3.2.0 2 --> FASTPATH-BOXSERVICES-PRIVATE-MIB::boxServicesFanItemState.2.0
# .1.3.6.1.4.1.4526.10.43.1.6.1.3.2.1 2 --> FASTPATH-BOXSERVICES-PRIVATE-MIB::boxServicesFanItemState.2.1
# .1.3.6.1.4.1.4526.10.43.1.6.1.3.2.2 2 --> FASTPATH-BOXSERVICES-PRIVATE-MIB::boxServicesFanItemState.2.2
# .1.3.6.1.4.1.4526.10.43.1.6.1.3.2.3 2 --> FASTPATH-BOXSERVICES-PRIVATE-MIB::boxServicesFanItemState.2.3
# .1.3.6.1.4.1.4526.10.43.1.6.1.3.2.4 2 --> FASTPATH-BOXSERVICES-PRIVATE-MIB::boxServicesFanItemState.2.4
# .1.3.6.1.4.1.4526.10.43.1.6.1.3.2.5 1 --> FASTPATH-BOXSERVICES-PRIVATE-MIB::boxServicesFanItemState.2.5
# .1.3.6.1.4.1.4526.10.43.1.6.1.4.1.0 3950 --> FASTPATH-BOXSERVICES-PRIVATE-MIB::boxServicesFanSpeed.1.0
# .1.3.6.1.4.1.4526.10.43.1.6.1.4.1.1 3700 --> FASTPATH-BOXSERVICES-PRIVATE-MIB::boxServicesFanSpeed.1.1
# .1.3.6.1.4.1.4526.10.43.1.6.1.4.1.2 3600 --> FASTPATH-BOXSERVICES-PRIVATE-MIB::boxServicesFanSpeed.1.2
# .1.3.6.1.4.1.4526.10.43.1.6.1.4.1.3 3400 --> FASTPATH-BOXSERVICES-PRIVATE-MIB::boxServicesFanSpeed.1.3
# .1.3.6.1.4.1.4526.10.43.1.6.1.4.1.4 0 --> FASTPATH-BOXSERVICES-PRIVATE-MIB::boxServicesFanSpeed.1.4
# .1.3.6.1.4.1.4526.10.43.1.6.1.4.1.5 0 --> FASTPATH-BOXSERVICES-PRIVATE-MIB::boxServicesFanSpeed.1.5
# .1.3.6.1.4.1.4526.10.43.1.6.1.4.2.0 3650 --> FASTPATH-BOXSERVICES-PRIVATE-MIB::boxServicesFanSpeed.2.0
# .1.3.6.1.4.1.4526.10.43.1.6.1.4.2.1 3400 --> FASTPATH-BOXSERVICES-PRIVATE-MIB::boxServicesFanSpeed.2.1
# .1.3.6.1.4.1.4526.10.43.1.6.1.4.2.2 3300 --> FASTPATH-BOXSERVICES-PRIVATE-MIB::boxServicesFanSpeed.2.2
# .1.3.6.1.4.1.4526.10.43.1.6.1.4.2.3 3500 --> FASTPATH-BOXSERVICES-PRIVATE-MIB::boxServicesFanSpeed.2.3
# .1.3.6.1.4.1.4526.10.43.1.6.1.4.2.4 0 --> FASTPATH-BOXSERVICES-PRIVATE-MIB::boxServicesFanSpeed.2.4
# .1.3.6.1.4.1.4526.10.43.1.6.1.4.2.5 0 --> FASTPATH-BOXSERVICES-PRIVATE-MIB::boxServicesFanSpeed.2.5

# Just assumed
factory_settings["netgear_fans_default_levels"] = {
    "lower": (1500, 1200),
}


def netgear_map_state_txt_to_int(state_nr, version):
    map_state_txt_to_int = {
        "operational": 0,
        "failed": 2,
        "powering": 0,
        "not powering": 1,
        "not present": 1,
        "no power": 2,
        "incompatible": 2,
    }

    if version.startswith("8."):
        map_states = {
            "1": "operational",
            "2": "failed",
            "3": "powering",
            "4": "not powering",
            "5": "not present",
        }
    elif version.startswith("10."):
        map_states = {
            "1": "notpresent",
            "2": "operational",
            "3": "failed",
            "4": "powering",
            "5": "no power",
            "6": "not powering",
            "7": "incompatible",
        }
    else:
        map_states = {
            "1": "not present",
            "2": "operational",
            "3": "failed",
        }

    state_txt = map_states.get(state_nr, "unknown(%s)" % state_nr)
    return map_state_txt_to_int.get(state_txt, 3), state_txt


def parse_netgear_fans(info):
    versioninfo, sensorinfo = info
    if versioninfo == []:
        parsed = {"__fans__": {}}
    else:
        parsed = {
            "__version__": versioninfo[0][0],
            "__fans__": {},
        }
    for oid_end, sstate, reading_str in sensorinfo:
        parsed["__fans__"].setdefault("%s" % oid_end.replace(".", "/"), {
            "state": sstate,
            "reading_str": reading_str,
        })
    return parsed


def inventory_netgear_fans(parsed):
    for sensorname, sensorinfo in parsed["__fans__"].items():
        state = sensorinfo["state"]
        if state != "1" and not ( state == "2" and \
           sensorinfo["reading_str"] in [ "0" ] ):
            yield sensorname, {}


def check_netgear_fans(item, params, parsed):
    data = parsed["__fans__"].get(item)
    if data is None:
        return

    reading_str = data["reading_str"]
    if reading_str != "Not Supported":
        yield check_fan(int(data["reading_str"]), params)
    state, state_readable = netgear_map_state_txt_to_int(data["state"],
                                                         parsed.get("__version__", ""))
    yield state, "Status: %s" % state_readable


check_info['netgear_fans'] = {
    'parse_function': parse_netgear_fans,
    'inventory_function': inventory_netgear_fans,
    'check_function': check_netgear_fans,
    'service_description': 'Fan %s',
    'snmp_info': [
        ('.1.3.6.1.4.1.4526.10.1.1.1.13', ['']),
        (
            '.1.3.6.1.4.1.4526.10.43.1.6.1',
            [
                OID_END,
                "3",  # FASTPATH-BOXSERVICES-PRIVATE-MIB::boxServicesFanItemState
                "4",  # FASTPATH-BOXSERVICES-PRIVATE-MIB::boxServicesFanSpeed
            ])
    ],
    'snmp_scan_function': lambda oid: oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.4526.100"),
    'default_levels_variable': 'netgear_fans_default_levels',
    'includes': ['fan.include'],
    'group': 'hw_fans',
}
