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

adva_fsp_if_inventory_porttypes = ['1', '6', '56']
adva_fsp_if_inventory_portstates = ['1']
adva_fsp_if_inventory_uses_description = True
adva_fsp_if_inventory_uses_alias = False

adva_fsp_if_operstates = {
    "1": ("up", 0),
    "2": ("down", 2),
    "3": ("testing", 1),
    "4": ("unknown", 3),
    "5": ("dormant", 1),
    "6": ("notPresent", 2),
    "7": ("lowerLayerDown", 2),
}

adva_fsp_if_adminstates = {
    "1": ("up", 0),
    "2": ("down", 2),
    "3": ("testing", 1),
}


@network_interface_scan_registry.register
def snmp_scan_function_adva_fsp_if(oid):
    return oid(".1.3.6.1.2.1.1.1.0") == "Fiber Service Platform F7"


def inventory_adva_fsp_if(info):
    inventory = []
    for line in info:
        if line[2] in adva_fsp_if_inventory_porttypes and line[
                3] in adva_fsp_if_inventory_portstates:
            if adva_fsp_if_inventory_uses_description and line[1] != "":
                item = line[1]
            elif adva_fsp_if_inventory_uses_alias and line[5] != "":
                item = line[5]
            else:
                item = line[0]
            inventory.append((item, {}))
    return inventory


def adva_fsp_if_getindex(item, info):
    for line in info:
        if adva_fsp_if_inventory_uses_description:
            if line[1] == item:
                return line[0]
        elif adva_fsp_if_inventory_uses_alias:
            if line[5] == item:
                return line[0]
        else:
            return item


def check_adva_fsp_if(item, params, info):
    index = adva_fsp_if_getindex(item, info)
    for line in info:
        if line[0] == index:
            state = 0
            admintxt, adminstate = adva_fsp_if_adminstates[line[3]]
            state = max(adminstate, state)
            opertxt, operstate = adva_fsp_if_operstates[line[4]]
            state = max(operstate, state)
            if state == 2:
                statesym = "(!!)"
            elif state == 1:
                statesym = "(!)"
            else:
                statesym = ""
            infotext = "Admin/Operational State: %s/%s %s" % (admintxt, opertxt, statesym)

            perfdata = []
            for power, name in [(line[6], "output"), (line[7], "input")]:
                try:
                    sym = ""
                    climits = ""
                    fpower = float(power) / 10.0
                    if params.get("limits_%s_power" % name):
                        lower, upper = params.get("limits_%s_power" % name)
                        climits = "%s:%s" % params.get("limits_%s_power" % name)
                        if fpower < lower or fpower > upper:
                            state = 2
                            sym = "(!!)"
                    infotext += ", %s Power: %.1f%s" % (name.title(), fpower, sym)
                    perfdata.append(("%s_power" % name, "%.1fdBm" % fpower, "", climits))

                except:
                    if not re.match("S", item):  # if no service interface and no power parameter
                        infotext += ", %s Power: n.a. (!)" % name.title()
                        state = max(1, state)

            return (state, infotext, perfdata)

    return (3, "no such interface found")


check_info['adva_fsp_if'] = {
    "inventory_function": inventory_adva_fsp_if,
    "check_function": check_adva_fsp_if,
    "service_description": 'Interface %s',
    "has_perfdata": True,
    "group": "adva_ifs",
    "snmp_info": (
        ".1.3.6.1",
        [
            "2.1.2.2.1.1",  # ifIndex
            "2.1.2.2.1.2",  # ifDescr
            "2.1.2.2.1.3",  # ifType
            "2.1.2.2.1.7",  # ifAdminStatus
            "2.1.2.2.1.8",  # ifOperStatus
            "31.1.1.1.18",  # ifAlias
            "4.1.2544.1.11.2.4.3.5.1.4",  # opticalIfDiagOutputPower
            "4.1.2544.1.11.2.4.3.5.1.3",  # opticalIfDiagInputPower
        ],
    ),
    "includes": ["if.include"],
    "snmp_scan_function": snmp_scan_function_adva_fsp_if,
}
