#!/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',
   );

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 $im = new GD::Image(170,230);

# 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,215,"Current Usage",$black);


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


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,110,$num3,gdStyled);
        $num4 = $num4 + $tenth;
	$num4 = ceil($num4);
        $num3 = $num3 - 20;
}


#total used
#$num3 = 200 - ($memused / $memsize * 200);
#$num3 = $num3 + 10;
$num3 = $memused / $memsize;
$num3 = $num3 * 200;
$num3 = 200 - $num3;
$num3 = $num3 + 10;


#cached
#my $num4 = $cached / 1024;
$num4 = $cached/$memsize;  
$num4 = $num4 * 200;
$num4 = 200 - $num4;
$num4 = $num4 + 10;

#buffers used
#my $num5 = $buffers / 1024;
#$num5 = 200 + ($buffers / $memsize * 200);
#$num5 = $num5 + 10;
$num5 = $buffers + $cached;
$num5 = $num5 / $memsize;
$num5 = $num5 * 200;
$num5 = 200 - $num5;
$num5 = $num5 + 10;


#$im->line(60,$num3,60,210,$red);
#$im->line(60,$num3,110,$num3,$red);
#$im->line(85,$num3,85,210,$red);
#$im->line(110,$num3,110,210,$red);
#$im->line(60,$num3,110,210,$red);
#$im->line(110,$num3,60,210,$red);


#$num5 = $num5 - $num4 + 10;



#total used
$im->filledRectangle(60,$num3,110,210,$red);
#buffers
$im->filledRectangle(60,$num4,110,$num5,$yellow);
#cached
$im->filledRectangle(60,210,110,$num4,$orange);
#free
$im->filledRectangle(60,10,110,$num3,$green);


$im->string(gdSmallFont,120,10,Free,$black);
$im->string(gdSmallFont,120,70,Other,$black);
$im->string(gdSmallFont,120,130,Buffers,$black);
$im->string(gdSmallFont,120,190,Cached,$black);
$im->line(110,10,120,10,$black);
$im->line(110,$num3,120,70,$black);
$im->line(110,$num5,120,130,$black);
$im->line(110,$num4,120,190,$black);



##############GD STUFF FOR FINAL IMAGE
# make sure we are writing to a binary stream
#binmode STDOUT;
# Convert the image to PNG and print it on standard output
#print $im->png;

#initialize to delete old data
#open FILE1, ">", "/tmp/mem.png" or die $!;
#close(FILE1);

open FILE1, "> /var/www/htdocs/lsnet/mem.png" or die $!;

 #       while (<FILE1>) {
#		print FILE1 $im->png;
#	}

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 - 1440;
                       	}
			
			#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 - 1440;


                        }

                        #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);


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 "Swap: $swapused / $swaptotal MB <br><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/lsnet3.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;
			}
		}
		$errorvar = 1;
	}
	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";
