#!/usr/bin/perl
use IO::File;
use DB_File;
use Fcntl;
use Time::localtime;


#creating and/or opening database
tie (%lsnic_db, DB_File, "/tmp/lsnic.db", O_CREAT|O_RDWR, 0644) ||
die ("Cannot create or open /tmp/lsnic.db");


#for ($i=0; $i<61;++$i)
#{
#	# Delete a key/value pair.
#	delete $lsnet_db{"net$i"};
#}
#exit;

my $num = 0;
if (exists $lsnic_db{"net$num"}) {
        my $var = $lsnic_db{"net$num"};
        $num = $var;
	my $test = $lsnic_db{"net$num"};
	print "this is the net0 db entry: $test\n";

        my $var2 = $lsnic_db{"net$var"};

	$var2 =~ /(.{3}\s+.{3}\s+\d{1,2}\s+.{8}\s+\d{4})/;
        print "Last entry was $var on $1\n";
}else{
        print "place holder key net$num does not exist. created!\n";
	$lsnic_db{"net0"} = "$num";
}

#close database
untie(%lsnic_db);

$num++;
my $i = 1;
my $stupidvar = 0;
my %value = ();
$value{0} = 0;
my $stop = 1;


####### MAIN LOOP  WRITES 10 1MIN ENTRIES TO DB ONCE EVERY 10MIN 
do{
	my(%coins) = minutes();
	$i = 1;
        $stop = 1;
        
        while ( $stop == 1)
        {
                if (-e "/tmp/lsniclock")
                {
                        $stop = 1;
                        
                        #if lock file exists copy %coins to %value
         
                        #$value{0} = $value{0} + 10;
                        for my $key ( keys %coins )
                        {
                                $stupidvar = $key + $value{0};
                                
                                $value{$stupidvar} = $coins{$key};
                        }
        
                        #let db writing logic know 10 more are going to be added ($backup needs to be made 0 once these are written)
                        #$backup = $backup + 10;
         
                        # 10 were added
                        $value{0} = $value{0} + 10;
                        
                        #grab a fresh set
                        my(%coins) = minutes();
                        
                } else {
                        #if lock file does not exist cause while loop to exit and write data to db
                        $stop = 0;
                }
        }

        #create lock file
        open lsniclock, ">/tmp/lsniclock";
        #opening database
        tie (%lsnic_db, DB_File, "/tmp/lsnic.db") ||
        die ("Cannot open /tmp/lsnic.db");

        if ($value{0} > 0)
        {
        
                $i = 1;
                        
                do{
                        #$value{$i} =~ /\s*\d+\s+(.{3}\s+.{3}\s+\d{1,2}\s+.{8}\s+\d{4}\s+\d+.?\d*\s+\d+.?\d*\s+\d+.?\d*\s+\d+.?\d*\s+\d+.?\d*\s+\d+.?\d*\s+\d+.?\d*\s+\d+.?\d*)\s*/;
                        #Wed Feb  4 10:03:37 2009 2026.77 437.45 459.96 459.29 1589.32 10722.93 10722.92 0.01
                	$num = $lsnic_db{"net0"};
                	$num++;
                	$lsnic_db{"net$num"} = "$value{$i}";
                	$lsnic_db{"net0"} = "$num";
                        $i++;
                }while($i <= $value{0});

                $value{0} = 0;  
        }
	
	$i = 1;
	do{
		#$coins{$i} =~ /\s*\d+\s+(.{3}\s+.{3}\s+\d{1,2}\s+.{8}\s+\d{4}\s+\d+.?\d*\s+\d+.?\d*\s+\d+.?\d*\s+\d+.?\d*\s+\d+.?\d*\s+\d+.?\d*\s+\d+.?\d*\s+\d+.?\d*)\s*/;
		#Wed Feb  4 10:03:37 2009 2026.77 437.45 459.96 459.29 1589.32 10722.93 10722.92 0.01
		#print "$1";
		#$string = ("$datetime $nin $nout $pin $pout");
		$num = $lsnic_db{"net0"};
		$num++;
		$lsnic_db{"net$num"} = "$coins{$i}";
		$lsnic_db{"net0"} = "$num";
		#$lsnet_db{"mem$1"} = "$2";
		#$test = $lsnet_db{"mem$num"} + 1;
		#$lsnet_db{"mem0"} = "$test";
		#my $test2 = $lsnet_db{"net$num"};
		#print "db entry net$num just wrote: $test2\n";
		$i++;
	}while($i <= 10);

	#close database
	untie(%lsnic_db);

	#remove lock file
        close lsniclock;
        unlink "/tmp/lsniclock";

}while(1 == 1);
######### END MAIN LOOP

######## GRAB 10min WORTH OF DATA FROM SECONDSARRAY AND PUT IT INTO AN ARRAY
sub minutes{
	my (%coins);
	$i = 1;
	#print "in minutes sub! (every 10min) \n";
	#my @coins = ("test","test","test");
	do {
		my($string) = secondsarray();
		#print "string returned to minutes: $string\n";
		$coins{$i} = $string;
		#print "@coins";
		#print "printed from array: $coins{$i}\n";
		$i++;
	} while ($i<=10);
	return (%coins);
}
####### RETURN ARRAY TO MAIN LOOP



###### POLL NETINFO 
sub secondsarray{
	my ($string);
	my $secs = 1;
	my $lastnetin = 0;
	my $lastnetout = 0;
	my $lastpackout = 0;
	my $lastpackin = 0;
	my $nin = 0;
	my $nout = 0;
	my $pin = 0;
	my $pout = 0;

	my($netin,$netout,$packin,$packout) = netinfo();
        $lastnetin = $netin;
        $lastnetout = $netout;
        $lastpackout = $packout;
        $lastpackin = $packin;



	#print "in secondsarray sub! (every 1 min) \n";
		sleep(60);

		my($netin,$netout,$packin,$packout) = netinfo();
		my $datetime = ctime();

		#Network in/out: 1506.89 / 618.31 MB 
		
		$nin = $netin - $lastnetin; ## MB in for last minute, if number is negative then device was reset	
		$nout = $netout - $lastnetout;
		$pin = $packin - $lastpackin;
		$pout = $packout - $lastpackout;
				
		#$lastnetin = $netin; 
		#$lastnetout = $netout;
		#$lastpackout = $packout;
		#$lastpackin = $packin;

	$string = ("$datetime $nin $nout $pin $pout");
	#print "in secondsarray sub! (every 1 min) $string\n";
	return ($string);
}
########### RETURN MB AND PACKETS TRANSFERED OVER LAST MINUTE


sub netinfo{
        my($netin,$netout,$packin,$packout);
        open(NETINFO, "/proc/net/dev") or return undef;
        while(<NETINFO>){
                chomp;
                if(/^\s+(.*?):(\d+)\s+(\d+)\s+\d+\s+\d+\s+\d+\s+\d+\s+\d+\s+\d+\s+(\d+)\s+(\d+)/){
#               # eth0:1062991973 5083742    0    0    0     0          0         0 265559036 2897289    0    0    0     0       0          0

                        $netin = sprintf("%.2f",$2 / 1048576);
			#$netin = sprintf("%.2f",$2);
                        $packin = sprintf("%.2f",$3);
			$netout = sprintf("%.2f",$4 / 1048576);
			#$netout = sprintf("%.2f",$4);
			$packout = sprintf("%.2f",$5);

                }
        }
        close(NETINFO);
        return($netin,$netout,$packin,$packout);
}

