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


# TODO memory simple for items


def parse_sap_hana_mem(info):
    pparsed = {}
    section = None
    for line in info:
        if line[0] in ["[[[resident]]]", "[[[database]]]"]:
            section = line[0][3:-3]
            continue
        if section is not None:
            instance = line[0].replace('"', '')
            pparsed.setdefault(instance, {})
            # all memory values in GB
            if section == 'resident':
                # used
                data = float(line[1]) * 1024**3
            elif section == 'database':
                # used, max, resident_max
                data = map(lambda x: float(x) * 1024**3, line[1:])
            else:
                continue
            pparsed[instance].setdefault(section, data)

    parsed = {}
    for instance, attrs in pparsed.iteritems():
        parsed.setdefault(instance, {})
        db_attrs = attrs['database']
        parsed[instance].setdefault('database', db_attrs[:2])
        parsed[instance].setdefault('resident', [attrs['resident'], db_attrs[-1]])

    return parsed


def inventory_sap_hana_mem(parsed, _type):
    return [(instance, {}) for instance, attrs in parsed.iteritems() if _type in attrs]


def check_sap_hana_mem(item, params, parsed, _type):
    if item in parsed:
        data = parsed[item]
        used, total = data[_type]
        return check_memory_simple(used, total, params)


#   .--resident------------------------------------------------------------.
#   |                              _     _            _                    |
#   |                _ __ ___  ___(_) __| | ___ _ __ | |_                  |
#   |               | '__/ _ \/ __| |/ _` |/ _ \ '_ \| __|                 |
#   |               | | |  __/\__ \ | (_| |  __/ | | | |_                  |
#   |               |_|  \___||___/_|\__,_|\___|_| |_|\__|                 |
#   |                                                                      |
#   '----------------------------------------------------------------------'


factory_settings["sap_hana_mem_resident_default_levels"] = {
    "levels" : ("perc_used", (80.0, 90.0))
}


def inventory_sap_hana_mem_resident(parsed):
    return inventory_sap_hana_mem(parsed, 'resident')


def check_sap_hana_mem_resident(item, params, parsed):
    return check_sap_hana_mem(item, params, parsed, 'resident')


check_info['sap_hana_mem'] = {
    'parse_function'        : parse_sap_hana_mem,
    'inventory_function'    : inventory_sap_hana_mem_resident,
    'check_function'        : check_sap_hana_mem_resident,
    'service_description'   : 'SAP HANA Resident Memory',
    'includes'              : ['memory.include'],
    #'group'                 : 'memory_simple',
    'has_perfdata'                : True,
    'default_levels_variable' : 'sap_hana_mem_resident_default_levels',
}


#.
#   .--database------------------------------------------------------------.
#   |                  _       _        _                                  |
#   |               __| | __ _| |_ __ _| |__   __ _ ___  ___               |
#   |              / _` |/ _` | __/ _` | '_ \ / _` / __|/ _ \              |
#   |             | (_| | (_| | || (_| | |_) | (_| \__ \  __/              |
#   |              \__,_|\__,_|\__\__,_|_.__/ \__,_|___/\___|              |
#   |                                                                      |
#   '----------------------------------------------------------------------'


factory_settings["sap_hana_mem_database_default_levels"] = {
    "levels" : ("perc_used", (80.0, 90.0))
}


def inventory_sap_hana_mem_db(parsed):
    return inventory_sap_hana_mem(parsed, 'database')


def check_sap_hana_mem_db(item, params, parsed):
    return check_sap_hana_mem(item, params, parsed, 'database')


check_info['sap_hana_mem.database'] = {
    'inventory_function'    : inventory_sap_hana_mem_db,
    'check_function'        : check_sap_hana_mem_db,
    'service_description'   : 'SAP HANA Used Memory',
    'includes'              : ['memory.include'],
    #'group'                 : 'memory_simple',
    'has_perfdata'                : True,
    'default_levels_variable' : 'sap_hana_mem_database_default_levels',
}
