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


factory_settings['informix_logusage_default_levels'] = {
    'levels_perc': (80.0, 85.0)
}


def parse_informix_logusage(info):
    parsed = {}
    instance = None
    entry = None
    for line in info:
        if instance is not None and line == ["(constant)", "LOGUSAGE"]:
            entry = {}
            parsed.setdefault(instance, [])
            parsed[instance].append(entry)

        elif line[0].startswith("[[[") and line[0].endswith("]]]"):
            instance = line[0][3:-3]

        elif entry is not None:
            if line[0] == "(expression)":
                k, v = line[1].split(":")
            else:
                k, v = line[0],  line[1]
            entry.setdefault(k, v)

    return parsed


def inventory_informix_logusage(parsed):
    return [(instance, {}) for instance in parsed]


def check_informix_logusage(item, params, parsed):
    if item in parsed:
        data = parsed[item]
        logfiles = len(data)
        if not logfiles:
            yield 1, "Log information missing"
            return

        size = 0
        used = 0
        for entry in data:
            pagesize = int(entry['sh_pagesize'])
            size += int(entry['size']) * pagesize
            used += int(entry['used']) * pagesize

        infotext = "Files: %s, Size: %s, Used: %s" % \
                   (logfiles, get_bytes_human_readable(size),
                    get_bytes_human_readable(used))
        state = 0
        if 'levels' in params:
            warn, crit = params['levels']
            if size >= crit:
                state = 2
            elif size >= warn:
                state = 1
            if state:
                infotext += " (warn/crit at %s/%s)" % \
                            (get_bytes_human_readable(warn),
                             get_bytes_human_readable(crit))

        yield state, infotext, [("file_count", logfiles),
                                ("log_files_total", size),
                                ("log_files_used", used)]

        if size:
            used_perc = used * 100.0 / size
            infotext = "%.2f%%" % used_perc
            warn_perc, crit_perc = params['levels_perc']
            state = 0
            if used_perc >= crit_perc:
                state = 2
            elif used_perc >= warn_perc:
                state = 1
            if state:
                infotext += " (warn/crit at %.2f%%/%.2f%%)" % (warn_perc, crit_perc)

            yield state, infotext


check_info['informix_logusage'] = {
    'parse_function'        : parse_informix_logusage,
    'inventory_function'    : inventory_informix_logusage,
    'check_function'        : check_informix_logusage,
    'has_perfdata'          : True,
    'service_description'   : 'Informix Log Usage %s',
    'default_levels_variable': 'informix_logusage_default_levels',
}
