linux生产系统性能检测脚本,Shell脚本查看linux系统性能瓶颈

#!/bin/bash

#

os_check() {if [ -e /etc/redhat-release ]; then

REDHAT=`cat /etc/redhat-release |cut -d‘ ‘ -f1`elseDEBIAN=`cat /etc/issue |cut -d‘ ‘ -f1`

fiif [ "$REDHAT" == "CentOS" -o "$REDHAT" == "Red"]; then

P_M=yum

elif ["$DEBIAN" == "Ubuntu" -o "$DEBIAN" == "ubutnu"]; then

P_M=apt-get

elseOperating system does not support.

exit1fi

}if [ $LOGNAME !=root ]; then

echo"Please use the root account operation."exit1fiif ! which vmstat &>/dev/null; then

echo"vmstat command not found,now the install."sleep1os_check

$P_M install procps-y

echo"-----------------------------------------------------------------------"fiif ! which iostat &>/dev/null; then

echo"iostat command not found,now the install."sleep1os_check

$P_M install sysstat-y

echo"-----------------------------------------------------------------------"fiwhile 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 incpu_load)

#cpu利用率与负载

echo"---------------------------------------"i=1

while [[ $i -le 3 ]]; doecho-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))

sleep1done

echo"---------------------------------------"

break;;

disk_load)

#硬盘I/O负载

echo"---------------------------------------"i=1

while [[ $i -le 3 ]]; doecho-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:\n$READ"echo-e "Write/s:\n$WRITE"i=$(($i+1))

sleep1done

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

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_LOGelseecho"---------------------------------------"echo"Disk use rate no than 90% of the partition."echo"---------------------------------------"fibreak;;

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"$PART = ${i}%" >>$INODE_LOG

fi

doneif [ -f $INODE_LOG ]; then

echo"---------------------------------------"rm-f $INODE_LOGelseecho"---------------------------------------"echo"Inode use rate no than 90% of the partition."echo"---------------------------------------"fibreak;;

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#ps aux|awk ‘{if($3>0.1)print "cpu: "$3"% -->",$11,$12,$13,$14,$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"\n";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_LOGelseecho"No process using the cpu."

breakfi

i=$(($i+1))

sleep1done

echo"---------------------------------------"

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)print "Memory: "$4"% -->","(PID:"$2")" |"sort -k2 -nr |head -n 10"}‘ >$MEM_LOG

ps aux|awk ‘{if($4>0.1){{printf "PID: "$2" Memory: "$3"% --> "}for(i=11;i<=NF;i++)if(i==NF)printf $i"\n";else printf $i}}‘ |sort -k4 -nr |head -10 >$MEM_LOGif [[ -n `cat $MEM_LOG` ]]; then

echo-e "\033[32m 参考值${i}\033[0m"cat $MEM_LOG>$MEM_LOGelseecho"No process using the Memory."

breakfi

i=$(($i+1))

sleep1done

echo"---------------------------------------"

break;;

traffic)

#查看网络流量while true; doread-p "Please enter the network card name(eth[0-9] or em[0-9]):"eth#if [[ $eth =~ ^eth[0-9]$ ]] || [[ $eth =~ ^em[0-9]$ ]] && [[ `ifconfig |grep -c "\"` -eq 1 ]]; then

if [ `ifconfig |grep -c "\"` -eq 1]; thenbreak

elseecho"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#OLD_IN=`ifconfig $eth |awk ‘/RX bytes/{print $2}‘ |cut -d: -f2`

#OLD_OUT=`ifconfig $eth |awk ‘/RX bytes/{print $6}‘ |cut -d: -f2`

OLD_IN=`ifconfig $eth |awk -F‘[: ]+‘ ‘/bytes/{if(NR==8)print $4;else if(NR==5)print $6}‘`

#CentOS6和CentOS7 ifconfig输出进出流量信息位置不同,CentOS6中RX与TX行号等于8,CentOS7中RX行号是5,TX行号是5,所以就做了个判断.

OLD_OUT=`ifconfig $eth |awk -F‘[: ]+‘ ‘/bytes/{if(NR==8)print $9;else if(NR==7)print $6}‘`

sleep1NEW_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{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))

sleep1done

echo"---------------------------------------"

break;;

quit)

exit0;;*)

echo"---------------------------------------"echo"Please enter the number."echo"---------------------------------------"

break;;

esac

done

done

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值