#!/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_diskusage(info):
    parsed = {}
    for sid_instance, lines in parse_sap_hana(info).iteritems():
        for line in lines:
            if len(line) < 3:
                continue
            inst = parsed.setdefault("%s - %s" % (sid_instance, line[0]), {
                "state_name": line[1],
            })
            inst.update(_extract_size_and_used_from_line(line))
    return parsed


def _extract_size_and_used_from_line(line):
    # Values are measured in GB. Are other factors possible? (Query)
    inst_values = {}
    splitted_line = line[-1].split()
    for key, index in [
        ("size", 1),
        ("used", 4),
    ]:
        try:
            inst_values[key] = float(splitted_line[index]) * 1024
        except (ValueError, IndexError):
            pass
    return inst_values


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


@get_parsed_item_data
def check_sap_hana_diskusage(item, params, data):
    state_name = data['state_name']
    if state_name == 'OK':
        state = 0
    elif state_name == "UNKNOWN":
        state = 3
    else:
        state = 2
    yield state, "Status: %s" % state_name

    size_mb = data['size']
    used_mb = data['used']
    avail_mb = size_mb - used_mb
    yield df_check_filesystem_list(item, params, [(item, size_mb, avail_mb, 0)])


check_info['sap_hana_diskusage'] = {
    'parse_function': parse_sap_hana_diskusage,
    'inventory_function': inventory_sap_hana_diskusage,
    'check_function': check_sap_hana_diskusage,
    'service_description': 'SAP HANA Disk %s',
    'includes': ['sap_hana.include', 'size_trend.include', 'df.include'],
    "has_perfdata": True,
    'group': 'filesystem',
    'default_levels_variable': 'filesystem_default_levels',
}
