查看系统运行情况脚本:
#!/bin/bash#用于查看系统运行情况#功能选择菜单menu(){ echo -e "\033[31;32m 查看系统运行情况 \033[0m" echo -e "\033[31;32m================================================================================\033[0m" echo -e "\033[34m请选择:\033[0m" echo -e "\033[33m1、查看系统负载 2、查看CPU消耗% 3、查看内存消耗% 4、查看SWAP消耗% \033[0m" echo -e "\033[33m5、查看磁盘消耗% 6、查看inode消耗% 7、查看磁盘IO 8、查看网络流量 \033[0m" echo -e "\033[33m9、一键查看所有情况 10、退出脚本 \033[0m" echo -e "\033[31;32m================================================================================\033[0m" echo read -p "请输入数字:1-8[单独查看],9[一键查看],10[退出脚本]: " num}#(1)查看系统负载load_average(){ cpu_core=`grep 'model name' /proc/cpuinfo | wc -l` echo -e "\033[36mCPU核数: $cpu_core\033[0m" load=`uptime |awk -F ',' '{print $3 $4 $5}' |sed 's/^ *//'` echo -e "\033[36m$load\033[0m" echo ""}#(2)查看CPU消耗%cpu_use_percent(){ cpu_idle=`vmstat |awk '{print $15}' |sed '1,2d'` cpu_use=$[100-$cpu_idle] echo -e "\033[36mCPU使用率%: $cpu_use\033[0m" echo ""}#(3)查看内存消耗%mem_use_percent(){ mem_used=`free -m |grep Mem |awk '{print $3}'` mem_tol=`free -m |grep Mem |awk '{print $2}'` mem_use=`awk 'BEGIN{print '$mem_used'/'$mem_tol'*100}'` echo -e "\033[36m内存已使用"$mem_used"M,总内存"$mem_tol"M,内存使用率%: $mem_use\033[0m" echo ""}#(4)查看SWAP消耗%swap_use_percent(){ swap_used=`free -m |grep Swap |awk '{print $3}'` swap_tol=`free -m |grep Swap |awk '{print $2}'` swap_use=`awk 'BEGIN{print '$swap_used'/'$swap_tol'*100}'` echo -e "\033[36mSwap已使用"$swap_used"M,总Swap"$swap_tol"M,Swap使用率%: $swap_use\033[0m" echo ""}#(5)查看磁盘消耗%disk_use_percent(){ disk_use=`df -h |sed '1d'` echo -e "\033[36m磁盘使用情况: \n$disk_use\033[0m" echo ""}#(6)查看inode消耗%inode_use_percent(){ inode_use=`df -i |sed '1d'` echo -e "\033[36minode使用情况: \n$inode_use\033[0m" echo ""}#(7)查看磁盘IOdisk_io(){ disk_io_bi=`vmstat |awk '{print $9}' |sed '1,2d'` echo -e "\033[36m发送到块设备的块数: "$disk_io_bi"块每秒\033[0m" disk_io_bo=`vmstat |awk '{print $10}' |sed '1,2d'` echo -e "\033[36m从块设备接收到的块数: "$disk_io_bo"块每秒\033[0m" echo ""}#(8)查看网络流量network_flow(){ #安装所需命令sysstat if ! rpm -q sysstat > /dev/null then yum install -y sysstat &>/dev/null if [ $? -ne 0 ] then echo -e "\033[31msysstat 安装失败\033[0m" exit 1 fi fi explain=`sar -n DEV |sed -n '3p'` echo -e "\033[35m网络流量使用情况: \n$explain\033[0m" network_ifs=`ifconfig |grep "" |awk -F ':' '{print $1}'` network_flow=`sar -n DEV |grep -v Average |grep $network_ifs |tail` echo -e "\033[36m$network_flow\033[0m"}#脚本运行入口run(){ while true;do menu case $num in "1") #1、查看系统负载 load_average ;; "2") #2、查看CPU消耗% cpu_use_percent ;; "3") #3、查看内存消耗% mem_use_percent ;; "4") #4、查看SWAP消耗% swap_use_percent ;; "5") #5、查看磁盘消耗% disk_use_percent ;; "6") #6、查看inode消耗% inode_use_percent ;; "7") #7、查看磁盘IO disk_io ;; "8") #8、查看网络流量 network_flow ;; "9") #9、一键查看所有情况 load_average cpu_use_percent mem_use_percent swap_use_percent disk_use_percent inode_use_percent disk_io network_flow exit 0 ;; "10") #10、退出脚本 exit 0 ;; *) ;; esac done}#调用脚本运行入口run