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

factory_settings["sap_hana_memrate_default_levels"] = {"levels": ('perc_used', (70.0, 80.0))}


def parse_sap_hana_memrate(info):
    parsed = {}
    for sid_instance, lines in parse_sap_hana(info).iteritems():
        inst_data = {}
        for line in lines:
            if len(line) < 3 or line[0] != 'mem_rate':
                continue

            for index, key in [(1, "used"), (2, "total")]:
                try:
                    inst_data[key] = int(line[index])
                except ValueError:
                    pass
        if inst_data:
            parsed.setdefault(sid_instance, inst_data)
    return parsed


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


@get_parsed_item_data
def check_sap_hana_memrate(item, params, data):
    yield check_memory_simple(data['used'], data['total'], params)


check_info['sap_hana_memrate'] = {
    'parse_function': parse_sap_hana_memrate,
    'inventory_function': inventory_sap_hana_memrate,
    'check_function': check_sap_hana_memrate,
    'service_description': 'SAP HANA Memory %s',
    'includes': ['sap_hana.include', 'memory.include'],
    "has_perfdata": True,
    'group': 'sap_hana_memory',
    'default_levels_variable': 'sap_hana_memrate_default_levels',
}
