select循环使用
#!/usr/bin/bash
## PS 会修改提示符
PS3="input choice [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 'input error'
exit
esac
done
收集信息分析性能瓶颈
命令 | 功能 |
---|---|
top vmstat sar | 查看cup利用率与负载 |
df iostat iotop sar | 查看磁盘,inode利用率与I/O负载 |
free vmstat | 查看内存利用率 |
ss netstat | 查看TCP链接状态 |
top ps | 查看cpu与内存占用高的进程 |
ifconfig iftop iptraf | 查看网络流量 |
- 演示代码
##不同系统又些许差异,先判断系统
#!/usr/bin/bash
os_check(){
if [ -e /etc/redhat-release ];then
REDHAT=$(cat /etc/redhat-release|awk '{print $1}')
else
DEBIAN=$(cat /etc/issue|awk '{print $1}')
fi
if [ "$REDHAT" == "CentOS" -o "$REDHAT" == "Red" ];then
PM=yum
elif ["$DEBIAN" == "Ubuntu" -o "$DEBIAN" == "ubuntu" ];then
PM=apt-get
else
OS not support
exit 1
fi
}
if [ "$USER" != "root" ];then
echo "please user root account"
exit 1
fi
if ! which vmstat &>/dev/null;then
echo "vmstat command not funt,start install"
sleep 1
os_check
$PM install procps -y
fi
which iostat &>/dev/null
if [ $? -ne 0 ];then
echo "iostat command not funt,start install"
sleep 1
os_check
$PM install sysstat -y
fi
function men_chose(){
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)
echo "----------------------------"
i=1
for i in $(seq 3) ;do
echo -e "\033[32m 参考值${i} \033[0m"
UTIL=$(vmstat|awk '{if((NR==3)) print 100-$15"%"}')
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 "Utile: ${UTIL}"
echo "User use: $USER"
echo "System use: $SYS"
echo "I/O wait: $IOWAIT"
let i++
sleep 1
done
echo "----------------------------"
break
;;
disk_load)
echo "----------------------------"
for((i=0;i<3;i++))
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 "Utile: ${UTIL}"
echo "User use: $USER"
echo "I/O Read : $READ"
echo "I/O wait: $WRITE"
echo "I/O wait: $IOWAIT"
sleep 1
done
echo "----------------------------"
break
;;
disk_use)
DISK_LOG=/tem/disk_use.tmp
DISK_TOTAL=$(fdisk -l|awk '/^Disk.*bytes/ && /\/dev/{print $2" ";printf "%d",$3;print "GB"}')
USE_RATE=$(df -h|awk '/^\/dev/{print int($5)}')
userrate=90
for i in $USER_RATE ;
do
if [ $i -gt $userrate ];then
PART=$(df -h|awk '{if(int($5)=='''$i'''){print $6}}') # 外部变量引用i
echo "$PART =${i}%" >> $DISK_LOG
fi
done
echo "----------------------------"
echo -e "Disk toltal:\n${DISK_TOTAL}"
if [ -f $DISK_LOG ];then
cat "${DISK_LOG}"
rm -f $DISK_LOG
else
echo "Disk use rate no than ${userate}% of then partition"
fi
break
;;
disk_inode)
INODE_LOG=/tmp/inode_use.tmp
INODE_USE=$(df -i|awk '/^\/dev/{print int($5)}')
USE_RATE=90
for i in $INODE_USE;
do
if [ $i -gt $USE_RATE ];then
PART=$(df -h|awk '{if(int($5)=='''$i'''){print $6}}') # 外部变量引用i
echo "$PART=${i}%">>$INODE_LOG
fi
done
if [ -f $INODE_LOG ];then
cat $INODE_LOG
rm -f $INODE_LOG
else
echo "Inode use rate no than ${USE_RATE}% of the partition"
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:\n$COUNT"
echo "-------------------------------------"
break
;;
cpu_top10)
echo "-------------------------------------"
CPU_LOG=/tmp/cpu_top.tmp
i=1
while [[ $i -le 3 ]];do
ps aux|awk '{if(($3>0.1)){printf "PID:"$2 "CPU:" $3 "%--->";for(i=11;i<=NF;i++){if((i==NF))printf $i"\n";else printf $i}}}'|sort -nr -k4|head -10 >$CPU_LOG
if [[ -n $(cat $CPU_LOG) ]];then # -n 长度不为0
echo -e "\033[32m 参考值${i}\033[0m"
cat $CPU_LOG
>$CPU_LOG
else
echo "No proecess using th CPU"
break
fi
let i++
sleep 1
done
echo "--------------------------------------"
break
;;
mem_top10)
echo "--------------------------------------"
MEM_LOG=/tmp/mem_top.tmp
i=1
while [[ $i -le 3 ]];do
ps aux|awk '{if(($3>0.1)){printf "PID:"$2 "MEM:" $4 "%--->";for(i=11;i<=NF;i++){if((i==NF))printf $i"\n";else printf $i}}}'|sort -nr -k4|head -10 >$MEM_LOG
if [[ -n `cat $MEM_LOG` ]];then
ehco -e "\003[32m 参考值${i}\033[0m"
cat $MEM_LOG
>$MEM_LOG
else
echo "No process using the memory"
break
fi
sleep 1
let i++
done
break
;;
traffic)
#查看网络流量
while :
do
read -p "please enter the network card name(eth[0-9]) or em[0-9]" eth
eth_count=$(ifconfig|grep -c "\<$eth\>")
if [ $eth_count -ne 1 ];then
echo "input format error or do not have the card name,please input again"
break
fi
echo "------------------------------------"
echo -e "In ---------------------------- Out"
i=1
while [[ $i -le 3 ]];do
#这个主机的RX行号是5 行号是7 Sentos6都是8
IN1=$(ifconfig $eth|awk -F'[: ]+' '/bytes/{if(NR==8) print $4;else if(NR==3) print $6}')
OUT1=$(ifconfig $eth|awk -F'[: ]+' '/bytes/{if(NR==8) print $9;else if(NR==5) print $6}')
echo "$IN1 $OUT1"
sleep 1
IN2=$(ifconfig $eth|awk -F'[: ]+' '/bytes/{if(NR==8) print $4;else if(NR==3) print $6}')
OUT2=$(ifconfig $eth|awk -F'[: ]+' '/bytes/{if(NR==8) print $9;else if(NR==5) print $6}')
echo "$IN2 $OUT2"
IN3=$(awk 'BEGIN{printf "%.1f\n",'$((($IN2 - $IN1)/131072))'}') # 单位到Mbit
OUT3=$(awk 'BEGIN{printf "%.1f\n",'$((($OUT2 - $OUT1)/131072))'}') # 单位到Mbit
echo "${IN3}MB/s-------------${OUT3}MB/s"
let i++
sleep 1
done
echo "------------------------------------"
break
done
break
;;
quit)
exit 0
;;
*)
echo "chose error"
break
;;
esac
done
}
while :
do
PS3="please chose:"
men_chose
done