Shell Script View Linux system performance bottlenecks (go to: http://blog.51cto.com/lizhenliang/1687612)
[[email protected] ~]# cat show_sys_info.sh #!/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 '-F 1 ' fi if ["$REDHAT" = = "CentOS"-o "$REDHAT" = = "Red"]; Then P_m=yum elif ["$DEBIAN" = = "Ubuntu"-o "$DEBIAN" = = "Ubutnu"]; Then P_m=apt-get else Operating system does not support. Exit 1 fi}if [$LOGNAME! = root]; Then echo "Please use the root account operation." Exit 1fiif! Which Vmstat &>/dev/null; then echo "Vmstat command is found, now the install." Sleep 1 os_check $P _m install procps-y echo "------------------------------------------------------- ----------------"Fiif! Which Iostat &>/dev/null; then echo "Iostat command is found, now the 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 usage and load echo "-------------------- -------------------"I=1 while [[$i-le 3]]; Do echo-e "\033[32m Reference value${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 the "echo" System use: $SYS "echo" I/O wait: $IOWAIT "i=$ (($i + 1)) sleep1 done echo "---------------------------------------" break;; disk_load) #硬盘I/O load echo "---------------------------------------" I=1 while [[$i-le 3]]; Do echo-e "\033[32m Reference value${i}\033[0m" Util= ' iostat-x-K |awk '/^[v|s]/{ Ofs= ":";p rint $, $NF "%"} ' read= ' Iostat-x-K |awk '/^[v|s]/{ofs= ":";p rint $1,$6 "KB"} " Write= ' iostat-x-K |awk '/^[v|s]/{ofs= ":";p rint $1,$7 "KB"} ' iowait= ' Vmstat |awk ' {if (nr==3) PRI NT $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/{printf $ "";p rintf "%d", $3;print "GB"} ' use_rate= ' df-h |awk '/^\/dev/{print in T ($)} ' for i in $USE _rate; do if [$i-gt];then part= ' df-h |awk ' {if (int ($) = = ' $i ") print $6} ' echo "$PART = ${i}%" >> $DISK _log fi done Echo "---------------------------------------" Echo-e "Disk total:\n${disk_total}" if [-f $DIS K_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) #disk inode useage inode_log=/tmp/inode_use.tmp inode_use= ' df-i |awk '/^\/dev/{print Int ($ $)} ' for i in $INODE _use; do if [$i-GT 90]; Then part= ' Df-h |awk ' {if (int ($) = = "$i") print $6} ' echo ' $PART = ${i} % ">> $INODE _log fi done if [-f $INODE _log]; Then echo "---------------------------------------" Rm-f $INODE _log else echo "---------------------------------------" echo "Inode use rate no than 90% of the partition. " echo "---------------------------------------"fi break;; Mem_use) #memory useage echo "---------------------------------------" mem_to Tal= ' 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 _to TAL "ECHO-E" use: $USE "echo-e" Free: $FREE "Echo-e" Cache: $CACHE " echo "---------------------------------------" break;; Tcp_status) #tcp Connection 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) # Top Ten of the CPU processes echo "---------------------------------------" Cpu_log=/tmp/cpu_top.tmp I=1 while [[$i-le 3]]; Do #ps aux |awk ' {if ($3>0.1) print "CPU:" $ "--", $11,$12,$13,$14,$15,$16, "(PID:" $ ")" | " Sort-k2-nr |head-n "} ' > $CPU _log ps aux |awk ' {if ($3>0.1) {{printf] PID:" $ $ "CPU:" $ $ "%--& Gt "}for (i=11;i<=nf;i++) if (I==NF) printf $i" \ n "; else printf $i}} ' |sort-k4-nr |head-10 > $CPU _log #循环从11列 (process name) starts printing, if I equals the last line, I print the column of I and wrap it, otherwise I print the column if [[-N ' Cat $CPU _log ']]; Then Echo-e "\033[32m Reference value${i}\033[0m" Cat $CPU _log > $CPU _log Else echo "No process using the CPU." Break fi i=$ (($i + 1)) Sleep 1 done E Cho "---------------------------------------" break;; MEM_TOP10) #top of memory processes echo "---------------------------------------" Mem_log=/tmp/mem_top.tmp I=1 while [[$i-le 3]]; Do #ps aux |awk ' {if ($4>0.1) print "Memory:" $4 "and", $11,$12,$13,$14,$15,$16, "(PID:" $ ")" | " Sort-k2-nr |head-n "} ' > $MEM _log ps aux |awk ' {if ($4>0.1) {{printf" PID: "$ $" Memory: "$ $"% --"}for (i=11;i<=nf;i++) if (I==NF) printf $i" \ n "; else printf $i}} ' |sort-k4-nr |head-10 > $MEM _log if [[-N ' Cat $MEM _log]]; Then Echo-e "\033[32m Reference value${i}\033[0m" Cat $MEM _log > $MEM _log Else echo "No process using the Memory." Break fi i=$ (($i + 1)) Sleep 1 D One echo "---------------------------------------" break;; Traffic) # Check network while true; Do read-p "Please enter the network card name (eth[0-9] or em[0-9]):" ETH #if [[$e Th =~ ^eth[0-9]$] | | [[$eth =~ ^em[0-9]$]] && [[' Ifconfig |grep-c ' \< $eth \> "'-eq 1]]; Then if [' Ifconfig |grep-c "\< $eth \>" '-eq 1]; Then break else echo "Input format error or Don t hav E 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 ' |cut-d:-f2 ' #OLD_OUT = ' ifconf IG $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 output in and out of traffic information location, CentOS6 Rx and TX line number equals 8,CENTOS7 r The X line number is the 5,TX line number is 5, so make a judgment. 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 ' B egin{printf "%.1f\n", ' $ ((${new_in}-${old_in}) '/1024/128} ' out= ' awk ' begin{printf "%.1f\n", ' $ ((${new_o ut}-${Old_out})) '/1024/128} ' echo ' ${in}mb/s ${out}mb/s ' i=$ (($i + 1)) s Leep 1 done echo '---------------------------------------' break ;; Quit) exit 0;; *) echo "---------------------------------------" echo "Please enter the number." echo "---------------------------------------" break;; Esac Donedone
Shell Script View Linux system performance bottlenecks (RPM)