LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   need to get average idle time using mpstat (https://www.linuxquestions.org/questions/linux-newbie-8/need-to-get-average-idle-time-using-mpstat-4175545709/)

kumarjohn 06-18-2015 01:41 AM

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

Guttorm 06-18-2015 01:55 AM

Is it always the last word of the last line?

Code:

mpstat 1 2 | tr '\n' ' ' | tail -n 1
(untested)

kumarjohn 06-18-2015 02:03 AM

Hi Guttorm,

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

Thanks.

Guttorm 06-18-2015 02:11 AM

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.

kumarjohn 06-18-2015 02:18 AM

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.

syg00 06-18-2015 03:33 AM

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.

kumarjohn 06-18-2015 03:54 AM

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


All times are GMT -5. The time now is 11:03 PM.