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

# <<<ruckus_spot_ap:sep(9)>>>
# [band]  1
# 1-28    1
# 1-25    1
# 1-2 1
# [band]  2
# 1-28    1
# 1-16    1
# 1-9     1


ruckus_spot_ap_bands = {
    "1": "2.4 GHz",
    "2": "5 GHz",
}
ruckus_spot_ap_default_levels = ((None, None), (None, None))

def parse_ruckus_spot_ap(info):
    bands        = {}
    current_band = None
    for line in info:
        if line[0] == "[band]":
            current_band = line[1]
            bands.setdefault(current_band, [])
        else:
            bands[current_band].append(line)
    return bands

def inventory_ruckus_spot_ap(parsed):
    for band in parsed.keys():
        if band in ruckus_spot_ap_bands:
            yield ruckus_spot_ap_bands[band], "ruckus_spot_ap_default_levels"

def check_ruckus_spot_ap(item, params, parsed):
    inverse_map = dict((v, k) for k, v in ruckus_spot_ap_bands.items())
    band = parsed.get(inverse_map.get(item))
    if not band:
        return

    (drift_warn, drift_crit), (down_warn, down_crit) = params
    state = 0
    info  = 0
    perfdata = [("ap_devices_total", len(band))]

    extra_info = []
    for what, ap_state, warn, crit in [ ("drifted",        "2", drift_warn, drift_crit),
                                        ("not responding", "0", down_warn,  down_crit) ]:
        problems = len([x for x in band if x[1] == ap_state])
        if crit and problems >= crit:
            state = 2
        elif warn and problems >= warn:
            state = 1

        if problems:
            extra_info.append("%d %s%s" % (problems, what, state_markers[state]))
        perfdata.append(("ap_devices_%s" % what.replace(" ", "_"), problems, warn, crit))

    info = "%d devices" % len(band)
    if extra_info:
        info += " (%s)" % " / ".join(extra_info)

    yield state, info, perfdata

check_info["ruckus_spot_ap"] = {
    "parse_function"      : parse_ruckus_spot_ap,
    "inventory_function"  : inventory_ruckus_spot_ap,
    "check_function"      : check_ruckus_spot_ap,
    "service_description" : "Ruckus Spot Access Points %s",
    "group"               : "ruckus_ap",
    "has_perfdata"        : True
}


