#!/usr/bin/perl
use IO::File;
use GD;
use Time::localtime;
use LWP::Simple;
use LWP::UserAgent;
use DB_File;
use Fcntl;
use POSIX qw(ceil);
require "dates.pl";


##### For Hour format conversion 00:59:59 to 12am to 11pm
my %hourhash = ( 
        00 => '12',
        23 => '11',
        22 => '10',
        21 => '9',
        20 => '8',
        19 => '7',
        18 => '6',  
        17 => '5',
        16 => '4',
        15 => '3',
        14 => '2',
        13 => '1',
        12 => '12',   
        11 => '11',
        10 => '10',
        9 => '9',        
        8 => '8',
        7 => '7',
        6 => '6',
        5 => '5',
        4 => '4',
        3 => '3',
        2 => '2',
        1 => '1',
   );


##########Determine number of cores in system
$coresdetected = 0;
open(CPUINFO, "/proc/stat") or return undef;
while(<CPUINFO>){
        chomp;
        if(/^cpu\d+/)
        {
                $coresdetected++;
        }
}
close(CPUINFO);
#print "CORES detected: $coresdetected\n";
#--$coresdetected;
#print "decremented: $coresdetected\n";
##############################################




my($memsize,$memfree,$buffers,$cached,$memused,$swaptotal,$swapfree,$swapused) = meminfo();
my($cpu,$cache,$cores,$mhz) = cpuinfo();
my($netin,$netout) = netinfo();
my($days) = uptime();

sub meminfo{
	my($memsize,$memfree,$buffers,$cached,$memused,$swaptotal,$swapfree,$swapused);
	open(MEMINFO, "/proc/meminfo") or return undef;
	while(<MEMINFO>){
		chomp;
		if(/^MemTotal:\s+(\d+)/){
			$memsize = sprintf("%.2f",$1/1024);
		}elsif(/^MemFree:\s+(\d+)/){
			$memfree = sprintf("%.2f",$1/1024);
			#Buffers:         82248 kB
			#Cached:        1314520 kB

		}elsif(/^Buffers:\s+(\d+)/){
			$buffers = sprintf("%.2f",$1/1024);
                }elsif(/^Cached:\s+(\d+)/){
                        $cached = sprintf("%.2f",$1/1024);

		}elsif(/^SwapTotal:\s+(\d+)/){
			$swaptotal = sprintf("%.2f",$1/1024);
		}elsif(/^SwapFree:\s+(\d+)/){
			$swapfree = sprintf("%.2f",$1/1024);
		}
   	}
	my $memused = sprintf("%.2f" ,$memsize - $memfree);
	my $swapused = sprintf("%.2f" ,$swaptotal - $swapfree);
	close(MEMINFO);
	return($memsize,$memfree,$buffers,$cached,$memused,$swaptotal,$swapfree,$swapused);
}

sub cpuinfo{
	my($cpu,$cache,$cores,$mhz);
	open(CPUINFO, "/proc/cpuinfo") or return undef;
	while(<CPUINFO>){
		chomp;
                if(/^model name\s+\:\s+(.*?)$/){
			#model name	: Intel(R) Core(TM)2 Quad  CPU   Q9450  @ 2.66GHz
                        $cpu = $1;
                }elsif(/^cache size\s+:\s+([\d\.]*)/){
			#cache size	: 6144 KB
                        $cache = $1;
                }elsif(/^cpu cores\s+:\s+(\d+)/){
			#cpu cores	: 4
                        $cores = $1;
			#cpu MHz		: 3304.131
		}elsif(/^cpu MHz\s+:\s+([\d\.]*)/){
			$mhz = $1;
                }
        }
	close(CPUINFO);
        if ($cores == ""){
                $cores = 1;
        }

	return($cpu,$cache,$cores,$mhz);
}

sub netinfo{
	my($netin,$netout);
	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+/){
			# eth0:1062991973 5083742    0    0    0     0          0         0 265559036 2897289    0    0    0     0       0          0

			$netin = sprintf("%.2f",$2 / 1048576);
			$netout = sprintf("%.2f",$3 / 1048576);

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

sub uptime{
	my ($days);
	open (UPTIME, "/proc/uptime") or return undef;
	while (<UPTIME>){
		chomp;
		if(/(\d+\.\d+)\s+\d+\.+\d+/){
			#288359.76 15415.53
			$days = sprintf("%.2f",$1 / 86400);
		}
	}
	close (UPTIME);
	return ($days);
}

chomp(my $uname = `uname -sr`);




############CREATE BASIC CHART LAYOUT PRIOR TO DATA LINES
my $stop = 1;

while ( $stop == 1)
{
        if (-e "/tmp/lsmemlock")
        {
                $stop = 1;
                sleep(1);
        } else {
                $stop = 0;
        }
}

#create lock file
open lsmemlock, ">/tmp/lsmemlock";

#opening database
tie (%lsmem_db, DB_File, "/tmp/lsmem.db") ||
die ("Cannot open /tmp/lsmem.db");

my $im = new GD::Image(1620,155);

# allocate some colors
$white = $im->colorAllocate(255,255,255);
$black = $im->colorAllocate(0,0,0);
$red = $im->colorAllocate(255,0,0);
$blue = $im->colorAllocate(0,0,255);
$green = $im->colorAllocate(0,255,0);
$yellow = $im->colorAllocate(255,255,0);
$orange = $im->colorAllocate(255,128,0);
$purple = $im->colorAllocate(255,0,255);
$brown = $im->colorAllocate(128,64,0);
        
$im->fill(0,0,$white);

#$im->string(gdSmallFont,400,50,"Memory Usage",$black);
$im->line(50,10,50,110,$black);
$im->line(10,110,1500,110,$black);
$im->string(gdSmallFont,5,5,"MB",$black);
$im->string(gdSmallFont,50,215,"Current Swap Usage",$black);


#coordinate
my $num3 = 110;
#points
my $num4 = 0;
my $tenth = $swaptotal / 10;


for ($i = 0; $i < 11; ++$i)
{
        $im->string(gdSmallFont,20,$num3,$num4,$black);
        $im->line(20,$num3,30,$num3,$black);
        $im->setStyle($yellow,$yellow,$yellow,$yellow,$blue,$blue,$blue,$blue,gdTransparent,gdTransparent);
        $im->dashedLine(30,$num3,1500,$num3,gdStyled);
        $num4 = $num4 + $tenth;
	$num4 = ceil($num4);
        $num3 = $num3 - 10;
}

my $coordinate = 1500;
my $var2 = $lsmem_db{"mem0"};
my $difference = 0;
my $lastdate = 0;
my $lastentry = $var2;
my $i = 1;
my $stagger = 130;

#plot the 1440min 1min/px lines on chart
for ($i = 1440; $i >= 0; --$i)
{
        #if the db entry exists
        if (exists $lsmem_db{"mem$lastentry"})
        {
                #if db entry is >= mem1 key not counting place-holder mem0
                if ($lastentry >= 1)
                {
                       my $var = $lsmem_db{"mem$lastentry"};
                        #my($minsince) = &dates($var);
                       $var =~ /.{3}\s+(.{3})\s+(\d{1,2})\s+(\d{1,2}).(\d{1,2}).(\d{1,2})\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*/;
  
                        my $hourstring = sprintf("%d",$3);
                        my $minstring = sprintf("%d",$4);
                        my $secstring = sprintf("%d",$5);
                        my($minsince) = dates($var);

                        my $dbswapsize = sprintf("%.2f",$12);
                        my $dbswapused = sprintf("%.2f",$14); 

			#total used
			$num3 = $dbswapused / $dbswapsize;
			$num3 = $num3 * 100;
			$num3 = 100 - $num3;
			$num3 = $num3 + 10;

                        #if the most recent entry, drawl lines labels
                        if ($lastentry == $var2)
                        {
	                        $im->string(gdSmallFont,1540,10,Free,$black);
        	                $im->string(gdSmallFont,1540,40,used,$black);
                	        $im->line(1500,10,1540,10,$black);
                        	$im->line(1500,$num3,1540,40,$black);
                                my $lastmin = $minsince;
                                $lastdate = $lastmin - 1442;
                        }
                        #not older than 1day from first entry, or nothing gets plotted
                        if ($lastdate <= $minsince )
                        {
                                #if new entry is older than previous, everything but first entry
                                if ($minsince < $lastmin)
                                {
                                        $difference = $lastmin - 2;
                                        #$difference = ceil($difference);
                                        #if new entry is older than or equal to 2min ago
                                
                                        if ($minsince <= $difference)
                                        {
                                                $difference = $lastmin - $minsince;
                                                $difference = ceil($difference);
                                                #leave a blank line for each 1min lost
                                                for ($m = 1; $m < $difference; $m++)
                                                {
                                                        $coordinate--; 
                                                }
                                        }
                                }
                                if ($coordinate > 59)
                                {
					#total used
					$im->line($coordinate,$num3,$coordinate,110,$red);
					#free
					$im->line($coordinate,10,$coordinate,$num3,$green);
				}

                                my $modulo4 = $coordinate % 60;
                                if ($modulo4 == 0)
                                {
	                                my $datestring = $hourhash{$hourstring};
                                        my($isam) = &ampm($hourstring);
                                        if ($isam == 1)
                                        {  
 	                                       $datestring = "$datestring:$4:$5 AM";
                                         }else{
        	                               $datestring = "$datestring:$4:$5 PM";  
                                         }
                                         if ($stagger == 130)
                                         {
                  	                       $stagger = 120;
                                         }else{
                        	                 $stagger = 130;
                                         }
                                         $im->string(gdSmallFont,$coordinate - 30,$stagger,$datestring,$black);
                                         $im->line($coordinate,110,$coordinate,$stagger,$black);
				}
			}
                        $lastmin = $minsince;
                }
        }
$coordinate--;
$lastentry--;                                                 
}

#close database
untie(%lsmem_db);
                                                
#remove lock file
close lsmemlock;
unlink "/tmp/lsmemlock";                        
                                 
                         
                        
open FILE1, "> /var/www/htdocs/lsnet/swap.png" or die $!;
binmode FILE1;
print FILE1 $im->png;
close(FILE1);

############################################################
############# CREATE 2nd mem graph for last 24hrs
############################################################

my $stop = 1;

while ( $stop == 1)
{
	if (-e "/tmp/lsmemlock")
	{
		$stop = 1;
		sleep(1);
	} else {
		$stop = 0;
	}
}

#create lock file
open lsmemlock, ">/tmp/lsmemlock";

#opening database   
tie (%lsmem_db, DB_File, "/tmp/lsmem.db") ||
die ("Cannot open /tmp/lsmem.db");
#86400 seconds per day

my $im = new GD::Image(1620,255);


# allocate some colors
$white = $im->colorAllocate(255,255,255);
$black = $im->colorAllocate(0,0,0);
$red = $im->colorAllocate(255,0,0);
$blue = $im->colorAllocate(0,0,255);
$green = $im->colorAllocate(0,255,0);
$yellow = $im->colorAllocate(255,255,0);
$orange = $im->colorAllocate(255,128,0);
$purple = $im->colorAllocate(255,0,255);
$brown = $im->colorAllocate(128,64,0);


$im->fill(0,0,$white);
#$im->string(gdSmallFont,400,50,"Memory Usage",$black);  
$im->line(50,10,50,210,$black);
$im->line(10,210,110,210,$black);
$im->string(gdSmallFont,5,5,"MB",$black);
$im->string(gdSmallFont,50,240,"Hours",$black);
#use POSIX qw(ceil);

#coordinate
my $num3 = 210;
#points
my $num4 = 0;
my $tenth = $memsize / 10;


#use POSIX qw(ceil);

for ($i = 0; $i < 11; ++$i)
{
        $im->string(gdSmallFont,25,$num3,$num4,$black);
        $im->line(25,$num3,30,$num3,$black);
        $im->setStyle($yellow,$yellow,$yellow,$yellow,$blue,$blue,$blue,$blue,gdTransparent,gdTransparent);
        $im->dashedLine(30,$num3,1500,$num3,gdStyled);
        $num4 = $num4 + $tenth;
        $num4 = ceil($num4);
        $num3 = $num3 - 20;
}



my $lastentry = $lsmem_db{"mem0"};
my $hournum = 60;
#for ($i = 1; $i < 25; ++$i)
#{
#       $im->string(gdSmallFont,$hournum,220,$i,$black);
#        $im->line($hournum,210,$hournum,220,$black);
#        #$im->setStyle($yellow,$yellow,$yellow,$yellow,$blue,$blue,$blue,$blue,gdTransparent,gdTransparent);
#        #$im->dashedLine(30,$num3,110,$num3,gdStyled);
#        #$num4 = $num4 + $tenth;
#        #$num4 = ceil($num4);
#        #$num3 = $num3 - 20;
#	$hournum = $hournum + 60;
#	#my($memsize,$memfree,$buffers,$cached,$memused,$swaptotal,$swapfree,$swapused) = meminfo();
#	#Wed Feb  4 10:03:37 2009 2026.77 437.45 459.96 459.29 1589.32 10722.93 10722.92 0.01
#	#86400 seconds in day
#	#every 10min =144pixels/day@1px/10min  (600s/10m)
#}

#my $num = 1;

my $coordinate = 1500;
$hournum = 60;
my $var2 = $lsmem_db{"mem0"};
my $difference = 0;
my $lastdate = 0;
my $i = 1;
my $stagger = 230;

#plot the 1440min 1min/px lines on chart
for ($i = 1440; $i >= 0; --$i)
{
	#if ($coordinate > 59)
	#{
	#	my $modulo1 = $coordinate % 60;
	#	if ($modulo1 == 0)
	#	{
	#		my $datestring = $hours{$3};
	#		my($isam) = &ampm($3);
	#		if ($isam == 1)
	#		{
	#			$datestring = "$datestring $4 $5 AM";
	#		}else{
	#			$datestring = "$datestring $4 $5 PM";
	#		}
	#		$im->string(gdSmallFont,$coordinate,220,$datestring,$black);
	#		$im->line($coordinate,210,$coordinate,220,$black);
	#	}
	#}

	#if the db entry exists
	if (exists $lsmem_db{"mem$lastentry"}) 
	{
		#if db entry is >= mem1 key not counting place-holder mem0 
		if ($lastentry >= 1)
		{
			my $var = $lsmem_db{"mem$lastentry"};
			#my($minsince) = &dates($var);	
			$var =~ /.{3}\s+(.{3})\s+(\d{1,2})\s+(\d{1,2}).(\d{1,2}).(\d{1,2})\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*/;
			my $hourstring = sprintf("%d",$3);
			my $minstring = sprintf("%d",$4);
			my $secstring = sprintf("%d",$5);
			my($minsince) = dates($var);
			#my $passvar = "$1 $2 $3 $4 $5 $6";
			#my($minsince) = &dates($passvar);
			#Sat Feb  7 14:19:32 2009
			#$line  =~ /.{3}\s+(.{3})\s+(\d{1,2})\s+(\d{2}).(\d{2}).(\d{2})\s+(\d{4})\s+/;
			#my($minsince) = &dates($var);
			my $dbmemsize = sprintf("%.2f",$7);
			my $dbmemfree = sprintf("%.2f",$8);
			my $dbbuffers = sprintf("%.2f",$9);
			my $dbcached = sprintf("%.2f",$10); 
			my $dbmemused = sprintf("%.2f",$11);
			#$var  =~ /.{3}\s+(.{3})\s+(\d{1,2})\s+(\d{2}).(\d{2}).(\d{2})\s+(\d{4})\s+/;
			#my($minsince) = &dates($var);

			#total used
			$num3 = $dbmemused / $dbmemsize;
			$num3 = $num3 * 200;
			$num3 = 200 - $num3;
			$num3 = $num3 + 10; 

			#cached
			$num4 = $dbcached/$dbmemsize;
			$num4 = $num4 * 200;
			$num4 = 200 - $num4;
			$num4 = $num4 + 10;

			#buffers used
			$num5 = $dbbuffers + $dbcached;
			$num5 = $num5 / $dbmemsize;
			$num5 = $num5 * 200;
			$num5 = 200 - $num5;
			$num5 = $num5 + 10;
		
			#if the most recent entry, drawl lines labels
                       	if ($lastentry == $var2)
                       	{
                               	$im->string(gdSmallFont,1540,10,Free,$black);
                               	$im->string(gdSmallFont,1540,70,Other,$black);
                               	$im->string(gdSmallFont,1540,130,Buffers,$black);
                               	$im->string(gdSmallFont,1540,190,Cached,$black);
				#$im->string(gdSmallFont,1400,225,$minsince,$black);
                               	$im->line(1500,10,1540,10,$black);
                               	$im->line(1500,$num3,1540,70,$black);
                               	$im->line(1500,$num5,1540,130,$black);
                               	$im->line(1500,$num4,1540,190,$black);
				my $lastmin = $minsince;
				$lastdate = $lastmin - 1442;
                       	}
			
			#not older than 1day from first entry, or nothing gets plotted
			if ($lastdate <= $minsince )
			{
				#if new entry is older than previous, everything but first entry
				if ($minsince < $lastmin) 
				{
					$difference = $lastmin - 2;
					#$difference = ceil($difference);
					#if new entry is older than or equal to 2min ago
					
					if ($minsince <= $difference)
					{
						$difference = $lastmin - $minsince;
						$difference = ceil($difference);
						#leave a blank line for each 1min lost
						for ($m = 1; $m < $difference; $m++) 
						{ 
							$coordinate--;	
						}
					}
				}

				if ($coordinate > 59)
				{
					#$im->string(gdSmallFont,$coordinate,225,$minsince,$black);
					#total used
					$im->line($coordinate,$num3,$coordinate,210,$red);
					#buffers
					$im->line($coordinate,$num4,$coordinate,$num5,$yellow);
					#cached
					$im->line($coordinate,210,$coordinate,$num4,$orange);
					#free
					$im->line($coordinate,10,$coordinate,$num3,$green);
					my $modulo4 = $coordinate % 60;
					if ($modulo4 == 0)
					{
						#$monthcur = $months1{$monthcur};
						my $datestring = $hourhash{$hourstring};
						#$monthcur = $months1{$monthcur};
						my($isam) = &ampm($hourstring);
						if ($isam == 1)
						{
							$datestring = "$datestring:$4:$5 AM";
						}else{
							$datestring = "$datestring:$4:$5 PM";
						}
						if ($stagger == 230)
						{
							$stagger = 220;
						}else{
							$stagger = 230;
						}
						$im->string(gdSmallFont,$coordinate - 30,$stagger,$datestring,$black);
						$im->line($coordinate,210,$coordinate,$stagger,$black);
					}

				}
			}
			$lastmin = $minsince;
		}
	}
$coordinate--; 
$lastentry--;

}

#close database  
untie(%lsmem_db);

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



open FILE1, "> /var/www/htdocs/lsnet/mem2.png" or die $!;
binmode FILE1;
print FILE1 $im->png;
close(FILE1);  


################ 3RD IMAGE NETWORK USAGE
$stop = 1;

while ( $stop == 1 )
{
        if (-e "/tmp/lsniclock")
        {
                $stop = 1;
                sleep(1);
        } else {
                $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");

my $im = new GD::Image(1600,255);


# allocate some colors
$white = $im->colorAllocate(255,255,255);
$black = $im->colorAllocate(0,0,0);
$red = $im->colorAllocate(255,0,0);
$blue = $im->colorAllocate(0,0,255);
$green = $im->colorAllocate(0,255,0);
$yellow = $im->colorAllocate(255,255,0);
$orange = $im->colorAllocate(255,128,0);
$purple = $im->colorAllocate(255,0,255);
$brown = $im->colorAllocate(128,64,0);


$im->fill(0,0,$white);
#$im->string(gdSmallFont,400,50,"Network Usage",$black);
$im->line(50,10,50,210,$black);
$im->line(10,210,110,210,$black);
$im->string(gdSmallFont,1,1,"KB/s",$black);
$im->string(gdSmallFont,50,240,"Hours",$black);
$im->filledRectangle(1520,65,1540,85,$red);
$im->string(gdSmallFont,1545,85,"Upload",$red);
$im->filledRectangle(1520,105,1540,125,$green);
$im->string(gdSmallFont,1545,125,"Download",$green);  



#use POSIX qw(ceil);

#coordinate
$num3 = 210;
#points
$num4 = 0;

my $max = 0;
my $oldmax = 0;
my $lastentry = $lsnic_db{"net0"};

#get the max for chart height
for ($i = 1440; $i > 0; --$i)  
{
        if (exists $lsnic_db{"net$lastentry"})
        {
                if ($lastentry >= 1)
                {
			$var = $lsnic_db{"net$lastentry"};
                        $var =~ /.{3}\s+(.{3})\s+(\d{1,2})\s+(\d\d).(\d\d).(\d\d)\s+(\d{4})\s+(\d+.?\d*)\s+(\d+.?\d*)\s+(\d+.?\d*)\s+(\d+.?\d*)\s*/;
			#Fri Feb  6 12:13:52 2009 0.0699999999999932 0.00999999999999091 855 45
			#$string = ("$datetime $nin $nout $pin $pout");
                        my $netin = sprintf("%.2f",$7);
                        my $netout = sprintf("%.2f",$8);
                        #my $packin = sprintf("%.2f",$9);
                        #my $packout = sprintf("%.2f",$10);

			my $max = sprintf("%.2f",$7 + $8);
			if ($max > $oldmax)
			{
				$oldmax = $max;
			}

		}

	}
	$lastentry--;
}

#$oldmax = 100000;
#convert from MB/Min to KB/Sec
$oldmax = $oldmax / 60; 
$oldmax = $oldmax * 1024;

$tenth = $oldmax / 10;
#highest of $nin + $nout from last 1440 entries 


#use POSIX qw(ceil);

for ($i = 0; $i < 11; ++$i)
{
        $im->string(gdSmallFont,25,$num3,$num4,$black);
        $im->line(25,$num3,30,$num3,$black);
        $im->setStyle($yellow,$yellow,$yellow,$yellow,$blue,$blue,$blue,$blue,gdTransparent,gdTransparent);
        $im->dashedLine(30,$num3,1500,$num3,gdStyled);
        $num4 = $num4 + $tenth;
        $num4 = ceil($num4);   
        $num3 = $num3 - 20;
}

$hournum = 60;
#for ($i = 1; $i < 25; ++$i)
#{
#        $im->string(gdSmallFont,$hournum,220,$i,$black);
#        $im->line($hournum,210,$hournum,220,$black);
#        $hournum = $hournum + 60;
#        #86400 seconds in day
#        #every 10min =144pixels/day@1px/10min  (600s/10m)
#}
        

$lastentry = $lsnic_db{"net0"};                        
$coordinate = 1500;
$var2 = $lsnic_db{"net0"};
$difference = 0;
$lastdate = 0;
$stagger = 230;

#plot the 1440min 1min/px lines on chart
for ($i = 1440; $i >= 0; --$i)
{
        if (exists $lsnic_db{"net$lastentry"})
        {               
                if ($lastentry >= 1)
                {
                        $var = $lsnic_db{"net$lastentry"};
			#my($minsince) = &dates($var);
			$var =~ /.{3}\s+(.{3})\s+(\d{1,2})\s+(\d{1,2}).(\d{1,2}).(\d{1,2})\s+(\d{4})\s+(\d+.?\d*)\s+(\d+.?\d*)\s+(\d+.?\d*)\s+(\d+.?\d*)\s*/;
                        my $hourstring = sprintf("%d",$3);
                        my $minstring = sprintf("%d",$4);
                        my $secstring = sprintf("%d",$5);
			my($minsince) = &dates($1, $2, $3, $4, $5, $6);
                        #$var =~ /.{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*/;
			#my $passvar = "$1 $2 $3 $4 $5 $6";
			#my($minsince) = &dates($var);
                        my $netin = sprintf("%.2f",$7/60*1024);
                        my $netout = sprintf("%.2f",$8/60*1024);
                        #my $packin = sprintf("%.2f",$9);
                        #my $packout = sprintf("%.2f",$10);
                        #$var  =~ /.{3}\s+(.{3})\s+(\d{1,2})\s+(\d{2}).(\d{2}).(\d{2})\s+(\d{4})\s+/;
                        #my($minsince) = &dates($var);



			$max = $netin + $netout;
			
			#netin = green (on bottom)  netout = red (on top)
			
			#top of netout
                        $num3 = $max/$oldmax;
                        $num3 = $num3 * 200;
                        $num3 = 200 - $num3;
                        $num3 = $num3 + 10;

                        #top of netin
                        $num4 = $netin/$oldmax;
                        $num4 = $num4 * 200;
                        $num4 = 200 - $num4;
                        $num4 = $num4 + 10;
                        
			#only if it is the most recent db entry drawl labels first
                        if ($lastentry == $var2)
                        {
                        #        $im->string(gdSmallFont,1540,10,Free,$black);
                        #        $im->string(gdSmallFont,1540,70,Other,$black);
                        #        $im->string(gdSmallFont,1540,130,Buffers,$black);
                        #        $im->string(gdSmallFont,1540,190,Cached,$black);
                        #        $im->line(1500,10,1540,10,$black);
                        #        $im->line(1500,$num3,1540,70,$black);
                        #        $im->line(1500,$num5,1540,130,$black);
                        #        $im->line(1500,$num4,1540,190,$black);
                                $lastmin = $minsince;
                                $lastdate = $lastmin - 1442;


                        }

                        #not older than 1day from first entry, or nothing gets plotted
                        if ($lastdate <= $minsince )
                        {
                                #if new entry is older than previous, everything but first entry
                                if ($minsince < $lastmin)
                                {
                                        $difference = $lastmin - 2;
					#$difference = ceil($difference);
                                        #if new entry is older than or equal to 2min ago
 
                                        if ($minsince <= $difference)
                                        {
                                                $difference = $lastmin - $minsince;
                                                $difference = ceil($difference);
                                                #leave a blank line for each 1min lost
                                                for ($m = 1; $m < $difference; $m++)
                                                {
                                                        $coordinate--;
                                                }
                                        }
                                }
				if ($coordinate > 59)
				{
			                #net out line
		               		$im->line($coordinate,$num3,$coordinate,210,$red);
                	        	#net in line
                        		$im->line($coordinate,$num4,$coordinate,210,$green);
                        		#blank space above
                        		#$im->line($coordinate,10,$coordinate,$num3,$green);
                                        my $modulo4 = $coordinate % 60;
                                        if ($modulo4 == 0)
                                        {
                                                my $datestring = $hourhash{$hourstring};
                                                my($isam) = &ampm($hourstring);
                                                if ($isam == 1)
                                                {
                                                        $datestring = "$datestring:$4:$5 AM";
                                                }else{
                                                        $datestring = "$datestring:$4:$5 PM";
                                                }
                                                if ($stagger == 230)
                                                {
                                                        $stagger = 220;
                                                }else{
                                                        $stagger = 230;
                                                }
                                                $im->string(gdSmallFont,$coordinate - 30,$stagger,$datestring,$black);
                                                $im->line($coordinate,210,$coordinate,$stagger,$black);
                                        }

				}
			}
			$lastmin = $minsince;
                }
        }
$coordinate--;
$lastentry--;
}

#close database  
untie(%lsnic_db);

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

open FILE1, "> /var/www/htdocs/lsnet/net.png" or die $!;
binmode FILE1;
print FILE1 $im->png;
close(FILE1);


###############################
######4th IMAGE FOR CPU USAGE
################################

my $stop = 1;

while ( $stop == 1)
{
        if (-e "/tmp/lscpulock")
        {
                $stop = 1;
                sleep(1);
        } else {
                $stop = 0;
        }
}

#create lock file
open lscpulock, ">/tmp/lscpulock";

#opening database
tie (%lscpu_db, DB_File, "/tmp/lscpu.db") ||
die ("Cannot open /tmp/lscpu.db");

my $im = new GD::Image(1620,955);

# allocate some colors
$white = $im->colorAllocate(255,255,255);
$black = $im->colorAllocate(0,0,0);
$red = $im->colorAllocate(255,0,0);
$blue = $im->colorAllocate(0,0,255);
$green = $im->colorAllocate(0,255,0);
$yellow = $im->colorAllocate(255,255,0);
$orange = $im->colorAllocate(255,128,0);
$purple = $im->colorAllocate(255,0,255);
$brown = $im->colorAllocate(128,64,0);
$grey = $im->colorAllocate(128,128,128);

$im->fill(0,0,$white);


my $coordinate = 1500;
my $var2 = $lscpu_db{"cpu0"};
my $lastdate = 0;
my $lastentry = $var2;  

$im->setStyle($yellow,$yellow,$yellow,$yellow,$blue,$blue,$blue,$blue,gdTransparent,gdTransparent);

#########find section height for processes
my $processhgt = 100;
for ($i = 1440; $i >= 0; --$i)
{       
	if (exists $lscpu_db{"cpu$lastentry"})
        { 
                if ($lastentry >= 1)
                {

			my $var = $lscpu_db{"cpu$lastentry"};
			$var =~ /.{3}\s+(.{3})\s+(\d{1,2})\s+(\d{1,2}).(\d{1,2}).(\d{1,2})\s+(\d{4})\s+(\d+.\d+)/;
			if ($7 > $processhgt)
			{
				$processhgt = $7;
			}
		}
	}
$lastentry--;
}
$tenth = $processhgt / 10;
$tenth = ceil($tenth);

my $bullshit2 = 110;
my $bullshit = $tenth;
while ($bullshit <= $processhgt)
{
	$bullshit2 = $bullshit2 - 10;
        $im->string(gdSmallFont,25,$bullshit2,$bullshit,$black);
                
        #if ($bullshit == 100)
        #{
        #	$im->line(30,$bullshit2,1500,$bullshit2,$black);
        #}else{
                $im->dashedLine(30,$bullshit2,1500,$bullshit2,gdStyled);
        #}
        $bullshit += $tenth;
}

###########

my $ylines = 800 / $coresdetected;
my $boxes = 800 / $ylines;

my $y10 = $ylines + 110;
my $x10 = 110;

$tenth = $ylines / 10;

#$im->line(50,10,50,110,$black);
$im->line(50,110,1500,110,$black);
$im->line(50,10,50,910,$black);

$im->line(50,910,1500,910,$black);

#######drawl horizontal dashed lines and % usage labels
for ($i = 1; $i <= $boxes; $i++ )
{	
	my $bullshit = 10;
	my $bullshit2 = ($i * $ylines) + 110;
	while ($bullshit <= 100)
	{
		$bullshit2 = $bullshit2 - $tenth;
		if ($bullshit == 50)
		{
			$thecorenumber = $i - 1;
			$im->string(gdGiantFont,25,$bullshit2,"CPU$thecorenumber",$black);
		}else{
        		$im->string(gdSmallFont,25,$bullshit2,"$bullshit\%",$black);
		}
		if ($bullshit == 100)
		{
			$im->line(30,$bullshit2,1500,$bullshit2,$black);
		}else{
        		$im->dashedLine(30,$bullshit2,1500,$bullshit2,gdStyled);
		}
		$bullshit += 10;		  
	}
}

#my $ylines = 400 / $coresdetected;
#my $boxes = 400 / $ylines;

#my $y10 = $ylines + 10;
#my $x10 = 10;



#for ($i = 0; $i < 11; ++$i)
#{
#        $im->string(gdSmallFont,25,$num3,$1,$black);
#        $im->line(25,$num3,30,$num3,$black);
#        $im->setStyle($yellow,$yellow,$yellow,$yellow,$blue,$blue,$blue,$blue,gdTransparent,gdTransparent);
#        $im->dashedLine(30,$num3,1500,$num3,gdStyled);
#        $num4 = $num4 + $tenth;
#        #$num4 = ceil($num4);
#        $num3 = $num3 - 20;
#}



for ($plotnums = 1; $plotnums <= $coresdetected; $plotnums++)
{
	my $coordinate = 1500;
	$hournum = 60;
	my $var2 = $lscpu_db{"cpu0"};
	my $difference = 0;
	my $lastdate = 0;
	my $i = 1;
	my $stagger = 930;
	my $lastentry = $var2;



	#####determine lowest pixel for this core's box
	#my $boxes = 400 / $ylines;
	my $lowest = 110 + ($ylines * $plotnums);
	my $highest = $lowest - $ylines;
	#my $stagger = $lowest - 20;

	#plot the 1440min 1min/px lines on chart
	for ($i = 1440; $i >= 0; --$i)
	{	##dealing with most recent data first
        	if (exists $lscpu_db{"cpu$lastentry"})
        	{
                	#if db entry is >= cpu1 key not counting place-holder mem0
                	if ($lastentry >= 1)
                	{
                        	my $var = $lscpu_db{"cpu$lastentry"};
				#Sun Feb 15 18:17:22 2009 130.00+0.00 100.00 0.00 0.00 0.00 0.00 0.00+0.02 99.93 0.02 0.02 0.00 0.02 0.00+0.30 99.56 0.13 0.00 0.00 0.00 0.00+0.98 98.68 0.10 0.20 0.00 0.00 0.03
				###split by + and grab only the core by current $plotnum
				#my %current = split(/\+/, $var);
                        	#$var =~ /\s*.{3}\s+(.{3})\s+(\d{1,2})\s+(\d{2}).(\d{2}).(\d{2})\s+(\d{4}).(\d+.\d+)\s+(\d+.\d+)\s+(\d+.\d+)\s+(\d+.\d+)\s+(\d+.\d+)\s+(\d+.\d+)\s+(\d+.\d+).
				#my $thevar1 = $current{0};

				#my $thevar2 = $current{1};
				
				#$coresdetected = number of times pattern is repeated
                        	$var =~ /.{3}\s+(.{3})\s+(\d{1,2})\s+(\d{1,2}).(\d{1,2}).(\d{1,2})\s+(\d{4})\s+(\d+.\d+)/;
                        	my $hourstring = sprintf("%d",$3);
                        	my $minstring = sprintf("%d",$4);
                        	my $secstring = sprintf("%d",$5);
                        	my($minsince) = dates($var);


				#$var  =~ s/.{3}\s+//;
				#$var  =~ /\s*(.{3})\s+(\d{1,2})\s+(\d{2}).(\d{2}).(\d{2})\s+(\d{4})\s*/;
				#$datedstring = "$1 $2 $3:$4:$5 $6";				
                                #my $hourstring = sprintf("%d",$3);
                                #my $minstring = sprintf("%d",$4);
                                #my $secstring = sprintf("%d",$5);
                                #my $monthcur = sprintf("%d",$1);
				my $processes = sprintf("%.2f",$7);
                                
                                #my($minsince) = dates($var);
				
				$var  =~ s/^(.{3})\s+(\d{1,2})\s+(\d{2}).(\d{2}).(\d{2})\s+(\d{4})\s+(\d+.\d+)//;
				$choppervar = 1;
				while ($choppervar < $plotnums)
				{
					$var =~ s/\+(\d+.\d+)\s+(\d+.\d+)\s+(\d+.\d+)\s+(\d+.\d+)\s+(\d+.\d+)\s+(\d+.\d+)\s+(\d+.\d+)\s*//;
					$choppervar++;
				}
				
				$var =~ /\+(\d+.\d+)\s+(\d+.\d+)\s+(\d+.\d+)\s+(\d+.\d+)\s+(\d+.\d+)\s+(\d+.\d+)\s+(\d+.\d+)\s*/;

				#$thevar2  =~ /\s*(\d+.\d+)\s+(\d+.\d+)\s+(\d+.\d+)\s+(\d+.\d+)\s+(\d+.\d+)\s+(\d+.\d+)\s+(\d+.\d+)\s*/;
                        	my $user = sprintf("%.2f",$1);
                        	my $nice = sprintf("%.2f",$2);
                        	my $system = sprintf("%.2f",$3);
                        	my $idle = sprintf("%.2f",$4);
                        	my $iowait = sprintf("%.2f",$5);
				my $irq = sprintf("%.2f",$6);				        
				my $softirq = sprintf("%.2f",$7);

                		
				#blue-$user yellow-$nice red-$system green-$idle orange-$iowait purple-$irq brown-$softirq
				#$ylines=lines to work with
				#
				
	                        #softirq brown, give me highest pixel

				my $procnum1 = (($ylines * $plotnums) + 110);
				my $procnum1 = $procnum1 - (($softirq / 100) * $ylines);

				#irq purple,
				my $procnum2 = $procnum1;
				my $procnum2 = $procnum2 - (($irq / 100) * $ylines);

				#$iowait orange
				my $procnum3 = $procnum2;
				my $procnum3 = $procnum3 - (($iowait / 100) * $ylines);
				
                                #$system red
                                my $procnum4 = $procnum3;   
                                $procnum4 = $procnum4 - (($system / 100) * $ylines);
 
                                #$user blue
                                my $procnum5 = $procnum4;   
                                $procnum5 = $procnum5 - (($user / 100) * $ylines); 

                                #$nice yellow
                                my $procnum6 = $procnum5;   
                                $procnum6 = $procnum6 - (($nice / 100) * $ylines);
				                        
                        	#if the most recent entry, drawl lines labels
                        	if ($lastentry == $var2)
                        	{
					#if ($plotnums == 0){
					#$im->string(gdGiantFont,60,20,$procnum1,$black);
					#$im->string(gdGiantFont,60,40,$procnum2,$black);
					#$im->string(gdGiantFont,60,60,$procnum3,$black);
					#$im->string(gdGiantFont,60,80,$procnum4,$black);
					#$im->string(gdGiantFont,60,100,$procnum5,$black);
					#$im->string(gdGiantFont,60,120,$procnum6,$black);
					#}
					$im->filledRectangle(1520,65,1540,85,$black);
					$im->string(gdSmallFont,1545,85,"Processes",$black);
                                        $im->filledRectangle(1520,165,1540,185,$green);
                                        $im->string(gdSmallFont,1545,185,"Idle",$green);
                                        $im->filledRectangle(1520,205,1540,225,$red);
                                        $im->string(gdSmallFont,1545,225,"System",$red);
                                        $im->filledRectangle(1520,245,1540,265,$blue);
                                        $im->string(gdSmallFont,1545,265,"User",$blue);
                                        $im->filledRectangle(1520,285,1540,305,$grey);
                                        $im->string(gdSmallFont,1545,305,"Nice",$grey);
                                        $im->filledRectangle(1520,325,1540,345,$orange);
                                        $im->string(gdSmallFont,1545,345,"IOWait",$orange);
                                        $im->filledRectangle(1520,365,1540,385,$purple);
                                        $im->string(gdSmallFont,1545,385,"Irq",$purple);
					$im->filledRectangle(1520,405,1540,425,$brown);
					$im->string(gdSmallFont,1545,425,"SoftIrq",$brown);
                                

                                #	$im->string(gdSmallFont,1540,10,Free,$black);
                                #	$im->string(gdSmallFont,1540,70,Other,$black);
                                #	$im->string(gdSmallFont,1540,130,Buffers,$black);
                                #	$im->string(gdSmallFont,1540,190,Cached,$black);   
                                #	#$im->string(gdSmallFont,1400,225,$minsince,$black);
                                #	$im->line(1500,10,1540,10,$black);
                                #	$im->line(1500,$num3,1540,70,$black);
                                #	$im->line(1500,$num5,1540,130,$black);
                                #	$im->line(1500,$num4,1540,190,$black);
                                	my $lastmin = $minsince;
                                	$lastdate = $lastmin - 1442;
                        	}
                        
                        	#not older than 1day from first entry, or nothing gets plotted
                        	if ($lastdate <= $minsince )
                        	{
                                	#if new entry is older than previous, everything but first entry
                                	if ($minsince < $lastmin)
                                	{
                                        	$difference = $lastmin - 2;
                                        	#$difference = ceil($difference);
                                        	#if new entry is older than or equal to 2min ago
                                
                                        	if ($minsince <= $difference)
                                        	{
                                                	$difference = $lastmin - $minsince;
                                                	$difference = ceil($difference);
                                                	#leave a blank line for each 1min lost
                                                	for ($m = 1; $m < $difference; $m++)
                                                	{
                                                        	$coordinate--;
                                                	}
                                        	}
                                	}
                        
                                	if ($coordinate > 59)
                                	{
						#my $maxdown = ($ylines*$plotnums)+10;
                                        	#$im->string(gdSmallFont,$coordinate,225,$minsince,$black);
                                        	#total, what's not overwritten will show idle green
                                        	$im->line($coordinate,$lowest,$coordinate,$highest,$green);
                                        	#softirq brown
                                        	$im->line($coordinate,$procnum1,$coordinate,$lowest,$brown);
                                        	#irq purple  
                                        	$im->line($coordinate,$procnum2,$coordinate,$procnum1,$purple);
                                        	#$iowait orange
                                        	$im->line($coordinate,$procnum3,$coordinate,$procnum2,$orange);
						#$system red
						$im->line($coordinate,$procnum4,$coordinate,$procnum3,$red);
						#$user blue
						$im->line($coordinate,$procnum5,$coordinate,$procnum4,$blue);
						#$nice yellow
						$im->line($coordinate,$procnum6,$coordinate,$procnum5,$grey);
						if ($plotnums == 1)
						{
							my $processx = 110 - ((100 / $processes) * 100);  
							#$processy = 
							$im->line($coordinate,$processx,$coordinate,110,$black);
							#$processes
							
						}    


						if ($plotnums == $coresdetected)
						{
							

							
	                                    		my $modulo4 = $coordinate % 60;
                                        		if ($modulo4 == 0)
                                        		{
                                                		#$monthcur = $months1{$monthcur};
                                                		my $datestring = $hourhash{$hourstring};
                                                		#$monthcur = $months1{$monthcur};
                                                		my($isam) = &ampm($hourstring);
                                                		if ($isam == 1)
                                                		{
                                                        		$datestring = "$datestring:$minstring:$secstring AM";
                                                		}else{
                                                        		$datestring = "$datestring:$minstring:$secstring PM";
                                                		}
                                                		if ($stagger == 930)
                                                		{  
                                                        		$stagger = 920;
                                                		}else{
                                                        		$stagger = 930;
                                                		}
                                                		$im->string(gdSmallFont,$coordinate - 30,$stagger,$datestring,$black);
                                                		$im->line($coordinate,910,$coordinate,$stagger,$black);
                                        		}
                                        	}
                                	}
                        	}
                        	$lastmin = $minsince;
                	}
        	}
	$coordinate--;
	$lastentry--;
                                                 
	}
}


#Sun Feb 15 18:17:22 2009 130.00+0.00 100.00 0.00 0.00 0.00 0.00 0.00+0.02 99.93 0.02 0.02 0.00 0.02 0.00+0.30 99.56 0.13 0.00 0.00 0.00 0.00+0.98 98.68 0.10 0.20 0.00 0.00 0.03


#close database
untie(%lscpu_db);
                                                 
#remove lock file
close lscpulock;
unlink "/tmp/lscpulock";
                                                
open FILE1, "> /var/www/htdocs/lsnet/cpu.png" or die $!;
binmode FILE1;
print FILE1 $im->png;
close(FILE1);

#####################################
####################THE WEBPAGE CRAP
######################################

my $ua = LWP::UserAgent->new(agent => "google suck");
my $result = $ua->get($url);

open netinout, ">/var/www/htdocs/lsnet/netinout";
print netinout "Network in/out: $netin / $netout MB (since last device reset) <br>";
close netinout;


print "Content-type: text/html\n\n";
print "<html><head>\n";
print "<title>stats</title></head>\n";
print "<body bgcolor=\"#FFFFFF\" text=\"#000000\" link=\"#FF0000\" link=\"#800000\">\n";
print "<br>";
#print "OS: $uname <br>";
#print "Uptime: $days Days<br>";
#print "CPU Type: $cpu <br>";
#print "CPU Clock: $mhz MHz <br>";
#print "CPU Cache: $cache KB<br>";
#print "CPU Cores: $cores <br>";
#print "Swap: $swapused / $swaptotal MB <br><br>";
#print "Memory: $memused / $memsize MB <br>";




#print "<img alt=\"mem\" title=\"memory usage\" src=\"/srv/httpd/htdocs/lsnet/mem.png\"></img>";
#print "<br>";
#print "<img alt=\"mem\" title=\"memory usage\" src=\"/srv/httpd/htdocs/lsnet/mem2.png\"></img>";
#print "<br><br>";
#print "Network in/out: $netin / $netout MB (since last device reset) <br>";
#print "<img alt=\"net\" title=\"network usage\" src=\"/srv/httpd/htdocs/lsnet/net.png\"></img>";
#print "<br><br>";


my $errorvar = 1;
if (-e "/var/www/cgi-bin/lsnet.conf")
{

	open lsnetconf, "</var/www/cgi-bin/lsnet.conf";
	while(<lsnetconf>)
	{
		chomp;

		if(/^Main\sHost:\s*(.+)\s*$/)
                {
			print "<span style=\"color: rgb(2, 2, 255);font-size: 25;\">$1<br></span>";
			print "<br>";
			print "OS: $uname <br>";
			print "Uptime: $days Days<br>";
			print "CPU Type: $cpu <br>";
			print "CPU Clock: $mhz MHz <br>";
			print "CPU Cache: $cache KB<br>";
			print "CPU Cores: $cores <br>";
			print "<img alt=\"cpu\" title=\"cpu usage\" src=\"http://$1/lsnet/cpu.png\"></img>";
                        print "<br>";
			print "Swap: $swapused / $swaptotal MB <br><br>";
                        print "<img alt=\"mem\" title=\"swap usage\" src=\"http://$1/lsnet/swap.png\"></img>";
                        print "<br>";
			print "Memory: $memused / $memsize MB <br>";
#		print "<img alt=\"mem\" title=\"memory usage\" src=\"http://$1/lsnet/mem.png\"></img>";
#			print "<br>";
			print "<img alt=\"mem\" title=\"memory usage\" src=\"http://$1/lsnet/mem2.png\"></img>";
			print "<br><br>";
			print "Network in/out: $netin / $netout MB (since last device reset) <br>";
			print "<img alt=\"net\" title=\"network usage\" src=\"http://$1/lsnet/net.png\"></img>";
			print "<br><br>";
			$errorvar = 0;

		
		}elsif(/^Host:\s*(.+)\s*$/)
		{
			my $site1=get "http://$1/cgi-bin/lsnet5.pl";
			#my $site2=get "http://$1/lsnet/netinout";
			#print "<span style=\"color: rgb(2, 2, 255);font-size: 25;\">$1<br></span>";
			print "$site1";
			#print "<img alt=\"mem\" title=\"memory usage\" src=\"http://$1/lsnet/mem.png\"></img>";
			#print "<br>";
			#print "<img alt=\"mem\" title=\"memory usage\" src=\"http://$1/lsnet/mem2.png\"></img>";
			#print "<br><br>";
			#print "$site2";
			#print "<img alt=\"net\" title=\"network usage\" src=\"http://$1/lsnet/net.png\"></img>";
			print "<br><br>";
			$errorvar = 0;
		}else{
			if ($errorvar == 1)
			{
				print "<span style=\"color: rgb(2, 2, 255);font-size: 25;\">ERROR: Wrong lsnet.conf format consult the instructions<br></span>";
				close lsnetconf;
				exit;
			}
		}
	}
	close lsnetconf;
}else{
	print "<span style=\"color: rgb(2, 2, 255);font-size: 25;\">ERROR: Configure your lsnet.conf with atleast 1 host per line<br></span>";
}

print "</body></html>\n";
