How-To: Display Server Stats in Windows

Josh-Josh- Royal Oak, MI
edited April 2004 in Internet & Media
You know you wana show me how you did uptime and the network packets thing (http://parksdev.sytes.net/?stats) now don't ya? :)

Comments

  • Park_7677Park_7677 Missouri Member
    edited December 2003
    How-To: Display Server Stats in Windows

    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
  • Josh-Josh- Royal Oak, MI
    edited December 2003
    Thank you Mr. Park : - D
  • Josh-Josh- Royal Oak, MI
    edited December 2003
    One problem..
    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
  • Josh-Josh- Royal Oak, MI
    edited December 2003
    Notice: Undefined variable: result in c:\program files\apache group\apache\htdocs\temp\uptime.php on line 35

    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.
  • Josh-Josh- Royal Oak, MI
    edited December 2003
    Ran into yet another error..I really wish someone would help me.
    Notice: Undefined variable: result in c:\program files\apache group\apache\htdocs\index.php on line 42
  • Park_7677Park_7677 Missouri Member
    edited December 2003
    Doh.. it was really late. I'll edit the original post with the correct code.
  • Josh-Josh- Royal Oak, MI
    edited December 2003
    After long agitation and getting myself pissed off..
    php.ini
    Change:
    error_reporting  = E_ALL; display all errors, warnings and notices
    
    to
    error_reporting  = E_ALL & ~E_NOTICE
    
  • Josh-Josh- Royal Oak, MI
    edited December 2003
    Park_7677 had this to say
    Doh.. it was really late. I'll edit the original post with the correct code.
    The only thing that needs to be changed is the $minutes var to $min, and error_reporting in your php.ini file.
  • EnverexEnverex Worcester, UK Icrontian
    edited December 2003
    Though changing the PHP ini file isn't really the best way as getting around errors by hiding them may get you in trouble in the future....

    NS
  • Josh-Josh- Royal Oak, MI
    edited December 2003
    I know..but there wasn't a need for those "notices" to appear, IMO.
  • edited April 2004
    If you have not visited the phpSysInfo homepage on SourceForge then you maybe unaware that it is now available for Windows, all Windows (but not doors! lol)

    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
Sign In or Register to comment.