How-To: Display Server Stats in Windows
Josh-
Royal Oak, MI
You know you wana show me how you did uptime and the network packets thing (http://parksdev.sytes.net/?stats) now don't ya?
0
Comments
For Linux, Unix, FreeBSD, etc there is phpSysInfo. For Windows, there isn't much. This is a script I put together using some of my own code, and some code from PHP.net and other community sources. Free to use, please don't claim it as your own code... it's not even 100% mine to claim! Note, there isn't much to this code.. just some uptime & network stats for now View it in action!
[PHP]<?php
// Format Filesizes
function format_size($size) {
$a = array("B", "KB", "MB", "GB", "TB", "PB");
$pos = 0;
while ($size >= 1024) {
$size /= 1024;
$pos++;
}
return number_format($size, 2, ".",",")." ".$a[$pos];
}
// Format Uptime
function format_uptime($uptime) {
$result = "";
$min = $uptime / 60;
$hours = $min / 60;
$days = floor($hours / 24);
$hours = floor($hours - ($days * 24));
$min = floor($min - ($days * 60 * 24) - ($hours * 60));
if ($days == 1) {
$result .= "$days day, ";
} elseif ($days != 0) {
$result .= "$days days, ";
}
if ($hours == 1) {
$result .= "$hours hour, ";
} elseif ($hours != 0) {
$result .= "$hours hours, ";
}
if ($min == 1) {
$result .= "$min minute";
} else {
$result .= "$min minutes";
}
return $result;
}
$raw = `netstat -e`;
$network = explode("\n", $raw );
$bytes = explode(" ",$network[4]);
$packets = explode(" ",$network[5]);
$cnt = 0;
for ($i=0;$i<count($packets);$i++){
if ($packets[$i]=="") continue;
if ($cnt==2) $packet_recv = number_format($packets[$i], 0, ".",",");
elseif ($cnt==3) $packet_sent = number_format($packets[$i], 0, ".",",");
$cnt++;
}
$cnt = 0;
for ($i=0;$i<count($bytes);$i++){
if ($bytes[$i]=="") continue;
if ($cnt==1) $byte_recv = format_size($bytes[$i]);
elseif ($cnt==2) $byte_sent = format_size($bytes[$i]);
$cnt++;
}
unset($raw, $network, $bytes, $packets, $cnt, $i);
$curr_time = time();
$start_time = filemtime("C:\\pagefile.sys"); // Make sure this is set to your PAGEFILE!
$uptime = $curr_time - $start_time;
$uptime = format_uptime($uptime);
?>[/PHP]
Place that anywhere in your code, at the top will work.
To display the stats, you need to ECHO them.
[PHP]<?php
echo $uptime; // System Uptime
echo $packet_sent; // Packets Sent
echo $packet_recv; // Packets Received
echo $byte_sent; // Bytes Sent**
echo $byte_recv; // Bytes Received**
?>
[/PHP]
<i><b><div align="right">Enjoy, <br>-PaRK</div></i></b>
** Windows resets the bytes sent/received after 4.00 GB
Notice: Undefined variable: minutes in c:\program files\apache group\apache\htdocs\index.php on line 41
Notice: Undefined variable: result in c:\program files\apache group\apache\htdocs\index.php on line 44
Only error left now. I changed
[php]
if ($minutes == 1) {
$result .= "$min minute";
} else {
$result .= "$min minutes";
}
[/php]
to
[php]
if ($min == 1) {
$result .= "$min minute";
} else {
$result .= "$min minutes";
}
[/php]
and I'm not sure whats wrong on line 35.
Notice: Undefined variable: result in c:\program files\apache group\apache\htdocs\index.php on line 42
php.ini
Change: to
NS
For further information read the text at the top of the page, ie here http://kondev.bounceme.net/SysInfo/ , servers statistics are live and correct and it is available for download from my server.
Have fun and may the source be with you
Cobra El Diablo
http://kondev.bounceme.net/
Liquid Silicon Developments