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

# Author: Lars Michelsen <lm@mathias-kettner.de>

# Example outputs from agent:
#
# <<<heartbeat_nodes>>>
# smwp active lanb up lanb up lana up lana up
# swi04 ping swi04 up
# swi03 ping swi03 up


def inventory_heartbeat_nodes(info):
    return [(line[0], None) for line in info if line[0] != ""]


def check_heartbeat_nodes(item, params, info):
    for line in info:
        if line[0] == item:
            status = 0
            nodeStatus = line[1]

            linkOutput = ""
            for link, state in zip(line[2::2], line[3::2]):
                state_txt = ""
                if state != "up":
                    status = 2
                    state_txt = " (!!)"
                linkOutput += "%s: %s%s, " % (link, state, state_txt)
            linkOutput = linkOutput.rstrip(", ")

            if nodeStatus in ["active", "up", "ping"] and status <= 0:
                status = 0
            elif nodeStatus == "dead" and status <= 2:
                status = 2

            if nodeStatus not in ["active", "up", "ping", "dead"]:
                return (3, "Node %s has an unhandled state: %s" % (line[0], nodeStatus))

            return (
                status,
                'Node %s is in state "%s". Links: %s' % (line[0], nodeStatus, linkOutput),
            )

    return (3, "Node is not present anymore")


check_info["heartbeat_nodes"] = {
    "check_function": check_heartbeat_nodes,
    "inventory_function": inventory_heartbeat_nodes,
    "service_description": "Heartbeat Node %s",
}
