#!/bin/bash
#show system information
#time 2019-1-2
#use zidonghuajianchangxitong
PS3="Your choice is:"
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 [ "$REDHAT" =="Ubunt" -o "$DEBIAN" == "Ubutun" ];then
P_M=apt-get
else
Operating system dose not support.
exit
fi
}
if [ $LOGNAME != root ];then
echo "Please use the root account operation."
exit
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 -e "\e[1;31m -------------------------------------------------------------------------------------------------------------------------------------------------------------\e[0m"
fi
if ! which iostat &>/dev/null; then
echo "vmstat command not found, now the install."
sleep 1
os_check
$P_M install sysstat -y
echo -e "\e[1;31m -------------------------------------------------------------------------------------------------------------------------------------------------------------\e[0m"
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 #select循环打印菜单显示
case $input in #后面是case语句的内容
cpu_load)
#CPU利用率与负载
echo -e "\e[1;31m ----------------------------------------------------------------------------------------------------------------------------------------------------------\e[0m"
i=1
while [[ $i -le 2 ]]; do
echo -e "\033[1;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: $SYS"
echo "System use: $SYS"
echo "I/O wait: $IOWAIT"
let i++
sleep 1
done
echo -e "\e[1;31m -----------------------------------------------------------------------------------------------------------------------------------------------------------\e[0m"
break #执行完跳出去
;;
disk_load)
#硬盘I/O负载
echo -e "\e[1;31m -----------------------------------------------------------------------------------------------------------------------------------------------------------\e[0m"
i=1
while [[ $i -le 2 ]]; do
echo -e "\e[1;32m 参考值${i}\e[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 "I/O Wait: $IOWAIT"
echo -e "Read/s: \n$READ"
echo -e "Write: \n$WRITE"
i=$(($i+1))
done
echo -e "\e[1;31m ---------------------------------------------------------------------------------------------------------------------------------------------------------\e[0m"
break
;;
disk_use)
#硬盘利用率
DISK_LOG=/tmp/disk_use.tmp
DISK_TOTAL=`fdisk -l | awk '/^Disk.*bytes/&&/\/dev/{printf $2" ";printf "%d",$3;print " "$4}'`
USE_TOTAL=`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 -e "\e[1;31m ------------------------------------------------------------------------------------------------------------------------------------------------------\e[0m"
echo -e "Disk total:\n${DISK_TOTAL}"
if [ -f $DISK_LOG ];then
echo -e "\e[1;31m ------------------------------------------------------------------------------------------------------------------------------------------------\e[0m"
cat $DISK_LOG
echo -e "\e[1;31m ----------------------------------------------------------------------------------------------------------------------------------------------------\e[0m"
rm -f $DISK_LOG
else
echo -e "\e[1;31m --------------------------------------------------------------------------------------------------------------------------------------------------\e[0m"
echo "Disk use rate no than 90% of the partition."
echo -e "\e[1;31m --------------------------------------------------------------------------------------------------------------------------------------------------\e[0m"
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 "$PART = ${i}%" >> $INODE_LOG
fi
done
if [ -f $INODE_LOG ];then
echo -e "\e[1;31m ---------------------------------------------------------------------------------------------------------------------------------------------------\e[0m"
cat $INODE_LOG
rm -f $INODE_LOG
else
echo -e "\e[1;31m -------------------------------------------------------------------------------------------------------------------------------------------------\e[0m"
echo "Inode use rate no than 90% of the paritition."
echo -e "\e[1;31m -------------------------------------------------------------------------------------------------------------------------------------------------\e[0m"
fi
break
;;
mem_use)
#内存利用率
echo -e "\e[1;31m -------------------------------------------------------------------------------------------------------------------------------------------------\e[0m"
MEM_TOYAL=`free -m | awk '{if(NR==2)printf "%.lf",$2/1024}END{print "G"}'`
USE=`free -m | awk '{if(NR==2)printf "%.lf",$3/1024}END{print "G"}'`
FREE=`free -m | awk '{if(NR==2)printf "%.lf",$4/1024}END{print "G"}'`
CACHE=`free -m | awk '{if(NR==2)printf "%.lf",$6/1024}END{print "G"}'`
echo -e "Total: $MEM_TOYAL"
echo -e "Use: $USE"
echo -e "Free: $FREE"
echo -e "Cache: $CACHE"
echo -e "\e[1;31m -------------------------------------------------------------------------------------------------------------------------------------------------\e[0m"
break
;;
tcp_status)
#网络连接状态
echo -e "\e[1;31m ----------------------------------------------------------------------------------------------------------------------------------------------\e[0m"
COUNT=`ss -ant | awk '!/State/{status[$1]++}END{for(i in status) print i,status[i]}'`
echo -e "TCP connection status:\n$COUNT"
echo -e "\e[1;31m ----------------------------------------------------------------------------------------------------------------------------------------------\e[0m"
;;
cpu_top10)
#占用CPU高的前10个进程
echo -e "\e[1;31m ----------------------------------------------------------------------------------------------------------------------------------------------\e[0m"
CPU_LOG=/tmp/cpu_top.tmp
i=1
while [[ $i -le 1 ]];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 "\e[1;32m 参考值$[i]\e[0m"
cat $CPU_LOG
> $CPU_LOG
else
echo "No process using the CPU."
break
fi
let i++
sleep 1
done
echo -e "\e[1;31m -------------------------------------------------------------------------------------------------------------------------------------------\e[0m"
break
;;
mem_top10)
#占用内存高的前10个进程
echo -e "\e[1;31m ----------------------------------------------------------------------------------------------------------------------------------------------\e[0m"
MEM_LOG=/tmp/mem_top.tmp
i=1
while [[ $i -le 1 ]];do
ps aux |awk '{if($4>0.1){{printf "PID: "$2" Mwmory: "$4"% --> "}for(i=11;i<=NF;i++)if(i==NF)printf $i"\n";else printf $i}}' |sort -k4 -nr | head -10 >$MEM_LOG
#循环语句从11列(进程名)开始打印,如果i等于最后一行,就打印i的列并换行,否则就打印i的列
if [[ -n `cat $MEM_LOG` ]];then
echo -e "\e[1;32m 参考值$[i]\e[0m"
cat $MEM_LOG
> $MEM_LOG
else
echo "No process using the Memory."
break
fi
let i++
sleep 1
done
echo -e "\e[1;31m -------------------------------------------------------------------------------------------------------------------------------------------\e[0m"
break
;;
traffic) #查看网络流量
while true;do
read -p "Please enter the network card name(eth[0-9] or ens[33-39] or em[0-9]): " eth
if [ `ifconfig |grep -c "\<$eth\>"` -eq 1 ];then
break
else
echo "Input format error or Dont't have the card name, please input again."
fi
done
echo -e "\e[1;31m -------------------------------------------------------------------------------------------------------------------------------------------\e[0m"
echo -e " In <-------> Out"
i=1
while [[ $i -le 2 ]];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{printf "%.lf\n",'$((${NEW_IN}-${OLD_IN}))'/1024/128}'` #使用BEGIN开始计算带宽/1024/128得到M单位也就是1M=1024kb/8bit=123kB
OUT=`awk 'BEGIN{printf "%.lf\n",'$((${NEW_OUT}-${OLD_OUT}))'/1024/128}'`
echo "${IN}MB/s <---> ${OUT}MB/s"
i=$(($i+1))
sleep 1
done
echo -e "\e[1;31m --------------------------------------------------------------------------------------------------------------------------------------------\e[0m"
break
;;
quit)
exit 0
;;
*)
echo -e "\e[1;31m ------------------------------------------------------------------------------------------------------------------------------------\e[0m"
echo “:lease enter the number.”
echo -e "\e[1;31m ------------------------------------------------------------------------------------------------------------------------------------\e[0m"
break
;;
esac
done
done