First a little background:
I had read about a software called WAMP which is a software bundle containing Apache, PHP and mySQL (w/ phpmyadmin). So I reckoned that I save myself some time and give it a try instead of having to install everything manually and configure everything so that it plays nicely. It payed off except for the part where I had to give the root user for phpmyadmin a password and tell it not to log on automatically, and I couldn't get the whole pmad DB to work even after setting all the variables correctly and defining the control user (root).
Anyhow, so I have a database with 16 tables containing 1,029 rows. While it's not really a big database, it's the biggest one I've ever had control over. The computer all of this is running is on is a $300 rig with an Intel Atom 230 1.6GHz and 1GB of RAM, real sucky, I know.
The next thing I did is look for PHP snippets that would give me info about the server, stuff like CPU Load, Memory, and if I'm lucky, network utilization. Stuff you can find in the more expensive software cPanel. After a couple minutes on Google I find out, to my dismay, that a newly implemented PHP function, sys_getloadavg() only works for *nix systems because it depends on /proc/loadavg which isn't present on Windows Operating Systems. A half hour later I've been through at least 4 different PHP scripts that attempt at getting a CPU load for Windows systems, only one of which works:
<?phpdefine ( 'CPU_NAME', '.' );$obj = new COM ( 'winmgmts:{impersonationLevel=impersonate}//' . CPU_NAME . '/root/cimv2' );$pc = 0;foreach ( $obj->instancesof ( 'Win32_Processor' ) as $mp ){echo "<pre>";echo " CPU Load: " . $mp->LoadPercentage . "%\r\n";echo "</pre>";}?>
It loads the Windows Management Instrumentation which is needed to get the cpu load. It creates a COM class and goes through all instances of the Win32_Processor WMI class which contain a bunch of properties, the one we want is the LoadPercentage class.
In the same fashion I displayed the available memory:
<?phpforeach ( $obj->instancesof ( 'Win32_OperatingSystem' ) as $mp ){$AvailMem = $mp->FreePhysicalMemory;$AvailMem = round($AvailMem/1024);$TotalMem = $mp->TotalVisibleMemorySize;$TotalMem= round($TotalMem/1024);$Processes = $mp->NumberOfProcesses;$Status = $mp->Status;echo "<pre>";echo " Free Physical Memory: " . $AvailMem . " MB \r\n";echo "Total Physical Memory: " . $TotalMem . " MB \r\n\r\n";echo " Processes Running: " . $Processes . " \r\n</pre>";}}?>
Now you could either check the Microsoft Software Developer Network Library (MSDN Library) for all the properties so you can fetch its value or you could just loop through all the properties with this convenient snippet:
foreach ( $obj->instancesof ( 'Win32_NetworkAdapter' ) as $np ){foreach($np->Properties_ as $propertyVal){$arrProperties[$propertyVal->Name] = $propertyVal->Value;}print_r($arrProperties);}
Here I used Win32_NetworkAdapter as the class that I'd like to see all the properties and their values. Pretty sweet stuff. If I were to print_r($np) then I'll only get 'variant Object' as the output.
If you have the time and use for the WMI then it's definitely a cool thing to mess around with for Windows Systems, just make sure you protect the php page because could be juicy info for hackers.
No comments:
Post a Comment