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

# exemplary output of special agent agent_ucs_bladecenter (separator is <TAB> and means tabulator):
#
# <<<ucs_c_rack_server_motherboard_power:sep(9)>>>
# computeMbPowerStats<TAB>dn sys/rack-unit-1/board/power-stats<TAB>consumedPower 88<TAB>inputCurrent 6.00<TAB>inputVoltage 12.100
# computeMbPowerStats<TAB>dn sys/rack-unit-2/board/power-stats<TAB>consumedPower 88<TAB>inputCurrent 6.00<TAB>inputVoltage 12.100

# Default values for consumed power selected according to exemplary monitored real world values
# of a rack servers motherboards. Reasonable values for the actual use case depend on the rack
# servers configuration (racks used in rack server) and require customization via WATO rule.
factory_settings["ucs_c_rack_server_util_power_default_levels"] = {
    "power_upper_levels": (90, 100),
}


def parse_ucs_c_rack_server_power(info):
    """
    Returns dict with indexed rack motherboards mapped to keys and consumed power,
    input current status and input voltage status as value.
    """
    parsed = {}
    # The element count of info lines is under our control (agent output) and
    # ensured to have expected length. It is ensured that elements contain a
    # string. Handles invalid values provided by the XML API which cannot be
    # casted by setting corresponding values to None.
    for _, dn, power, current, voltage in info:
        motherboard = dn.replace("dn ", "").replace("sys/",
                                                    "").replace("rack-unit-", "Rack Unit ").replace(
                                                        "/board", "").replace("/power-stats", "")
        parsed.setdefault(motherboard, {})
        for ds_key, ds, cast_function in (
            ('consumedPower', power, int),
            ('inputCurrent', current, float),
            ('inputVoltage', voltage, float),
        ):
            try:
                # Power values are of type int. Current and voltage values are of type float but
                # converted to int. Hogher accuracy of float is not required.
                parsed[motherboard][ds_key] = cast_function(ds.replace(ds_key + " ", ""))
            except ValueError:
                # The default value set by setdefault is None. These values are handled in the
                # check function appropriatelly.
                pass
    return parsed


@get_parsed_item_data
def check_ucs_c_rack_server_power(item, params, data):
    yield check_levels(data["consumedPower"],
                       'power',
                       params['power_upper_levels'],
                       unit='W',
                       infoname='Power')
    yield 0, "Current: %s A" % data["inputCurrent"]
    yield 0, "Voltage: %s V" % data["inputVoltage"]


check_info["ucs_c_rack_server_power"] = {
    'parse_function': parse_ucs_c_rack_server_power,
    'inventory_function': discover(),
    'check_function': check_ucs_c_rack_server_power,
    'group': 'power_multiitem',
    'service_description': 'Motherboard Power Statistics %s',
    'default_levels_variable': 'ucs_c_rack_server_util_power_default_levels',
    'has_perfdata': True,
}
