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

factory_settings['ibm_svc_host_default_levels'] = {
    'always_ok'  : False
}

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


def check_ibm_svc_host_state(state, aw_ok):
    if aw_ok:
        return 0
    else:
        return state

def check_ibm_svc_host(item, params, info):
    degraded = 0
    offline  = 0
    active   = 0
    inactive = 0
    other    = 0
    status   = 0
    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

    perfdata = [ ("active",   active),
                 ("inactive", inactive),
                 ("degraded", degraded),
                 ("offline",  offline),
                 ("other",    other),
               ]
    yield 0, "%s hosts active, %s inactive" % (active, inactive), perfdata

    aw_ok = params['always_ok'] # Needed in function check_ibm_svc_host_state
    if degraded > 0:
        yield check_ibm_svc_host_state(1, aw_ok), "%s degraded" % degraded
    if offline > 0:
        yield check_ibm_svc_host_state(2, aw_ok), "%s offline" % offline
    if other > 0:
        yield check_ibm_svc_host_state(1, aw_ok),  "%s in an unidentified state(!)" % other


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

