#!/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();
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);
}


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;

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

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

my $firstentryvar = 1;
#plot the 1440min 1min/px lines on chart
for ($i = 1440; $i >= 0; --$i)
{        
        #$line1 = <MEMFILE>;
        #$line1 = @mylist[$i];

        if (exists $mylist[$i])
        {
		$line1 = @mylist[$i];
		print "line1: $line1\n";
	}
}
exit;
