Solved

How to use sar command

Posted on 2004-10-02
18
2,820 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
ID: 12208837
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
ID: 12209886
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
ID: 12210152
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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 48

Expert Comment

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

Author Comment

by:A_HASSAN
ID: 12212697
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
ID: 12212713
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
ID: 12212751
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
ID: 12212937
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
ID: 12213120
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
 
LVL 38

Expert Comment

by:yuzh
ID: 12214175
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
ID: 12217369
0
 

Author Comment

by:A_HASSAN
ID: 12242239
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
ID: 12243450
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
ID: 12244720
To learn more details:

man prstat

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

Author Comment

by:A_HASSAN
ID: 12269092
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
ID: 12284812
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
ID: 12367262
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
ID: 12388029
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
repeatSeparator  java  challenge 13 57
Excel Use VBA to get user's Mac Address for their computer 5 166
move a line in eclipse 3 102
isEverywhere  challenge 19 75
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

772 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