#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2015             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.

# <<<aix_hacmp_resources:sep(58)>>>
# pdb213rg:ONLINE:pasv0450:non-concurrent:OHN:FNPN:NFB:ignore::: : :::
# pdb213rg:OFFLINE:pasv0449:non-concurrent:OHN:FNPN:NFB:ignore::: : :::
# pmon01rg:ONLINE:pasv0449:non-concurrent:OHN:FNPN:NFB:ignore::: : :::
# pmon01rg:OFFLINE:pasv0450:non-concurrent:OHN:FNPN:NFB:ignore::: : :::

# parsed =
# {u'pdb213rg': [(u'pasv0450', u'online'), (u'pasv0449', u'offline')],
#  u'pmon01rg': [(u'pasv0449', u'online'), (u'pasv0450', u'offline')]}

factory_settings["aix_hacmp_resources"] = {
    "expect_online_on": "first",
}


def parse_aix_hacmp_resources(info):
    parsed = {}
    for line in info:
        joined_line = " ".join(line)
        if 'There is no cluster definition' in joined_line or 'Status of the RSCT subsystems' in joined_line:
            continue
        try:
            parsed.setdefault(line[0], []).append((line[2], line[1].lower()))
        except IndexError:
            pass
    return parsed


def inventory_aix_hacmp_resources(parsed):
    return [(key, None) for key in parsed]


@get_parsed_item_data
def check_aix_hacmp_resources(item, params, data):
    if params is None:
        expected_behaviour = "first"
    else:
        expected_behaviour = params.get("expect_online_on", "first")

    resource_states = []
    infotext = []
    for node_name, resource_state in data:
        resource_states.append(resource_state)
        infotext.append("%s on node %s" % (resource_state, node_name))

    state = 0
    if expected_behaviour == "first":
        if resource_states[0] != "online":
            state = 2
    elif expected_behaviour == "any":
        if not any((resource_state == u"online" for resource_state in resource_states)):
            state = 2

    return state, ", ".join(infotext)


check_info['aix_hacmp_resources'] = {
    'default_levels_variable': "aix_hacmp_resources",
    'parse_function': parse_aix_hacmp_resources,
    'inventory_function': inventory_aix_hacmp_resources,
    'check_function': check_aix_hacmp_resources,
    'service_description': 'HACMP RG %s',
    'group': 'hacmp_resources',
}
