#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2013             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 from agent:
#<<<winperf_msx_dc_access>>>
#1377600100.83 40156
#2 instances: srvc1dc02.ft.de srvc1dc01.ft.de
#2 1043618 792918 counter
#4 898494 654026 counter
#6 0 0 rawcount
#8 0 0 rawcount
#10 0 0 rawcount
#12 0 0 rawcount

def winperf_msx_dc_access_convert(info):
    i = 2
    data = {}
    servers = info[1][2:] 
    num_srv = len(servers)
    for server in servers:
        data[server] = (int(info[i][2]), int(info[i+num_srv][2]))
        i += 1
    return data

winperf_msx_dc_accss_default_levels = {
    "read_time" : ( 50, 60 ), 
    "search_time" : ( 50, 60 )
}

def inventory_winperf_msx_dc_access(info):
    return [ (x, 'winperf_msx_dc_accss_default_levels') for x in info[1][2:] ]

def check_winperf_msx_dc_access(item, params, info):
    info = winperf_msx_dc_access_convert(info)
    for server, counter in info.items():
        if server == item:
            act = {}
            act['read_time'], act['search_time'] = counter
            now = time.time()
            data = {}
            wrapped = False
            state = 0
            message = []
            perf = []
            for what in ['read_time', 'search_time']:
                try:
                    timediff, data[what] = get_counter("msx_dc_access_%s_%s" % (item, what), now, act[what])
                except:
                    wrapped = True
                    continue
                warn, crit = params[what]
                msg = "%s: AVG %dms for last %ds" % ( what, data[what], timediff )
                if data[what] >= crit:
                    state = 2
                    msg += "(!!) (Levels at %d/%d)" % (warn, crit)
                elif data[what] >= warn:
                    msg += "(!) (Levels at %d/%d)" % (warn, crit)
                    state = max(1, state)
                message.append(msg)
                perf.append((what, data[what], warn, crit ))

            if wrapped:
                return 3, "Initialing counters"
            return state, ", ".join(message), perf
    return 3, "Configuration problem"

check_info["winperf_msx_dc_access"] = {
    "check_function"        : check_winperf_msx_dc_access,
    "inventory_function"    : inventory_winperf_msx_dc_access,
    "service_description"   : "MSX DC %s Access",
    "has_perfdata"          : True,
}

