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


# Anzahl_Message Channelname MaxMessages_Moeglich Status"
# <<<websphere_mq_channels>>>
# 0   CHANNEL(C000052.C000051)  5000  Unknown
# 0   CHANNEL(C000052.CATSOS.03)  5000  RUNNING
# 0   CHANNEL(C000052.DXUZ001)  5000  RUNNING
# 0   CHANNEL(C000052.N000011)  5000  RUNNING
# 0   CHANNEL(C000052.SI0227450.T1)  10000  RUNNING
# 0   CHANNEL(C000052.SOX10.T1)  10000  STOPPED
# 0   CHANNEL(C000052.SV1348520.T1)  5000  RUNNING
# 0   CHANNEL(C000052.SV2098742.T1)  5000  Unknown


factory_settings["websphere_mq_channels_default_levels"] = {
    'message_count': ( 900, 1000 ),
    'status': {
        'RUNNING': 0,
        'STOPPED': 1
    }
}


def parse_websphere_mq_channels(info):
    parsed = {}
    for line in info:
        if len(line) == 2:
            messages, max_messages = 0, 0
            channel_name   = line[0]
            channel_status = line[1]
        elif len(line) == 4:
            messages       = int(line[0])
            channel_name   = line[1]
            max_messages   = int(line[2])
            channel_status = line[3]
        else:
            continue

        parsed.setdefault(channel_name, {
            "messages"       : messages,
            "max_messages"   : max_messages,
            "channel_status" : channel_status,
        })
    return parsed


def inventory_websphere_mq_channels(parsed):
    for channel_name in parsed:
        yield channel_name, {}


def check_websphere_mq_channels(item, params, parsed):
    if isinstance(params, tuple):
        params = { 'message_count' : params,
                   'status' : { 'RUNNING': 0,
                                'STOPPED': 1, }}

    if item in parsed:
        data = parsed[item]
        messages       = data["messages"]
        max_messages   = data["max_messages"]
        channel_status = data["channel_status"]

        state = params['status'].get(channel_status, params['status'].get('other', 2))
        yield state, "Channel status: %s" % channel_status, []

        infotext = "%d/%d messages" % (messages, max_messages)
        state    = 0
        if params['message_count']:
            warn, crit = params['message_count']
            if messages >= crit:
                state = 2
            elif messages >= warn:
                state = 1
            if state > 0:
                infotext += " (warn crit at %d/%d messages)" % (warn, crit)
        else:
            warn, crit = None, None
        yield state, infotext, [('messages', messages, warn, crit, 0, max_messages)]


check_info["websphere_mq_channels"] = {
    "parse_function"            : parse_websphere_mq_channels,
    "check_function"            : check_websphere_mq_channels,
    "inventory_function"        : inventory_websphere_mq_channels,
    "service_description"       : "MQ Channel %s",
    "has_perfdata"              : True,
    "default_levels_variable"   : "websphere_mq_channels_default_levels",
    "group"                     : "websphere_mq_channels",
}

