LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-05-2007, 12:08 AM   #1
Networks
LQ Newbie
 
Registered: Feb 2006
Posts: 11

Rep: Reputation: 0
PHP Load Time Script?


Does anyone know of a way to get the server load times through a PHP script.

I'd like to track load times and shut down the site if it gets above a certain load time.
 
Old 05-05-2007, 12:35 AM   #2
jaykup
Member
 
Registered: Jan 2006
Location: Mukwonago, WI
Distribution: Slackware 12
Posts: 77

Rep: Reputation: 16
Show Load Average:

PHP Code:
$os strtolower(PHP_OS);
         
if ( 
$os !== 'winnt' AND $os !== 'win32' )
{
    if ( 
file_exists('/proc/loadavg') )
    {
         
$data file_get_contents('/proc/loadavg');
         
$loads explode(' '$data);

         return 
$loads[0];
    }

Top is the only program on linux that I know of to show the processor usage. However, I am unsure how to display that in php.


If you were talking about page generation times:

At the top of the page:
PHP Code:
<?php
$time 
microtime();
$time explode(' '$time);
$time $time[1] + $time[0];
$start $time;
?>
At the bottom of the page:
PHP Code:
<?php
$time 
microtime();
$time explode(' '$time);
$time $time[1] + $time[0];
$finish $time;
$total_time round(($finish $start), 4);
echo 
'Page generated in '.$total_time.' seconds.';
?>

Last edited by jaykup; 05-05-2007 at 12:37 AM.
 
Old 05-05-2007, 04:27 AM   #3
Networks
LQ Newbie
 
Registered: Feb 2006
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by jaykup
Show Load Average:

PHP Code:
$os strtolower(PHP_OS);
         
if ( 
$os !== 'winnt' AND $os !== 'win32' )
{
    if ( 
file_exists('/proc/loadavg') )
    {
         
$data file_get_contents('/proc/loadavg');
         
$loads explode(' '$data);

         return 
$loads[0];
    }

Top is the only program on linux that I know of to show the processor usage. However, I am unsure how to display that in php.
Hey thanks, I am not sure I have a file in any directory called /proc/loadavg. Am I supposed to tell my server to save it every so often?

I looked into it, I think exec() or shell_exec() can be used but unfortunately I get some sort of infamous backtick safemod error that doesn't allow me to execute the commands and retrieve the data to parse so I need an other way around it or fix that one which I don't know how yet.

The code you provided does not get load times seems like I am missing a piece to the puzzle?
 
Old 05-05-2007, 02:12 PM   #4
jaykup
Member
 
Registered: Jan 2006
Location: Mukwonago, WI
Distribution: Slackware 12
Posts: 77

Rep: Reputation: 16
I'm supprised you don't have the /proc/loadavg. I assume you tried "cat /proc/loadavg" ?

Either way, the server may not have permissions to access that file.

Here is a new way of formatting the output of "uptime" to get the load averages.

PHP Code:
<?php

$os 
strtolower(PHP_OS);
         
if ( 
$os !== 'winnt' AND $os !== 'win32' )
{
         
$data exec('uptime');
         
$load explode(' average: '$data);
         
$load_each explode(', ',$load[1]);
         echo 
"Real: {$load_each[0]}, User: {$load_each[1]}, Sys: {$load_each[2]}";
}  

?>
Provided you are on linux and are able to run "uptime" this will work for you.

But it looks like you can't run the exec command. Try to write a bash script to dump the output of uptime into a file, and read the file.

bash script:

Code:
#!/bin/bash
uptime > /wwroot/load_averages.txt
Add that to your crontab to run every X minutes

Then replace the $data = exec('uptime') with $data = file_get_contents('load_averages.txt')

That will look something like this:

PHP Code:
<?php

$os 
strtolower(PHP_OS);
         
if ( 
$os !== 'winnt' AND $os !== 'win32' )
{
         
$data file_get_contents('load_averages.txt');
         
$load explode(' average: '$data);
         
$load_each explode(', ',$load[1]);
         echo 
"Real: {$load_each[0]}, User: {$load_each[1]}, Sys: {$load_each[2]}";
}  

?>
The load_averages.txt will need to be in the same directory as this current php file, or change the path.

Hope this helps

Both work on my Slackware 10.2 server running php 4.1

Last edited by jaykup; 05-05-2007 at 02:24 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
finding out which time zone a person is in from a PHP script microsoft/linux Programming 7 03-01-2007 05:52 PM
execute php script by a time () afbase Linux - General 1 12-30-2006 03:30 PM
script to load software during boot time dsids Linux - Newbie 3 11-06-2006 06:52 AM
shell_exec() and friends terminate PHP script (or time out?) MicahCarrick Programming 3 09-05-2006 04:37 AM
PHP last time modified script time way off GabeF Programming 2 11-05-2002 08:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration