linux服务器敲命令反应慢,网站访问慢,到底什么情况?根据本人的经验,主要原因可能是系统资源到达瓶颈,已经无法处理更多请求。在有监控系统情况下,可以直接通过WEB页面可视化看出是CPU瓶颈?硬盘瓶颈?还是网络瓶颈?如果公司服务器较少或者云服务器,就有可能没有一套监控系统,这时就要登陆到服务器,一条一条的敲命令,查找分析性能瓶颈。命令这么多,咋记得住啊!就算记得住,输入也费劲,于是就有了这个脚本,为了以后自己使用,另外也想分享给博友,学shell朋友能从中得到一丢丢启发。写的比较仓促,内容有点粗略,还望君见谅!
脚本目的:分析系统资源性能瓶颈
脚本功能:
1、查看CPU利用率与负载(top、vmstat、sar)
2、查看磁盘、Inode利用率与I/O负载(df、iostat、iotop、sar)
3、查看内存利用率(free、vmstat)
4、查看TCP连接状态(netstat)
5、查看CPU与内存占用最高的10个进程(top、ps)
6、查看网络流量(ifconfig)
脚本说明:通过一些常用的性能分析工具,计算出我们想知道的信息。
脚本如下:
# cat show_sys_info.sh
#!/bin/bash # # blog:lizhenliang.blog.51cto.com which vmstat &>/dev/null if [ $? -ne 0 ]; then echo "vmstat command no found, please install procps package." exit 1 fi which iostat &>/dev/null if [ $? -ne 0 ]; then echo "iostat command no found, please install sysstat package." exit 1 fi if [ $LOGNAME != root ]; then echo "Please use the root account operation." exit 1 fi while true; do select input in cpu_load disk_load disk_use disk_inode mem_use tcp_status cpu_top10 mem_top10 traffic; 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"%"}‘` 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" i=$(($i+1)) 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 ‘/^s/{OFS=": ";print $1,$NF"%"}‘` READ=`iostat -x -k |awk ‘/^s/{OFS=": ";print $1,$6"KB"}‘` WRITE=`iostat -x -k |awk ‘/^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:\n$READ" echo -e "Write/s:\n$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\/sd[a-z]/{printf $2" ";printf "%d",$3;print "GB"}‘` USE_RATE=`df -h |awk ‘/^\/dev\/sd[a-z0-9]/{print int($5)}‘` for i in $USE_RATE; do if [ $i -gt 90 ];then PART=`df -h |awk ‘{if(int($5)==‘‘‘$i‘‘‘) print $6}‘` echo "$PART = ${i}%" >> $DISK_LOG fi done echo "---------------------------------------" echo -e "Disk total:\n${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\/sd[a-z][0-9]/{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 "$PART = ${i}%" >> $INODE_LOG fi done if [ -f $INODE_LOG ]; then echo "---------------------------------------" cat $INODE_LOG echo "---------------------------------------" 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==3) printf "%.1f",$3/1024}END{print "G"}‘` FREE=`free -m |awk ‘{if(NR==3) printf "%.1f",$4/1024}END{print "G"}‘` CACHE=`free -m |awk ‘{if(NR==2) printf "%.1f",($6+$7)/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=`netstat -antp |awk ‘{status[$6]++}END{for(i in status) print i,status[i]}‘` echo -e "TCP connection status:\n$COUNT" echo "---------------------------------------" ;; cpu_top10) #占用CPU高的前10个进程 echo "---------------------------------------" CPU_LOG=/tmp/cpu_top.tmp i=1 while [[ $i -le 3 ]]; do echo -e "\033[32m 参考值${i}\033[0m" ps aux |awk ‘{if($3>0.1)print "CPU: "$3"% -->",$NF|"sort -k2 -nr |head -n 10"}‘ > $CPU_LOG cat $CPU_LOG i=$(($i+1)) sleep 1 done echo "---------------------------------------" break ;; mem_top10) #占用内存高的前10个进程 echo "---------------------------------------" MEM_LOG=/tmp/mem_top.tmp i=1 while [[ $i -le 3 ]]; do echo -e "\033[32m 参考值${i}\033[0m" ps aux |awk ‘{if($4>0.1)print "CPU: "$4"% -->",$NF|"sort -k2 -nr |head -n 10"}‘ > $MEM_LOG cat $MEM_LOG i=$(($i+1)) sleep 1 done echo "---------------------------------------" break ;; traffic) #查看网络流量 while true; do read -p "Please enter the network card name(eth[0-9] or em[0-9]): " eth if [[ $eth =~ ^eth[0-9]$ ]] || [[ $eth =~ ^em[0-9]$ ]]; then break else echo "Input error,please input again." fi done echo "---------------------------------------" echo -e " In ------ Out" i=1 while [[ $i -le 3 ]]; do OLD_IN=`ifconfig $eth |awk ‘/RX bytes/{print $2}‘ |cut -d: -f2` OLD_OUT=`ifconfig $eth |awk ‘/RX bytes/{print $6}‘ |cut -d: -f2` sleep 1 NEW_IN=`ifconfig $eth |awk ‘/RX bytes/{print $2}‘ |cut -d: -f2` NEW_OUT=`ifconfig $eth |awk ‘/RX bytes/{print $6}‘ |cut -d: -f2` IN=`awk ‘BEGIN{printf "%.1f\n",‘$((${NEW_IN}-${OLD_IN}))‘/1024/128}‘` OUT=`awk ‘BEGIN{printf "%.1f\n",‘$((${NEW_OUT}-${OLD_OUT}))‘/1024/128}‘` echo "${IN}MB/s ${OUT}MB/s" i=$(($i+1)) sleep 1 done echo "---------------------------------------" break ;; *) echo "---------------------------------------" echo "Please enter the number." echo "---------------------------------------" break ;; esac done done
运行效果如下:
# sh show_sys_info.sh
时间: 2024-12-30 03:47:01