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

_STATE_TO_MONSTATE = {
    "unknown": 3,
}


def inventory_sentry_pdu(section):
    yield from (
        (
            name,
            {"discovered_state": pdu.state},
        )
        for name, pdu in section.items()
    )


def check_sentry_pdu(item, params, section):
    if not (pdu := section.get(item)):
        return

    infotext = "Status: %s" % pdu.state
    if pdu.state != params.get(
        "required_state",
        params["discovered_state"],
    ):
        yield 2, infotext
    else:
        yield _STATE_TO_MONSTATE.get(
            pdu.state,
            0,
        ), infotext

    if pdu.power:
        infotext = "Power: %s Watt" % pdu.power
        perfdata = [("power", pdu.power)]
        yield 0, infotext, perfdata


check_info["sentry_pdu"] = {
    "check_function": check_sentry_pdu,
    "inventory_function": inventory_sentry_pdu,
    "service_description": "Plug %s",
    "group": "plugs",
}
