#!/usr/bin/perl -w

# Author:       John Stile <john@stilen.com>
# Usage:  	stilen_network_traffic.pl [ethN] [in/out]
#
# Description:  This will return the netstat RX or TX value for the requested nic.
# netstat does not work because of column overflow into adjacent columns.
#                command IN(RX) 	netstat -i |grep eth0 |awk '{print $3$4 }'
#                command OUT(TX) 	netstat -i |grep eth0 |awk '{print $6$7$8 }'
# the root of the data on a linux box is /proc/net/dev, so I'm using a file handle
#                grab TX data     $bla =~ m/\beth0\:\s*(\d+\s+){9}(\d+)\b.*/;
#                grab RX data     $bla =~ m/\b$ARGV[0]\:\s*\d+\s+(\d+)\b.*/xig ;
#

# determine the number of arguments passed to the program is 2
$arguments=@ARGV;
if ( $arguments != "2" ){
	die "USAGE: stilen_network_traffic.pl [ethN] [in/out]\n";
}
# If called with proper number of arguments (nic name and "in" or "out")
if ( "$ARGV[0]" && "$ARGV[1]" ){
	#
	# Check second agument for "in" or "out", and preform the netstat 
	#
	
	# Receive 
	if ( $ARGV[1] =~ m/^in$/i ){
		open (DEV, "</proc/net/dev") || die;
		while (<DEV>){
			#$response = `netstat -i |grep $ARGV[0] |awk '\{print \$3\$4 \}'`;
			print "$1" if m{
                                                \b           # beginning of line or word boundry
                                                $ARGV[0]\:   # search for the line with the nic 
                                                \s*          # 0 or more spaces
                                                (\d+)          # 1 or more digits from the bytes column
                                                \s+          # 1 or more spaces 
                                                \d+        # 1 or more digits from the packets column or RX-OK
                                                \b           # 1 word boundry
                                                .*           # followed by anything
                                                }xig ;       # Allow commets, case insensitive, global
                }
		close (DEV) || die;
	# Transmit
	} elsif ( $ARGV[1] =~ m/^out$/i ){
                open (DEV, "</proc/net/dev") || die;
                while (<DEV>){

			#$response = `netstat -i |grep $ARGV[0] |awk '\{print \$3\$4 \}'`;
			print "$1" if m{
                                                \b           # beginning of line or word boundry
                                                $ARGV[0]\:   # search for the line with the nic 
                                                \s*          # 0 or more spaces
                                                \d+          # 1 or more digits from the RX bytes column
                                                \s+          # 1 or more spaces 
                                                \d+          # 1 or more digits from the RX packets column or RX-OK
                                                \s+           # 1 word boundry 
						\d+          # 1 or more digits from RX error column
						\s+           # 1 word boundry
                                                \d+          # 1 or more digits from RX drop column 
						\s+           # 1 word boundry
                                                \d+          # 1 or more digits from RX fifo column 
						\s+           # 1 word boundry
                                                \d+          # 1 or more digits from RX frame column 						
						\s+           # 1 word boundry
                                                \d+          # 1 or more digits from RX compressed column 
						\s+           # 1 word boundry
                                                \d+          # 1 or more digits from RX multicast column 						
						\s+           # 1 word boundry
                                                (\d+)        # 1 or more digits from TX bytes column 
						\s+           # 1 word boundry 
						\d+          # 1 or more digits from TX packets column
						\s+           # 1 word boundry
						\d+          # 1 or more digits from drop column
						\s+           # 1 word boundry from 
                                                .*           # followed by anything
                                                }xig ;       # Allow commets, case insensitive, global
                }
                close (DEV) || die;
        } else {
		die #"ERROR: Missing \"IN\" or \"OUT\" aguments.\n";
	}        
}else{
	die #"ERROR: Missing Network Interface name.\n";
}
