Sigularity
Sigularity

Reputation: 967

How to get CPU idle on AIX ( awk )

I am using a 'awk' to get the CPU idle(vmstat), and it works well on Linux. Strangely, below command doesn't show anything on AIX.

vmstat 1 1 | awk '{for(i=NF;i>0;i--) if($i=="id") {x=i;break} } END{print $x}' 

I can see the correct result with the command above for the text result of AIX on Linux, but I can't on AIX.

# Vmstat 
# AIX
System Configuration: lcpu=8 mem=16384MB
kthr     memory             page              faults          cpu
----- ----------- ------------------------ ------------ -----------
 r  b   avm   fre  re  pi  po  fr   sr  cy  in   sy  cs us sy id wa
 1  1 1566673 633894   0   0   0   0    1   0 895 7958 348  1  1 98  1

# Linux    
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0 210564 109296 103864 580288    1    1   145  4152   12    8  7  7 86  0  0    

Output

86 (on Lnux)

Nothing (on AIX)<----- means just blank line.

Can you see what I am missing?

UPDATE

vmstat 1 1 With above command, we can just get the average CPU since reboot, so should consider to use below commands instead.

vmstat 1 2 | tail -3 | sed '2d' 

Upvotes: 0

Views: 604

Answers (1)

Aif
Aif

Reputation: 11220

I was thinking maybe the first line is an issue to find the id column. How about (for linux):

vmstat 1 1 | awk '
NR == 2 { for(i=NF;i>0;i--) if($i=="id") {x=i;break} }
NR == 3 { print $x }'

And for AIX:

vmstat 1 1 | awk '
NR == 4 { for(i=NF;i>0;i--) if($i=="id") {x=i;break} }
NR == 5 { print $x }'

NR is the line number, so I've assumed those from your output.

Edit: after some extended work at home, I came through an awk solution keeping tracks of lines in an array:

vmstat 1 1 | awk '{ line[NR] = $0 } 
END { 
  split(line[NR-1],params); 
  for(i in params) if(params[i] == "id") { break; }
  split(line[NR],values);
  print values[i] 
}'

Edit: About the tail solution, you have to use NR accordingly:

vmstat 1 1 | tail -2 | awk '
NR == 1 { for(i=NF;i>0;i--) if($i=="id") {x=i;break} }
NR == 2 { print $x }'

Upvotes: 1

Related Questions