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


# example output

factory_settings["nimble_latency_default_levels"] = {
        "0.1"  : (0, 20),
        "0.2"  : (0, 20),
        "0.5"  : (0, 20),
        "1"    : (0, 20),
        "2"    : (0, 20),
        "5"    : (0, 20),
        "10"   : (1, 20),
        "20"   : (1, 20),
        "50"   : (1, 20),
        "100"  : (2, 20),
        "200"  : (2, 20),
        "500"  : (2, 20),
        "1000" : (2, 20),
}


def inventory_nimble_latency(info):
    for line in info:
        if line[1] == "1":
            yield ( line[0], {} )


def check_nimble_read_latency(item, params, info):
    timestamp = time.time()
    for line in info:
        if line[0] == item:
            counter_base = "nimble_read_latency.%s." % item
            names = {
                "0-0.1 ms"      : ("0.1",  int(line[3])),
                "0.1-0.2 ms"    : ("0.2",  int(line[4])),
                "0.2-0.5 ms"    : ("0.5",  int(line[5])),
                "0.5-1.0 ms"    : ("1",    int(line[6])),
                "1-2 ms"        : ("2",    int(line[7])),
                "2-5 ms"        : ("5",    int(line[8])),
                "5-10 ms"       : ("10",   int(line[9])),
                "10-20 ms"      : ("20",   int(line[10])),
                "20-50 ms"      : ("50",   int(line[11])),
                "50-100 ms"     : ("100",  int(line[12])),
                "100-200 ms"    : ("200",  int(line[13])),
                "200-500 ms"    : ("500",  int(line[14])),
                "500+ ms"       : ("1000", int(line[15])),
            }
            total_reads = get_rate(counter_base + "total", timestamp, int(line[2]))
            yield 0, "Total Reads: %d" % total_reads
            for key, value in names.iteritems():
                counter = get_rate(counter_base + value[0], timestamp, value[1])
                state, state_count = params[value[0]]
                if counter > state_count:
                    yield state, "%s: %d" % (key, counter)
                else:
                    continue


check_info['nimble_latency'] = {
    'inventory_function'        : inventory_nimble_latency,
    'check_function'            : check_nimble_read_latency,
    'service_description'       : 'Volume %s Read IO',
    'snmp_info'                 : ('.1.3.6.1.4.1.37447.1.2.1', [
                                        "3",  # NIMBLE-MIB::volName
                                        "10", # NIMBLE-MIB::volOnline
                                        "13", # NIMBLE-MIB::volIoReads
                                        "21", # NIMBLE-MIB::volIoReadLatency0uTo100u
                                        "22", # NIMBLE-MIB::volIoReadLatency100uTo200u
                                        "23", # NIMBLE-MIB::volIoReadLatency200uTo500u
                                        "24", # NIMBLE-MIB::volIoReadLatency500uTo1m
                                        "25", # NIMBLE-MIB::volIoReadLatency1mTo2m
                                        "26", # NIMBLE-MIB::volIoReadLatency2mTo5m
                                        "27", # NIMBLE-MIB::volIoReadLatency5mTo10m
                                        "28", # NIMBLE-MIB::volIoReadLatency10mTo20m
                                        "29", # NIMBLE-MIB::volIoReadLatency20mTo50m
                                        "30", # NIMBLE-MIB::volIoReadLatency50mTo100m
                                        "31", # NIMBLE-MIB::volIoReadLatency100mTo200m
                                        "32", # NIMBLE-MIB::volIoReadLatency200mTo500m
                                        "33", # NIMBLE-MIB::volIoReadLatency500mTomax
                                        "39", # NIMBLE-MIB::volIoWriteLatency0uTo100u
                                        "40", # NIMBLE-MIB::volIoWriteLatency100uTo200u
                                        "41", # NIMBLE-MIB::volIoWriteLatency200uTo500u
                                        "42", # NIMBLE-MIB::volIoWriteLatency500uTo1m
                                        "43", # NIMBLE-MIB::volIoWriteLatency1mTo2m
                                        "44", # NIMBLE-MIB::volIoWriteLatency2mTo5m
                                        "45", # NIMBLE-MIB::volIoWriteLatency5mTo10m
                                        "46", # NIMBLE-MIB::volIoWriteLatency10mTo20m
                                        "47", # NIMBLE-MIB::volIoWriteLatency20mTo50m
                                        "48", # NIMBLE-MIB::volIoWriteLatency50mTo100m
                                        "49", # NIMBLE-MIB::volIoWriteLatency100mTo200m
                                        "50", # NIMBLE-MIB::volIoWriteLatency200mTo500m
                                        "51", # NIMBLE-MIB::volIoWriteLatency500mTomax
                                  ]),
    'snmp_scan_function'        : lambda oid: oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.37447.3.1"),
    'group'                     : "nimble_latency",
    'default_levels_variable'   : "nimble_latency_default_levels",
}


def check_nimble_write_latency(item, params, info):
    timestamp = time.time()
    for line in info:
        if line[0] == item:
            counter_base = "nimble_write_latency.%s." % item
            names = {
                "0-0.1 ms"      : ("0.1",  int(line[3])),
                "0.1-0.2 ms"    : ("0.2",  int(line[4])),
                "0.2-0.5 ms"    : ("0.5",  int(line[5])),
                "0.5-1.0 ms"    : ("1",    int(line[6])),
                "1-2 ms"        : ("2",    int(line[7])),
                "2-5 ms"        : ("5",    int(line[8])),
                "5-10 ms"       : ("10",   int(line[9])),
                "10-20 ms"      : ("20",   int(line[10])),
                "20-50 ms"      : ("50",   int(line[11])),
                "50-100 ms"     : ("100",  int(line[12])),
                "100-200 ms"    : ("200",  int(line[13])),
                "200-500 ms"    : ("500",  int(line[14])),
                "500+ ms"       : ("1000", int(line[15])),
            }
            total_writes = get_rate(counter_base + "total", timestamp, int(line[2]))
            yield 0, "Total Writes: %d" % total_writes
            for key, value in names.iteritems():
                counter = get_rate(counter_base + value[0], timestamp, value[1])
                state, state_count = params[value[0]]
                if counter > state_count:
                    yield state, "%s: %d" % (key, counter)
                else:
                    continue


check_info['nimble_latency.write'] = {
    'inventory_function'        : inventory_nimble_latency,
    'check_function'            : check_nimble_write_latency,
    'service_description'       : 'Volume %s Write IO',
    'group'                     : "nimble_latency",
    'default_levels_variable'   : "nimble_latency_default_levels",
}
