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

mcdata_fcport_speedbits = {"2": '1000000000', "3": '2000000000'}
mcdata_fcport_opstatus = {"1": "1", "2": "2", "3": "testing", "4": "faulty"}


def mcdata_bin_to_64(bin_):
    return str(binstring_to_int(bin_))


def mcdata_fcport_convert_to_if64(info):
    return map(mcdata_fcport_convert_line_to_if64, info)


def mcdata_fcport_convert_line_to_if64(line):
    index, opStatus, speed, txWords64, rxWords64, txFrames64, rxFrames64, c3Discards64, crcs = line

    speed = mcdata_fcport_speedbits.get(speed, '')
    opStatus = mcdata_fcport_opstatus.get(opStatus, 'unknown')
    index = "%02d" % int(index)

    return [
        index,  # 0 ifIndex
        index,  # 1 ifDescr
        '6',  # 2 ifType
        speed,  # 3 ifSpeed
        opStatus,  # 4 ifOperStatus
        mcdata_bin_to_64(rxWords64) * 4,  # 5 ifHCInOctets
        mcdata_bin_to_64(rxFrames64),  # 6 ifHCInUcastPkts
        '0',  # 7 ifHCInMulticastPkts
        '0',  # 8 ifHCInBroadcastPkts
        '0',  # 9 ifInDiscards
        crcs,  # 10 ifInErrors
        mcdata_bin_to_64(txWords64) * 4,  # 11 ifHCOutOctets
        mcdata_bin_to_64(txFrames64),  # 12 ifHCOutUcastPkts
        '0',  # 13 ifHCOutMulticastPkts
        '0',  # 14 ifHCOutBroadcastPkts
        mcdata_bin_to_64(c3Discards64),  # 15 ifOutDiscards
        '0',  # 16 ifOutErrors
        '0',  # 17 ifOutQLen
        index,  # 18 ifAlias
        '',  # 19 ifPhysAddress
    ]


def inventory_mcdata_fcport(info):
    return inventory_if_common(mcdata_fcport_convert_to_if64(info))


def check_mcdata_fcport(item, params, info):
    return check_if_common(item, params, mcdata_fcport_convert_to_if64(info))


check_includes['mcdata_fcport'] = ["if.include"]
check_info["mcdata_fcport"] = {
    'check_function': check_mcdata_fcport,
    'inventory_function': inventory_mcdata_fcport,
    'service_description': 'Port %s',
    'has_perfdata': True,
    'snmp_info': (
        '.1.3.6.1.4.1.289.2.1.1.2.3.1.1',
        [
            1,  # EF-6000-MIB::ef6000PortIndex
            3,  # EF-6000-MIB::ef6000PortOpStatus
            11,  # EF-6000-MIB::ef6000PortSpeed
            67,  # EF-6000-MIB::ef6000PortTxWords64
            68,  # EF-6000-MIB::ef6000PortRxWords64
            69,  # EF-6000-MIB::ef6000PortTxFrames64
            70,  # EF-6000-MIB::ef6000PortRxFrames64
            83,  # EF-6000-MIB::ef6000PortC3Discards64
            65,  # EF-6000-MIB::ef6000PortCrcs
        ]),
    # check if number of network interfaces (IF-MIB::ifNumber.0) is at least 2
    'snmp_scan_function': lambda oid: oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.289."),
    'default_levels_variable': 'if_default_levels',
}
