Register
It is currently Fri Oct 24, 2014 6:46 am

need help: to get cpu usage with 1 request


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Wed Aug 29, 2012 7:08 am   

Joined: Wed Aug 29, 2012 6:53 am
Posts: 1
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:

Code:
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 ( becouse the first line do not show proper value ) and if there is about 30 virtual machines to go all script around it take`s 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 thouse 2 variables then script would run twice faster

thanks for any advice and sorry for my bad english .


Top
 Profile  
 PostPosted: Wed Aug 29, 2012 11:08 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 550
hi,

you'd better use a whole awk code to parse top's output, for example
Code:
read cpu wa < <(top -bn2 | gawk -F',' '/Cpu\(s\)/{ gsub("[[:alpha:]%]","");sub(/\(\): */,"",$1);c=100-$2;w=$6}END{print c,w}')

and remember:
grep is useless with sed, or awk
sed is useless with awk (so is awk with sed)
:)


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP