Friday, April 25, 2014

Use SAR to Monitor CPU Utilization

SAR (System Activity Reporting) is a common *nix utiliy that can be used to track cpu utilization, memory utilization etc.
 
A common scenario is the need capture cpu statistics during some kind of load testing and then later report on those.  To capture cpu statistics with SAR you can do the following:
 
  1. Logon to the host in question and make sure that sar is installed (run sar from the command line).
  2. To capture statistics every 2 seconds to a file called "mystats.sar" in your home directory, you would execute the following command: sar -o ~/mystats.sar -u 2 0 >/dev/null 2>&1.  Note that this will keep gathering statistics until you kill it using ctrl-c.
  3. To run that same command in the background add an "&" to the end.  You can also "nohup" it so that it keeps running after you logoff - but don't forget to turn it off.  If you background it, then you can find it by typing in "jobs" and it will show you all processes you have put in the background.  You can then kill it with "kill %<jobnum>".  Or you can put it in the foreground "fg %<jobnum>" and then kill it with ctrl-c.
  4. Now you have a file that contains sar statistics, you can retrieve the contents of the file using something like: sar -f ~/mystats.sar.  If you want to just take a timeslice from the file, then you can use the -s hh:mm:ss and -e hh:mm:ss parameters.
See the sar manual page (man sar) for more details.
On Production, SAR are being generated under this directory: /var/log/sa/

No comments:

Post a Comment