0

I am trying to extract the values of the %user,%nice etc from the output of the sar command.

sar -P ALL 1 1

Output of this:

Linux 2.6.32-358.el6.x86_64 (ftizsldapp009.ftiz.cummins.com)    09/28/2015      _x86_64_        (4 CPU)

02:49:40 PM     CPU     %user     %nice   %system   %iowait    %steal     %idle
02:49:41 PM     all      3.01      0.00      2.51      0.00      0.00     94.49
02:49:41 PM       0      1.98      0.00      3.96      0.00      0.00     94.06
02:49:41 PM       1      6.00      0.00      4.00      0.00      0.00     90.00
02:49:41 PM       2      2.00      0.00      1.00      0.00      0.00     97.00
02:49:41 PM       3      1.98      0.00      2.97      0.00      0.00     95.05

Average:        CPU     %user     %nice   %system   %iowait    %steal     %idle
Average:        all      3.01      0.00      2.51      0.00      0.00     94.49
Average:          0      1.98      0.00      3.96      0.00      0.00     94.06
Average:          1      6.00      0.00      4.00      0.00      0.00     90.00
Average:          2      2.00      0.00      1.00      0.00      0.00     97.00
Average:          3      1.98      0.00      2.97      0.00      0.00     95.05

I have written following string to get the values but this doesn't seen to work.

My Command :

sar -P ALL 1 1 | \
awk '{cpu=$3; pctUser=$4; pctNice=$5; pctSystem=$6; pctIowait=$7; pctIdle=$NF}' \
     '{printf "%-3s %9s %9s %9s %9s %9s\n", cpu, pctUser, pctNice, pctSystem, \
       pctIowait, pctIdle}'
0

try

 sar -P ALL 1 1 | 
 awk  'NF == 9 && $3 != "all" {cpu=$3; pctUser=$4; pctNice=$5; pctSystem=$6; pctIowait=$7; pctIdle=$NF ;
     printf "%-3s %9s %9s %9s %9s %9s\n", cpu, pctUser, pctNice, pctSystem, pctIowait, pctIdle}'
  • NF == 9 you need to filter on nine filed (NF)
  • $3 != "all" skip the line that summarize cpus
  • no caracter but end-of-line after |

a more compact version

 sar -P ALL 1 1 | 
 awk  'NF == 9 && $3 != "all" { printf "%-3s %9s %9s %9s %9s %9s\n",$3,$4,$5,$6,$7,$NF}'

a more more compact version

 sar -P ALL 1 1 | 
 awk  'NF == 9 && $3 != "all" { $1=$2=$8="" ; print ; }'
  • Thanks a lot. The following seems to give me expected view. sar -P ALL 1 1|awk '{ printf "%-3s %9s %9s %9s %9s %9s %9s\n",$2,$3,$4,$5,$6,$7,$8}' I also need to put the output in format like. all CPU %user = 51.50 0 CPU %user = 97.00 – user133617 Sep 28 '15 at 19:38
  • remove && $3 != "all" to get the line with all cpus. – Archemar Sep 28 '15 at 19:41
  • sar -P ALL 1 1 | awk 'NF == 9 {cpu=$3; pctUser=$4; pctNice=$5; pctSystem=$6; pctIowait=$7; pctIdle=$NF ; printf "%-3s %9s %9s %9s %9s %9s\n", cpu, pctUser, pctNice, pctSystem, pctIowait, pctIdle}' This seems to give me the correct view. Is there a way in which i can iterate the values of cpu cores and their corresponding %user,%nice values. I am looking for output like 0CPU %user= 68.38 1CPU %user=57.43 0CPU %nice=0.24 1CPU %nice=4.21 – user133617 Sep 28 '15 at 20:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.