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


def parse_raritan_emx(info):
    raritan_map_type = {
        "0": ("temp", "Air"),
        "1": ("temp", "Water"),
        "2": ("fanspeed", ""),
        "3": ("binary", ""),
        "4": ("valve", ""),
    }
    parsed = {}
    for rack_id, rack_name, sensor_number, value_text, unit, sensor_state in info:
        rack_type, rack_type_readable = raritan_map_type[sensor_number]

        extra_name = ""
        if rack_type_readable != "":
            extra_name += " " + rack_type_readable

        rack_name = ("Rack %s%s %s" % (rack_id, extra_name, rack_name)).replace("DC", "").strip()

        if rack_type in ["binary", ""]:
            rack_value = None
        else:
            if rack_type == "temp":
                rack_value = float(value_text) / 10
            else:
                rack_value = int(value_text)

        parsed[rack_name] = {
            "rack_type": rack_type,
            "rack_unit": raritan_map_unit[unit],
            "rack_value": rack_value,
            "state": raritan_map_state[sensor_state],
        }

    return parsed


def inventory_raritan_emx(parsed, rack_type):
    for rack_name, values in parsed.items():
        if values["rack_type"] == rack_type:
            yield rack_name, None


#   .--temperature---------------------------------------------------------.
#   |      _                                      _                        |
#   |     | |_ ___ _ __ ___  _ __   ___ _ __ __ _| |_ _   _ _ __ ___       |
#   |     | __/ _ \ '_ ` _ \| '_ \ / _ \ '__/ _` | __| | | | '__/ _ \      |
#   |     | ||  __/ | | | | | |_) |  __/ | | (_| | |_| |_| | | |  __/      |
#   |      \__\___|_| |_| |_| .__/ \___|_|  \__,_|\__|\__,_|_|  \___|      |
#   |                       |_|                                            |
#   +----------------------------------------------------------------------+
#   |                             main check                               |
#   '----------------------------------------------------------------------'


def inventory_raritan_emx_temp(parsed):
    for rack_name, values in parsed.items():
        if values["rack_type"] == "temp":
            yield rack_name, {}


def check_raritan_emx_temp(item, params, parsed):
    if "Temperature" in item:
        # old style (pre 1.2.8) item name, convert
        item = "Rack " + item.replace(" Temperature", "")
    elif "Fan Speed" in item:
        item = "Rack " + item.replace(" Fan Speed", "")
        return check_raritan_emx_fan(item, params, parsed)
    elif "Door Contact" in item:
        item = "Rack " + item.replace(" Door Contact DC", "")
        return check_raritan_sensors_binary(item, params, parsed)

    if item in parsed:
        rack_value = parsed[item]["rack_value"]
        rack_unit = parsed[item]["rack_unit"]
        state, state_readable = parsed[item]["state"]
        return check_temperature(
            rack_value,
            params,
            item,
            dev_unit=rack_unit,
            dev_status=state,
            dev_status_name=state_readable,
        )


check_info["raritan_emx"] = {
    "parse_function": parse_raritan_emx,
    "inventory_function": inventory_raritan_emx_temp,
    "check_function": check_raritan_emx_temp,
    "service_description": "Temperature %s",
    "has_perfdata": True,
    "snmp_info": (
        ".1.3.6.1.4.1.13742.9",
        [
            "1.4.1.1.1",  # Rack ID
            "1.4.1.1.4",  # Name
            "1.4.1.1.2",  # Sensor
            "2.1.1.3",  # Value
            "1.4.1.1.5",  # Unit
            "2.1.1.2",  # State
        ],
    ),
    "snmp_scan_function": lambda oid: oid(".1.3.6.1.2.1.1.2.0") in [".1.3.6.1.4.1.13742.8"],
    "group": "temperature",
}

# .
#   .--fan-----------------------------------------------------------------.
#   |                            __                                        |
#   |                           / _| __ _ _ __                             |
#   |                          | |_ / _` | '_ \                            |
#   |                          |  _| (_| | | | |                           |
#   |                          |_|  \__,_|_| |_|                           |
#   |                                                                      |
#   '----------------------------------------------------------------------'


def check_raritan_emx_fan(item, _no_params, parsed):
    if item in parsed:
        fan_speed = parsed[item]["rack_value"]
        fan_unit = parsed[item]["rack_unit"]
        state, state_readable = parsed[item]["state"]
        return state, "Speed: %d%s, status: %s" % (fan_speed, fan_unit, state_readable)


check_info["raritan_emx.fan"] = {
    "inventory_function": lambda parsed: inventory_raritan_emx(parsed, "fanspeed"),
    "check_function": check_raritan_emx_fan,
    "service_description": "Fan %s",
}

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

check_info["raritan_emx.binary"] = {
    "inventory_function": lambda parsed: inventory_raritan_emx(parsed, "binary"),
    "check_function": check_raritan_sensors_binary,
    "service_description": "Door %s",
}
