linickx Posted January 20, 2007 Share Posted January 20, 2007 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 Loopforeach($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 ? Quote Link to comment https://forums.phpfreaks.com/topic/34976-snmp-results-inaccurate/ Share on other sites More sharing options...
Destruction Posted January 20, 2007 Share Posted January 20, 2007 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 Link to comment https://forums.phpfreaks.com/topic/34976-snmp-results-inaccurate/#findComment-164960 Share on other sites More sharing options...
redbullmarky Posted January 20, 2007 Share Posted January 20, 2007 more of a maths question, but it'll depend what the MAX total could be. you'd need something like(ACTUAL TOTAL / MAX POSSIBLE TOTAL) * 100which you'd need to check. Quote Link to comment https://forums.phpfreaks.com/topic/34976-snmp-results-inaccurate/#findComment-164964 Share on other sites More sharing options...
linickx Posted January 20, 2007 Author Share Posted January 20, 2007 [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 ? Quote Link to comment https://forums.phpfreaks.com/topic/34976-snmp-results-inaccurate/#findComment-165068 Share on other sites More sharing options...
Destruction Posted January 21, 2007 Share Posted January 21, 2007 Just to clarify: I was pointing out that you have strlen val1 instead of val2, wondering if it made any difference. Quote Link to comment https://forums.phpfreaks.com/topic/34976-snmp-results-inaccurate/#findComment-165194 Share on other sites More sharing options...
linickx Posted January 21, 2007 Author Share Posted January 21, 2007 [quote author=Destruction link=topic=123245.msg509352#msg509352 date=1169343275]Just to clarify: I was pointing out that you have strlen val1 instead of val2, wondering if it made any difference.[/quote] DOH !! :-[ Quote Link to comment https://forums.phpfreaks.com/topic/34976-snmp-results-inaccurate/#findComment-165569 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.