#!/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.15983.1.1.4.2.1.1.2.1 AP1
# .1.3.6.1.4.1.15983.1.1.4.2.1.1.4.1 1
# .1.3.6.1.4.1.15983.1.1.4.2.1.1.8.1 
# .1.3.6.1.4.1.15983.1.1.4.2.1.1.17.1 990625700
# .1.3.6.1.4.1.15983.1.1.4.2.1.1.26.1 1
# .1.3.6.1.4.1.15983.1.1.4.2.1.1.27.1 3
# .1.3.6.1.4.1.15983.1.1.3.1.7.1.3.1 "00 0C E6 XX XX XX "
# .1.3.6.1.4.1.15983.1.1.3.1.7.1.5.1 1
# .1.3.6.1.4.1.15983.1.1.3.1.7.1.9.1 1

ap_state = {
    '1' :   ( 0, 'AP is up' ),
    '2' :   ( 1, 'AP is down' ),
    }

ap_avail = {
    '1' :   ( 3, 'availibility status unknown' ),
    '2' :   ( 2, 'availibility status is disabled' ),
    '3' :   ( 0, 'availibility status is enabled' ),
    }

def parse_fortinet_controller_aps(info):
    new_info = []
    for ap_table in info[0]:
        new_element = []
        for client_table in info[1]:
            if ap_table[1] == client_table[2]:
                if new_element == []:
                    new_element = [ ap_table, client_table ]
                else:
                    new_element.append(client_table)
        if new_element != []:
            new_info.append(new_element)
    return new_info


def inventory_fortinet_controller_aps(info):
    for element in info:
        yield element[0][1], {}


def check_fortinet_controller_aps(item, params, info):
    for element in info:
        if item == element[0][1]:
            client_count_24 = 0
            client_count_5 = 0
            for client in element[1:]:
                if client[1] == '1':
                    client_count_24 += 1
                elif client[1] == '2':
                    client_count_5 += 1
                else:
                    continue
            perfdata = [
                    ( 'uptime', element[0][3] ),
                    ( '5ghz_clients', client_count_5 ),
                    ( '24ghz_clients', client_count_24 ),
                    ]
            yield 0, 'AP %s' % element[0][1], perfdata
            if element[0][4] == '1' and element[0][5] == '3':
                yield 0, 'Status: online'
            else:
                yield ap_state[element[0][4]]
                yield ap_avail[element[0][5]]
            yield 0, 'connected Clients (2,4 ghz / 5 Ghz): %s/%s' % ( client_count_24, client_count_5 )
            if element[0][2] != '':
                yield 0, 'located at %s' % element[0][2]


check_info['fortinet_controller_aps'] = {
    'parse_function'            : parse_fortinet_controller_aps,
    'inventory_function'        : inventory_fortinet_controller_aps,
    'check_function'            : check_fortinet_controller_aps,
    'service_description'       : 'AP %s',
    'has_perfdata'              : True,
    'snmp_info'                 : [('.1.3.6.1.4.1.15983.1.1.4.2.1.1', [
                                  '2',    # mwApDescr
                                  '4',    # mwApID
                                  '8',    # mwApLocation
                                  '17',   # mwApUpTime
                                  '26',   # mwApOperationalState
                                  '27'    # mwApAvailabilityStatus
                                  ] ),
                                  ( '.1.3.6.1.4.1.15983.1.1.3.1.7.1', [
                                  '3',    # Client Mac
                                  '5',    # Iface Name
                                  '9'     # AP_ID
                                  ]
                                  )],
    'snmp_scan_function'        : lambda oid : '.1.3.6.1.4.1.15983' in oid('.1.3.6.1.2.1.1.2.0').lower(),
}
