dslreports logo
site
 
    All Forums Hot Topics Gallery
spc

spacer




how-to block ads


Search Topic:
uniqs
233
share rss forum feed


Mospaw
My socks don't match.
Hawaiian Jellyfish
join:2001-01-08
Mile High
kudos:1

Server load in PHP

In response to Ping Pong's "Share Code" thread, here's my contribution.

I've always wondered how Justin got the server load. I don't know how he pulls it off, but I wrote a small PHP function to store page hits by time in a MySQL database. It automatically deletes old page hits based on the $duration variable (I like very 60 seconds) so they don't figure into the count.

The number provided is the number of page hits from pages that call the function. It's in PHP and MySQL, but conversion to another database or even language should be trivial.
<?PHP

// FILE:
// serverload.php
//
// Author:
// Chris Mospaw - www.zaffix.com

function serverload () {

// Use:
// INCLUDE the file in your script (or cut and paste in)
// assign to a variable $serverload = serverload();
// This will ONLY track page counts from pages that call the
// serverload() function. If you want a page counted, but do
// not need the results, simply call the function without
// assigning a variable.
//
// Requirements:
// MySQL database set up with a table (user selectable) in a
// database (user selectable). Table has one field, time, which
// is defined as INT(14) type.

// Define variables

$database = "serverload"; // Name of MySQL database
$hostname = "localhost"; // Name of database server
$username = "root"; // MySQL user
$password = ""; // Password for user
$tablename = "serverload"; // Table in database above that information is stored in

$duration = "60"; // How many seconds load will represent.

// Open MySQL connection and database

mysql_pconnect($hostname,$username,$password) or
die ( "Could not connet to MySQL" );

mysql_select_db($database) or
die ( "Could not connect to database" );

// Delete old page counts

$sql = "DELETE FROM $tablename WHERE time " . (time()-$duration) ;

$result = mysql_query($sql) or
die ("Error:" . mysql_error() );

// Insert the current page count

$sql = "INSERT INTO $tablename (time) VALUES (" . time() . ") ";

$result = mysql_query($sql) or
die ("Error:" . mysql_error() );

// Get page count (number of rows in the table)

$sql = "SELECT time FROM $tablename";

$result = mysql_query($sql) or
die ("Error:" . mysql_error() );

return mysql_num_rows ($result);

} // END FUNCTION serverload

?>
--
"You can't truly understand something until you can take it out of context."
--
[text was edited by author 2002-01-19 15:11:10]


ChipmunkWeb

join:2001-08-28
Spokane, WA
»/aboutload

The server load indicator is created from the unix 'load average' system statistic. It is calculated dynamically from the last few minutes of activity on the servers. Each » that is lit indicates 1/5th of what we consider to be maximum efficient load. With all » lit up, you may notice some complex pages on the site take a while to load.

The RPM figure indicates how many pages per minute we are currently printing.

If the 5 » icons are replaced by the word overload, our server may not produce all pages correctly, or maybe in some difficulty. In that case, please visit later when the problem has been corrected.
--
"Is your father a Millionare or a senate member?""No, Sir""Then you're up shit creek, Popinjay, without a Paddle" -- Catch 22


Zhen-Xjell
Prolific Bunny
Premium,ExMod 2001-04
join:2000-10-08
Bordentown, NJ
reply to Mospaw
Thanks for sharing.. guess I'll have to create another top level link for shared code.


Mospaw
My socks don't match.
Hawaiian Jellyfish
join:2001-01-08
Mile High
kudos:1
reply to ChipmunkWeb
ChipmunkWeb, great link. Thanks for the explanation.

After thinking about the whole thing a little bit, I could just pull the last 60 seconds (or however long) requests out of the Apache log file, but I suspect that would put more strain on the server than my script.

Plus, my script will work anywhere that PHP and MySQL are available, no matter the web server being used or the hosting OS.
--
"You can't truly understand something until you can take it out of context."
--