The online racing simulator
Getting Load Percent in All Operating Systems.
(12 posts, started )
Getting Load Percent in All Operating Systems.

<?php 
function getServerLoad()
{
    if (
        
strtoupper(substr(PHP_OS03)) == 'WIN'
    
) {    // Windows
        
$loadAvg explode(PHP_EOL, `wmic cpu get loadpercentage`);
        return 
sprintf('%1.2f'$loadAvg[1]);
    } else if (
        
function_exists('exec') AND
        
$stats = @exec('uptime 2>&1') AND
        
trim($stats) != '' AND
        
preg_match('#: ([\d.,]+),?\s+([\d.,]+),?\s+([\d.,]+)$#'$stats$loadAvg)
    ) {
        return 
sprintf('%1.2f %1.2f %1.2f'$loadAvg[1], $loadAvg[2], $loadAvg[3]);
    } else if (
        
file_exists('/proc/loadavg') AND
        
$stats = @file_get_contents('/proc/loadavg') AND
        
trim($stats) != ''
    
) {
        return 
$stats;
    }
    return 
FALSE# Load Unobtainable.
}
?>

wow, now that's an example of unreadable code formatting! (I'm quite sure you didn't write this )
Quote from Victor :wow, now that's an example of unreadable code formatting! (I'm quite sure you didn't write this )

Yeah, it's quite compact. I did write this, when I first started out programming. So I think it's safe to say I've become a better programmer since then.
Quote from Dygear :

<?php 
function getServerLoad()
{
    if (
        
strtoupper(substr(PHP_OS03)) == 'WIN'
    
) {    // Windows
?>


btw, i didn't know about the PHP_OS constant! Using that now for the prism windows detection
I just wanted to point at sys_getloadavg but then I read the Notes... ^^

Well at least you could replace all the non-win stuff by it
Quote from Victor :btw, i didn't know about the PHP_OS constant! Using that now for the prism windows detection

Yeah, I knew I had that some where and I was going to change it in my version but I did not want to tred on your toes. (That and you might be using SHELL as a way to detect if the client is running in Cygwin or not.)
No I don't use the shell variable for anything. Cygwin seemed to behave the same as plain windows, which makes sense because Cygwin isn't a shell, but a collection of programs to present the appearance of linux, on a windows platform. And so far I haven't been interested in any of those programs.

There is also the php_uname() function that could be used I noticed. Lotsa handy function on this page : http://nl3.php.net/manual/en/ref.info.php
Quote :wmic cpu get loadpercentage

this is supposed to be a command to run on windows computers, right? for some reason it's not working on mine...
Quote from bunder9999 :this is supposed to be a command to run on windows computers, right? for some reason it's not working on mine...

Really! I was sure WMIC (Windows Management Instrumentation Command) came preinstalled.

Quote from http://ss64.com/nt/wmic.html :Notes

WMIC is available on XP Professional and Windows 2003, for older machines download & install: WMI core for Win 9x / NT 4

The availability of WMI information does vary across different versions of Windows
e.g. ODBC, SNMP, Windows Installer.

To run WMIC requires administrator rights.

In Windows 2000, around 4,000 properties can be monitored, and around 40 can be configured.
In Windows XP around 6,000 properties can be monitored, and around 140 can be configured.

Windows 2003 offers a few improvements and bug fixes: the global option /locale:ms_409 is not required (it defaults to English US.)

When you type WMIC for the first time in Windows 2003 all the aliases are compiled. The second, and subsequent times you run WMIC, it will start immediately. Under XP WMIC is slower to initialise, therefore to run several WMI queries it can be quicker to use interactive mode.

* WMI information for installed software packages (PACKAGE and SOFTWAREFEATURE) is often incomplete and inconsistent for a variety of historical reasons. A more reliable method is to retrieve a list of installed programs directly from the Add/Remove list in the registry, with a WSH script like this from Torgeir Bakken.

Do'h looks like I was wrong!
i wonder if windows load can be detected at all, without special gadgetry. vBulletin has a load sensor as well for example, but only for *nix.
Quote from Victor :i wonder if windows load can be detected at all, without special gadgetry. vBulletin has a load sensor as well for example, but only for *nix.

As far as I know, there is no built in way to just get this data. I mean you can poll the data from the Windows API / SDK but you'd have to make the exe yourself.
Don't forget that CPU load on *ix doesn't equate directly to utilisation as windows understands it Load is more to do with how much stuff is waiting rather than how much work the CPU is actually doing, if that makes sense?

However, using the COM extension (http://uk3.php.net/manual/en/book.com.php) you could query the WMI without using exec(), and just barf if the extension isn't loaded (http://uk.php.net/extension_loaded), although most windows versions of PHP should have it by default, unless it's been custom compiled


<?php
$obj = new COM ( 'winmgmts://./root/cimv2' );
$pc = 0;

foreach ( $obj->instancesof ( 'Win32_Processor' ) as $p )
printf("CPU%d @ %d%%", $pc++, $p->LoadPercentage);
?>

That'll work fine if you're using a regular user account like you would be with PRISM (well, tested with a w2003 box, I've not got a w2008 box spun up right this second, but I don't see why it wouldn't work - I'm not aware of any changes in the default perms for WMI under that).

However, if you want to run that as your webserver user, unless you're running your webserver with access to the WMI it'll fail like a bastard. For example, using IIS with an anonymous user the WMI root/cimv2 permissions would need the IUSR_MACHINENAME or IUSR account added with remote enable permissions.

There are, naturally, possible security problems in making your webserver able to poke at WMI

Getting Load Percent in All Operating Systems.
(12 posts, started )
FGED GREDG RDFGDR GSFDG