Jump to content

Recommended Posts

Hi,

I want to write my cpu usage into a database (which will eventually be graphed), I've read that you have to add up ssCpuRawUser.0, ssCpuRawSystem.0, ssCpuRawNice.0 to get total cpu usage.

I've started my poller...

[code]
<?php

# Note to me !
# Command Line snmpget -v 2c -c NOT-public localhost ssCpuRawSystem.0

// ez_sql setup.
require_once "config.php";

// SNMP Setup
$TARGET = "localhost";
$CSTRING = "public";
$OIDs = array( 1 => 'ssCpuRawUser.0', 'ssCpuRawSystem.0', 'ssCpuRawNice.0');

// The Loop
foreach($OIDs as $CURRENT_IOD) {
        // Do SNMP Stuff
        $VAL1 = snmpget($TARGET, $CSTRING, $CURRENT_IOD);
        sleep(2);
        $VAL2 = snmpget($TARGET, $CSTRING, $CURRENT_IOD);
        // Fix Values to do Maths...
        $stringlength = strlen($VAL1);
        $VAL1 = substr($VAL1, 11, $stringlength);
        $stringlength = strlen($VAL1);
        $VAL2 = substr($VAL2, 11, $stringlength);
        $DELTA = ( $VAL1 - $VAL2 );
        if ( $DELTA < 1 ) {
                $DELTA = $DELTA * -1;
        }
        $ANSWER = ( $DELTA / 200 ) * 100;

        // debuggered
        // echo "$CURRENT_IOD : $VAL1 : $VAL2 = $ANSWER% <br />";

        $NOW = time();
        // Save Results
        $db->query("INSERT INTO snmp_results (id, oid, date, result) VALUES (NULL, \"$CURRENT_IOD\", $NOW, $ANSWER)");
        // more debuggered
        #$db->debug();
}

?>
[/code]

But one of the ssCpuRawUser.0 has come back as 200% !!!!! can anyone spot what I'm doing wrong ?
Link to comment
https://forums.phpfreaks.com/topic/34976-snmp-results-inaccurate/
Share on other sites

Could it be this part...
$stringlength = strlen($VAL1);
$VAL2 = substr($VAL2, 11, $stringlength);

If not, at least that's one thing that'd need changing perhaps.  Although realistically it sounds like it's the math that's at fault.  Also dividing by 200 then multiplying by 100 is the same as dividing by 2, so I'm wondering why the extra work?  Also, why multipy by negative 1?

Dest
[quote author=Destruction link=topic=123245.msg509118#msg509118 date=1169293045]
Could it be this part...
$stringlength = strlen($VAL1);
$VAL2 = substr($VAL2, 11, $stringlength);
[/quote]

This bit was to fix, the output, which by default is "Counter32: 123456", and I just wanted to grab "123456"

[quote author=Destruction link=topic=123245.msg509118#msg509118 date=1169293045]
If not, at least that's one thing that'd need changing perhaps.  Although realistically it sounds like it's the math that's at fault.  Also dividing by 200 then multiplying by 100 is the same as dividing by 2, so I'm wondering why the extra work?  Also, why multiply by negative 1?
[/quote]

You're right, the maths is rubbish ([i]& my maths[/i]), I was trying to get it readable... I've lost the reference I read, but CPU percentage, was something like, take 2 snmp_get readings, take the delta* of the two counter readings, and divide that number by the time (in ticks**) between the two readings... then finally multiply by 100 to get a percentage.

*delta, I reckon, this is a positive number between numbers, i.e. the difference between 3 & 5 is -2, where as the delta is 2... hence multiply by -1 ([i]I might be wrong[/i]).

**ticks, I think a tick is 0.01 seconds ([i]again could be wrong again[/i]).

I wonder if anyone here has tried to do anything similar ?
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.