負荷計測用の簡易スクリプト

※IO待ちなどはTOPからは取得できないので、「vmstat -t -n 1」等と併せて比較する。

・TOPコマンド結果の1行表示(リアルタイム表示)

#!/bin/sh

###################################################
# 処理概要 : TOPコマンドの結果を各瞬間毎に1行表示する
# 詳細 : 各行に表示される内容は以下の通り
#    ロードアベレージ
#    CPU使用率の一番高いプロセス名、及び使用率
#    メモリ使用率の一番高いプロセス名、及び使用率
#    メモリ使用状況(total,used,free,buffers)
#    スワップ状況(total,used,free,cached)
###################################################

# TOPログの保存場所 
toplog="/tmp/mytop_`date +%y%m%d%H%M%S`.log"

# TOPコマンド結果の取得
top -d 1 -b > $toplog &

echo ""
echo "***** Infomation : all top log => $toplog *****"
echo ""

# 1秒delay
sleep 1

echo "Press Start to display any key!!"
read dummy

# TOPコマンド結果の1行表示
tail -f $toplog | grep -P "^(top[ ]+|Mem:|Swap:|[ ]+([0-9]|PID))" | awk '
BEGIN {
  dt=""
  la="";
  max_cpu=0;pname_cpu="";
  max_mem=0;pname_mem="";
  print "date,load average,cpu high process,,memory high process,,Mem Info,,,,Swap Info";
  print "date,,process,used,process,used,total,used,free,buffers,total,used,free,cache";
}
{
  match_lavg = match($0,"load average:");
  match_mem  = match($0,"Mem:");
  match_swap = match($0,"Swap:");
  if (match_lavg>0) {
    la=$0;
    gsub(".+load average: ","",la);
    la=substr(la,1,4);
    dt=$0;
    gsub("^top - ","",dt);
    dt=substr(dt,1,8);
  } else if (match_mem>0) {
    meminfo=$0;
    gsub(".*Mem:[ ]+","",meminfo);
    gsub(" ","",meminfo);
    gsub(",","\t",meminfo);
    gsub("total",",",meminfo);
    gsub("used",",",meminfo);
    gsub("free",",",meminfo);
    gsub("buffers","",meminfo);
    gsub("\t","",meminfo);
    gsub(" ","",meminfo);
  } else if (match_swap>0) {
    swapinfo=$0;
    gsub(".*Swap:[ ]+","",swapinfo);
    gsub(" ","",swapinfo);
    gsub(",","\t",swapinfo);
    gsub("total",",",swapinfo);
    gsub("used",",",swapinfo);
    gsub("free",",",swapinfo);
    gsub("cached","",swapinfo);
    gsub("\t","",swapinfo);
    gsub(" ","",swapinfo);
  } else if ($1=="PID") {
    if (pname_cpu!="") {
      print dt","la","pname_cpu","max_cpu","pname_mem","max_mem","meminfo","swapinfo;
      max_cpu=0;
      max_mem=0;
    }
  } else {
    if ($9>max_cpu) {
      max_cpu=$9;
      pname_cpu=$12;
    }
    if ($10>max_mem) {
      max_mem=$10;
      pname_mem=$12;
    }
  }
}'

・計測結果をCSVとして出力

#!/bin/sh

#######################
toplog=$1
reslog=`echo $toplog | sed s/log$/csv/`
#######################


if test -r $toplog;then
  echo ""
  echo "Analyze top log($toplog) , output csv($reslog)"
  echo ""
else
  echo "$toplog is not found!"
  exit 1
fi

# CSV出力
cat $toplog | grep -P "^(top[ ]+|Mem:|Swap:|[ ]+([0-9]|PID))" | awk '
BEGIN {
  dt=""
  la="";
  max_cpu=0;pname_cpu="";
  max_mem=0;pname_mem="";
  print "date,load average,cpu high process,,memory high process,,Mem Info,,,,Swap Info";
  print "date,,process,used,process,used,total,used,free,buffers,total,used,free,cache";
}
{
  match_lavg = match($0,"load average:");
  match_mem  = match($0,"Mem:");
  match_swap = match($0,"Swap:");
  if (match_lavg>0) {
    la=$0;
    gsub(".+load average: ","",la);
    la=substr(la,1,4);
    dt=$0;
    gsub("^top - ","",dt);
    dt=substr(dt,1,8);
  } else if (match_mem>0) {
    meminfo=$0;
    gsub(".*Mem:[ ]+","",meminfo);
    gsub(" ","",meminfo);
    gsub(",","\t",meminfo);
    gsub("total",",",meminfo);
    gsub("used",",",meminfo);
    gsub("free",",",meminfo);
    gsub("buffers","",meminfo);
    gsub("\t","",meminfo);
    gsub(" ","",meminfo);
  } else if (match_swap>0) {
    swapinfo=$0;
    gsub(".*Swap:[ ]+","",swapinfo);
    gsub(" ","",swapinfo);
    gsub(",","\t",swapinfo);
    gsub("total",",",swapinfo);
    gsub("used",",",swapinfo);
    gsub("free",",",swapinfo);
    gsub("cached","",swapinfo);
    gsub("\t","",swapinfo);
    gsub(" ","",swapinfo);
  } else if ($1=="PID") {
    if (pname_cpu!="") {
      print dt","la","pname_cpu","max_cpu","pname_mem","max_mem","meminfo","swapinfo;
      max_cpu=0;
      max_mem=0;
    }
  } else {
    if ($9>max_cpu) {
      max_cpu=$9;
      pname_cpu=$12;
    }
    if ($10>max_mem) {
      max_mem=$10;
      pname_mem=$12;
    }
  }
}'

トップ   差分 バックアップ リロード   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2008-04-10 (木) 01:17:44 (4176d)