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

# <<<heartbeat_rscstatus>>>
# all
#
# Status can be "local", "foreign", "all" or "none"


def parse_heartbeat_rscstatus(info):
    try:
        return info[0][0]
    except IndexError:
        return


def inventory_heartbeat_rscstatus(heartbeat_rsc_status):
    if heartbeat_rsc_status is not None:
        yield None, {"discovered_state": heartbeat_rsc_status}


def check_heartbeat_rscstatus(_no_item, params, heartbeat_rsc_status):
    if heartbeat_rsc_status is None:
        return

    if not isinstance(params, dict):
        # old params comes styled with double qoutes
        params = {"discovered_state": params.replace('"', "")}

    expected_state = params.get("discovered_state")
    if "expected_state" in params:
        expected_state = params["expected_state"]

    if expected_state == heartbeat_rsc_status:
        yield 0, "Current state: %s" % heartbeat_rsc_status
    else:
        yield 2, "Current state: %s (Expected: %s)" % (heartbeat_rsc_status, expected_state)


check_info["heartbeat_rscstatus"] = {
    "parse_function": parse_heartbeat_rscstatus,
    "check_function": check_heartbeat_rscstatus,
    "inventory_function": inventory_heartbeat_rscstatus,
    "service_description": "Heartbeat Ressource Status",
    "group": "heartbeat_rscstatus",
}
