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





########creating and/or opening database

if (-e "/tmp/lsniclock")
{
        print "LOCKFILE /tmp/lsniclock detected!!!\n";
}else{
        #create lock file
        open lsniclock, ">/tmp/lsniclock";

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

        my $num = 0;
        if (exists $lsnic_db{"net$num"}) {
                my $var = $lsnic_db{"net$num"};
                $num = $var;
                print "DB detected started where left off at entry $num\n";

        }else{
                print "place holder key net0 does not exist. created!\n";
                $lsnic_db{"net0"} = "$num";
        }

                #close database
                untie(%lsnic_db);
                #remove lock file
                close lsniclock;
                unlink "/tmp/lsniclock";
}

#NET POLLING: 60
#my $success = 0;
#if (-e "/var/www/cgi-bin/lsnet.conf")
#{
#        open lsnetconf, "</var/www/cgi-bin/lsnet.conf";
#        while(<lsnetconf>)
#        {
#                chomp;
#                if(/^NET\sPOLLING\:\s*(\d+)\s*$/)
#                {
#                        $frequency = $1;
#                        $success = 1;
#                        print "success $frequency\n";
#                }
#        }
#        print "$success\n";
#        if($success == 0)
#        {
#                print "ERROR: Wrong lsnet.conf format consult the instructions\n";
#                close lsnetconf;
#                exit;
#        }
#}
#close lsnetconf;

main();
sub main{
        my %value = ();
        my %tenarray = ();
                 
        do{
                %tenarray = minutes();
                if (-e "/tmp/lsmemlock")
                {
                        print "LSNICLOCK FOUND\n";
                        for my $key ( keys %tenarray )
                        {
                                $stupidvar = $key + $value{0};
                                $value{"$stupidvar"} = $tenarray{"$key"};
                        }
                        # 10 were added
                        $value{0} += 10;
                }else{
                        writer(\%tenarray,\%value);
                        #$value{0} = 0;
                        my %value = ();
                }

        }while(1 == 1);
}

######### END MAIN LOOP
           
sub writer {
        my %tenarray = %{$_[0]};
        my %value = %{$_[1]};
        #create lock file
        open lsniclock, ">/tmp/lsniclock";
        #opening database
        tie (%lsnic_db, DB_File, "/tmp/lsnic.db") ||
        die ("Cannot open /tmp/lsnic.db");
                         
        if (exists $value{0})
        {
                for ($i = 1; $i <= $value{0}; $i++)
                {
                        $num = $lsnic_db{"net0"};
                        $num++;
                        $lsnic_db{"net$num"} = $value{$i};
                        $lsnic_db{"net0"} = "$num";
                }
        }

        for ($i = 1; $i <= 10; $i++)
        {  

                $num = $lsnic_db{"net0"};
                $num++;
                $lsnic_db{"net$num"} = $tenarray{$i};
                $lsnic_db{"net0"} = "$num";
        }

        #close database
        untie(%lsnic_db);
        #remove lock file
        close lsniclock;
        unlink "/tmp/lsniclock";
        #print "DATABASE CLOSED\n";

}

######## 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 $poll = 60 / $frequency;
	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);
}

