编写Bash Shell通过gnuplot绘制系统性能数据图的方法

使用步骤:
1.设置一个定时任何 执行getperf.sh,采集性能数据
2.将采集到性能数据文件,如:192.168.1.1.tar.gz 解压
3.将性能分析的脚步performance_analyse.sh 放到解压后的目录中
4.安装gnuplot程序(这里下载)

5.将字体文件夹,拷贝到/usr/share/fonts/目录
6.直接运行 bash performance_analyse.sh
getperf.sh脚本内容如下:

#################################################
#说明:
# 定义时间TIMES参数,表示采集的次数,
# 采集时间 = TIMES * INTERVAL
#eg:
# TIMES=180 , INTERVAL=10 采集时间就是半小时
#
#################################################
#!/bin/bash
TIMES=3600
INTERVAL=2
PWD=`pwd`
TIME=`date "+%F %H:%M:%S"`
TAR=`whereis tar|awk -F ":" '{print $2}'|awk '{print $1}'`
SAR=`whereis sar|awk -F ":" '{print $2}'|awk '{print $1}'`
IOSTAT=`whereis iostat|awk -F ":" '{print $2}'|awk '{print $1}'`
# Check Moniter Tool
SysInfo(){
  echo "sysip : $SYSIP"|tee $PWD/$SYSIP/sysinfo
  echo "starttime : $TIME" |tee -a $PWD/$SYSIP/sysinfo
  /sbin/ifconfig >>$PWD/$SYSIP/sysinfo
  echo "===================================" >>$PWD/$SYSIP/sysinfo
  /usr/sbin/dmidecode >>$PWD/$SYSIP/sysinfo
  echo "===================================" >>$PWD/$SYSIP/sysinfo
  /bin/cat /proc/cpuinfo >> $PWD/$SYSIP/sysinfo
  echo "===================================" >>$PWD/$SYSIP/sysinfo
  /sbin/fdisk -l >> $PWD/$SYSIP/sysinfo
  echo "===================================" >>$PWD/$SYSIP/sysinfo
  /bin/df -Th >>$PWD/$SYSIP/sysinfo
  echo "===================================" >>$PWD/$SYSIP/sysinfo
  /usr/bin/free -m >> $PWD/$SYSIP/sysinfo
  echo "===================================" >>$PWD/$SYSIP/sysinfo
  echo ""
}
CheckEnv(){
  PUB_IP=`/sbin/ifconfig |grep "inet addr" | awk -F: '{print $2}'| awk '{print $1}'|grep -v "172\.\|10\.\|127\.\|192\."|sed -n 1p`
  PRI_IP=`/sbin/ifconfig |grep "inet addr" | awk -F: '{print $2}'| awk '{print $1}'|grep "10\.\|127\.\|192\."|sed -n 1p`
  if [ "snda$PUB_IP" == "snda" ];then
    SYSIP=$PRI_IP
  else
    SYSIP=$PUB_IP
  fi
  if [ -d $PWD/$SYSIP ];then
    rm -rf $PWD/$SYSIP
  fi
  mkdir -p $PWD/$SYSIP
  if ! grep iostat /usr/bin/iostat ;then
  yum -y install sysstat
  fi
}
GetPerf(){
  CPUUSAGE="$PWD/$SYSIP/cpuusage.log"
  MEMUSAGE="$PWD/$SYSIP/memusage.log"
  DISKUSAGE="$PWD/$SYSIP/diskusage.log"
  NETWORK="$PWD/$SYSIP/network.log"
  $SAR -P ALL $INTERVAL $TIMES>> $CPUUSAGE &
  $IOSTAT -dkx $INTERVAL $TIMES>> $DISKUSAGE &
  $SAR -n DEV $INTERVAL $TIMES>> $NETWORK &
  $SAR -r $INTERVAL $TIMES>> $MEMUSAGE &
  for ((i=0;i<$TIMES;i++))
  do
    sleep $INTERVAL
  done
}
CheckEnv
SysInfo
GetPerf
#在同一台机器上第二次采集数据时,会删除之前采集的数据,重新采集
#采集完成之后,会生产一个以 IP.tar.gz的压缩包。将这个压缩包,放到
#分析脚本performance_analyse.sh 的同级目录。
if [ -d $PWD/$SYSIP ];then
  cd $PWD
  rm -f $SYSIP.tar.gz
  tar zcvf $SYSIP.tar.gz $SYSIP
fi

performance_analyse.sh脚本内容如下:

#################################################
#
#  这个脚本的作用是处理由性能采集脚本收集到的性能数据
#然后使用gunplot生产直观的性能图。
#
#################################################
#!/bin/bash
SysInfo(){
  local file=$1
  local productname="unknow"
  local cpumodel="unknow"
  local cpucore="unknow"
  local cpumhz="unknow"
  local physical="unknow"
  local realcpucore="unknow"
  local diskpart="unknow"
  local memory="unknow"
  ipaddr=`cat $file |grep -i "net addr"|grep -v "127"`
  productname=`cat $file |grep -i "product name"`
  cpumodel=`cat $file |grep -i "model name"|uniq -d`
  cpucore=`cat $file |grep "processor"|wc -l`
  cpumhz=`cat $file |grep -i "cpu MHz"|uniq -d`
  physical=`cat $file |grep -i "physical id"|sort -n|uniq -d|wc -l`
  realcpucore=`cat $file |grep -i "cpu cores"|uniq -d|awk -F ":" '{print $2}'`
  memory=` cat $file |grep -i -EB1 "mem:"`
  diskpart=`cat $file |grep -i "disk"|grep -E "[shv][d][a-z]"`
  echo "System Information:"|tee -a $REPORTFILE
  echo "IP address:"|tee -a $REPORTFILE
  echo -e "$ipaddr"|tee -a $REPORTFILE
  echo "$productname" |tee -a $REPORTFILE
  echo -e "\t$cpumodel" |tee -a $REPORTFILE
  echo -e "\tCPU cores\t:$cpucore"|tee -a $REPORTFILE
  echo -e "\t$cpumhz"|tee -a $REPORTFILE
  echo -e "\tPhysical cpu number:$physical"|tee -a $REPORTFILE
  echo -e "\tEach CPU real core:$realcpucore"|tee -a $REPORTFILE
  echo "$diskpart"|tee -a $REPORTFILE
  echo -e "Memory(MB):\n$memory"|tee -a $REPORTFILE
}
CpuAllUsage(){
  local file=$1
  cat $file|grep -i "all"|grep -v -i -E "average|linux|system" >$GNUPLOTFOLDER/sar_cpu.$$
  TITLE=`cat $file |sed "/^$/d"|grep -v -i "average|linux"|sed 1d|sed -n 1p`
  local SOURCE_SAR_CPU="$GNUPLOTFOLDER/sar_cpu.$$"
  local USER_UASGE=`echo $TITLE |awk '{print $3}'`
  local NICE_UASGE=`echo $TITLE |awk '{print $4}'`
  local SYSTEM_UASGE=`echo $TITLE |awk '{print $5}'`
  local IOWAIT_UASGE=`echo $TITLE |awk '{print $6}'`
  local STEAL_UASGE=`echo $TITLE |awk '{print $7}'`
  local IDLE_UASGE=`echo $TITLE |awk '{print $8}'`
  local cpuusagemax=`cat $SOURCE_SAR_CPU|awk '{print $3+$4+$5+$6+$7}'|sort -r|sed -n 1p`
  local Tmp_ylable=`echo $cpuusagemax|awk -F "." '{print $1}'`
  local ylable=`echo $Tmp_ylable+5|bc`
  local cpuusagemin=`cat $SOURCE_SAR_CPU|awk '{print $3+$4+$5+$6+$7}'|sort|sed -n 1p`
   local cpuusageavg=`awk 'BEGIN{total=0}{total+=$8}END{print 100-total/NR}' $SOURCE_SAR_CPU`
  echo "`date '+%F %H:%M:%S'`: CPU Performance analysis" |tee -a $REPORTFILE
  echo -e "\t1.System Cpu load(%) \tmax=$cpuusagemax,average=$cpuusageavg,mim=$cpuusagemin" |tee -a $REPORTFILE
/usr/local/bin/gnuplot --persist <<EOF
set term png size 800,600
#================= Cpu usage area pic ===========
set output "TotalCpuUsage.png"
set key title "Total CPU usage(%)"
set key box 3
set key below
set xlabel "times"
set ylabel "TOTAL CPU USAGE(%)"
set style fill solid 1
set style histogram rowstacked
plot [1:][0:$ylable] '$SOURCE_SAR_CPU' using 3 with histogram lt rgb "#FFB3B3" title "$USER_UASGE",\
'' using 4 with histogram title "$NICE_UASGE",\
'' using 5 with histogram lt rgb "#B3CA7E" title "$SYSTEM_UASGE",\
'' using 6 with histogram lt rgb "#A464BF" title "$IOWAIT_UASGE",\
'' using 7 with histogram title "$STEAL_UASGE",\
'' using 8 with histogram lt rgb "#212121" title "$IDLE_UASGE",\
$cpuusagemax lt 4 lw 2 title "Max Usage ($cpuusagemax%)"
EOF
}
CpuEachCoreUsage(){
  local file=$1
  sed -i 's/PM//g' $file
  local corenu=`cat $file|grep -v -i -E "average|system|all|linux"|sed "/^$/d"|awk '{print $2}'|sort -n -r|uniq -d|sed -n 1p`
#  echo $corenu
  PLOT=""
  echo "">$GNUPLOTFOLDER/idle_sum.$$
  for (( i=0;i<=$corenu;i++ ))
  do
    cat $file |grep -v -i -E "average|system|all|linux"|sed "/^$/d"|awk "(\$2==$i){print}"|awk '{print $1 ," ",100-$8}'>$GNUPLOTFOLDER/$i.txt
    local idlesum=`awk 'BEGIN{total=0}{total+=$2}END{print total}' $GNUPLOTFOLDER/$i.txt`
    echo $i $idlesum >>$GNUPLOTFOLDER/idle_sum.$$
  done
  first_load=`cat $GNUPLOTFOLDER/idle_sum.$$|sort -n -k 2 -r|sed -n 1p|awk '{print $1}'`
  second_load=`cat $GNUPLOTFOLDER/idle_sum.$$|sort -n -k 2 -r|sed -n 2p|awk '{print $1}'`
  third_load=`cat $GNUPLOTFOLDER/idle_sum.$$|sort -n -k 2 -r|sed -n 3p|awk '{print $1}'`
  load=($first_load $second_load $third_load)
  echo -e "\t2.Each core load:"
  local cpuload=("First" "Second" "Third")
  local nu=0
  for i in ${load[@]}
  do
    local coreloadmax=`cat $GNUPLOTFOLDER/$i.txt|sort -n -k 2 -r|sed -n 1p|awk '{print $2}'`
    local coreloadavg=`awk 'BEGIN{total=0}{total+=$2}END{print total/NR}' $GNUPLOTFOLDER/$i.txt`
    local coreloadmin=`cat $GNUPLOTFOLDER/$i.txt|sort -n -k 2|sed -n 1p|awk '{print $2}'`

    echo -e "\t\t\t Load ${cpuload[$nu]} core $i : max=$coreloadmax , avg=$coreloadavg , min=$coreloadmin"|tee -a $REPORTFILE
    nu=`echo $nu+1|bc`
  done
  for ((i=0;i<=corenu;i++))
  do
    if [ $i -eq $first_load ];then
      LW=4
    elif [ $i -eq $second_load ];then
      LW=3
    elif [ $i -eq $third_load ];then
      LW=2
    else
      LW=1
    fi
    TMP1="$GNUPLOTFOLDER/$i.txt"
    TMP2="using 1:2 with l lw $LW"
    TMP3="core $i "
    PLOT="$PLOT \"$TMP1\" $TMP2 title \"$TMP3\","

  done
  local tmp_ylabel_range=`cat $file|grep -v -i -E "average|system|all|linux"|sed "/^$/d"|awk '{print 100-$8}'|sort -n -r|sed -n 1p|awk -F "." '{print $1}'|sed -n 1p`
  local ylabel_range=`echo $tmp_ylabel_range+5|bc`
/usr/local/bin/gnuplot --persist <<EOF
#=============== Each core usage =====================
set term png size 800,600
set output "CpuCoreIdle.png"
set key title "Each Core Usage(%)"
set key box 3
set key below
set ylabel "CPU Core Usage(%)"
set xdata time
set timefmt "%H:%M:%S"
plot [:][0:$ylabel_range] $PLOT
EOF
}
MemoryUsage(){
  local file=$1
  local title=`cat $file |sed '/^$/d'|grep -i -E -v "average|linux"|grep -i mem`
  sed -i 's/PM//g' $file
  local kbmemused=`echo $title|awk '{print $3}'`
  local memused=`echo $title|awk '{print $4}'`
  local kbbuffers=`echo $title|awk '{print $5}'`
  local kbcached=`echo $title|awk '{print $6}'`
  local kbcommit=`echo $title|awk '{print $7}'`
  cat $file |sed '/^$/d'|grep -i -E -v "average|linux"|grep -i -v mem|awk '{print $1,$2/1024,($2+$3)/1024,$3/1024,$4,$5/1024,$6/1024}'>$GNUPLOTFOLDER/memory.$$
  SOURCE_FILE=$GNUPLOTFOLDER/memory.$$
  local memtotal=`awk 'BEGIN{total=0}{total+=$3}END{print total/NR}' $SOURCE_FILE`
  local memusedmax=`awk '{print $4}' $SOURCE_FILE|sort -n -r|sed -n 1p`
  local memusedavg=`awk 'BEGIN{total=0}{total+=$4}END{print total/NR}' $SOURCE_FILE`
  local memusedmin=`awk '{print $4}' $SOURCE_FILE|sort -n|sed -n 1p`
  local memfreemax=`awk '{print $2}' $SOURCE_FILE|sort -n -r|sed -n 1p`
  local memfreeavg=`awk 'BEGIN{total=0}{total+=$2}END{print total/NR}' $SOURCE_FILE`
  local memfreemin=`awk '{print $2}' $SOURCE_FILE|sort -n|sed -n 1p`
  local memcachemax=`awk '{print $7}' $SOURCE_FILE|sort -n -r|sed -n 1p`
  local memcacheavg=`awk 'BEGIN{total=0}{total+=$7}END{print total/NR}' $SOURCE_FILE`
  local memcachemin=`awk '{print $7}' $SOURCE_FILE|sort -n|sed -n 1p`
  local memused_cachemax=`awk '{print $4-$7}' $SOURCE_FILE|sort -n -r|sed -n 1p`
  local memused_cacheavg=`awk 'BEGIN{total=0}{total+=($4-$7)}END{print total/NR}' $SOURCE_FILE`
  local memused_cachemin=`awk '{print $4-$7}' $SOURCE_FILE|sort -n|sed -n 1p`
  local used_percent=`awk 'BEGIN{total=0}{total+=$5}END{print total/NR}' $SOURCE_FILE`
  echo "`date '+%F %H:%M:%S'`: Memory usage analysis" |tee -a $REPORTFILE
  echo -e "\t\t1.total memory: $memtotal MB"|tee -a $REPORTFILE
  echo -e "\t\t2.memory used: max=$memusedmax MB ,avg=$memusedavg MB ,min=$memusedmin MB"|tee -a $REPORTFILE
  echo -e "\t\t3.memory free: max=$memfreemax MB ,avg=$memfreeavg MB ,min=$memfreemin MB"|tee -a $REPORTFILE
  echo -e "\t\t4.memory cache: max=$memcachemax MB ,avg=$memcacheavg MB ,min=$memcachemin MB"|tee -a $REPORTFILE
  echo -e "\t\t4.memory used-cache: max=$memused_cachemax MB ,avg=$memused_cacheavg MB ,min=$memused_cachemin MB"|tee -a $REPORTFILE
/usr/local/bin/gnuplot --persist <<EOF
set term png size 800,600
set output "MemoryUsage.png"
set key title "Memory Use state"
set key box 3
set key below
set ylabel "MB"
set y2label "(%)"
set x2range [0:]
set y2range [0:100]
set xdata time
set timefmt "%H:%M:%S"
set xtics;set x2tics;set ytics ;set y2tics;
plot '$SOURCE_FILE' using 1:3 w p title "Total Memory",'' using 1:4 w p title "Used",'' using 1:7 w p title "Cached" ,\
'' using 5 with l lt rgb "red" title "memused($used_percent%)" axis x2y2
EOF
}
DiskUsage(){
  local file=$1
  cat $file |sed "/^$/d"|grep -v -i -E "device|linux"|sed 1d >$GNUPLOTFOLDER/disk.$$
  local SOURCE_FILE=$GNUPLOTFOLDER/disk.$$
  plot_readiops=""
  plot_writeiops=""
  local nu=1
  echo "`date '+%F %H:%M:%S'`: Disk Performance analysis" |tee -a $REPORTFILE
  for diskpart in `cat $file |sed '1,2d'|grep -v -i "device"|awk -F " " '{print $1}'|sort|uniq -d|sed '/^$/d'|grep -E "^[a-z][a-z][a-z]$"`
  do
    awk "(\$1==\"$diskpart\"){print}" $SOURCE_FILE >$GNUPLOTFOLDER/gnu_tmpfile.$diskpart
    plot_readiops="$plot_readiops \"$GNUPLOTFOLDER/gnu_tmpfile.$diskpart\" using 4 w l title \"$diskpart read IOPS\","
    awk "(\$1==\"$diskpart\"){print}" $SOURCE_FILE|awk 'BEGIN{total=0}{total+=$4}END{print total/NR}'>$GNUPLOTFOLDER/t.$$
    local read_avg_iops=`cat $GNUPLOTFOLDER/t.$$`
    rm -f $GNUPLOTFOLDER/t.$$
    awk "(\$1==\"$diskpart\"){print}" $SOURCE_FILE|awk '{print $4}'|sort -n|sed -n 1p>$GNUPLOTFOLDER/t.$$
    local read_min_iops=`cat $GNUPLOTFOLDER/t.$$`
    rm -f $GNUPLOTFOLDER/t.$$
    awk "(\$1==\"$diskpart\"){print}" $SOURCE_FILE|awk '{print $4}'|sort -n -r |sed -n 1p>$GNUPLOTFOLDER/t.$$
    local read_max_iops=`cat $GNUPLOTFOLDER/t.$$`
    rm -f $GNUPLOTFOLDER/t.$$
    plot_writeiops="$plot_writeiops \"$GNUPLOTFOLDER/gnu_tmpfile.$diskpart\" using 5 w l title \"$diskpart write IOPS\","

    awk "(\$1==\"$diskpart\"){print}" $SOURCE_FILE|awk 'BEGIN{total=0}{total+=$5}END{print total/NR}'>$GNUPLOTFOLDER/t.$$
    local write_avg_iops=`cat $GNUPLOTFOLDER/t.$$`
    rm -f $GNUPLOTFOLDER/t.$$
    awk "(\$1==\"$diskpart\"){print}" $SOURCE_FILE|awk '{print $5}'|sort -n|sed -n 1p>$GNUPLOTFOLDER/t.$$
    local write_min_iops=`cat $GNUPLOTFOLDER/t.$$`
    rm -f $GNUPLOTFOLDER/t.$$
    awk "(\$1==\"$diskpart\"){print}" $SOURCE_FILE|awk '{print $5}'|sort -n -r |sed -n 1p>$GNUPLOTFOLDER/t.$$
    local write_max_iops=`cat $GNUPLOTFOLDER/t.$$`
    rm -f $GNUPLOTFOLDER/t.$$

    awk "(\$1==\"$diskpart\"){print}" $SOURCE_FILE|awk 'BEGIN{total=0}{total+=$6}END{print total/NR}'>$GNUPLOTFOLDER/t.$$
    local avg_read_throughput=`cat $GNUPLOTFOLDER/t.$$`
    rm -f $GNUPLOTFOLDER/t.$$
    awk "(\$1==\"$diskpart\"){print}" $SOURCE_FILE|awk '{print $6}'|sort -n|sed -n 1p>$GNUPLOTFOLDER/t.$$
    local min_read_throughput=`cat $GNUPLOTFOLDER/t.$$`
    rm -f $GNUPLOTFOLDER/t.$$
    awk "(\$1==\"$diskpart\"){print}" $SOURCE_FILE|awk '{print $6}'|sort -n -r |sed -n 1p>$GNUPLOTFOLDER/t.$$
    local max_read_throughput=`cat $GNUPLOTFOLDER/t.$$`
    rm -f $GNUPLOTFOLDER/t.$$
    awk "(\$1==\"$diskpart\"){print}" $SOURCE_FILE|awk 'BEGIN{total=0}{total+=$7}END{print total/NR}'>$GNUPLOTFOLDER/t.$$
    local avg_write_throughput=`cat $GNUPLOTFOLDER/t.$$`
    rm -f $GNUPLOTFOLDER/t.$$
    awk "(\$1==\"$diskpart\"){print}" $SOURCE_FILE|awk '{print $7}'|sort -n|sed -n 1p>$GNUPLOTFOLDER/t.$$
    local min_write_throughput=`cat $GNUPLOTFOLDER/t.$$`
    rm -f $GNUPLOTFOLDER/t.$$
    awk "(\$1==\"$diskpart\"){print}" $SOURCE_FILE|awk '{print $7}'|sort -n -r |sed -n 1p>$GNUPLOTFOLDER/t.$$
    local max_write_throughput=`cat $GNUPLOTFOLDER/t.$$`
    rm -f $GNUPLOTFOLDER/t.$$

    awk "(\$1==\"$diskpart\"){print}" $SOURCE_FILE|awk 'BEGIN{total=0}{total+=$10}END{print total/NR}'>$GNUPLOTFOLDER/t.$$
    local avg_await=`cat $GNUPLOTFOLDER/t.$$`
    rm -f $GNUPLOTFOLDER/t.$$
    awk "(\$1==\"$diskpart\"){print}" $SOURCE_FILE|awk '{print $10}'|sort -n|sed -n 1p>$GNUPLOTFOLDER/t.$$
    local min_await=`cat $GNUPLOTFOLDER/t.$$`
    rm -f $GNUPLOTFOLDER/t.$$
    awk "(\$1==\"$diskpart\"){print}" $SOURCE_FILE|awk '{print $10}'|sort -n -r |sed -n 1p>$GNUPLOTFOLDER/t.$$
    local max_await=`cat $GNUPLOTFOLDER/t.$$`
    rm -f $GNUPLOTFOLDER/t.$$

    echo -e "\t\t$nu.$diskpart performance:"|tee -a $REPORTFILE
    echo -e "\t\t\t read iops:\t\t max=$read_max_iops ,\t avg=$read_avg_iops ,\t min=$read_min_iops"|tee -a $REPORTFILE
    echo -e "\t\t\t write iops:\t\t max=$write_max_iops ,\t avg=$write_avg_iops ,\t min=$write_min_iops"|tee -a $REPORTFILE
    echo -e "\t\t\t read data per second:\t max=$max_read_throughput KB,\t avg=$avg_read_throughput KB,\t min=$min_read_throughput KB"|tee -a $REPORTFILE
    echo -e "\t\t\t write data per second:\t max=$max_write_throughput KB,\t avg=$avg_write_throughput KB,\t min=$min_write_throughput KB"|tee -a $REPORTFILE
    echo -e "\t\t\t each io wait time:\t max=$max_await ms ,\t avg=$avg_await ms ,\t min=$min_await ms"|tee -a $REPORTFILE
  done

/usr/local/bin/gnuplot --persist <<EOF
set term png size 800,600
set output "DiskIOPSPerformance.png"
set key title "Disk IOPS"
set key box 3
set key below
set ylabel "IOPS"
plot $plot_readiops $plot_writeiops
EOF
}
NetworkPerformance(){
    local file=$1
    sed -i 's/PM//g' $file
    cat $file |grep -E "eth|em"|grep -v -i "average">$GNUPLOTFOLDER/network_sourcefile.txt
    local sourcefile=$GNUPLOTFOLDER/network_sourcefile.txt
    local titlerxpackage=`cat $file |grep -i "IFACE"|awk '{print $3}'|uniq -d`
    local titletxpackage=`cat $file |grep -i "IFACE"|awk '{print $4}'|uniq -d`
    local titlerxbyte=`cat $file |grep -i "IFACE"|awk '{print $5}'|uniq -d`
    local titletxbyte=`cat $file |grep -i "IFACE"|awk '{print $6}'|uniq -d`
    if [ $titlerxbyte == 'rxkB/s' ];then
      unit="KB"
    elif [ $titlerxbyte == 'rxbyt/s' ];then
      unit="byte"
    fi
    local rxpackage=''
    local txpackage=''
    local rxbyte=''
    local txbyte=''
    local nu=1
    echo "`date '+%F %H:%M:%S'`: Network Performance analysis" |tee -a $REPORTFILE
    for netcard in `cat $file |grep -E "eth|em"|grep -v -i "average"|awk '{print $2}'|sort|uniq -d`
    do
      cat $sourcefile|grep $netcard>$GNUPLOTFOLDER/gnu_network.$netcard
      rxpackage="$rxpackage \"$GNUPLOTFOLDER/gnu_network.$netcard\" using 1:3 w l title \"$netcard $titlerxpackage\","
      txpackage="$txpackage \"$GNUPLOTFOLDER/gnu_network.$netcard\" using 1:4 w l title \"$netcard $titletxpackage\","
      rxbyte="$rxbyte \"$GNUPLOTFOLDER/gnu_network.$netcard\" using 1:5 w l title \"$netcard $titlerxbyte\","
      txbyte="$txbyte \"$GNUPLOTFOLDER/gnu_network.$netcard\" using 1:6 w l title \"$netcard $titletxbyte\","
      max_rxpck=` cat $GNUPLOTFOLDER/gnu_network.$netcard|sort -n -r -k 3|sed -n 1p|awk '{print $4}'`
      avg_rxpck=`awk 'BEGIN{total=0}{total+=$3}END{print total/NR}' $GNUPLOTFOLDER/gnu_network.$netcard`
      min_rxpck=` cat $GNUPLOTFOLDER/gnu_network.$netcard|sort -n -k 3|sed -n 1p|awk '{print $4}'`
      max_txpck=` cat $GNUPLOTFOLDER/gnu_network.$netcard|sort -n -r -k 4|sed -n 1p|awk '{print $5}'`
      avg_txpck=`awk 'BEGIN{total=0}{total+=$4}END{print total/NR}' $GNUPLOTFOLDER/gnu_network.$netcard`
      min_txpck=` cat $GNUPLOTFOLDER/gnu_network.$netcard|sort -n -k 4|sed -n 1p|awk '{print $5}'`
      max_rxbyt=` cat $GNUPLOTFOLDER/gnu_network.$netcard|sort -n -r -k 5|sed -n 1p|awk '{print $6}'`
      avg_rxbyt=`awk 'BEGIN{total=0}{total+=$5}END{print total/NR}' $GNUPLOTFOLDER/gnu_network.$netcard`
      min_rxbyt=` cat $GNUPLOTFOLDER/gnu_network.$netcard|sort -n -k 5|sed -n 1p|awk '{print $6}'`
      max_txbyt=` cat $GNUPLOTFOLDER/gnu_network.$netcard|sort -n -r -k 6|sed -n 1p|awk '{print $7}'`
      avg_txbyt=`awk 'BEGIN{total=0}{total+=$6}END{print total/NR}' $GNUPLOTFOLDER/gnu_network.$netcard`
      min_txbyt=` cat $GNUPLOTFOLDER/gnu_network.$netcard|sort -n -k 6|sed -n 1p|awk '{print $7}'`
      echo -e "\t\t$nu.$netcard load:"|tee -a $REPORTFILE
      echo -e "\t\t\t rxpck/s:\t\t max=$max_rxpck ,\t avg=$avg_rxpck ,\t min=$min_rxpck"|tee -a $REPORTFILE
      echo -e "\t\t\t txpck/s:\t\t max=$max_txpck ,\t avg=$avg_txpck ,\t min=$min_txpck"|tee -a $REPORTFILE
      echo -e "\t\t\t rxbyt/s:\t max=$max_rxbyt $unit,\t avg=$avg_rxbyt $unit,\t min=$min_rxbyt $unit"|tee -a $REPORTFILE
      echo -e "\t\t\t txbyt/s:\t max=$max_txbyt $unit,\t avg=$avg_txbyt $unit,\t min=$min_txbyt $unit"|tee -a $REPORTFILE
      nu=`echo $nu+1|bc`

    done
/usr/local/bin/gnuplot --persist <<EOF
set term png size 800,600
set output "NetworkPackagePerformance.png"
set key title "network performance"
set key box 3
set key below
set ylabel "Package/s"
set xdata time
set timefmt "%H:%M:%S"
plot $rxpackage $txpackage
EOF
/usr/local/bin/gnuplot --persist <<EOF
set term png size 800,600
set output "NetworkThougtputPerformance.png"
set key title "Throughput performance"
set key box 3
set key below
set ylabel "$unit"
set xdata time
set timefmt "%H:%M:%S"
plot $rxbyte $txbyte
EOF
}
#定义gnuplot的字体msttcore目录及字体,这个在压缩包里有,放到指定目录即可。
export GDFONTPATH="/usr/share/fonts/msttcore"
export GNUPLOT_DEFAULT_GDFONT="arial"
SYSINFO_FILE=sysinfo
CPU_USAGE_FILE=cpuusage.log
MEMORY_USAGE_FILE=memusage.log
DISK_USAGE_FILE=diskusage.log
NETWORK_USAGE_FILE=network.log
PWD=`pwd`
REPORTFILE=report.txt
GNUPLOTFOLDER="/tmp/gnuplotlinux"
mkdir -p $GNUPLOTFOLDER
#Time=`date '+%F %H:%M:%S'`
echo -e "\t\t\t\t\t\tSDG Aystem Analysis Report" > $REPORTFILE
SysInfo $SYSINFO_FILE
echo "" >> $REPORTFILE
CpuAllUsage $CPU_USAGE_FILE
CpuEachCoreUsage $CPU_USAGE_FILE
echo "" >> $REPORTFILE
MemoryUsage $MEMORY_USAGE_FILE
echo "" >> $REPORTFILE
DiskUsage $DISK_USAGE_FILE
echo "" >> $REPORTFILE
NetworkPerformance $NETWORK_USAGE_FILE
echo "" >> $REPORTFILE
#脚本执行完成之后,会在性能数据所在的目录中生成
#report.txt   性能报告文本
#TotalCpuUsage.png  CPU利用率图表
#CpuCoreIdle.png   每个CPU核心的Idle
#MemoryUsage.png   内存使用率
#DiskIOPSPerformance.png  磁盘IOPS性能
#NetworkPackagePerformance.png  网卡发包率性能
#NetworkThougtputPerformance.png 网卡吞吐性能
#################################################
(0)

相关推荐

  • 编写Bash Shell通过gnuplot绘制系统性能数据图的方法

    使用步骤: 1.设置一个定时任何 执行getperf.sh,采集性能数据 2.将采集到性能数据文件,如:192.168.1.1.tar.gz 解压 3.将性能分析的脚步performance_analyse.sh 放到解压后的目录中 4.安装gnuplot程序(这里下载) 5.将字体文件夹,拷贝到/usr/share/fonts/目录 6.直接运行 bash performance_analyse.sh getperf.sh脚本内容如下: ############################

  • extjs4图表绘制之折线图实现方法分析

    本文实例讲述了extjs4图表绘制之折线图实现方法.分享给大家供大家参考,具体如下: 本篇文章将介绍extjs中自带的图表 在本次案例中,提供一下功能: 1.从后端请求数据并运用到图表中,形成动态数据. 2.查询出每年各个月中人数. 请看下面代码: Ext.define('ChartLineTest', { extend: 'Ext.panel.Panel', autoScroll : true, selectYear:null,//定义年份 initComponent: function ()

  • Python几种绘制时间线图的方法

    目录 Matplotlib 制作 Plotly 绘制 Excel 绘制 Matplotlib 制作 Matplotlib 作为 Python 家族最为重要的可视化工具,其基本的 API 以及绘制流程还是需要掌握的.尤其是该库的灵活程度以及作为众多工具的基础,重要性不言而喻 下面我们来看下该如何绘制一个时间线图表 导入库以及设置 XY 轴数据 import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['SimHei'] p

  • 使用idea的database模块绘制数据库er图的方法

    在idea中使用database连接数据库 建立测试库表: 建立测试库test_user: test_user库创建过程如下 建立测试表: 建立表user.表user_ext如下,表user_ext中user_id用来关联表user中的id,以此建立逻辑外键关系 绘制er图: 选中user表,打开绘制er图界面: 字段命名规范的情况: 当前user_ext表user_id 作为虚拟外键关联user表的id字段,由于user_id,命名规范,可以推测到是关联user表的id字段,所以idea会自动

  • EasyX绘制透明背景图的方法详解

    目录 三元光栅操作 优化方案 三元光栅操作 根据在网上的搜索总结得到两种方案,最常见的绘制带有透明背景的图像的方案都是采用如下的源图像和掩码图像叠加来消去边缘部分: IMAGE img[2]; loadimage(&img[0], "sun1.png", 100, 100); // 掩码图像 loadimage(&img[1], "sun0.png", 100, 100); // 源图像 putimage(0, 0, &img[0], NOT

  • 利用Python绘制MySQL数据图实现数据可视化

    本教程的所有Python代码可以在网上的IPython notebook中获取. 考虑在公司里使用Plotly?可以看一下Plotly的on-premises企业版.(注:On-premises是指软件运行在工作场所或公司内部,详见维基百科) 注意操作系统:尽管Windows或Mac用户也可以跟随本文操作,但本文假定你使用的是Ubuntu系统(Ubuntu桌面版或Ubuntu服务器版).如果你没有Ubuntu Server,你可以通过Amazon的Web服务建立一个云平台(阅读这份教程的前半部分

  • Python使用plotly绘制数据图表的方法

    导语:使用 python-plotly 模块来进行压测数据的绘制,并且生成静态 html 页面结果展示. 不少小伙伴在开发过程中都有对模块进行压测的经历,压测结束后大家往往喜欢使用Excel处理压测数据并绘制数据可视化视图,但这样不能很方便的使用web页面进行数据展示.本文将介绍使用python-plotly模块来进行压测数据的绘制,并且生成静态html页面方便结果展示. Plotly简介 Plotly是一款使用JavaScript开发的制图工具,提供了与主流数据分析语言交互的API(如:Pyt

  • 使用Python脚本在Linux下实现部分Bash Shell的教程

    对于Linux用户来说,命令行的名声相当的高.不像其他操作系统,命令行是一个可怕的命题,但是对于Linux社区中那些经验丰富的大牛,命令行却是最值得推荐鼓励使用的.通常,命令行对比图形用户界面,更能提供更优雅和更高效的解决方案. 命令行伴随着Linux社区的成长,UNIX shells,例如 bash和zsh,已经成长为一个强大的工具,也是UNIX shell的重要组成部分.使用bash和其他类似的shells,可以得到一些很有用的功能,例如,管道,文件名通配符和从文件中读取命令,也就是脚本.

  • Linux系统中bash shell编程的10个基础问题讲解

    第1问:为何叫做shell? 在介绍 shell 是什么东西之前,不妨让我们重新审视使用者与电脑的关系.我们知道电脑的运作不能离开硬件,但使用者却无法直接对硬件作驱动,硬件的驱动只能透过一个称为"操作系统(Operating System)"的软件来控管,事实上,我们每天所谈的linux,严格来说只是一个操作系统,我们称之为"核心(kernel)".然而,从使用者的角度来说,使用者也没办法直接操作kernel,而是透过kernel的"外壳"程序,

  • linux环境下编写shell脚本实现启动停止tomcat服务的方法

    第一步:以管理员的身份进入控制台,在指定目录下新建一个shell脚本,我这里命名为tomcat.sh 第二步:编写shell脚本 #!/bin/bash tomcat_home=/usr/tomcat/apache-tomcat-8.0.48 SHUTDOWN=$tomcat_home/bin/shutdown.sh STARTTOMCAT=$tomcat_home/bin/startup.sh case $1 in start) echo "启动$tomcat_home" $STAR

随机推荐