#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2016             mk@mathias-kettner.de |
# +------------------------------------------------------------------+
#
# This file is part of Check_MK.
# The official homepage is at http://mathias-kettner.de/check_mk.
#
# check_mk is free software;  you can redistribute it and/or modify it
# under the  terms of the  GNU General Public License  as published by
# the Free Software Foundation in version 2.  check_mk is  distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY;  with-
# out even the implied warranty of  MERCHANTABILITY  or  FITNESS FOR A
# PARTICULAR PURPOSE. See the  GNU General Public License for more de-
# tails. You should have  received  a copy of the  GNU  General Public
# License along with GNU Make; see the file  COPYING.  If  not,  write
# to the Free Software Foundation, Inc., 51 Franklin St,  Fifth Floor,
# Boston, MA 02110-1301 USA.


# Version 2
# healthy if all nodes are reachable (at discovery)
# <<<pvecm_nodes>>>
# Node Sts Inc Joined Name
# 1 M 35960 2016-01-20 13:17:24 BAR-host002
# 2 M 36020 2016-01-20 13:30:18 FOO-host001
# 3 M 35960 2016-01-20 13:17:24 FOO-host003
# 4 M 35964 2016-01-20 13:17:24 FOO-host004
# 5 M 35764 2016-01-20 12:59:03 FOO-host002
# 6 M 36032 2016-01-20 13:34:27 BAR-host003
# 7 M 35960 2016-01-20 13:17:24 BAR-host001

# faulty because some nodes are missing
# <<<pvecm_nodes>>>
# Node Sts Inc Joined Name
# 1 M 35960 2016-01-20 13:17:24 BAR-host002
# 2 M 36020 2016-01-20 13:30:18 FOO-host001
# 4 M 35964 2016-01-20 13:17:24 FOO-host004
# 6 M 36032 2016-01-20 13:34:27 BAR-host003
# 7 M 35960 2016-01-20 13:17:24 BAR-host001

# faulty communication between nodes
# <<<pvecm_nodes>>>
# Node Sts Inc Joined Name
# 1 M 35960 2016-01-20 13:17:24 BAR-host002
# 2 M 36020 FOO-host001
# 3 M 35960 2016-01-20 13:17:24 FOO-host003
# 4 M 35964 2016-01-20 13:17:24 FOO-host004
# 5 M 35764 FOO-host002
# 6 M 36032 BAR-host003
# 7 M 35960 2016-01-20 13:17:24 BAR-host001

# Version >2
# <<<pvecm_nodes>>>
#
# Membership information
# ~~~~~~~~~~~~~~~~~~~~~~
#     Nodeid      Votes Name
#          1          1 hp1 (local)
#          2          1 hp2
#          3          1 hp3
#          4          1 hp4


def parse_pvecm_nodes(info):
    parsed = {}
    header = None
    for line in info:
        if line == ["Node", "Sts", "Inc", "Joined", "Name"]:
            header = ["node_id", "status", "joined", "name"]
            parse_func = _parse_version_eq_2
            continue

        elif line == ["Nodeid", "Votes", "Name"]:
            header = ["node_id", "votes", "name"]
            parse_func = _parse_version_gt_2
            continue

        if header is None:
            continue

        k, v = parse_func(line, header)
        parsed.setdefault(k, v)
    return parsed


def _parse_version_eq_2(line, header):
    if len(line) == 6:
        data = dict(zip(header[:3], line[:2] + [" ".join(line[3:5])]))
    else:
        data = dict(zip(["node_id", "status"], line[:2]))
    return line[-1], data


def _parse_version_gt_2(line, header):
    return " ".join(line[2:]), dict(zip(header[:2], line[:2]))


def inventory_pvecm_nodes(parsed):
    for name in parsed:
        yield name, None


def check_pvecm_nodes(item, _no_params, parsed):
    if item not in parsed:
        return 2, "Node is missing"

    map_states = {
        "m" : (0, "member of the cluster"),
        "x" : (1, "not a member of the cluster"),
        "d" : (2, "known to the cluster but disallowed access to it"),
    }
    data = parsed[item]
    infotexts = ["ID: %s" % data["node_id"]]
    state = 0
    if "status" in data:
        state, state_readable = map_states[data["status"].lower()]
        infotexts.append("Status: %s" % state_readable)
    if "joined" in data:
        infotexts.append("Joined: %s" % data["joined"])
    if "votes" in data:
        infotexts.append("Votes: %s" % data["votes"])
    return state, ", ".join(infotexts)


check_info['pvecm_nodes'] = {
    'parse_function'            : parse_pvecm_nodes,
    'inventory_function'        : inventory_pvecm_nodes,
    'check_function'            : check_pvecm_nodes,
    'service_description'       : 'PVE Node %s',
}
