#!/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-
# ails.  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_queues>>>
# 12947176002.19
# 1 instances: _total
# 10334 0 rawcount
# 10336 810 rawcount
# 10338 0 rawcount
# 10340 0 rawcount
# 10342 0 rawcount
# 10344 0 rawcount
# 10346 810 rawcount
# 10348 810 rawcount
# 10350 821 rawcount
# 10352 821 counter
# 10354 10 rawcount
# 10356 10 counter
# 10358 0 rawcount
# 10360 0 rawcount
# 10362 0 rawcount
# 10364 811 rawcount

# Example output from a Exchange 2013 server:
# <<<winperf_msx_queues>>>
# 1385554029.05 12048
# 4 instances: niedrige_priorität normale_priorität hohe_priorität _total
# 2 0 0 0 0 rawcount
# 4 0 0 0 0 rawcount
# 6 0 0 0 0 rawcount

# For Legacy reasons we need still this var.
msx_queues_default_levels = (500, 2000)

# Default warn/crit levels for length of queues
factory_settings['winperf_msx_queues_factory'] = {
        'levels': ( 500, 2000 ),
}


# Queues to be inventorized (number are relative to counter base)
winperf_msx_queues = {
   "Active Remote Delivery"  :  "2",
   "Retry Remote Delivery"   :  "4",
   "Active Mailbox Delivery" :  "6",
   "Poison Queue Length"     :  "44",
}

winperf_msx_queues_inventory = []

def inventory_winperf_msx_queues(info):
    if len(info) > 1:
        num_instances = int(info[1][0])
        if num_instances > 0:
            # Its possible to set the wanted queues via wato
            inventory_rules = {}
            for rulset in host_extra_conf(g_hostname, winperf_msx_queues_inventory):
                inventory_rules.update(dict(rulset))
            # In case that rules for this host are set,
            # only use this rules
            if inventory_rules:
                queues = inventory_rules
            else:
                queues = winperf_msx_queues
            return [ (name, { "offset" : offset } ) for name, offset in queues.items() ]

def check_winperf_msx_queues(item, params, info):
    # current windows  agents should not produce winperf sections with no data after the header but
    # this ensures compatibility with older agents
    if len(info) < 2 or int(info[1][0]):
        return 3, "no counters available, transport service running?"

    # Old default case:
    if type(params) == tuple:
        warn, crit = params
        offset = winperf_msx_queues.get(item)
    else:
        warn, crit = params['levels']
        if params.get('offset'):
            offset = str(params['offset'])
        # If no offset is set, we assume that still the default counters are used
        else:
            offset = winperf_msx_queues.get(item)

    for line in info[2:]:
        if line[0] == offset:
            length = int(line[-2])
            perfdata = [("length", length, warn, crit)]
            infotext = "%d entries" % length
            if length >= crit:
                return (2, infotext, perfdata)
            elif length >= warn:
                return (1, infotext, perfdata)
            return (0, infotext, perfdata)

    return (3, "counter not found")


check_config_variables.append("winperf_msx_queues")

check_info["winperf_msx_queues"] = {
    'check_function':          check_winperf_msx_queues,
    'inventory_function':      inventory_winperf_msx_queues,
    'service_description':     'Queue %s',
    'has_perfdata':            True,
    "default_levels_variable" : "winperf_msx_queues_factory",
    'group':                   'msx_queues',
}
