Solved

How can I accurately graph the average CPU usage... not just load average?

Posted on 2004-04-25
8
569 Views
Last Modified: 2012-08-13
I am writing a perl script that does some forking work, and I would like to monitor the loads of the computer running it.  The load averages are typically around 3 - 6 over 1, 5, 15 minutes, but I seek more information than that.

I already watch the CPU usage on a 10 second interval, as presented by "ps -axo %cpu", but I don't think I am getting the whole picture.

When I use "top" it shows % User, % System, % Nice, % Interrupt, % Idle, but I don't think that "ps -axo %cpu" shows anything but the % User.  How can I get a more accurate picture of how much load the system is under?

I haven't been able to find a perl module that does this.

Thank you.
0
Comment
Question by:vancetech
8 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 10914597
You could try

time perlscript
0
 
LVL 20

Expert Comment

by:jmcg
ID: 10915004
There are multiple meanings for the phrase "how much load the system is under". Since it sounds like you're trying to get a more comprehensive picture rather than a single "number", you might like a utility like 'vmstat' or combine it with 'iostat'.

Within your application, you can use the 'times' operator to get information similar to what is reported by the command-line utility 'time'.
0
 
LVL 3

Expert Comment

by:PerlKing
ID: 10915675
I have a suggestion for you.

Once you have chosen your metric, you could very easily write a perl script that call "top" periodically, pick up the relevant information and print it in a file. Say you chose, the "memory usage". You call top every second and pick up the "memory usage" of your script. Now print all this in a file.

After you are done with the script, you could use "gnuplot" to plot a graph of this data which gives you a broader picture or the trend of memory usage by your script.

Of course, once you have such a monitoring script in place, you could make it look for multiple parameters at a time.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 12

Accepted Solution

by:
stefan73 earned 63 total points
ID: 10916716
Hi PerlKing,
> call "top" periodically

You can simply open the output of top, like:

open(TOP,"top -b -s10 -d15 -u 0|") or die "No top";

And then just parse the output. An empty line ends a single output.

(check top man pages for the options used)


Cheers,
Stefan
0
 
LVL 3

Expert Comment

by:PerlKing
ID: 10916736
Sorry I was not clear with my wordings, but that is exactly what I meant too!
Thanks for clarifying it stefan.
0
 
LVL 18

Assisted Solution

by:kandura
kandura earned 62 total points
ID: 10917100
try parsing the contents of /proc/stat yourself instead of using top.
the first line will give you counters for overall cpu usage.
A sample:

cpu  3028583 89870834 2870091 9335

Then use something like:
         ($cpu[$r], $usr[$r], $nice[$r], $sys[$r], $idle[$r])= split / +/,$line;

when you take a new measurement. flip $r
         $r = 1 - $r
and convert the last two measurements to a percentage:

      $usr_100  =rate_prcnt($usr [$r],$usr [1-$r],$total[$r],$total[1-$r]);
      $nice_100 =rate_prcnt($nice[$r],$nice[1-$r],$total[$r],$total[1-$r]);
      $sys_100  =rate_prcnt($sys [$r],$sys [1-$r],$total[$r],$total[1-$r]);
      $idle_100  =rate_prcnt($idle[$r],$idle[1-$r],$total[$r],$total[1-$r]);

These four numbers give you the average cpu usage during the time between the two measurements.

sub rate_prcnt {
   my ($a1,$a2,$b1,$b2) = @_;
   if (!($a1&&$b1&&$a2&&$b2)) {
      return 0;
   }

   return (abs($b1-$b2)==0)?0:sprintf ("%.2f",100*abs($a1-$a2)/abs($b1-$b2));
}


Note that I grabbed the code for this from procallator.pl, which is a contribution to Orca, which is a systems monitoring and graphing package using RRD.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
transpose into pipe delemited 8 67
Executing multiple sybase statements in perl dbi 2 88
PERL - Find newest folder 12 118
Ned Perl Snippet to Read Files in Directory 5 62
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now