>>> Michael Schwartzkopff <[email protected]> schrieb am 03.02.2011 um 13:09
>>> in
Nachricht <[email protected]>:
> On Thursday 03 February 2011 12:35:34 Ulrich Windl wrote:
> > Hi!
> >
> > I'm starting to explore Linux-HA. Examining one of the monitors, I think
> > things could be made much more efficient. For example: To get the percent
> > of idle CPU the monitor uses 4 processes: top -b -n2 | grep Cpu | tail -1
> > | awk -F",|\.[0-9]%id" '{ print $4 }'
> >
> > However awk can do the effect of grep and tail as well. My first attempt is
> > this: top -b -n2 | awk -F",|\.[0-9]%id" '/^Cpu/{ print $4; exit }'
> >
> > My second attempt uses /proc/stat instead, avoiding the slow top process:
> > awk '$1 == "cpu" { print $7; exit }' /proc/stat
> >
> > time (top -b -n2 | grep Cpu | tail -1 | awk -F",|\.[0-9]%id" '{ print $4
> > }') awk: warning: escape sequence `\.' treated as plain `.'
> > 99
> >
> > real 0m3.533s
> > user 0m0.008s
> > sys 0m0.008s
> >
> > time (top -b -n2| awk -F",|\.[0-9]%id" '/^Cpu/{ print $4; exit }')
> > awk: warning: escape sequence `\.' treated as plain `.'
> > 99
> >
> > real 0m0.518s
> > user 0m0.000s
> > sys 0m0.008s
> >
> > time awk '$1 == "cpu" { print $7; exit }' /proc/stat
> > 98
> >
> > real 0m0.004s
> > user 0m0.000s
> > sys 0m0.000s
> >
> > Regards,
> > Ulrich
>
> Hi,
>
> good idea. The only problem it that the information in /proc/stats is in
> ticks
> and does not give you an absolute value. So you would have to calculate the
> difference yourself, which makes the task much more difficult.
OK,
what about this:
time (procinfo | awk '$1 == "idle" && $2 == ":" { if (sub("%", "", $5)) { print
$5 } else { sub("%", "", $4); print $4} }')
99.4
real 0m0.010s
user 0m0.000s
sys 0m0.000s
# Maybe use "print int($x)" to see an integer
(I didn't know the details of /proc/stat. Those who want to might read:
/usr/src/linux/kernel/sched.c, /usr/src/linux/include/linux/kernel_stat.h,
/usr/src/linux/include/asm-generic/cputime.h)
Regards,
Ulrich
P.S: The lines that are processed look like this:
system: 1:21:29.98 0.6% page act: 2141840
IOwait: 3:58:58.89 1.9% page dea: 1635314
hw irq: 0:02:17.38 0.0% page flt: 766785934
sw irq: 0:13:44.90 0.1% swap in : 562
idle : 7d 21:37:23.52 90.4% swap out: 899
uptime: 2d 4:25:34.97 context : 119011606
_______________________________________________
Linux-HA mailing list
[email protected]
http://lists.linux-ha.org/mailman/listinfo/linux-ha
See also: http://linux-ha.org/ReportingProblems