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

# <<<db2_counters>>>
# CMDBS1 4711 0 +2.00000000000000E+000   +0.00000000000000E+000

factory_settings["db2_counters_defaults"] = {
    "deadlocks"    : (10.0, 5.0),
    "lockwaits"    : (11.0, 6.0),
    "sortoverflows"  : (12.0, 7.0),
}

counters = {
    "deadlocks"   : "Deadlocks/sec",
    "lockwaits"   : "Lockwaits/sec",
    "sortoverflows" : "Sortoverflows/sec",
}

def inventory_db2_counters(info):
    inventory = []
    for line in info:
        if len(line) == 3:
            db_name = line[0]
            if line[1] in counters.keys():
                inventory.append((db_name, db2_counters_defaults))
    return inventory

def check_db2_counters(item, params, info):
    perfdata = []
    output = []
    wrapped = False
    state = 0
    now = time.time()
    for line in info:
        if len(line) > 2 and line[0] == item and line[1] in counters.keys():
            counter = line[1]
            label = counters.get(counter)
            value = float(line[2])
            # compute rate from counter value
            countername = "db2_counters.%s.%s" % (item, counter)
            try:
                timedif, rate = get_counter(countername, now, value)
            except MKCounterWrapped:
                wrapped = True
                continue

            warn, crit = params.get(counter)
            sym = ""
            if rate > crit:
                state = max(state, 2)
                sym = "(!!)"
            elif rate > warn:
                state = max(state, 1)
                sym = "(!)"


            output.append('%s: %.1f/s%s' % (label, rate, sym))
            perfdata.append((counter, rate, warn, crit))

    if wrapped:
        raise MKCounterWrapped("", "Some counter wrapped, no data this time")
    if output:
        return (state, ', '.join(output), perfdata)
    else:
        return (3, 'Counters for %s could not be found in agent output' % (item))

check_info['db2_counters'] = {
    "service_description"     : "DB2 Counters %s",
    "check_function"          : check_db2_counters,
    "inventory_function"      : inventory_db2_counters,
    "has_perfdata"            : True,
    "group"                   : "db2_counters",
    "default_levels_variable" : "db2_counters_defaults",
}
