TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 04-26-2009, 06:28 PM   #1 (permalink)
The Acquainted
 
Join Date: Sep 2007
Posts: 126
Thanks: 4
Sam Granger is on a distinguished road
Default CPU load function - division by zero error :(

The following function seems to give me a Division by Zero error - not all the time but quite a lot of the time.

PHP Code:
class Linux_Server extends ServerLoad {
    function 
loadAverage($bar false) {
        
$results = array();
        if (
$fd = @fopen('/proc/loadavg''r')) {
            
$results['avg'] = preg_split("/\s/"fgets($fd4096),4);
            unset(
$results['avg'][3]);    // don't need the extra values, only first three
            
fclose($fd);
        } else {
            
$results['avg'] = array('N.A.''N.A.''N.A.');
        } 
        if (
$bar) {
            if (
$fd = @fopen('/proc/stat''r')) {
                
fscanf($fd"%*s %Ld %Ld %Ld %Ld"$ab$ac$ad$ae);
                
// Find out the CPU load
                // user + sys = load 
                // total = total
                
$load $ab $ac $ad;    // cpu.user + cpu.sys
                
$total $ab $ac $ad $ae;    // cpu.total
                
fclose($fd);

                
// we need a second value, wait 1 second befor getting (< 1 second no good value will occure)
                
sleep(1);
                
$fd fopen('/proc/stat''r');
                
fscanf($fd"%*s %Ld %Ld %Ld %Ld"$ab$ac$ad$ae);
                
$load2 $ab $ac $ad;
                
$total2 $ab $ac $ad $ae;
                
$results['cpupercent'] = (100*($load2 $load)) / ($total2 $total);
                
fclose($fd);
            }
        }
        return 
$results;
    }

PHP Code:
$results['cpupercent'] = (100*($load2 $load)) / ($total2 $total); 
- the line that gives the error.

What am I doing wrong?
Sam Granger is offline  
Reply With Quote
Old 04-26-2009, 10:01 PM   #2 (permalink)
The Acquainted
 
Join Date: Sep 2007
Posts: 126
Thanks: 4
Sam Granger is on a distinguished road
Default

Doh! total and total2 sre the same value in many cases ofcourse! Silly me. Solved :)
Sam Granger is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create a gallery class Tanax Advanced PHP Programming 25 02-19-2013 04:25 AM
Next class project? allworknoplay The Lounge 6 04-18-2009 08:33 PM
[Tutorial] How to organize your classes | Part 1 Tanax Advanced PHP Programming 10 03-01-2009 10:08 PM
Getting Error: is not a function when viewed in firefox - works in IE almac007 Javascript, AJAX, E4X 9 12-05-2008 09:27 AM
Using Exceptions to Provide Better Feedback Wildhoney Tips & Tricks 5 11-19-2007 06:55 AM


All times are GMT. The time now is 09:48 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design