+ Reply to Thread
Results 1 to 2 of 2

Thread: TUT: How to get server's uptime and avgload

  1. #1
    Full Member dzone is on a distinguished road
    Join Date
    Dec 2003
    Posts
    75

    TUT: How to get server's uptime and avgload

    TUT: How to get server’s uptime and avgload

    Hi !

    I am quite sure that most of us would be interested in a script which would show you the uptime and load averages of the server it’s hosted on. So here I am with a small article on how could you get an uptime-displaying script up and running

    First of all, let’s find out who might need this snippet. Are you building a website and want to impress people with your rock-solid ultra-fast megaserver ? Are you running a hosting website and want to convince users choose your company? Just curious about your server’s performance and uptime? So it’s right for you.

    There are two common ways of getting such information on a unix server. Either you could run the “uptime” command and parse its output; either take the values directly from special system files “/proc/uptime” and “/proc/loadavg”. “Uptime” takes the values from the last ones so usually if you can run arbitrary server commands, using it instead of taking stuff from /proc/… is an unneeded waste of resources.

    Uptime has another drawback. There is no standard which describes its output format; it just has to be “human-readable” which means that the output format may differ from one distributive to another. Such a script may just not work with your hosting.

    So we decided to use /proc/… . But how to deal with? Usually most of hosters make PHP to run in the safe mode to prevent abuses from malicious users so we can’t use “system()”. But on the contrary, Perl scripts are usually granted with sufficient permissions to access /proc/.. Thus our system will consist from two parts:

    1) A Perl script which would get uptime and loadavg from /proc/..
    2) A PHP wrapper class (as Borland says, be objective ) which has to be easily embeddable in your PHP scripts

    Let’s start with the Perl script. This is an easy job: “cat”, the unix command which echoes the contents of the specified file, will help us. Finally you’ll come up with something like this:

    Code:
    #!/usr/bin/perl
    
    print "Content-type: text/html\r\n\r\n";
    
    chop( $load   = `cat /proc/loadavg` );
    chop( $uptime = `cat /proc/uptime`  );
    
    print $load . "::" . $uptime;
    The first line which starts with a sharp is the path of the Perl interpreter. You can usually find it on your hoster’s website (FAQ section?) or in the server information section of your control panel.

    The script echoes the standard headers to the browser, then gets the uptime and load from /proc/… , trims the whitespaces and finally echoes the stuff.

    Note that usually Perl scripts should be uploaded to the website in ASCII mode (which is supported by most of FTP clients) and sometimes you might need to CHMOD it (grant the “execute” permission). That entirely depends on your hoster!

    Now the PHP wrapper class. You will need “remote fopen” (which is usually enabled with most of hosters) for this to work. In fact the script will attempt to run the Perl one from exterior and parse its output.

    Anyway, this might not be the best way to do if you have access to the cron tab (unix task scheduler). If you do, it would be reasonable to cron the script getting the uptime from /proc/.. and writing in to the text file, because that way it will take less server resources. This will be described in the next article (as well as image generation) if someone is interested in. But in any case if you don’t have zillions of visitors the approach described here will do for you.

    So the class has to be initialized with the URL of the Perl script, e.g. “http://yoursite.com/uptime/uptime.pl”. It will parse the output in the following properties:

    load_now – is the current server load
    load_5 – the average for the last 5 minutes
    load_15 – the average for the last 15 minutes

    “uptime” is an array containing the following information:
    uptime["d"] – X days up
    uptime["h"] – + Y hours up
    uptime["m"] – + Z minutes up
    uptime["s"] – + N seconds up
    “uptime.php” is an example of use…

    The code is well commented, but if you have a question just ask… Feel free to download the whole pack – it’s attached to the post. Happy loading and uptiming
    Attached Files Attached Files
    ------------------------------------
    [URL=http://www.dedicatedzone.com]Managed Dedicated Servers[\URL]

  2. #2
    Active Member Revenant is on a distinguished road
    Join Date
    May 2004
    Location
    Chicago, IL US of A
    Posts
    108
    pretty good, i've no clue about perl tho
    // Rev
    // Ivan Alfaro
    -- Professional Web Developer

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts