#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

# .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"
                                                                          ),
}
