#!/usr/bin/perl

#
# 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 #"ERROR: Missing argument, 2 required\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 
	if ( $ARGV[1] =~ m/^in$/i ){
		open (DEV, "</proc/net/dev") || die;
		while (<DEV>){
			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;
	} elsif ( $ARGV[1] =~ m/^out$/i ){
                open (DEV, "</proc/net/dev") || die;
                while (<DEV>){
                        #print "$2" if m{
                        #                        \b           # beginning of line or word boundry
                        #                        $ARVG[0]\:   # search for the line with the nic
                        #                        \s*          # 0 or more spaces
                        #                        (\d+\s+){9}  # 9 sets of 1 or more digits followed by one or more spaces
                        #                        (\d+)        # 1 or more digits from the packets column or TX-OK
                        #                        \b           # 1 word boundry
                        #                        .*           # followed by anything
                        #                        }xi ;       # Allow commets, case insensitive, global
			print $2 if  m/\beth0\:\s*(\d+\s+){8}(\d+)\b.*/;
                }
                close (DEV) || die;
        } else {
		die #"ERROR: Missing \"IN\" or \"OUT\" aguments.\n";
	}        
	chomp  $response;
        $response = $response;
        $$db = ":0";
	print "$response";
}else{
	die #"ERROR: Missing Network Interface name.\n";
}
