Retrieving CPU load using PHP classes for web applications

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
madhuj1260
Forum Newbie
Posts: 5
Joined: Wed Jun 10, 2009 3:26 am

Retrieving CPU load using PHP classes for web applications

Post by madhuj1260 »

hi all,

I am writing an application which needs to retrieve the CPU load of the server a particular instant of time. can nyone plz help me. plz note that this is for a web application.

Thanks in advance. :)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Retrieving CPU load using PHP classes for web applications

Post by pickle »

You can execute shell commands with PHP. Maybe investigate that.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
madhuj1260
Forum Newbie
Posts: 5
Joined: Wed Jun 10, 2009 3:26 am

Re: Retrieving CPU load using PHP classes for web applications

Post by madhuj1260 »

sorry, I havent informed u one thing I want this for windows system
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Retrieving CPU load using PHP classes for web applications

Post by pickle »

Oh, then I don't know.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Retrieving CPU load using PHP classes for web applications

Post by mikemike »

There is no command for checkig CPU usage on Windows I don't think. You can write a simple C program though and just call that using system().

Code: Select all

# include <stdio.h>
# include <pdh.h>
 
double cpuLoad[10];
double avgCpuLoad = 0;
int i;
 
void DispTime()
{
  HQUERY hQuery;
  HCOUNTER hCounter;
  PDH_FMT_COUNTERVALUE FmtValue;
  PdhOpenQuery(NULL, 0, &hQuery);
  PdhAddCounter(hQuery, "\\Processor(_Total)\\% Processor Time", 0, &hCounter);
  printf("Starting the process...\n");
  PdhCollectQueryData(hQuery);
 
  PdhGetFormattedCounterValue(hCounter, PDH_FMT_DOUBLE, NULL, &FmtValue);
  printf("The cpu usage is : %f%%\n", FmtValue.doubleValue);
  PdhCloseQuery(hQuery);
}
 
void main()
{
  DispTime();
  getchar();
}
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Retrieving CPU load using PHP classes for web applicatio

Post by McInfo »

Related topic: Average CPU load on Windows?

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 10:55 pm, edited 1 time in total.
madhuj1260
Forum Newbie
Posts: 5
Joined: Wed Jun 10, 2009 3:26 am

Re: Retrieving CPU load using PHP classes for web applications

Post by madhuj1260 »

As I am beginner in php can u tell me how can I link up this c program to my php web page :|
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Retrieving CPU load using PHP classes for web applications

Post by mikemike »

Using exec()
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Retrieving CPU load using PHP classes for web applicatio

Post by McInfo »

The C code needs to be compiled into an executable (.exe) first. I would compile it, but I don't know where to download a compatible pdh.h helper file.

It would probably be easier to use this PHP code that I posted in the thread that I linked to before.

Code: Select all

<?php
header('Content-Type: text/plain');
echo system('typeperf "\Processor(*)\% Processor Time"');
?>
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 10:56 pm, edited 1 time in total.
madhuj1260
Forum Newbie
Posts: 5
Joined: Wed Jun 10, 2009 3:26 am

Re: Retrieving CPU load using PHP classes for web applications

Post by madhuj1260 »

I tried this but this cannot be used for maintaining the values in the variable as this is of a command line application.
Post Reply