sarコマンドからCPU使用率を抽出するには、awkを作成します。

sarコマンドからCPU使用率を抽出するには、awkを作成します。

コマンドの出力から%user、%niceなどの値を抽出しようとしていますsar

sar -P ALL 1 1

出力:

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

値を取得するために次の文字列を作成しましたが、うまくいかないようです。

私のコマンド:

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}'

ベストアンサー1

努力する

 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 == 99つのフィールド(NF)をフィルタリングする必要があります。
  • $3 != "all"CPUを要約する行をスキップしてください。
  • 文字はありませんが、行末以降|

よりコンパクトなバージョン

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

よりコンパクトなバージョン

 sar -P ALL 1 1 | 
 awk  'NF == 9 && $3 != "all" { $1=$2=$8="" ; print ; }'

おすすめ記事