f



Average CPU Usage for multi CPU box.

I have a Solaris 8 system, and I want to write a shell script that
returns only the current average CPU utilisation from 0 - 100% for all
n CPU's on the box, so for a 4 CPU box I want the average of the CPU
utilisations for all 4 CPU's. So if the utilisations were
25,26,27,34, the average  is 28%.

Currently I have been offered a script like this

#!/bin/bash
idle=`vmstat 1 1| grep -v procs | grep -v swap | awk '{print $22}'`
echo "100 - $idle " | bc -l

but vmstat can give a result > 0 for the idle value (it seems to add
up the idles of all CPUs like this.

procs     memory            page            disk          faults
cpu

 r b w   swap  free  re  mf pi po fr de sr s6 sd sd sd   in   sy   cs
us sy id

 0 1 0 61646264 13160960 885 616 665 1420 1452 0 88 0 9 9 11 1354 1050
904 94 49 194

You can see that id value is 194. If I were to use this I'd get a
negative result from the 100 - computation.

It's very important that the answer be 0-100%.

Any ideas?

Dave

0
5/8/2007 3:41:17 PM
comp.unix.solaris 25994 articles. 1 followers. Post Follow

3 Replies
866 Views

Similar Articles

[PageSpeed] 55

On May 8, 8:41 am, dave.cla...@reflective.com wrote:
> I have a Solaris 8 system, and I want to write a shell script that
> returns only the current average CPU utilisation from 0 - 100% for all
> n CPU's on the box, so for a 4 CPU box I want the average of the CPU
> utilisations for all 4 CPU's. So if the utilisations were
> 25,26,27,34, the average  is 28%.
> Currently I have been offered a script like this
> #!/bin/bash
> idle=`vmstat 1 1| grep -v procs | grep -v swap | awk '{print $22}'`
> echo "100 - $idle " | bc -l
> but vmstat can give a result > 0 for the idle value (it seems to add
> up the idles of all CPUs like this.
> procs     memory            page            disk          faults
> cpu
>  r b w   swap  free  re  mf pi po fr de sr s6 sd sd sd   in   sy   cs
> us sy id
>  0 1 0 61646264 13160960 885 616 665 1420 1452 0 88 0 9 9 11 1354 1050
> 904 94 49 194
> You can see that id value is 194. If I were to use this I'd get a
> negative result from the 100 - computation.
> It's very important that the answer be 0-100%.

I doubt it

> Any ideas?

Didnt we answer this already in another thread today?
If you arent the same poster using another account my bad!

0
gerryt
5/8/2007 4:38:45 PM
On 8 May, 09:38, gerryt <lepsys...@gmail.com> wrote:
> On May 8, 8:41 am, dave.cla...@reflective.com wrote:
>
>
>
>
>
> > I have a Solaris 8 system, and I want to write a shell script that
> > returns only the current average CPU utilisation from 0 - 100% for all
> > n CPU's on the box, so for a 4 CPU box I want the average of the CPU
> > utilisations for all 4 CPU's. So if the utilisations were
> > 25,26,27,34, the average  is 28%.
> > Currently I have been offered a script like this
> > #!/bin/bash
> > idle=`vmstat 1 1| grep -v procs | grep -v swap | awk '{print $22}'`
> > echo "100 - $idle " | bc -l
> > but vmstat can give a result > 0 for the idle value (it seems to add
> > up the idles of all CPUs like this.
> > procs     memory            page            disk          faults
> > cpu
> >  r b w   swap  free  re  mf pi po fr de sr s6 sd sd sd   in   sy   cs
> > us sy id
> >  0 1 0 61646264 13160960 885 616 665 1420 1452 0 88 0 9 9 11 1354 1050
> > 904 94 49 194
> > You can see that id value is 194. If I were to use this I'd get a
> > negative result from the 100 - computation.
> > It's very important that the answer be 0-100%.
>
> I doubt it
>
> > Any ideas?
>
> Didnt we answer this already in another thread today?
> If you arent the same poster using another account my bad!- Hide quoted text -
>
> - Show quoted text -


Sorry Gerry, I can confirm that I am not the author of the other
thread, although he is a colleague, who for other work reasons has had
to bow out.

The problem is that neither of us are UNIX script writers, and as I
explained the mpstat appears to return multiple rows one for each CPU,
and the vmstat, iostat etc seem to return the sum of the usages rather
than the average. So unless I have misread the suggestions, I still
don't think we have a definitive answer.

I am sorry if this is irritating to you, but all our UNIX support (in
Bulgaria) is unavailable at the moment, so I am hoping that someone
out there will take pity on me and give me a chapter and verse answer.

Thanks for taking the time to reply in any case

0
dave
5/8/2007 5:51:58 PM
On May 8, 10:51 am, dave.cla...@reflective.com wrote:
> On 8 May, 09:38, gerryt <lepsys...@gmail.com> wrote:
> > On May 8, 8:41 am, dave.cla...@reflective.com wrote:
> > > I have a Solaris 8 system, and I want to write a shell script that
> > > returns only the current average CPU utilisation from 0 - 100% for all
> > > n CPU's on the box, so for a 4 CPU box I want the average of the CPU
> > > utilisations for all 4 CPU's. So if the utilisations were
> > > 25,26,27,34, the average  is 28%.
> > > Currently I have been offered a script like this
> > > #!/bin/bash
> > > idle=`vmstat 1 1| grep -v procs | grep -v swap | awk '{print $22}'`
> > > echo "100 - $idle " | bc -l
> > > but vmstat can give a result > 0 for the idle value (it seems to add
> > > up the idles of all CPUs like this.
> > > procs     memory            page            disk          faults
> > > cpu
> > >  r b w   swap  free  re  mf pi po fr de sr s6 sd sd sd   in   sy   cs
> > > us sy id
> > >  0 1 0 61646264 13160960 885 616 665 1420 1452 0 88 0 9 9 11 1354 1050
> > > 904 94 49 194
> > > You can see that id value is 194. If I were to use this I'd get a
> > > negative result from the 100 - computation.
> > > It's very important that the answer be 0-100%.
>
> > I doubt it
>
> > > Any ideas?
>
> > Didnt we answer this already in another thread today?
> > If you arent the same poster using another account my bad!- Hide quoted text -
>
> > - Show quoted text -
>
> Sorry Gerry, I can confirm that I am not the author of the other
> thread, although he is a colleague, who for other work reasons has had
> to bow out.
>
> The problem is that neither of us are UNIX script writers, and as I
> explained the mpstat appears to return multiple rows one for each CPU,
> and the vmstat, iostat etc seem to return the sum of the usages rather
> than the average. So unless I have misread the suggestions, I still
> don't think we have a definitive answer.

mpstat may indeed sum all CPUs into one convenient field and it
appears to be an average. I could only easily test this on my
2 CPU workstation though. A snap:
mpstat
CPU minf mjf xcal  intr ithr  csw icsw migr smtx  srw syscl  usr sys
wt idl
  0   80   4  237   464  300  668   95   46   68    0 101634   26
19   0  55
  1   53   2  270  1784  109  644  139   46   24    0 77903   21  14
0  65
vmstat 1 1
 kthr      memory            page            disk          faults
cpu
 r b w   swap  free  re  mf pi po fr de sr s2 s1 s3 sd   in   sy   cs
us sy id
 0 0 0 5587608 1333320 89 133 498 26 26 0 5 11 0 22  6 2248 179581
1312 24 17 60

I get 60 too : >

> I am sorry if this is irritating to you, but all our UNIX support (in
> Bulgaria) is unavailable at the moment, so I am hoping that someone
> out there will take pity on me and give me a chapter and verse answer.

Lately some folks have been multi posting sometimes many times a day
so out came the BOFH - sorry

Nice about awk is it will add a field if its a number and ignore it
otherwise.
My suggestion was to simply use awk and account for the number of
records minus the records that have no data. Using mpstat 1 1.

awks builtins makes finding a field at the end of line simple. Use $NF
Another builtin is NR or number of lines. You can do (NR-1) in the
case of mpstat.
awk has C like constructs like += so its easy to total $NF.
It also handles basic regular expressions.
If vmstat does the averaging for you its even simpler.

I suppose you could do this:
printf `vmstat 1 1| awk '/^ [0123456789]/ {print $NF}'`\\n
to get the average...? sh foo.sh
59
Works for me


0
gerryt
5/8/2007 6:49:05 PM
Reply: