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


def parse_sap_hana_status(info):
    parsed = {}
    for (sid_instance, node), lines in parse_sap_hana_cluster_aware(info).iteritems():
        for line in lines:
            if line[0].lower() == "all started":
                item_name = 'Status'
                item_data = {
                    "instance": sid_instance,
                    "state_name": line[1],
                    "message": line[2],
                }
            else:  # Version
                item_name = line[0]
                item_data = {
                    "instance": sid_instance,
                    'version': line[2],
                }
            parsed.setdefault(item_name, {}).setdefault(node, item_data)
    return parsed


def _check_sap_hana_status_data(data):
    state_name = data['state_name']
    if state_name.lower() == "ok":
        state = 0
    elif state_name.lower() == "unknown":
        state = 3
    else:
        state = 2
    return state, "Status: %s" % state_name


def inventory_sap_hana_status(parsed):
    for item in parsed.iterkeys():
        yield item, {}


@get_parsed_item_data
def check_sap_hana_status(item, params, node_data):
    nodes = [n for n in node_data.keys() if n]
    if nodes:
        yield 0, 'Nodes: %s' % ", ".join(nodes)

    for data in node_data.itervalues():
        if item == 'Status':
            yield _check_sap_hana_status_data(data)
        else:
            yield 0, "Version: %s" % data['version']

        # It ONE physical device and at least two nodes.
        # Thus we only need to check the first one.
        return


check_info['sap_hana_status'] = {
    'parse_function': parse_sap_hana_status,
    'inventory_function': inventory_sap_hana_status,
    'check_function': check_sap_hana_status,
    'service_description': 'SAP HANA %s',
    'includes': ['sap_hana.include'],
    "node_info": True,
}
