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


# .1.3.6.1.4.1.14823.2.2.1.5.2.1.4.1.3.0.36.108.194.147.166 AP-NAME --> WLSX-WLAN-MIB::wlanAPName.'.$l...'
# .1.3.6.1.4.1.14823.2.2.1.5.2.1.4.1.19.0.36.108.194.147.166 2 --> WLSX-WLAN-MIB::wlanAPStatus.'.$l...'
# .1.3.6.1.4.1.14823.2.2.1.5.2.1.4.1.2.0.36.108.194.147.166 iii.jjj.kkk.lll --> WLSX-WLAN-MIB::wlanAPIpAddress.'.$l...'
# .1.3.6.1.4.1.14823.2.2.1.5.2.1.4.1.4.0.36.108.194.147.166 GROUP --> WLSX-WLAN-MIB::wlanAPGroupName.'.$l...'
# .1.3.6.1.4.1.14823.2.2.1.5.2.1.4.1.5.0.36.108.194.147.166 .1.3.6.1.4.1.14823.1.2.34 --> WLSX-WLAN-MIB::wlanAPModel.'.$l...'
# .1.3.6.1.4.1.14823.2.2.1.5.2.1.4.1.6.0.36.108.194.147.166 XYZ --> WLSX-WLAN-MIB::wlanAPSerialNumber.'.$l...'
# .1.3.6.1.4.1.14823.2.2.1.5.2.1.4.1.22.0.36.108.194.147.166 2 --> WLSX-WLAN-MIB::wlanAPUnprovisioned.'.$l...'
# .1.3.6.1.4.1.14823.2.2.1.5.2.1.4.1.32.0.36.108.194.147.166 --> WLSX-WLAN-MIB::wlanAPSysLocation.'.$l...'


def inventory_aruba_wlc_aps(info):
    # Here we discover only provisioned aps which are up
    return [ (line[0], None) for line in info \
              if line[1] == "1" and line[2] != "1" ]


def check_aruba_wlc_aps(item, params, info):
    map_state = {
        "1" : (0, "up"),
        "2" : (2, "down"),
    }
    for ap_name, ap_status, ap_unprovisioned, ap_ip, ap_group, \
        ap_model, ap_serial, ap_sysloc in info:
        if item == ap_name:
            state, state_readable = map_state[ap_status]
            infotext = "Status: %s" % state_readable
            if ap_group:
                infotext += ", Group: %s" % ap_group
            if ap_sysloc:
                infotext += ", System location: %s" % ap_sysloc
            yield state, infotext
            if ap_unprovisioned == "1":
                yield 1, "Unprovisioned: yes"


check_info["aruba_wlc_aps"] = {
    "inventory_function"    : inventory_aruba_wlc_aps,
    "check_function"        : check_aruba_wlc_aps,
    "service_description"   : "AP %s",
    "has_perfdata"          : True,
    # If you make changes here in snmp_info, don't forget to make
    # these changes in the related inventory plugin, too.
    "snmp_info"             : ( ".1.3.6.1.4.1.14823.2.2.1.5.2.1.4.1",[
                                  "3",    # wlanAPName
                                  "19",   # wlanAPStatus
                                  "22",   # wlanAPUnprovisioned
                                  "2",    # wlanAPIpAddress
                                  "4",    # wlanAPGroupName
                                  "5",    # wlanAPModel
                                  "6",    # wlanAPSerialNumber
                                  "32",   # wlanAPSysLocation
                              ]),
    "snmp_scan_function"    : lambda oid: oid('.1.3.6.1.2.1.1.2.0').startswith(".1.3.6.1.4.1.14823.1.1"),
}
