Topic: Server Load Script- Can you spot the output?

Hey folks,
I came across a little script for doing server load and it works great. Problem is it is outputing a text line that is displaying out of place. I load some stuff into variables, then dump the variable when done. Problem is this "text string" is showing up from within the function, and not down where I echo the return...

Example: http://www.phpmywebthing.com underneath center output is string:
10:45am up 31 days, 14:38, 0 users, load average: 2.33, 2.56, 2.86

Question- I can't seem to spot where the string is being echo'd / output other than the return of the function. Any help appreciated...

Here is the function:

// Get The Server Load
function get_ServerLoad()
    {
        if(PHP_OS != 'WINNT' && PHP_OS != 'WIN32') {
            if(file_exists('/proc/loadavg') ) {
                if ($fh = @fopen( '/proc/loadavg', 'r' )) {
                    $data = @fread( $fh, 6 );
                    @fclose( $fh );
                    $load_avg = explode( " ", $data );
                    $server_load = trim($load_avg[0]);
                }
            } else {
                $data = @system('uptime');
                preg_match('/(.*):{1}(.*)/', $data, $matches);
                $load_arr = explode(',', $matches[2]);
                $server_load = trim($load_arr[0]);
            }
        }
        if(!$server_load) {
            $server_load = 'N/A';
        }
        return $server_load;
    } 
Every Day Above Ground Is A Good One!!

Re: Server Load Script- Can you spot the output?

echo get_ServerLoad();

should make the output in the page using the function... I cannot spot anything in the function that puts out any text ... so look again on where it's used ...

Re: Server Load Script- Can you spot the output?

You can have a look at the code in admin_index.php that shows the load.

"Programming is like sex: one mistake and you have to support it for the rest of your life."

4 (edited by middleground 2004-06-15 16:29)

Re: Server Load Script- Can you spot the output?

Rickard wrote:

You can have a look at the code in admin_index.php that shows the load.

Thanks Rickard, I did peek at it, slightly different as this one seems to check for Win OS

Frank: I double checked and made two quick files:

http://www.phpmywebthing.com/addons/temp/sload.php

which runs ita couple ways- see text...

-----------------------------------------------------------------
and this one:
http://www.phpmywebthing.com/addons/temp/sload_raw.php

which is only the following code:

<?php
        if(PHP_OS != 'WINNT' && PHP_OS != 'WIN32') {
            if(file_exists('/proc/loadavg') ) {
                if ($fh = @fopen( '/proc/loadavg', 'r' )) {
                    $data = @fread( $fh, 6 );
                    @fclose( $fh );
                    $load_avg = explode( " ", $data );
                    $server_load = trim($load_avg[0]);
                }
            } else {
                $data = @system('uptime');
                preg_match('/(.*):{1}(.*)/', $data, $matches);
                $load_arr = explode(',', $matches[2]);
                $server_load = trim($load_arr[0]);
            }
        }
        if(!$server_load) {
            $server_load = 'N/A';
        }
?>

and it is printing out the line. It is running the else branch.


-------------------------------------------------------------------------

OK- figured it out, it is the @system function (or @passthru if used) that is outputing the string. RTFM I guess http://us3.php.net/manual/en/function.system.php

Change to @exec and is fine... smile

Which is what Rickard has smile

Thanks all....

Every Day Above Ground Is A Good One!!

Re: Server Load Script- Can you spot the output?

ah, nice that you figured it out ... gotta take a note in my brain on that one, if I would happen to do something similar with system/exec (already used exec though to start a dedicated gameserver from a webpage) big_smile

6 (edited by asd_sq 2004-06-16 06:56)

Re: Server Load Script- Can you spot the output?

that's tough