shellスクリプトインスタンス--システムパフォーマンス分析
10159 ワード
スクリプトの目的:システムリソースのパフォーマンスボトルネックの分析
スクリプト機能:
1.CPU使用率と負荷の表示(top,vmstat,sar)
2.ディスクの表示、INode使用率とI/O負荷(df,iostat,iotop,sar,dstat)
3.メモリ使用率の表示(free,vmstat)
4.TCPリンク状態の表示(netstat,ss)
5.CPUとメモリの占有率が最も高い10個のプロセス(top,ps)を確認する
6.ネットワークトラフィックの表示(ifconfig,iftop,iptraf)
selectスクリプト
スクリプト機能:
1.CPU使用率と負荷の表示(top,vmstat,sar)
2.ディスクの表示、INode使用率とI/O負荷(df,iostat,iotop,sar,dstat)
3.メモリ使用率の表示(free,vmstat)
4.TCPリンク状態の表示(netstat,ss)
5.CPUとメモリの占有率が最も高い10個のプロセス(top,ps)を確認する
6.ネットワークトラフィックの表示(ifconfig,iftop,iptraf)
selectスクリプト
#!/bin/bash
PS3="Your choice is[5 for quit]: "
select choice in disk_partition filesystem cpu_load mem_util quit
do
case "$choice" in
disk_partition)
fdisk -l
;;
filesystem)
df -h
;;
cpu_load)
uptime
;;
mem_util)
free -m
;;
quit)
break
;;
*)
echo "error"
esac
done
#!/bin/bash
os_check(){
if [ -e /etc/redhat-release ];then
REDHAT=`cat /etc/redhat-release | cut -d' ' -f1`
else
DEBIAN=`cat /etc/issue | cut -d' ' -f1`
fi
if[ "$REDHAT" == "CentOS" -o "$REDHAT" == "Red" ];then
P_M=yum
elif[ $"DEBIAN" == "Ubuntu" -o "$DEBIAN" == "ubuntu" ];then
P_M=apt-get
else
Operating system does not support
exit 1
fi
}
if [$LOGNAME != root ];then root
echo "Please user the root account operation."
exit 1
fi
if ! which vmstat &>/dev/null; then
echo "vmstat command not found,now the install."
sleep 1
os_check
$P_M install procps -y
echo "------------------------------"
fi
which iostat &>/dev/null
if [ $? -ne 0];then
echo "iostat command not found,now then install"
sleep 1
os_check
$P_M install sysstat -y
echo "-------------------------------"
fi
while true; do
select input in cpu_load disk_load disk_use disk_inode mem_use tcp_status cpu_top10 mem_top10 traffic quit;do
case $input in
cpu_load)
#CPU
echo "----------------------"
i=1
while [[ $i -le 3 ]];do
echo -e "\033[32m ${i}\033[0m"
UTIL=`vmstat | awk '{if(NR==3)print 100-$15"%"}'` 3
USER=`vmstat | awk '{if(NR==3)print $13"%"}'`
SYS=`vmstat | awk '{if(NR==3)print $14"%"}'`
IOWAIT=`vmstat | awk '{if(NR==3)print $16"%"}'`
echo "Util: $UTIL"
echo "User use : $USER"
echo "System use : $SYS"
echo "I/O wait : $IOWAIT"
let i++
sleep 1
done
echo "-----------"
break
;;
disk_load)
# I/O
echo "--------------------"
i=1
while [[ $i -le 3 ]];do
echo -e "\033[32m ${i}\033[0m"
UTIL=`iostat -x -k | awk '/^[v|s]/{OFS=": ";print $1,$NF"%"}'`
READ=`iostat -x -k | awk '/^[v|s]/{OFS=": ";print $1,$6"KB"}'`
WRITE=`iostat -x -k | awk '/^[v|s]/{OFS=": ";print $1,$7"KB"}'`
IOWAIT=`vmstat | awk '{if(NR==3)print $16"%"}'`
echo -e "Util:"
echo -e "${UTIL}"
echo -e "I/O Wait : $IOWAIT"
echo -e "Read/s:
$READ"
echo -e "Write/s:
$WRITE"
i=$(($i+1))
sleep 1
done
echo "----------------"
break
;;
disk_use)
#
DISK_LOG=/tmp/disk_use.tmp
DISK_TOTAL=`fdisk -l | awk '/^Disk.*bytes/ && /\/dev/{printf $2" ";printf "%d",$3;print "GB"}'`
USE_RATE=`df -h | awk '/^\/dev/{print int($5)}'`
for i in $USE_RATE;do
if [ $i -gt 90 ];then
PATH=`df -h | awk 'if(int($5)=='''$i''') print $6'`
echo "$PATH = ${i}%" >> $DISK_LOG
fi
done
echo "------------------------"
echo -e "Disk total:
${DISK_TOTAL}"
if [ -f $DISK_LOG ];then
echo "---------------------"
cat $DISK_LOG
echo "---------------------"
rm -f $DISK_LOG
else
echo "---------------------"
echo "Disk use rate no than 90% of the partition."
echo "---------------------"
fi
break
;;
disk_inode)
# inode
INODE_LOG=/tmp/inode_use.tmp
INODE_USE=`df -i | awk '/^\/dev/{print int($5)}'`
for i in $INODE_USE;do
if[ $i -gt 90 ];then
PART=`df -h | awk '{if(int($5)=='''$i''') print $6}'`
echo "$PATH = ${i}%" >> $INODE_LOG
fi
done
if [ -f $INODE_LOG ];then
echo "--------------------"
cat $INODE_LOG
rm -f $INODE_LOG
else
echo "--------------------"
echo "Inode use rate no than 90% of the partition."
echo "---------------------"
fi
break
;;
mem_use)
#
echo "---------------------------"
MEM_TOTAL=`free -m | awk '{if(NR==2)printf "%.1f", $2/1024}END{print "G"}'`
USE=`free -m | awk '{if(NR==2) printf "%.1f",$3/1024}END{print "G"}'`
FREE=`free -m | awk '{if(NR==2) printf "%.1f", $4/1024}END{print "G"}'`
CACHE=`free -m | awk '{if(NR==2) printf "%.1f",$6/1024}END{print "G"}'`
echo -e "Total: $MEM_TOTAL"
echo -e "Use : $USE"
echo -e "Free: $FREE"
echo -e "Cache: $CACHE"
echo "-------------"
break
;;
tcp_status)
#
echo "------------------------------"
COUNT=`ss -ant | awk '!/State/{status[$1]++}END{for(i in status) print i,status[i]}'`
echo -e "TCP connection status :
$COUNT"
echo "----------------------------------"
break
;;
cpu_top10)
# CPU 10
echo "----------------------------------"
CPU_LOG=/tmp/cpu_top.tmp
i=1
while [[ $i -le 3 ]];do
#ps aux | awk '{if($3>0.1)print "CPU: "$3“% -->”, $11,$12,$13,$14,$15,$15,$16, "(PID: "$2")" | "sort -k2 -nr | head -n 10"}' > $CPU_LOG
ps aux | awk '{if($3>0.1){{printf "PID: "$2" CPU: "$3"% --> "}for(i=11;i<=NF;i++)if(i==NF)printf $i"
";else printf $i}}' | sort -k4 -nr | head -10>$CPU_LOG
# 11 ( ) , i , i , i
if [[ -n `cat $CPU_LOG` ]];then
echo -e "\033[32m ${i}\033[0m"
cat $CPU_LOG
> $CPU_LOG
else
echo "No process using the CPU."
break
fi
let i++
sleep 1
done
break
;;
mem_top10)
# 10
echo "---------------------"
MEM_LOG=/tmp/mem_top.tmp
i=1
while [[ $i -le 3 ]];do
ps aux | awk '{if($4>0.1){{printf "PID: "$2" Memory: "$3"% --> "}for(i=11;i<=NF;i++)if(i==NF)printf $i"
";else printf $i}}' | sort -k4 -nr | head -10 > $MEM_LOG
if [[ -n `cat $MEM_LOG` ]];then
echo -e "\033[32m ${i}\033[0m"
cat $MEM_LOG
>$MEM_LOG
else
echo "NO process using the Memory."
break
fi
let i++
sleep 1
done
echo "---------------------"
break
;;
traffic)
#
while true;do
read -p "Please enter the network card name(eth[0-9] or em[0-9] or team[0-9]): " eth
if [ `ifconfig | grep -c "\"` -eq 1 ];then
break
else
echo "Input format error or Don't have the card name, please input again."
fi
done
echo "---------------------"
echo -e " In ---------- Out --------------"
i=1
while [[ $i -le 3 ]];do
#CentOS6 CentOS7 ifconfig
#CentOS6 RX TX 8
#CentOS7 RX 5,TX 7
OLD_IN=`ifconfig $eth | awk -F'[: ]+' '/bytes/{if(NR==8)print $4;else if(NR==5)print $6}'`
OLD_OUT=`ifconfig $eth | awk -F'[: ]+' '/bytes/{if(NR==8)print $9;else if(NR==7)print $6}'`
sleep 1
NEW_IN=`ifconfig $eth | awk -F'[: ]+' '/bytes/{if(NR==8)print $4;else if(NR==5)print $6}'`
NEW_OUT=`ifconfig $eth | awk -F'[: ]+' '/bytes/{if(NR==8)print $9;else if(NR==7)print $6}'`
IN=`awk 'BEGIN{print "%.1f
",'$((${NEW_IN}-${OLD_IN}))'/1024/128}'`
OUT=`awk 'BEGIN{print "%.1f
",'$((${NEW_OUT}-${OLD_OUT}))'/1024/128}'`
echo "${IN}MB/s ${OUT}MB/s"
i=$(($i+1))
sleep 1
done
echo "-------------------------------"
break
;;
quit)
exit 0
;;
*)
echo "--------------------------"
echo "Please enter the number."
echo "--------------------------"
break
;;
esac
done
done