#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

# NOTE: Careful when replacing the *-import below with a more specific import. This can cause
# problems because it might remove variables from the check-context which are necessary for
# resolving legacy discovery results such as [("SUMMARY", "diskstat_default_levels")]. Furthermore,
# it might also remove variables needed for accessing discovery rulesets.
from cmk.base.check_legacy_includes.elphase import *  # pylint: disable=wildcard-import,unused-wildcard-import
# NOTE: Careful when replacing the *-import below with a more specific import. This can cause
# problems because it might remove variables from the check-context which are necessary for
# resolving legacy discovery results such as [("SUMMARY", "diskstat_default_levels")]. Furthermore,
# it might also remove variables needed for accessing discovery rulesets.
from cmk.base.check_legacy_includes.temperature import *  # pylint: disable=wildcard-import,unused-wildcard-import
import cmk.base.plugins.agent_based.utils.ucs_bladecenter as ucs_bladecenter

# <<<ucs_bladecenter_psu:sep(9)>>>
# equipmentPsuInputStats  Dn sys/switch-A/psu-2/input-stats       Current 0.656250        PowerAvg 153.335938     Voltage 231.500000
# equipmentPsuStats       Dn sys/chassis-1/psu-1/stats    AmbientTemp 17.000000   Output12vAvg 12.008000  Output3v3Avg 3.336000


def ucs_bladecenter_psu_parse(info):
    data = ucs_bladecenter.generic_parse(info)
    psu = {}

    def get_item_name(key):
        tokens = key.split("/")
        tokens[1] = tokens[1].replace("psu-", " Module ")
        tokens = [x[0].upper() + x[1:] for x in tokens]
        return "".join(tokens).replace("-", " ")

    for component, key_low, key_high in [
        ("equipmentPsuInputStats", 4, -12),
        ("equipmentPsuStats", 4, -6),
    ]:
        for key, values in data.get(component, {}).items():
            name = get_item_name(key[key_low:key_high])
            del values["Dn"]
            psu.setdefault(name, {}).update(values)

    return psu


#.
#   .--Chassis Volt.-------------------------------------------------------.
#   |         ____ _                   _      __     __    _ _             |
#   |        / ___| |__   __ _ ___ ___(_)___  \ \   / /__ | | |_           |
#   |       | |   | '_ \ / _` / __/ __| / __|  \ \ / / _ \| | __|          |
#   |       | |___| | | | (_| \__ \__ \ \__ \   \ V / (_) | | |_ _         |
#   |        \____|_| |_|\__,_|___/___/_|___/    \_/ \___/|_|\__(_)        |
#   |                                                                      |
#   '----------------------------------------------------------------------'

factory_settings["ucs_bladecenter_psu_default_levels"] = {
    "levels_3v_lower": (3.25, 3.20),
    "levels_3v_upper": (3.4, 3.45),
    "levels_12v_lower": (11.9, 11.8),
    "levels_12v_upper": (12.1, 12.2)
}


def inventory_ucs_bladecenter_psu(parsed):
    for key in parsed:
        if key.startswith("Chassis"):
            yield key, {}


@get_parsed_item_data
def check_ucs_bladecenter_psu(item, params, psu):
    for param_key, perfname, key, text in [
        ("levels_3v_", "3_3v", "Output3v3Avg", "Output 3.3V-Average"),
        ("levels_12v_", "12v", "Output12vAvg", "Output 12V-Average")
    ]:
        voltage = float(psu[key])
        yield 0, "%s: %.2f V" % (text, voltage), [(perfname, voltage)]
        if key in psu:
            warn_lower, crit_lower = params[param_key + "lower"]
            if voltage <= crit_lower:
                yield 2, "too low (warn/crit below %.2f/%.2f V)" % params[param_key + "lower"]
            elif voltage <= warn_lower:
                yield 1, "too low (warn/crit below %.2f/%.2f V)" % params[param_key + "lower"]

            warn_upper, crit_upper = params[param_key + "upper"]
            if voltage >= crit_upper:
                yield 2, "too high (warn/crit at %.2f/%.2f V)" % params[param_key + "upper"]
            elif voltage >= warn_upper:
                yield 1, "too high (warn/crit at %.2f/%.2f V)" % params[param_key + "upper"]


check_info["ucs_bladecenter_psu"] = {
    'parse_function': ucs_bladecenter_psu_parse,
    'inventory_function': inventory_ucs_bladecenter_psu,
    'check_function': check_ucs_bladecenter_psu,
    'service_description': 'Voltage %s',
    'group': 'ucs_bladecenter_chassis_voltage',
    'has_perfdata': True,
    'default_levels_variable': "ucs_bladecenter_psu_default_levels",
}

#.
#   .--Power Supply--------------------------------------------------------.
#   |    ____                          ____                    _           |
#   |   |  _ \ _____      _____ _ __  / ___| _   _ _ __  _ __ | |_   _     |
#   |   | |_) / _ \ \ /\ / / _ \ '__| \___ \| | | | '_ \| '_ \| | | | |    |
#   |   |  __/ (_) \ V  V /  __/ |     ___) | |_| | |_) | |_) | | |_| |    |
#   |   |_|   \___/ \_/\_/ \___|_|    |____/ \__,_| .__/| .__/|_|\__, |    |
#   |                                             |_|   |_|      |___/     |
#   '----------------------------------------------------------------------'


def inventory_ucs_bladecenter_psu_switch_power(parsed):
    for key in parsed:
        if key.startswith("Switch"):
            yield key, {}


@get_parsed_item_data
def check_ucs_bladecenter_psu_switch_power(item, params, psu):
    # Convert fields
    KEY_MAP = {"Current": "current", "PowerAvg": "power", "Voltage": "voltage"}

    psu_new = {}
    for k, v in psu.items():
        if k in KEY_MAP:
            k, v = KEY_MAP[k], (float(v), None)
        psu_new[k] = v

    return check_elphase(item, params, {item: psu_new})


check_info["ucs_bladecenter_psu.switch_power"] = {
    'inventory_function': inventory_ucs_bladecenter_psu_switch_power,
    'check_function': check_ucs_bladecenter_psu_switch_power,
    'service_description': 'Power Supply %s',
    'has_perfdata': True,
    'group': "el_inphase"
}

#.
#   .--Temperature---------------------------------------------------------.
#   |     _____                                   _                        |
#   |    |_   _|__ _ __ ___  _ __   ___ _ __ __ _| |_ _   _ _ __ ___       |
#   |      | |/ _ \ '_ ` _ \| '_ \ / _ \ '__/ _` | __| | | | '__/ _ \      |
#   |      | |  __/ | | | | | |_) |  __/ | | (_| | |_| |_| | | |  __/      |
#   |      |_|\___|_| |_| |_| .__/ \___|_|  \__,_|\__|\__,_|_|  \___|      |
#   |                       |_|                                            |
#   '----------------------------------------------------------------------'

factory_settings["ucs_bladecenter_psu_chassis_temp_default_levels"] = {
    "levels": (35, 40),
}


def inventory_ucs_bladecenter_psu_chassis_temp(parsed):
    for key, values in parsed.items():
        if key.startswith("Chassis") and values.get("AmbientTemp"):
            yield "Ambient " + " ".join(key.split()[:2]), {}


def check_ucs_bladecenter_psu_chassis_temp(item, params, parsed):
    sensor_item = item[8:]  # drop "Ambient "
    sensor_list = []

    for key, values in sorted(parsed.items()):
        if key.startswith(sensor_item) and "AmbientTemp" in values:
            sensor_list.append((
                "Module %s" % key.split()[-1],
                float(values.get("AmbientTemp")),
            ))
    return check_temperature_list(sensor_list, params, "ucs_bladecenter_psu_chassis_temp_%s" % item)


check_info["ucs_bladecenter_psu.chassis_temp"] = {
    'inventory_function': inventory_ucs_bladecenter_psu_chassis_temp,
    'check_function': check_ucs_bladecenter_psu_chassis_temp,
    'service_description': 'Temperature %s',
    'group': 'temperature',
    'has_perfdata': True,
    'default_levels_variable': "ucs_bladecenter_psu_chassis_temp_default_levels",
}
