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
Occaasionally it makes sense to do something about performance in addition to
talk about it. I've written a complex monitor for HP Serviceguard in Perl that
uses about 2.5 seconds of CPU time per day (while running a few thousand times
per day)
_______________________________________________
Linux-HA mailing list
[email protected]
http://lists.linux-ha.org/mailman/listinfo/linux-ha
See also: http://linux-ha.org/ReportingProblems