#!/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);   
use File::ReadBackwards;
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();
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);
}


###################First Image Swap


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 = 1440;
my $i = 1;   
my $stagger = 130;

#Tue Feb 24 07:17:44 2009 2075412 495528 709548 245200 1579884 10980276 10604340 375936



$file1 = "/usr/lib/lsnet/lsmem.log";
my $numberoflines = "0";
open (MEMFILE,"$file1");
my @mylist = reverse <MEMFILE>;
seek (MEMFILE,0,0);
while ($testline = <MEMFILE>) {
$numberoflines++;
#print "$n). $line<br>\n";
} 


#open MEMFILE, "</usr/lib/lsnet/lsmem.log";
#$line1 = <MEMFILE>;
        
#my @mylist = <MEMFILE>;
        
#close MEMFILE;

#my $numberoflines;

#seek (MEMFILE,0,0);        

#while (my $linetest = <MEMFILE>) {
#$numberoflines++;
#} 

if ($numberoflines >= 1440)
{
	$numberoflines = 1440;
} 
print "numberoflines $numberoflines\n";
#$numberoflines - 10;

my $firstentryvar = 1;
#plot the 1440min 1min/px lines on chart
for ($i = 0; $i <= numberoflines; --$i)
{        
        #$line1 = <MEMFILE>;
        #$line1 = @mylist[$i];
	#$line1 = $mylist[$i];
	#print "line1: $line1\n";
	#if (
        if (exists $mylist[$i])
        {
		$line1 = $mylist[$i];
		print "line1: $line1\n";

		#$line1 = @mylist[$i];
                #if db entry is >= mem1 key not counting place-holder mem0
                #if ($lastentry >= 1)
                #{
                       #my $var = $lsmem_db{"mem$lastentry"};
                        #my($minsince) = &dates($var);
                       $line1 =~ /.{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($line1);
 
                        my $dbswapsize = sprintf("%.2f",$12);
                        my $dbswapused = sprintf("%.2f",$14);
			print "line1: $1 $2 $3 $4 $5 $6 $7 $8 $9 $10 $11 $12 $13 $14\n";
                        #total used
                        $num3 = $dbswapused / $dbswapsize;
                        $num3 = $num3 * 100;
                        $num3 = 100 - $num3;
                        $num3 = $num3 + 10;
        
                        #if the most recent entry, drawl lines labels
                        if ($firstentryvar == 1)
                        {
                                $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;
                                $firstentryvar = 0;
                        }
                        #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 MEMFILE;
                                         
open FILE1, "> /var/www/htdocs/lsnet/swaptest.png" or die $!;
binmode FILE1;
print FILE1 $im->png;
close(FILE1);

exit;
##############second Image Memory


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 = 1440;
my $hournum = 60;
my $coordinate = 1500;
$hournum = 60;

#my $var2 = $lsmem_db{"mem0"};

my $difference = 0;
my $lastdate = 0;
my $i = 1;
my $stagger = 230;


open MEMFILE, "</usr/lib/lsnet/lsmem.log";
#$line1 = <MEMFILE>;
        
@mylist = <MEMFILE>;
        
#close MEMFILE;

my $numberoflines;

while (my $linetest = <MEMFILE>) {
$numberoflines++;
}

if ($numberoflines >= 1440)
{
        $numberoflines = 1440;
}





my $firstentryvar = 1;
#plot the 1440min 1min/px lines on chart
for ($i = $numberoflines; $i >= 0; --$i)
{

        #if (exists $mylist[$i])
        #{
                $line1 = @mylist[$i];

                        $line1 =~ /.{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 ($firstentryvar == 1)
                        {
                                $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;
				$firstentryvar = 0;
                        }
                        
                        #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 MEMFILE;
                                         
open FILE1, "> /var/www/htdocs/lsnet/memtest.png" or die $!;
binmode FILE1;
print FILE1 $im->png;
close(FILE1);
                                                        
                                                 
                        
################ 3RD IMAGE NETWORK USAGE

