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


def hitachi_hnas_fc_if_convert_info(info):
    converted = []
    for line in info:
        converted.append(
            map(
                str,
                [
                    "%d%03d" % (int(line[0]), int(line[1])),  # ifIndex
                    line[0] + "." + line[1],  # ifDescr (use ClusterNode.InterfaceIndex)
                    "",  # ifType: do not set port type
                    int(line[3]) * 1000000000,  # ifHighSpeed
                    line[2] == "1" and 1 or 2,  # ifOperStatus (map other states to down)
                    line[4],  # ifHCInOctets
                    0,  # ifHCInUcastPkts
                    0,  # ifHCInMulticastPkts
                    0,  # ifHCInBroadcastPkts
                    line[13],  # ifInDiscards
                    sum(map(int, line[6:13])),  # ifInErrors
                    line[5],  # ifHCOutOctets
                    0,  # ifHCOutUcastPkts
                    0,  # ifHCOutMulticastPkts
                    0,  # ifHCOutBroadcastPkts
                    0,  # ifOutDiscards
                    0,  # ifOutErrors
                    0,  # ifOutQLen
                    line[0] + "." + line[1],  # ifAlias, same as description
                    "",  # ifPhysAddress
                ]))
    return converted


def inventory_hitachi_hnas_fc_if(info):
    converted = hitachi_hnas_fc_if_convert_info(info)
    return inventory_if_common(converted)


def check_hitachi_hnas_fc_if(item, params, info):
    converted = hitachi_hnas_fc_if_convert_info(info)
    return check_if_common(item, params, converted)


check_info["hitachi_hnas_fc_if"] = {
    "check_function": check_hitachi_hnas_fc_if,
    "inventory_function": inventory_hitachi_hnas_fc_if,
    "service_description": "Interface FC %s",
    "has_perfdata": True,
    "snmp_info": (
        ".1.3.6.1.4.1.11096.6.1.1.1.3.6.25.1",
        [
            1,  # fcStatsClusterNode           0
            2,  # fcStatsInterfaceIndex        1
            4,  # fcStatsInterfaceStatus       2
            5,  # fcStatsInterfaceLinkSpeed    3
            7,  # fcStatsInstantaneousInRate   4
            8,  # fcStatsInstantaneousOutRate  5
            13,  # fcStatsSignalLossErrors      6
            14,  # fcStatsBadRXCharErrors       7
            15,  # fcStatsLossSyncErrors        8
            16,  # fcStatsLinkFailErrors        9
            17,  # fcStatsRXEOFErrors          10
            19,  # fcStatsBadCRCErrors         11
            20,  # fcStatsProtocolErrors       12
            18,  # fcStatsDiscardedFrameErrors 13
        ]),
    "snmp_scan_function": hitachin_hnas_scan_function,
    "group": "if",
    "default_levels_variable": "if_default_levels",
    "includes": ["hitachi_hnas.include", "if.include"],
}
