[Solved] need to get cpu usage with 1 request

Hello,

i`m creating an script that collect`s data from virtual machines about cpu usage and store that data into RRD.

so everything now works fine, but it takes long time if in machine is a lot of virtual`s.

so i want to make my script to collect information with 1 request. at this time i`m collecting data like this:

cpu=`vzctl exec $veid top -bn2 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%\id.*/\1/" | tail -n1 | \
awk '{print 100-$1}'`
wa=`vzctl exec $veid top -bn2 | grep "Cpu(s)" | awk '{print $6/1}' | tail -n1`

 

i use top -bn2 so it takes second line from top ( because the first line do not show proper value ) and if there is about 30 virtual machines to go all script around it takes some time, becose there are 2 requests: 1 for cpu ( us+sy+ni+hi+si+st ) and second to get wa

so if there is posible to get with 1 request those 2 variables then script would run twice faster

thanks for any advice and sorry for my bad english .

You know, it does help to mention your OS and version...
When we get old it takes time ( a lot of) to figure what it can be in order to understand what you are up to...

Why you do tail -n1 after the sed and awk treatment (and not before) ? Is it possible to do this kind of thing ?

first_line=`vzctl exec $veid top -bn2 | grep "Cpu(s)" | tail -n1`
cpu=`echo "$first_line" | sed "s/.*, *\([0-9.]*\)%\id.*/\1/" | awk '{print 100-$1}'`
wa=`echo "$first_line" | awk '{print $6/1}'`

delugeag thanx, hou i did`nt invet`ed that, thnx again