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,
> UlrichHi, 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. Greetings, -- Dr. Michael Schwartzkopff Guardinistr. 63 81375 München Tel: (0163) 172 50 98
signature.asc
Description: This is a digitally signed message part.
_______________________________________________ Linux-HA mailing list [email protected] http://lists.linux-ha.org/mailman/listinfo/linux-ha See also: http://linux-ha.org/ReportingProblems
