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

# States:
# 1: OK, 2: Crit, 3: Warn

def cisco_wlc_convert(info):
    data = []

    for j in range(0, len(info), 4):
        for i in range(1, len(info[j])):
            if len(info[j][i][1]) > 0:
                data.append((info[j][i][1], info[j+1][i][1], info[j+2][i][1], info[j+3][i][1], info[j][0] ))

    return data

def inventory_cisco_wlc(info):
    data = cisco_wlc_convert(info)
    return [ (x[0], None) for x in data ]

def check_cisco_wlc(item, params, info):
    data = cisco_wlc_convert(info)
    for name, mac, state, model, node in data:
        if name == item:
            if node == None:
                node = ''
            else:
                node = ' (connected to %s)' % node

            state = saveint(state)

            if state == 1:
                return 0, "Accesspoint online" + node
            if state == 3:
                return 1, "Accesspoint state Warning" + node
            if state == 2:
                return 2, "State Critical" + node
            return 3, "Unknown state (%s) " % state

    # Special treatment if this device is missing
    if params:
        for ap_name, ap_state in params.get("ap_name", []):
            if item.startswith(ap_name):
                return ap_state, "Accesspoint 1 not found (State set to %s by rule)" % nagios_state_names[ap_state]

    return 2, "Accesspoint not found"

check_info["cisco_wlc"] = {
    "check_function"        : check_cisco_wlc,
    "inventory_function"    : inventory_cisco_wlc,
    "group"                 : "cisco_wlc",
    "service_description"   : "AP %s",
    "node_info"             : True,
    "snmp_scan_function"    : lambda oid: oid('.1.3.6.1.2.1.1.2.0') in [".1.3.6.1.4.1.9.1.1069",
                                                                        ".1.3.6.1.4.1.14179.1.1.4.3",
                                                                        ".1.3.6.1.4.1.9.1.1645",
                                                                        ".1.3.6.1.4.1.9.1.1279"],
    "snmp_info"             : [( ".1.3.6.1.4.1.14179.2.2.1.1.3", [ OID_END, '' ]),
                               ( ".1.3.6.1.4.1.14179.2.2.1.1.1", [ OID_END, '' ]),
                               ( ".1.3.6.1.4.1.14179.2.2.1.1.6", [ OID_END, '' ]),
                               ( ".1.3.6.1.4.1.14179.2.2.1.1.16", [ OID_END, '' ]),
                               ],
}

