#!/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 tmpl  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 tmpl for more de-
# tails. You should have  received  a copy of the  GNU  General Public
# tmpl 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_replication_status(info):
    parsed = {}
    for sid_instance, lines in parse_sap_hana(info).iteritems():
        inst = {}
        for line in lines:
            if line[0] == "mode:":
                inst["mode"] = line[1]
            elif line[0] == "systemReplicationStatus:":
                inst["sys_repl_status"] = line[1]
        if inst:
            parsed.setdefault(sid_instance, inst)
    return parsed


SAP_HANA_REPL_STATUS_MAP = {
    "0": (3, "unknown status from replication script"),
    "10": (2, "no system replication"),
    "11": (2, "error"),
    "12": (2, "unknown"),
    "13": (1, "initializing"),
    "14": (0, "syncing"),
    "15": (0, "active"),
}


def inventory_sap_hana_replication_status(parsed):
    for sid_instance, data in parsed.iteritems():
        if data["sys_repl_status"] != "10" and data.get("mode", "").lower() == "primary":
            yield sid_instance, {}


@get_parsed_item_data
def check_sap_hana_replication_status(item, params, data):
    sys_repl_status = data["sys_repl_status"]
    state, state_readable = SAP_HANA_REPL_STATUS_MAP.get(sys_repl_status,
                                                         (3, "unknown[%s]" % sys_repl_status))
    yield state, "System replication: %s" % state_readable

    yield 0, "Mode: %s" % data["mode"]


check_info['sap_hana_replication_status'] = {
    'parse_function': parse_sap_hana_replication_status,
    'inventory_function': inventory_sap_hana_replication_status,
    'check_function': check_sap_hana_replication_status,
    'service_description': 'SAP HANA Replication Status %s',
    'includes': ['sap_hana.include'],
}
