#!/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
#     inet 127.0.0.1/8 scope host lo
#       valid_lft forever preferred_lft forever
#     inet6 ::1/128 scope host
#       valid_lft forever preferred_lft forever
# 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
#     inet 127.0.0.1/8 scope host lo
#       valid_lft forever preferred_lft forever
#     inet6 ::1/128 scope host
#       valid_lft forever preferred_lft forever
# 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
#     inet 127.0.0.1/8 scope host lo
#       valid_lft forever preferred_lft forever
#     inet6 ::1/128 scope host
#       valid_lft forever preferred_lft forever
# [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

linux_nic_check = "lnx_if"


def _parse_lnx_if_node_info(lines):
    line = lines.next()
    return line[0], line[1:]


def _parse_lnx_if_ipaddress(lines):
    ip_stats = {}
    iface = None
    while True:
        try:
            node, line = _parse_lnx_if_node_info(lines)
        except StopIteration:
            break

        if line == ['[end_iplink]']:
            break

        if line[0].endswith(":") and line[1].endswith(":"):
            # Some (docker) interfaces have suffix "@..." but ethtool does not show this suffix.
            # 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default ...
            # 5: veth6a06585@if4: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue ...
            iface = ip_stats.setdefault((node, line[1][:-1].split("@")[0]), {})
            # The interface flags are summarized in the angle brackets.
            iface["state_infos"] = line[2][1:-1].split(",")
            continue

        if not iface:
            continue

        if line[0].startswith('link/'):
            # link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
            # link/none
            try:
                iface[line[0]] = line[1]
                iface[line[2]] = line[3]
            except IndexError:
                pass

        elif line[0].startswith('inet'):
            if "temporary" in line and "dynamic" in line:
                continue
            # inet 127.0.0.1/8 scope host lo
            # inet6 ::1/128 scope host
            iface.setdefault(line[0], []).append(line[1])
    return ip_stats


def _parse_lnx_if_sections(info):
    ip_stats = {}
    ethtool_stats = {}
    iface = None
    lines = iter(info)
    ethtool_index = 0
    while True:
        try:
            node, line = _parse_lnx_if_node_info(lines)
        except StopIteration:
            break

        if line[0].startswith("[start_iplink]"):
            # Parse 'ip link/address' section (as fallback in case ethtool is missing)
            ip_stats.update(_parse_lnx_if_ipaddress(lines))

        elif len(line) == 2 and len(line[1].strip().split()) >= 16:
            # Parse '/proc/net/dev'
            # IFACE_NAME: VAL VAL VAL ...
            iface = ethtool_stats.setdefault((node, line[0]), {})
            iface.update({"counters": map(int, line[1].strip().split())})
            continue

        elif line[0].startswith('[') and line[0].endswith(']'):
            # Parse 'ethtool' output
            # [IF_NAME]
            #       KEY: VAL
            #       KEY: VAL
            #       ...
            iface = ethtool_stats.setdefault((node, line[0][1:-1]), {})
            ethtool_index += 1
            iface["ethtool_index"] = ethtool_index
            continue

        if iface is not None:
            stripped_line0 = line[0].strip()
            if stripped_line0 == "Address":
                iface[stripped_line0] = ":".join(line[1:]).strip()
            else:
                iface[stripped_line0] = " ".join(line[1:]).strip()

    return ip_stats, ethtool_stats


def parse_lnx_if(info):
    ip_stats, ethtool_stats = _parse_lnx_if_sections(info)

    nic_info = []
    for (node_info, iface_name), iface in sorted(ethtool_stats.items(), key=lambda x: x[0][1]):
        iface.update(ip_stats.get((node_info, iface_name), {}))
        nic_info.append((node_info, iface_name, iface))

    if_table = []
    index = 0
    for node_info, nic, attr in nic_info:
        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("ethtool_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 is 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.
            state_infos = attr.get('state_infos')
            if state_infos is None:
                if ifInOctets > 0:
                    ifOperStatus = 1  # assume up
                else:
                    ifOperStatus = 4  # unknown (NIC has never been used)
            else:
                if "UP" in state_infos:
                    ifOperStatus = 1
                else:
                    ifOperStatus = 2

        raw_phys_address = attr.get("Address", attr.get("link/ether", ""))
        try:
            # We saw interface entries of tunnels for the address
            # is an integer, eg. '1910236'; especially on OpenBSD.
            ifPhysAddress = "".join([chr(int(x, 16)) for x in raw_phys_address.split(":")])
        except ValueError:
            ifPhysAddress = ''

        row = [node_info] + map(str, [
            ifIndex, ifDescr, ifType, ifSpeed, ifOperStatus, ifInOctets, inucast, inmcast, inbcast,
            ifInDiscards, ifInErrors, ifOutOctets, outucast, outmcast, outbcast, ifOutDiscards,
            ifOutErrors, ifOutQLen, ifAlias, ifPhysAddress
        ])

        if_table.append(row)

    return if_table, ip_stats


def inventory_lnx_if(parsed):
    if linux_nic_check == "legacy":
        return []
    # Always exclude dockers veth* interfaces on docker nodes
    if_table = [e for e in parsed[0] if not e[2].startswith("veth")]
    return inventory_if_common(if_table, has_nodeinfo=True)


def check_lnx_if(item, params, parsed):
    return check_if_common(item, params, parsed[0], has_nodeinfo=True)


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