#!/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 parse_ibm_svc_host(info):
    dflt_header = [
        u'id',
        u'name',
        u'port_count',
        u'iogrp_count',
        u'status',
        u'site_id',
        u'site_name',
        u'host_cluster_id',
        u'host_cluster_name',
    ]
    return parse_ibm_svc_with_header(info, dflt_header)


def check_ibm_svc_host(item, params, parsed):
    if params is None:
        # Old inventory rule until version 1.2.7
        # params were None instead of empty dictionary
        params = {'always_ok': False}

    degraded = 0
    offline = 0
    active = 0
    inactive = 0
    other = 0
    for rows in parsed.itervalues():
        for data in rows:
            status = data['status']
            if status == 'degraded':
                degraded += 1
            elif status == 'offline':
                offline += 1
            elif status in ['active', 'online']:
                active += 1
            elif status == '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 is not None and active <= crit:
            yield 2, "%s active" % active
        elif warn is not 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 is not None and value >= crit:
                state = 2
            if warn is not None and value >= warn:
                state = 1
            else:
                state = 0
            yield state, "%s %s" % (value, ident), [(ident, value, warn, crit)]


check_info["ibm_svc_host"] = {
    "parse_function": parse_ibm_svc_host,
    "check_function": check_ibm_svc_host,
    "inventory_function": discover_single,
    "service_description": "Hosts",
    "includes": ['ibm_svc.include'],
    "has_perfdata": True,
    "group": "ibm_svc_host",
}
