LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-18-2015, 01:41 AM   #1
kumarjohn
LQ Newbie
 
Registered: Oct 2012
Posts: 26

Rep: Reputation: Disabled
need to get average idle time using mpstat


I want to get average idle time of the server using mpstat. The problem I am having is %idle is not in same columns in all the versions of linux.

example 1:

Quote:
[root@testserver ~]# mpstat 1 2
Linux 2.6.32-358.el6.x86_64 (testserver) 06/18/2015 _x86_64_ (2 CPU)

12:41:17 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
12:41:18 AM all 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00
12:41:19 AM all 0.50 0.00 0.50 0.00 0.00 0.00 0.00 0.00 99.00
Average: all 0.25 0.00 0.25 0.00 0.00 0.00 0.00 0.00 99.50
example 2:

Quote:
[root@testserver1 ~]# mpstat 1 2
Linux 2.6.18-308.8.2.el5 testserver1 06/17/2015

10:47:38 PM CPU %user %nice %sys %iowait %irq %soft %steal %idle intr/s
10:47:39 PM all 0.12 0.00 0.00 0.00 0.00 0.00 0.00 99.88 191.09
10:47:40 PM all 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00 134.00
Average: all 0.06 0.00 0.00 0.00 0.00 0.00 0.00 99.94 162.69
I tried below command as generalized solution but as Average as one less column output is not proper.

Quote:
mpstat 1 2 | egrep -v '^Linux|^$' | awk -v c="%idle" 'NR==1 {for (i=1; i<=NF; i++) if ($i==c) break}''{print $i}'
I am getting below output.

Quote:
[root@testserver ~]# mpstat 1 2 | egrep -v '^Linux|^$' | awk -v c="%idle" 'NR==1 {for (i=1; i<=NF; i++) if ($i==c) break}''{print $i}'
%idle
100.00
100.00
But the desired output is

Quote:
[root@testserver ~]# mpstat 1 2 | egrep -v '^Linux|^$' | awk -v c="%idle" 'NR==1 {for (i=1; i<=NF; i++) if ($i==c) break}''{print $i}'
%idle
100.00
100.00
100.00
 
Old 06-18-2015, 01:55 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Is it always the last word of the last line?

Code:
mpstat 1 2 | tr '\n' ' ' | tail -n 1
(untested)
 
Old 06-18-2015, 02:03 AM   #3
kumarjohn
LQ Newbie
 
Registered: Oct 2012
Posts: 26

Original Poster
Rep: Reputation: Disabled
Hi Guttorm,

My requirement is to get only the details of %idle column. no all the output.

Thanks.
 
Old 06-18-2015, 02:11 AM   #4
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
I meant to change every space to newline, to only get the last word in the file. But my code was incorrect, changing newlines to spaces instead of the other way around.

Code:
mpstat 1 2 | tr ' ' '\n' | tail -n 1
If you need the last word of every line, try something like this:

Code:
awk '{print $NF}'
Then use head/tail/grep to skip the lines you dont want.
 
Old 06-18-2015, 02:18 AM   #5
kumarjohn
LQ Newbie
 
Registered: Oct 2012
Posts: 26

Original Poster
Rep: Reputation: Disabled
My problem is idle column varies with OS version so I am trying for generalized solution to get %idle column irrespective of the OS version. the solution I tried is working fine but failing in Average column as number of columns differ.
 
Old 06-18-2015, 03:33 AM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,135

Rep: Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122
So test for "Average" and use $(i-1). But seeing as how that what you want, why not just use it ?.
There is also no need for grep - you can do that elimination in your awk.
 
1 members found this post helpful.
Old 06-18-2015, 03:54 AM   #7
kumarjohn
LQ Newbie
 
Registered: Oct 2012
Posts: 26

Original Poster
Rep: Reputation: Disabled
Thanks it worked I was trying the same but was having syntax error thanks.


Code:
mpstat 1 2 | egrep -v '^Linux|^$' |  awk -v c="%idle" 'NR==1 {for (i=1; i<=NF; i++) if ($i==c) break}''{print $(i-1)}' | tail -1
 
  


Reply

Tags
awk, bash, scripting



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to understand user time, sys time, wait time, idle time of CPU guixingyi Linux - Server 1 08-24-2010 10:10 AM
Problem "$value=`mpstat 1 1 | grep "Average"`;" Alias pipe return nothing adamlucansky Linux - General 8 09-25-2009 07:26 AM
My load average is 7.00, but my cpu is idle. What's in the queue? BrianK Linux - General 3 03-27-2007 07:10 PM
Why is my load average so high when comp. is idle? BrianK Linux - General 1 11-18-2005 12:25 AM
my Load Average is always 1.0 when processors are idle ouimet Linux - Hardware 2 12-15-2004 09:39 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration