#!/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.9.9.117.1.1.1.1.2.16  "centiAmpsAt12V"
#  some more examples (but we dont know all):
#       milliAmps12v
#       centiAmpsAt12V
#       Amps @ 12V
#       CentiAmps @ 12V
#       Amps @ 50
#   => calculate power = factor * amps * volt

# .1.3.6.1.4.1.9.9.117.1.1.4.1.1.16 11333
# .1.3.6.1.4.1.9.9.117.1.1.4.1.2.16 9666
# .1.3.6.1.4.1.9.9.117.1.1.4.1.3.16 6000
# .1.3.6.1.4.1.9.9.117.1.1.4.1.4.16 122

# .1.3.6.1.4.1.9.9.117.1.1.4.1.1.13 11333
# .1.3.6.1.4.1.9.9.117.1.1.4.1.2.13 5583
# .1.3.6.1.4.1.9.9.117.1.1.4.1.3.13 6980
# .1.3.6.1.4.1.9.9.117.1.1.4.1.4.13 0       <= exclude


def parse_cisco_fru_powerusage(info):
    parsed = {}
    powerunit, powervals = info
    if powerunit and powervals:
        oidend, powerunit_str   = powerunit[0]
        factor_str, voltage_str = powerunit_str.lower().split("amps")

        if "milli" in factor_str.lower():
            factor = 0.001
        elif "centi" in factor_str.lower():
            factor = 0.01
        else:
            factor = 1.0

        voltage = float(voltage_str.lower().replace("at", "").\
                        replace("@", "").replace("v", "").strip())

        if oidend == powervals[0][0]:
            system_total, system_drawn, inline_total, inline_drawn = map(float, powervals[0][1:])
            for what, val in [
                ("system total", system_total),        # Gesamtstrom
                ("system drawn", system_drawn),        # aufgenommene Gesamtstromstaerke
                ("inline total", inline_total),
                ("inline drawn", inline_drawn)]:
                parsed.setdefault(what, {
                    "power"   : factor * val * voltage,
                    "current" : factor * val,
                    "voltage" : voltage,
                })

    return parsed


def inventory_cisco_fru_powerusage(parsed):
    for what, data in parsed.items():
        if data["current"] > 0:
            yield what, {}


check_info['cisco_fru_powerusage'] = {
    'parse_function'      : parse_cisco_fru_powerusage,
    'inventory_function'  : inventory_cisco_fru_powerusage,
    'check_function'      : check_elphase,
    'service_description' : 'FRU power usage %s',
    'has_perfdata'        : True,
    'snmp_info'           : [(".1.3.6.1.4.1.9.9.117.1.1.1.1.2", [
                                  OID_END,
                                  "",     # CISCO-ENTITY-FRU-CONTROL-MIB::cefcPowerUnits
                             ]),
                             (".1.3.6.1.4.1.9.9.117.1.1.4.1", [
                                  OID_END,
                                  "1",    # CISCO-ENTITY-FRU-CONTROL-MIB::cefcFRUTotalSystemCurrent
                                  "2",    # CISCO-ENTITY-FRU-CONTROL-MIB::cefcFRUDrawnSystemCurrent
                                  "3",    # CISCO-ENTITY-FRU-CONTROL-MIB::cefcFRUTotalInlineCurrent
                                  "4",    # CISCO-ENTITY-FRU-CONTROL-MIB::cefcFRUDrawnInlineCurrent
                            ])],
    'snmp_scan_function'  : lambda oid: "cisco" in oid(".1.3.6.1.2.1.1.1.0").lower(),
    'includes'            : [ "elphase.include" ],
    'group'               : 'el_inphase'
}
