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


# .1.3.6.1.4.1.2544.1.11.2.4.2.2.1.1.101318912  8110
# .1.3.6.1.4.1.2544.1.11.2.4.2.2.1.2.101318912  65600
# .1.3.6.1.4.1.2544.1.11.2.4.2.2.1.3.101318912  9
# .1.3.6.1.4.1.2544.2.5.5.1.1.1.101318912  "PSU/7HU-AC-800"
# .1.3.6.1.4.1.2544.2.5.5.1.1.5.101318912  "MOD-1-1"


def inventory_adva_fsp_current(info):
    for current_str, upper_threshold_str, power_str, \
        unit_name, index_aid in info:
        # Ignore non-connected sensors
        if index_aid != "" and power_str != "":
            yield index_aid, None


def check_adva_fsp_current(item, _no_params, info):
    for current_str, upper_threshold_str, power_str, \
        unit_name, index_aid in info:
        if index_aid == item:
            current = float(current_str) / 1000.0
            upper_threshold = float(upper_threshold_str) / 1000

            infotext = "[%s] %.3f A (crit at %.3f A)" % (
                       unit_name, current, upper_threshold)
            perfdata = [("current", current, None, upper_threshold,)]

            if current <= 0:
                return 3, "Invalid sensor data"
            elif current >= upper_threshold:
                return 2, infotext, perfdata
            else:
                return 0, infotext, perfdata


check_info['adva_fsp_current'] = {
    "inventory_function"    : inventory_adva_fsp_current,
    "check_function"        : check_adva_fsp_current,
    "service_description"   : "Power Supply %s",
    "has_perfdata"          : True,
    "snmp_info"             : ( ".1.3.6.1.4.1.2544", [
                                    "1.11.2.4.2.2.1.1", # currentDiagnosticsAmpere
                                    "1.11.2.4.2.2.1.2", # currentDiagnosticsUpperThres
                                    "1.11.2.4.2.2.1.3", # currentDiagnosticsPsuOutputPower
                                    "2.5.5.1.1.1", # inventoryUnitName
                                    "2.5.5.2.1.5", # entityIndexAid
                              ]),
    "snmp_scan_function"    : lambda oid: oid(".1.3.6.1.2.1.1.1.0") == "Fiber Service Platform F7",
}
