#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

linux_nic_check = "lnx_if"

# levels for warning/critical on error rate - in percentage of total packets!
netctr_default_params = (0.01, 0.1)

netctr_counters = [
    'rx_bytes', 'tx_bytes', 'rx_packets', 'tx_packets', 'rx_errors', 'tx_errors', 'tx_collisions'
]

# Check counters from network interfaces
# Item is devicename.countername, eg,
# eth0.tx_collisions. Available are:

netctr_counter_indices = {
    # Receive
    'rx_bytes': 0,
    'rx_packets': 1,
    'rx_errors': 2,
    'rx_drop': 3,
    'rx_fifo': 4,
    'rx_frame': 5,
    'rx_compressed': 6,
    'rx_multicast': 7,
    # Transmit
    'tx_bytes': 8,
    'tx_packets': 9,
    'tx_errors': 10,
    'tx_drop': 11,
    'tx_fifo': 12,
    'tx_collisions': 13,
    'tx_carrier': 14,
    'tx_compressed': 15
}


def inventory_netctr_combined(info):
    if linux_nic_check != "legacy":
        return []
    if len(info) == 0:
        return []
    return [(l[0], '', 'netctr_default_params')
            for l in info[1:]
            if l[0] != 'lo' and not l[0].startswith("sit")]


def check_netctr_combined(nic, params, info):
    try:
        warn, crit = params
    except ValueError:
        warn, crit = (0.01, 0.1)

    this_time = int(info[0][0])

    # Look for line describing this nic
    for nicline in info[1:]:
        if nicline[0] != nic:
            continue
        perfdata = []
        infotxt = ""
        problems_per_sec = 0.0
        packets_per_sec = 0.0
        for countername in netctr_counters:
            index = netctr_counter_indices[countername]
            value = int(nicline[index + 1])
            items_per_sec = get_rate("netctr." + nic + "." + countername, this_time, value)
            perfdata.append((countername, "%dc" % value))

            if countername in ["rx_errors", "tx_errors", "tx_collisions"]:
                problems_per_sec += items_per_sec
            elif countername in ["rx_packets", "tx_packets"]:
                packets_per_sec += items_per_sec
            if countername == 'rx_bytes':
                infotxt += ' - Receive: %.2f MB/sec' % (float(items_per_sec) / float(1024 * 1024))
            elif countername == 'tx_bytes':
                infotxt += ' - Send: %.2f MB/sec' % (float(items_per_sec) / float(1024 * 1024))

        error_percentage = 0.0
        if problems_per_sec > 0:
            error_percentage = (problems_per_sec / packets_per_sec) * 100.0  # fixed: true-division
            infotxt += ", error rate %.4f%%" % error_percentage
        if error_percentage >= crit:
            return (2, infotxt, perfdata)
        elif error_percentage >= warn:
            return (1, infotxt, perfdata)
        return (0, infotxt, perfdata)

    return (3, "NIC is not present")


check_config_variables.append("netctr_counters")

check_info["netctr.combined"] = {
    'check_function': check_netctr_combined,
    'inventory_function': inventory_netctr_combined,
    'service_description': 'NIC %s counters',
    'has_perfdata': True,
}
