Displaying a different image for different CPU loads?

 
Post new topic   Reply to topic    Aprelium Forum Index -> PHP
View previous topic :: View next topic  
Author Message
hc2995
-


Joined: 07 Aug 2006
Posts: 644
Location: Maryland, USA

PostPosted: Thu Jan 11, 2007 1:08 am    Post subject: Displaying a different image for different CPU loads? Reply with quote

I need a script that can display a different image for different levels of CPU load. Such as:

CPU load= 0-25% will display:


CPU load= 26-50% will display:


CPU load= 51-75% will display:


CPU load= 76% or more will display:



If someone could show me a site that can help me make this, post the link. If someone could make this for me, please do (take your time, no rush) i will give you FULL CREDIT for the script and anything else i can do to thank you for it.
_________________
Where have i been? School got heck-tick, had to move half way around the state, then back... and then i had to change jobs, so iv been away for a while :P
Back to top View user's profile Send private message AIM Address
AbyssUnderground
-


Joined: 31 Dec 2004
Posts: 3855

PostPosted: Thu Jan 11, 2007 8:47 am    Post subject: Reply with quote

You could take a copy of my server status script and use the cpu.php file to get the CPU usage, then a few simple PHP "if" statements to display the appropriate image. Use the cpu.php from the V3 Static version of the script. You may need config.php as well unless you can disect it.
_________________
Andy (AbyssUnderground) (previously The Inquisitor)
www.abyssunderground.co.uk
Back to top View user's profile Send private message Visit poster's website
hc2995
-


Joined: 07 Aug 2006
Posts: 644
Location: Maryland, USA

PostPosted: Thu Jan 11, 2007 12:52 pm    Post subject: Reply with quote

k, ill get right on that, when i get back from school :P
_________________
Where have i been? School got heck-tick, had to move half way around the state, then back... and then i had to change jobs, so iv been away for a while :P
Back to top View user's profile Send private message AIM Address
PaulK
-


Joined: 26 Apr 2006
Posts: 132
Location: London, UK

PostPosted: Fri Jan 12, 2007 12:37 pm    Post subject: Reply with quote

Nice graphics, mind if I steal them?

Paul
_________________
Back to top View user's profile Send private message Visit poster's website
hc2995
-


Joined: 07 Aug 2006
Posts: 644
Location: Maryland, USA

PostPosted: Fri Jan 12, 2007 1:31 pm    Post subject: Reply with quote

go ahead lol
_________________
Where have i been? School got heck-tick, had to move half way around the state, then back... and then i had to change jobs, so iv been away for a while :P
Back to top View user's profile Send private message AIM Address
pkSML
-


Joined: 29 May 2006
Posts: 952
Location: Michigan, USA

PostPosted: Thu Jan 18, 2007 2:38 am    Post subject: Reply with quote

I'd also like to access the server's load.

I've tried using Abyssunderground's Server status script (V3), but it's not yet working properly. I set the PHP version to 5 (in config.php and cpu-graph.php). It says usage is 100%, but doesn't give CPU type or speed.

I have XP home. Does XP home have a method to request the server's CPU usage?
_________________
Stephen
Need a LitlURL?


http://CodeBin.yi.org
Back to top View user's profile Send private message Visit poster's website
hc2995
-


Joined: 07 Aug 2006
Posts: 644
Location: Maryland, USA

PostPosted: Thu Jan 18, 2007 4:20 am    Post subject: Reply with quote

pkSML wrote:
I'd also like to access the server's load.

I've tried using Abyssunderground's Server status script (V3), but it's not yet working properly. I set the PHP version to 5 (in config.php and cpu-graph.php). It says usage is 100%, but doesn't give CPU type or speed.

I have XP home. Does XP home have a method to request the server's CPU usage?


If you visit my site, i have displayed the image at the bottom, i used php (places into the head then used a print(); function to show the image, here is the code im useing:


Code:

<?php

if($phpver=="4"){
if(class_exists("COM")) {
  $wmi = new COM("WinMgmts:\\\\.");
  $cpus = $wmi->InstancesOf("Win32_Processor");
 
  $i = 1;
 
      // Use the while loop on PHP 4 and foreach on PHP 5
  while ($cpu = $cpus->Next()) {
     //foreach ($cpus as $cpu) {
 
   $clockSpeed = $cpu->CurrentClockSpeed;
   $cpuDATA = $cpu->LoadPercentage;
   $i++;
   
  }
 }
}

else{

if(class_exists("COM")) {
  $wmi = new COM("WinMgmts:\\\\.");
  $cpus = $wmi->InstancesOf("Win32_Processor");
 
  $i = 1;
 
      // Use the while loop on PHP 4 and foreach on PHP 5
     //while ($cpu = $cpus->Next()) {
     foreach ($cpus as $cpu) {
 
   $clockSpeed = $cpu->CurrentClockSpeed;
   $cpuDATA = $cpu->LoadPercentage;
   $i++;
   
  }
 }
}



$message2="% CPU Load.";
$server="";

if($cpuDATA==""){
$status="error";
$cpuDATA="An error occured. Please check that your system supports WMI. Also check that you have set your PHP version correctly in the config.php file.<br>";
$cpu="<font color='red'>".$cpuDATA."</font>";
}

if($cpuDATA>"100") // compensates for cpuusage.exe displaying
$cpuDATA="100";  // 100.5% on a 100% load


if($cpuDATA > "0"){
$image="<img src='/images/load/norm.gif' border='0'>";
}

if($cpuDATA > "26"){
$image="<img src='/images/load/elev.gif' border='0'>";
}

if($cpuDATA > "51"){
$image="<img src='/images/load/high.gif' border='0'>";
}

if($cpuDATA > "76"){
$image="<img src='/images/load/extr.gif' border='0'>";
}



$cpu="<b>Server Load:</b><br> ".$image."";


and this is how i displayed the images:

Code:

<?php
print($cpu);
?>


Just change the paths to the images

For the graph, here is the code im using:

Code:

<?

$phpver="5";

if(class_exists("COM")) {
  $wmi = new COM("WinMgmts:\\\\.");
  $cpus = $wmi->InstancesOf("Win32_Processor");
 
  $i = 1;
 
      // Use the while loop on PHP 4 and foreach on PHP 5
if($phpver=="4"){
  while ($cpu = $cpus->Next()) {
   $clockSpeed = $cpu->CurrentClockSpeed;
   $one = $cpu->LoadPercentage;
   $i++;
    }
   } 
else{
  foreach ($cpus as $cpu) {
   $clockSpeed = $cpu->CurrentClockSpeed;
   $one = $cpu->LoadPercentage;
   $i++;
    } 
  }
}




if(class_exists("COM")) {
  $wmi = new COM("WinMgmts:\\\\.");
  $cpus = $wmi->InstancesOf("Win32_Processor");
 
  $i = 1;
 
      // Use the while loop on PHP 4 and foreach on PHP 5
if($phpver=="4"){
  while ($cpu = $cpus->Next()) {
   $clockSpeed = $cpu->CurrentClockSpeed;
   $two = $cpu->LoadPercentage;
   $i++;
    }
   } 
else{
  foreach ($cpus as $cpu) {
   $clockSpeed = $cpu->CurrentClockSpeed;
   $two = $cpu->LoadPercentage;
   $i++;
    } 
  }
}






if(class_exists("COM")) {
  $wmi = new COM("WinMgmts:\\\\.");
  $cpus = $wmi->InstancesOf("Win32_Processor");
 
  $i = 1;
 
      // Use the while loop on PHP 4 and foreach on PHP 5
if($phpver=="4"){
  while ($cpu = $cpus->Next()) {
   $clockSpeed = $cpu->CurrentClockSpeed;
   $three = $cpu->LoadPercentage;
   $i++;
    }
   } 
else{
  foreach ($cpus as $cpu) {
   $clockSpeed = $cpu->CurrentClockSpeed;
   $three = $cpu->LoadPercentage;
   $i++;
    } 
  }
}






if(class_exists("COM")) {
  $wmi = new COM("WinMgmts:\\\\.");
  $cpus = $wmi->InstancesOf("Win32_Processor");
 
  $i = 1;
 
      // Use the while loop on PHP 4 and foreach on PHP 5
if($phpver=="4"){
  while ($cpu = $cpus->Next()) {
   $clockSpeed = $cpu->CurrentClockSpeed;
   $four = $cpu->LoadPercentage;
   $i++;
    }
   } 
else{
  foreach ($cpus as $cpu) {
   $clockSpeed = $cpu->CurrentClockSpeed;
   $four = $cpu->LoadPercentage;
   $i++;
    } 
  }
}







if(class_exists("COM")) {
  $wmi = new COM("WinMgmts:\\\\.");
  $cpus = $wmi->InstancesOf("Win32_Processor");
 
  $i = 1;
 
      // Use the while loop on PHP 4 and foreach on PHP 5
if($phpver=="4"){
  while ($cpu = $cpus->Next()) {
   $clockSpeed = $cpu->CurrentClockSpeed;
   $five = $cpu->LoadPercentage;
   $i++;
    }
   } 
else{
  foreach ($cpus as $cpu) {
   $clockSpeed = $cpu->CurrentClockSpeed;
   $five = $cpu->LoadPercentage;
   $i++;
    } 
  }
}







if(class_exists("COM")) {
  $wmi = new COM("WinMgmts:\\\\.");
  $cpus = $wmi->InstancesOf("Win32_Processor");
 
  $i = 1;
 
      // Use the while loop on PHP 4 and foreach on PHP 5
if($phpver=="4"){
  while ($cpu = $cpus->Next()) {
   $clockSpeed = $cpu->CurrentClockSpeed;
   $six = $cpu->LoadPercentage;
   $i++;
    }
   } 
else{
  foreach ($cpus as $cpu) {
   $clockSpeed = $cpu->CurrentClockSpeed;
   $six = $cpu->LoadPercentage;
   $i++;
    } 
  }
}






if(class_exists("COM")) {
  $wmi = new COM("WinMgmts:\\\\.");
  $cpus = $wmi->InstancesOf("Win32_Processor");
 
  $i = 1;
 
      // Use the while loop on PHP 4 and foreach on PHP 5
if($phpver=="4"){
  while ($cpu = $cpus->Next()) {
   $clockSpeed = $cpu->CurrentClockSpeed;
   $seven = $cpu->LoadPercentage;
   $i++;
    }
   } 
else{
  foreach ($cpus as $cpu) {
   $clockSpeed = $cpu->CurrentClockSpeed;
   $seven = $cpu->LoadPercentage;
   $i++;
    } 
  }
}






if(class_exists("COM")) {
  $wmi = new COM("WinMgmts:\\\\.");
  $cpus = $wmi->InstancesOf("Win32_Processor");
 
  $i = 1;
 
      // Use the while loop on PHP 4 and foreach on PHP 5
if($phpver=="4"){
  while ($cpu = $cpus->Next()) {
   $clockSpeed = $cpu->CurrentClockSpeed;
   $eight = $cpu->LoadPercentage;
   $i++;
    }
   } 
else{
  foreach ($cpus as $cpu) {
   $clockSpeed = $cpu->CurrentClockSpeed;
   $eight = $cpu->LoadPercentage;
   $i++;
    } 
  }
}






if(class_exists("COM")) {
  $wmi = new COM("WinMgmts:\\\\.");
  $cpus = $wmi->InstancesOf("Win32_Processor");
 
  $i = 1;
 
      // Use the while loop on PHP 4 and foreach on PHP 5
if($phpver=="4"){
  while ($cpu = $cpus->Next()) {
   $clockSpeed = $cpu->CurrentClockSpeed;
   $nine = $cpu->LoadPercentage;
   $i++;
    }
   } 
else{
  foreach ($cpus as $cpu) {
   $clockSpeed = $cpu->CurrentClockSpeed;
   $nine = $cpu->LoadPercentage;
   $i++;
    } 
  }
}






if(class_exists("COM")) {
  $wmi = new COM("WinMgmts:\\\\.");
  $cpus = $wmi->InstancesOf("Win32_Processor");
 
  $i = 1;
 
      // Use the while loop on PHP 4 and foreach on PHP 5
if($phpver=="4"){
  while ($cpu = $cpus->Next()) {
   $clockSpeed = $cpu->CurrentClockSpeed;
   $ten = $cpu->LoadPercentage;
   $i++;
    }
   } 
else{
  foreach ($cpus as $cpu) {
   $clockSpeed = $cpu->CurrentClockSpeed;
   $ten = $cpu->LoadPercentage;
   $i++;
    } 
  }
}









// Add values to the graph
$graphValues=array($one,$two,$three,$four,$five,$six,$seven,$eight,$nine,$ten);

// Define .PNG image
header("Content-type: image/png");
$imgWidth=225;
$imgHeight=100;

// Create image and define colors
$image=imagecreate($imgWidth, $imgHeight);
$colorWhite=imagecolorallocate($image, 0, 0, 0);
$colorGrey=imagecolorallocate($image, 0, 64, 0);
$colorBlue=imagecolorallocate($image, 0, 255, 0);

// Create border around image
imageline($image, 0, 0, 0, 250, $colorGrey);
imageline($image, 0, 0, 250, 0, $colorGrey);
imageline($image, 249, 0, 249, 249, $colorGrey);
imageline($image, 0, 249, 249, 249, $colorGrey);

// Create grid
for ($i=1; $i<11; $i++){
imageline($image, $i*25, 0, $i*25, 250, $colorGrey);
imageline($image, 0, $i*25, 250, $i*25, $colorGrey);
}

// Create line graph
for ($i=0; $i<10; $i++){
imageline($image, $i*25, ($imgHeight-$graphValues[$i]), ($i+1)*25, ($imgHeight-$graphValues[$i+1]), $colorBlue);
}




// Create some colors
$white = imagecolorallocate($image, 255, 255, 255);
$green = imagecolorallocate($image, 0, 160, 0);
$black = imagecolorallocate($image, 0, 0, 0);

// The text to draw
$text = "Testing...";

// Replace path by your own font path
$font = "tahoma.ttf";


$averagecpu=($one+$two+$three+$four+$five+$six+$seven+$eight+$nine+$ten) / 10;
if(!($averagecpu=="0")){
$totalcpu="Average CPU Usage: " . $averagecpu . "%";
}
else{
$totalcpu="WMI failed. Check config.";
}

// Add some shadow to the text
imagettftext($image, 9, 0, 40, 55, $green, $font, $totalcpu);




// Output graph and clear image from memory
imagepng($image);
imagedestroy($image);

?>


NOTE: This is the index.php page for a directory /status/ FOR THIS TO WORK config.php AND the data directory MUST BE IN THE SAME DIRECTORY AS THE GRAPH!

I am also using WINXP Home :P
_________________
Where have i been? School got heck-tick, had to move half way around the state, then back... and then i had to change jobs, so iv been away for a while :P
Back to top View user's profile Send private message AIM Address
hc2995
-


Joined: 07 Aug 2006
Posts: 644
Location: Maryland, USA

PostPosted: Thu Jan 18, 2007 4:21 am    Post subject: Reply with quote

UPDATE: Server back up :D just a small network issue
_________________
Where have i been? School got heck-tick, had to move half way around the state, then back... and then i had to change jobs, so iv been away for a while :P
Back to top View user's profile Send private message AIM Address
Display posts from previous:   
Post new topic   Reply to topic    Aprelium Forum Index -> PHP All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB phpBB Group