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

# Example output from agent:
# <<<ibm_svc_host:sep(58)>>>
# 0:h_esx01:2:4:degraded
# 1:host206:2:2:online
# 2:host105:2:2:online
# 3:host106:2:2:online

def inventory_ibm_svc_host(info):
    return [(None, {})]

def check_ibm_svc_host(item, params, info):
    degraded = 0
    offline  = 0
    active   = 0
    inactive = 0
    other    = 0

    if params == None:
        # Old inventory rule until version 1.2.7
        # params were None instead of empty dictionary
        params = { 'always_ok': False }

    for line in info:
        if line[4] == 'degraded':
            degraded += 1
        elif line[4] == 'offline':
            offline += 1
        elif line[4] == 'active' or line[4] == 'online':
            active += 1
        elif line[4] == 'inactive':
            inactive += 1
        else:
            other +=1

    if 'always_ok' in params:
        # Old configuration rule
        # This was used with only one parameter always_ok until version 1.2.7
        perfdata = [ ("active",   active),
                     ("inactive", inactive),
                     ("degraded", degraded),
                     ("offline",  offline),
                     ("other",    other),
                   ]
        yield 0, "%s active, %s inactive" % (active, inactive), perfdata

        if degraded > 0:
            yield (not params['always_ok'] and 1 or 0), "%s degraded" % degraded
        if offline > 0:
            yield (not params['always_ok'] and 2 or 0), "%s offline" % offline
        if other > 0:
            yield (not params['always_ok'] and 1 or 0), "%s in an unidentified state" % other
    else:
        warn, crit = params.get('active_hosts', (None, None))

        if crit != None and active <= crit:
            yield 2, "%s active" % active
        elif warn != None and active <= warn:
            yield 1, "%s active" % active
        else:
            yield 0, "%s active" % active

        for ident, value in [ ('inactive', inactive),
                              ('degraded', degraded),
                              ('offline',  offline),
                              ('other',    other),
                            ]:
            warn, crit = params.get(ident+'_hosts', (None, None))

            if crit != None and value >= crit:
                state = 2
            if warn != None and value >= warn:
                state = 1
            else:
                state = 0
            yield state, "%s %s" % (value, ident), [ (ident, value, warn, crit) ]

check_info["ibm_svc_host"] = {
    "check_function"        : check_ibm_svc_host,
    "inventory_function"    : inventory_ibm_svc_host,
    "service_description"   : "Hosts",
    "has_perfdata"          : True,
    "group"                 : "ibm_svc_host",
}

