Showing server load

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Showing server load

Post by phice »

Can any of you guys help me with showing the server load? Is it possible to show it on Windows as well? Thanks very much.
Image Image
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

server load?

do you mean page generation time? :? :?:
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

Some pages have "Server Load: 0.12" or something to the similar.

If you look at the bottom of phpBB 2.2, you can see
[ Time : 0.091s | 7 Queries | GZIP : Off | Load : 0.13 ]
The value of Load: is what I'm trying to get.
Image Image
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

phice wrote:Some pages have "Server Load: 0.12" or something to the similar.

If you look at the bottom of phpBB 2.2, you can see
[ Time : 0.091s | 7 Queries | GZIP : Off | Load : 0.13 ]
The value of Load: is what I'm trying to get.
hmm interesting phice, do you know exactly how its calculated or what? i see no reference to this in the manual?
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

That's what I'm asking..

I found somewhere that you read a directory for some oblivious file. The code follows (though doesnt work on my linux host, nor my windows box (duh ;)):

Code: Select all

if($fh = @fopen("/proc/loadavg", "r"))
           {
              $data = @fread($fh, 6);
              @fclose($fh);
              $load_avg = explode(" ", $data);
              $load_avg = trim($load_avg[0]);
           } else {
              $load_avg = "<font color="#FF0000">error</font>";
           }
Image Image
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

here is how to access the api in Windows machines.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

so, what is the number rounded/counted by, cpu-usage or something?
sorry if you do not understand, can't come up with thw wird for it...
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

:BUMP:

this is a very interesting subject, does anyone know the answer?

phice, maybe you can shoot an e-mail of to the dev's of PHPBB asking them?
feyd wrote:here is how to access the api in Windows machines.
that cant be how PHPBB does it, because most hosting companies are linux, however this may prove useful
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

You sure that is httpd server load, and not MySQL load? I'm thinking the later and likely by using either:

Code: Select all

SHOW VARIABLES
SHOW STATUS
Not entirely sure tho, so dont bite my head of =)
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

malcolmboston wrote::BUMP:

this is a very interesting subject, does anyone know the answer?

phice, maybe you can shoot an e-mail of to the dev's of PHPBB asking them?
feyd wrote:here is how to access the api in Windows machines.
that cant be how PHPBB does it, because most hosting companies are linux, however this may prove useful
http://www.phpbb.com/phpBB/viewtopic.php?t=190156
Image Image
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Code: Select all

<?php
/*

Realtime Apache Bandwidth-Based Redirection
AKA *Very* Inexpensive Load Balancing
Copyright (C) 2002 Alex Moundalexis. All Rights Reserved.
Email: alexm at mail.rit.edu

o Purpose

  When hosting sites on-campus, the network guys get <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span> when you
  consume large amounts of bandwidth. So to get them off my back, I
  wrote this to distribute extraneous load to other servers.

o License

  This script is licensed as freeware. Use it however you wish,
  so long as my contact information remains on the header. I
  provide it so that others may learn from it.

  This script is provided as is, without any warranty of usability.
  Use it at your own risk.

o Requirements

  Apache with Extended Status and /server-status enabled.
  PHP 4.0+

o Instructions

  Setup the variables here, and uncomment the method
  you want to use below. Piece of cake, really. It's a quick snippet
  that I found useful. It'd be easy to add a randomizing script to pick
  the mirror.

  As usual, all this crap is longer than the actual script. 

*/

$server = "127.0.0.1";            // Your server.
$maxband = "500000";            // Maximum Bandwidth before rollover in BYTES.
                    // 500000 is 500k, etc.
$mirror = "mirror.yoursite.com";    // The mirror server.

$handle = fopen ("http://$server/server-status?auto", "r");
while (!feof($handle)) {
     $buffer=fread($handle,4096);
}
fclose($handle);

/* Returns the following stuff...
Total Accesses: 22403
Total kBytes: 434115
CPULoad: .341125
Uptime: 612319
ReqPerSec: .0365871
BytesPerSec: 725.984
BytesPerReq: 19842.6
BusyServers: 3
IdleServers: 7
Scoreboard: _W_____KW_... etc etc etc
*/

list($th,$tb,$cpu,$uptime,$rs,$bs,$br,$busy,$idle,$scoreboard)= split ("\n", $buffer);

/* Additional things to parse... don't need them here, but they're available.
$th = ereg_replace(".*Total Accesses: ", "", $th);
$tb = ereg_replace(".*Total kBytes: ", "", $tb);
$cpu = ereg_replace(".*CPULoad: ", "", $cpu);
$uptime = ereg_replace(".*Uptime: ", "", $uptime);
$rs = ereg_replace(".*ReqPerSec: ", "", $rs);
$br = ereg_replace(".*BytesPerReq: ", "", $br);
*/

$bs = ereg_replace(".*BytesPerSec: ", "", $bs);

if ($bs > $maxband) {
  $location = "http://$mirror/$file";
} else {
  $location = "http://$server/$file";
}

// Test Method
echo "<p>$server limit is $maxband bytes. Current bandwidth is $bs.</p>";
echo "<p>Sending you to <b>$location</b>.</p>";

// Method 1: Send them directly to the file. Good for files.
// header("Location: $location");

// Method 2: Embed the path as text (in an IMG tag, etc).
// To use, where you'd stick the source, call PHP with the following code:
// readfile("http://path.com/to/load.php?sn=file");
// echo $location;

?>
(Googled 0.87 minutes... ;))
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

[quote=PHP]
Warning: fopen(http://127.0.0.1/server-status?auto): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in J:\Server\htdocs\test.php on line 45
[/quote]
doesn't seem to work for me :?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

o Requirements

Apache with Extended Status and /server-status enabled.
Post Reply