#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2014             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 from agent:
# [['1', '24', 'SLOT #0: TEMP #1'],
# ['2', '12', 'SLOT #0: TEMP #2'],
# ['3', '12', 'SLOT #0: TEMP #3'],
# ['4', '4687', 'FAN #1'],
# ['5', '4560', 'FAN #2'],
# ['6', '4821', 'FAN #3'],
# ['7', '1', 'Power Supply #1'],
# ['8', '1', 'Power Supply #2']]


def brocade_sensor_convert(info, what):
    return_list = []
    for presence, state, name in info:
        name = name.lstrip()  # remove leading spaces provided via SNMP
        if name.startswith(what) and presence != "6" and (saveint(state) > 0 or what == "Power"):
            sensor_id = name.split('#')[-1]
            return_list.append([sensor_id, name, state])
    return return_list


def brocade_scan(oid):
    return oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.1588.2.1.1") or \
           oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.24.1.1588.2.1.1") or \
           oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.1588.2.2.1") or \
           oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.1588.3.3.1")


brocade_info = (
    '.1.3.6.1.4.1.1588.2.1.1.1.1.22.1',
    [
        3,  # swSensorStatus, 6 = absent
        4,  # swSensorValue, -2147483648 = unknown
        5,  # swSensorInfo
    ])

brocade_fan_default_levels = {"lower": (3000, 2800)}


def inventory_brocade_fan(info):
    converted = brocade_sensor_convert(info, "FAN")
    return [(x[0], 'brocade_fan_default_levels') for x in converted]


def check_brocade_fan(item, params, info):
    converted = brocade_sensor_convert(info, "FAN")
    if isinstance(params, tuple):  # old format
        params = {"lower": params}

    for snmp_item, _name, value in converted:
        if item == snmp_item:
            return check_fan(int(value), params)


check_info["brocade.fan"] = {
    "check_function": check_brocade_fan,
    "inventory_function": inventory_brocade_fan,
    "service_description": "FAN %s",
    "group": "hw_fans",
    "snmp_info": brocade_info,
    "snmp_scan_function": brocade_scan,
    "has_perfdata": True,
    "includes": ["fan.include"],
}


def inventory_brocade_power(info):
    converted = brocade_sensor_convert(info, "Power")
    return [(x[0], None) for x in converted]


def check_brocade_power(item, _no_params, info):
    converted = brocade_sensor_convert(info, "Power")
    for snmp_item, name, value in converted:
        if item == snmp_item:
            value = int(value)
            if value != 1:
                return 2, "Error on supply %s" % name
            return 0, "No problems found"

    return 3, "Supply not found"


check_info["brocade.power"] = {
    "check_function": check_brocade_power,
    "inventory_function": inventory_brocade_power,
    "service_description": "Power supply %s",
    "snmp_info": brocade_info,
    'snmp_scan_function': brocade_scan,
}

factory_settings["brocade_temp_default_levels"] = {"levels": (55.0, 60.0)}


def inventory_brocade_temp(info):
    converted = brocade_sensor_convert(info, "SLOT")
    return [(x[0], 'brocade_temp_default_levels') for x in converted]


def check_brocade_temp(item, params, info):
    converted = brocade_sensor_convert(info, "SLOT")
    for snmp_item, _name, value in converted:
        if item == snmp_item:
            return check_temperature(int(value), params, "brocade_temp_%s" % item)


check_info["brocade.temp"] = {
    "check_function": check_brocade_temp,
    "inventory_function": inventory_brocade_temp,
    "service_description": "Temperature Ambient %s",
    "has_perfdata": True,
    "group": "temperature",
    "snmp_info": brocade_info,
    'snmp_scan_function': brocade_scan,
    "default_levels_variable": "brocade_temp_default_levels",
    "includes": ["temperature.include"],
}
