Solved

How to use sar command

Posted on 2004-10-02
18
2,815 Views
Last Modified: 2013-12-26
I am creating now a shell script for non skilled users to monitor the CPU usage of SUN machine with solaris 8 using sar, and here is my command:
 sar 1 5|awk '{print "CPU usage at "$1" ïs"100-$5"%"}'
 and I want to ask the following:
   1) To get the usage of the CPU is it correct to subtract the idle vprecentage from 100?
   2) When I use this command the awk gives me the output just after it processes, and buffers the 5 outputs of sar, I have tried to increase  the value of 5, but it always gives me the output just after buffering all, is there any way to get the output of the script on the screen one by one instead of buffering it?
   3) I used in my command '5' to get 5 outputs of the sar, but is there any maximum for that, can I use a number to monitor the entire day second by second with sar? does it forms any impact on the system?
  4) Is there anyway to let this command works on the background to create a daily file for the usage of the CPU unattendanly?
0
Comment
Question by:A_HASSAN
  • 7
  • 4
  • 3
  • +3
18 Comments
 
LVL 20

Expert Comment

by:tfewster
Comment Utility
sar is not entirely accurate, but should be OK for an "average" workload; Note that running sar with samples every second will affect the results, as it consumes some CPU itself - Sun recommend intervals of > 5 seconds.

1) Seems fine to me; Note that this is an average across CPUs, so one CPU may be 100% used by a badly behaved app while others are idle.  `top` or dedicated performance tools like `cputrack` may be more useful to them.
2) Don't know
3) As far as I know, you can use very large numbers - Probably up to the limit of 32 bit numbers ;-)  As said above, it will use _some_ cpu
4) See `man sa1` - This is usually cronned, and collects samples (of ALL statistics) at specified intervals, placing them in /var/adm/sa/saDD (where DD=Day of month). Note that collecting stats every 15 minutes will create a daily datafile of about 2Mb - But this can be much larger if you have large numbers of disks! For a full month, ensure you have at least 60Mb free disk space. The users can then use sar with the -s and -e options to see what the usage was dusing a particular time period

Alternatively, you could create a custom cron job to just collect CPU stats, e.g.
0 * * * * sar -u 5 720 >> /var/adm/sa/my_sarlog_`date +%d` - But you'd need to write your own tools to query the file
0
 
LVL 48

Expert Comment

by:Tintin
Comment Utility
Why not use top or prstat for interactive use?

For setting up sar, do

crontab -e sys

and uncomment the entries and set appropriate time intervals.
0
 

Author Comment

by:A_HASSAN
Comment Utility
Is there any other alternative to calculate the CPU usage more accurately than sar, and without affecting the system, where I can get the load on each cpu?
I s there anysolution to solve the awk buffering point, as I am going to calculate the load over one day per second which would be too much buffer.
   Thanks
0
 
LVL 48

Expert Comment

by:Tintin
Comment Utility
top and prstat will give you much more acurate stats on cpu load.
0
 

Author Comment

by:A_HASSAN
Comment Utility
I have tested the prstat and I found it gives the cpu per process, and not the total usage, and I did not find any manual for top.
Can anyone guide me if the prstat can get the total usage of CPUs?
Can anyone guide me how to use the sa1 by the crontab, I did not find any directory created for in /var/adm/sa named as saDD, is this directory created automatically when I put the sa1 in the crontab, or I have to make a certain kind of scripts to create new directories, and purge old ones?
0
 

Author Comment

by:A_HASSAN
Comment Utility
I did not find any manual on the top command, can anyone guide me how to use the top command?
0
 
LVL 20

Expert Comment

by:tfewster
Comment Utility
Get `top` from http://www.sunfreeware.com if it's not already installed. Trust us, you'll love it ;-)  

The point about `prstat` is it allows user to track _their_ processes in detail, which is generally more useful to users than knowing how much CPU is being used overall.

If the directory /var/adm/sa already exists and is writable by the user "sys", just enabling sa1 in the crontab will start creating the files.  As the files are named sa01, sa02 etc, at the beginning of the next month sa01 will be overwritten. If you don't have enough space for a whole months sar logs, you'll need a cron job to clear down ones e.g. over a week old
0
 

Author Comment

by:A_HASSAN
Comment Utility
How can I enable the sa1 in the cronjob, can you give an example to get the average usage of the cpu per hour?
0
 
LVL 20

Expert Comment

by:tfewster
Comment Utility
From `man sa1` and Tintins comment:

`crontab -e sys`   and uncomment (or create) the entry:
0 * * * 0-6 /usr/lib/sa/sa1

(i.e. On the hour, every hour, every day: record the _average_ statistics since the last sample was taken by sa1)
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 38

Expert Comment

by:yuzh
Comment Utility
1) sar give you a statics figure, eg:

  # sar -u 2 100
     Will tell you how much time during the interval the CPU is
     spending in user, kernel and waiting for
     I/O modes.

man sar
to learn more details.

2) prstat utility  is a native Solaris tool (available from Solaris 8 or newer), better than "top"
see: http://developers.sun.com/solaris/articles/prstat.html
man prstat
to learn more details.

3) the old unix "ps" command (/usr/ucb/ps, Solaris shipped with two version of ps!)

/usr/ucb/ps -uax | head -15
will give you  the top 15 process which use most of your system resource.

/usr/ucb/ps -uax | awk '{if ($3 > 10 ) {print $2} }'

will print the process IDs which use more than 10% CPU at the CURRENT time.




0
 
LVL 9

Expert Comment

by:HamdyHassan
Comment Utility
0
 

Author Comment

by:A_HASSAN
Comment Utility
Dear All,
   As I am not having a system to extract the SNMP traps for my sun server, so I decided to go to PRSTAT, and MPSTAT, but I have some questions:
    1) Is mpstat tool accursate, and not forming any system load on contrary  to sar?
    2) In the prstat (only writing prstat) I usually get an output at the end of the command it is:
       Total: 97 processes, 190 lwps, load averages: 2.18, 2.15, 2.11
       Can any one please guide me the valuse 190,2.18,2.15,2.11 stands for what?
0
 
LVL 48

Expert Comment

by:Tintin
Comment Utility
lwps is Lightweight Processes

The load averages are the average number of jobs waiting in the run queue over the last 1 min, 5 min and 15 min.

0
 
LVL 38

Expert Comment

by:yuzh
Comment Utility
To learn more details:

man prstat

"man" is your friend for playing with *nix.
0
 

Author Comment

by:A_HASSAN
Comment Utility
I checked the tools and I found I am more intereseted in the mpstat tool, but my question, is accurate, and not forming any load on the system or it is like the sar process where it is not safe?
 
0
 
LVL 12

Expert Comment

by:stefan73
Comment Utility
Hi A_HASSAN,
Are you sure you have a use of output like:
c3user>mpstat
CPU minf mjf xcal  intr ithr  csw icsw migr smtx  srw syscl  usr sys  wt idl
  0  139   0  217    11    1  250    4   17   58    0   117   11   7   1  82
  1  125   0  193    11    1  244    4   16   56    0    57   11   6   1  82
  2  113   0  163    47   36  242    4   16   96    0    55   13   7   1  80
  3  110   0  390    94   85  237    4   16  133    0     0   12   7   1  80
 32  112   0  368    21   11  254    4   17   57    0   300   10   6   0  84
 33  111   0  304    28   18  253    4   17   56    0   273   10   6   1  84
 34  106   0   86    23   12  264    5   18   59    0   309   12   6   0  81
 35  107   0  135    74   63  260    5   18   59    0   357   12   6   1  81
 64  111   0  343    22   12  249    4   16   54    0   220    9   6   0  84
 65  109   0  273    13    3  254    4   16   54    0   221    9   6   0  84
 66  105   0   67    23   12  269    5   18   59    0   278   11   6   0  82
 67  108   0  158    14    2  270    5   18   60    0   323   12   6   0  82
 96  112   0  340    23   12  255    4   17   56    0   212    9   6   0  84
 97  110   0  305    12    1  255    4   17   56    0   217   10   6   0  84
 98  104   0  201    33   22  269    5   19   60    0   300   12   6   0  81
 99  107   0  222    13    1  265    5   19   59    0   329   12   6   1  81
384  115   0  102    30   21  228    3   14   49    0   121    7   6   0  86
385  108   0   42    10    1  225    3   14   47    0    75    7   6   0  86
386  101   0   99    10    1  237    4   15   49    0    81    8   6   0  86
387  100   0   69    10    1  237    4   14   48    0    63    8   6   0  86
416  100   0  356     9    1  224    3   13   45    0    11    7   5   0  87
417   98   0  312     9    1  223    3   13   45    0   383    7   5   0  87
418   94   0   31    10    1  236    4   14   47    0    23    8   5   0  86
419   95   0    1    71   63  233    3   14   46    0    33    8   5   0  86
448   97   0  310     9    1  224    3   13   44    0   387    7   5   0  87
449   96   0  313     9    1  224    3   13   44    0   372    7   5   0  87
450   94   0   21    10    1  233    3   14   47    0     7    8   5   0  86
451   94   0   14    71   63  232    3   14   46    0     4    8   6   0  86
480   95   0  340     9    1  225    3   13   44    0   366    7   5   0  87
481   94   0  315     9    1  224    3   13   44    0   359    7   5   0  87
482   91   0   20    10    1  235    4   14   46    0   388    8   5   0  86
483   91   0   25    10    1  231    3   14   45    0    13    9   5   0  85
512  121   0   11   304  192  310    8   28   94    0   354   21   8   1  71
513   73   0   57    16    1  310    9   29   68    0   142   29   7   1  63
514   92   0  135    18    1  329   10   35   76    0   266   27   8   1  65
515  106   0  215    18    1  341   10   36   81    0   353   25   8   1  67

...not too descriptive, don't you think?

Cheers!

Stefan
0
 

Author Comment

by:A_HASSAN
Comment Utility
Hi stefan73,
  The output of mpstat is good for me to determine which processor is ;loaded more that the other,, while vmstat is giving the use of the entire CPU of the box, the point is does anyone think that such commands might cause any harm to the the system and not causing ay overload on it, and do they give accurate output to the system.
0
 
LVL 20

Accepted Solution

by:
tfewster earned 500 total points
Comment Utility
The accounting processes won't cause any "harm" to the system; If all CPUs are 100% used all the time, then they will slow the system down very, very slightly, but it's worth it if it helps you diagnose the _real_ problem - I'm more concerned that you will end up with HUGE logfiles.

sar is "less accurate" than Suns proprietary tools as it's a standard across most versions of Unix, so it doesn't have the low-level system calls that Sun-proprietary tools (like mpstat) have - But on 99.9% of systems, the sar info will be good enough.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

763 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

11 Experts available now in Live!

Get 1:1 Help Now