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

# Example output from agent:

# <<<lnx_if>>>
# [start_iplink]
# 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default
#     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
# 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
#     link/ether 00:27:13:b4:a9:ec brd ff:ff:ff:ff:ff:ff
# 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DORMANT group default qlen 1000
#     link/ether 00:21:6a:10:8e:b8 brd ff:ff:ff:ff:ff:ff
# [end_iplink]
# <<<lnx_if:sep(58)>>>
#    lo:   4520   54  0  0  0  0  0  0    4520  54    0   0   0   0   0   0
#  eth0:      0    0  0  0  0  0  0  0    1710   5    0   0   0   0   0   0
#  eth1:  78505  555  0  0  0  0  0  0  132569  523   0   0   0   0   0    0
# [lo]
#         Link detected: yes
# [eth0]
#         Speed: 65535Mb/s
#         Duplex: Unknown! (255)
#         Auto-negotiation: on
#         Link detected: no
#         Address: de:ad:be:af:00:01
# [eth1]
#         Speed: 1000Mb/s
#         Duplex: Full
#         Auto-negotiation: on
#         Link detected: yes


check_includes['lnx_if'] = [ "if.include" ]

linux_nic_check = "lnx_if"

def if_lnx_extract_nic_info(info):
    nic_info = {}
    current_nic = None
    index = 0
    lines = iter(info)
    iplink_stats = {}
    while True:
        try:
            line = lines.next()
        except StopIteration:
            break

        # This extra info from 'ip link' is used as fallback in case ethtool is missing
        if line[0].startswith("[start_iplink]"):
            iplink_stats = {}
            try:
                while True:
                    line = lines.next()
                    if line[0].startswith("[end_iplink]"):
                        line = lines.next()
                        break
                    # There some cases with an additional line. These are currently unused
                    # but need to be handled anyway. This particular line contains information
                    # provided by cisco about neighors (CDP)
                    if line[0].startswith("alias"):
                        continue
                    # Each interface in this block is represented by two lines
                    status_info = line
                    link_info   = lines.next() # currently unused
                    try:
                        nic_name = status_info[1][:-1]
                        iplink_stats.setdefault(nic_name, { "extra_info": status_info[2][1:-1].split(",") })
                        iplink_stats[nic_name].update(dict(zip(status_info[3::2], status_info[4::2])))
                    except: # In case of parse errors we simply ignore these lines
                        pass
            except StopIteration:
                break

        # Be careful! On clustered hosts we have more than one perf-counters section
        # and ethtool section. This needs to be handled. Sadly we have no section
        # headers. Try to detect it by data format.
        if line[0].startswith('['):
            current_nic = line[0][1:-1]
            index += 1
            nic_info[current_nic]['index'] = index
            # The iplink_stats are only used within the perf-counters
            # The (optional) ethtool section invalidates this info, otherwise it would
            # be incorrectly reused in a followup section of another cluster node
            # When the ethtool section is missing the data is also reset by the next [start_iplink]
            iplink_stats = {}
        elif len(line) == 2 and len(line[1].split()) >= 16:
            # This looks like a perf-counter line
            nic = line[0]
            nic_info[nic] = { "counters": map(int, line[1].split()) }
            if nic in iplink_stats:
                nic_info[nic]['iplink_stats'] = iplink_stats[nic]
        elif current_nic:
            # ethtool data line
            nic_info[current_nic][line[0].strip()] = ":".join(line[1:]).strip()

    return nic_info


def if_lnx_convert_to_if64(info):
    nic_info = if_lnx_extract_nic_info(info)

    if_table = []
    index = 0
    for nic, attr in nic_info.items():
        counters = attr.get("counters", [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])
        index += 1
        ifIndex = attr.get("index",index)
        ifDescr = nic
        ifAlias = nic

        # Guess type from name of interface
        if nic == "lo":
            ifType = 24
        else:
            ifType = 6

        # Compute speed
        speed_text = attr.get("Speed")
        if speed_text == None:
            ifSpeed = ''
        else:
            if speed_text == '65535Mb/s': # unknown
                ifSpeed = ''
            elif speed_text.endswith("Kb/s"):
                ifSpeed = int(speed_text[:-4]) * 1000
            elif speed_text.endswith("Mb/s"):
                ifSpeed = int(speed_text[:-4]) * 1000000
            elif speed_text.endswith("Gb/s"):
                ifSpeed = int(speed_text[:-4]) * 1000000000
            else:
                ifSpeed = ''

        # Performance counters
        ifInOctets = counters[0]
        inucast = counters[1] + counters[7]
        inmcast = counters[7]
        inbcast = 0
        ifInDiscards = counters[3]
        ifInErrors = counters[2]
        ifOutOctets = counters[8]
        outucast = counters[9]
        outmcast = 0
        outbcast = 0
        ifOutDiscards = counters[11]
        ifOutErrors = counters[10]
        ifOutQLen = counters[12]

        # Link state from ethtool. If ethtool has no information about
        # this NIC, we set the state to unknown.
        ld = attr.get("Link detected")
        if ld == "yes":
            ifOperStatus = 1
        elif ld == "no":
            ifOperStatus = 2
        else:
            # No information from ethtool. We consider interfaces up
            # if they have been used at least some time since the
            # system boot.
            iplink_stats = attr.get("iplink_stats")
            if iplink_stats:
                if "UP" in iplink_stats.get("extra_info", []) or iplink_stats.get("state") == "UP":
                    ifOperStatus = 1
                elif iplink_stats.get("state") == "DOWN":
                    ifOperStatus = 2
                else:
                    ifOperStatus = 4
            else:
                if ifInOctets > 0:
                    ifOperStatus = 1 # assume up
                else:
                    ifOperStatus = 4 # unknown (NIC has never been used)

        if attr.get("Address"):
            ifPhysAddress = "".join([chr(int(x, 16)) for x in attr.get("Address", "").split(":")])
        else:
            ifPhysAddress = ''

        if_table.append(map(str, [
          ifIndex, ifDescr, ifType, ifSpeed, ifOperStatus,
          ifInOctets, inucast, inmcast, inbcast, ifInDiscards,
          ifInErrors, ifOutOctets, outucast, outmcast, outbcast,
          ifOutDiscards, ifOutErrors, ifOutQLen, ifAlias, ifPhysAddress ]))

    return if_table

def inventory_lnx_if(info):
    if linux_nic_check == "legacy":
        return []
    return inventory_if_common(if_lnx_convert_to_if64(info))

def check_lnx_if(item, params, info):
    return check_if_common(item, params, if_lnx_convert_to_if64(info))


check_info["lnx_if"] = {
    'check_function':          check_lnx_if,
    'inventory_function':      inventory_lnx_if,
    'service_description':     'Interface %s',
    'has_perfdata':            True,
    'group':                   'if',
    'default_levels_variable': 'if_default_levels',
}
