#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |          _           _           _       _   __   ________       |
# |       __| |_  ___ __| |__  _ __ | |__   / | /  \ |__ /__  |      |
# |      / _| ' \/ -_) _| / / | '  \| / /   | || () | |_ \ / /       |
# |      \__|_||_\___\__|_\_\_|_|_|_|_\_\   |_(_)__(_)___//_/        |
# |                                            check_mk 1.0.37       |
# |                                                                  |
# | Copyright Mathias Kettner 2009                mk@mathias-kettner |
# +------------------------------------------------------------------+
# 
# This file is part of check_mk 1.0.37.
# 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-
# ails.  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.


inventory_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: 

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(checkname, info):
    if len(info) == 0: return []
    return [ (l[0], '', '""') for l in info[1:] if l[0] != 'lo' and not l[0].startswith("sit") ]


def check_netctr_combined(nic, _no_params, info):
    global counter_indices
    try:
       this_time = int(info[0][0])
    except:
        return (3, "UNKNOWN - no output from mknagios agent")

    # Look for line describing this nic
    for nicline in info[1:]:
        if nicline[0] != nic: continue
        perfdata = []
        infotxt = ""
        try:
            for countername in inventory_netctr_counters:
                index = counter_indices[countername]
                value = int(nicline[index + 1])
                timedif, items_per_sec = get_counter( "netctr." + nic + "." + countername, this_time, value)
                perfdata.append( ( countername, "%dc" % value ) )
                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))
            return (0, "OK" + infotxt, perfdata)
                
        except:
            return (3, "UNKNOWN - invalid output from plugin")

    return (3, "UNKNOWN - nic is not present")


def inventory_netctr(checkname, info):
    inventory = []
    for nicline in info[1:]:
        nic = nicline[0]
        if nic != "lo" and not nic.startswith("sit"):
            for countername in inventory_netctr_counters:
                inventory.append( (nic + '.' + countername, '', None) )
    return inventory
    

def check_netctr(item, params, info):
    try:
       this_time = int(info[0][0])
    except:
        return (3, "UNKNOWN - no output from mknagios agent")
        
    netdev, countername = item.split(".")
    counters = False
    for i in info[1:]:
        if i[0] == netdev:
            counters = i[1:]
    if not counters:
        return (3, "UNKNOWN - no such network device")            

    try:
        counter_index = counter_indices[countername]
    except KeyError:
        return (3, "UNKNOWN - invalid counter name '%s'" % (countername,))
      
    this_val = int(counters[counter_index])
    timedif, items_per_sec = get_counter( "netctr." + item, this_time, this_val)
    perfdata = [ (countername, "%dc" % this_val ) ]
    return (0, "OK - %.0f/s (in last %d secs)" % (items_per_sec, timedif), perfdata)
    
check_info['netctr'] = (check_netctr, "NIC %s", 1,  no_inventory_possible)
check_info['netctr.combined'] = (check_netctr_combined, "NIC %s counters", 1,  inventory_netctr_combined )
