自动化监控CPU、MEM、disk、tcp
#!/bin/ bash
#query
echo " --------------------------------- - - "
echo "welcome to the dray League"
echo " ----------------------------------
#检查是否安装iostat命令
which iostat &> dev/null
if [ $? -ne 0 ] ; then
echo " not yet install,trying automatic installation"
#若没有安装将自动安装
yum -y install sysstat &> /dev/null
wait
echo " iostat install successful"
fi
#打印菜单:
PS3="please select: "
select choose in cpu mem disk tcp quit
do
case "$choose" in
#查看CPU的使用情况
cpu)
i=1
echo "*****************************"
while [ $i -le 3 ];do
timer=$(uptime | awk '{print $1,$2,$3}')
user=$(uptime | awk '{print $4,$5}')
load=$(uptime | awk '{print $8,$9,$10}')
echo "time:$timer"
echo "user:$user"
echo "load:$load"
echo "****************************"
let i++
sleep 1
done
;;
#查看内存的使用情况
mem)
echo"********************************"
tolal=$(free -h | sed -n "2p" | awk '{print $2}')
user=$(free -h | sed -n "2p" | awk '(print $3}')
free=$(free -h | sed -n "2p" | awk '{print $7}')
echo "tolal: $tolal"
echo "user: $user"
echo "free: $free"
echo"*******************************"
;;
#查看硬盘的使用情况
disk)
echo"********************************"
[ $(df -hT | awk 'NR==2{print $6}' | awk -F '%' '{print $1}') -gt 90 ] && echo "warning, exceed 90%!" || echo "No"
echo "********************************"
;;
#查看tcp的使用情况
tcp)
echo "*******************************"
tcp=$(iostat | sed -n "7,18p" | awk '{print $1,":"$2}')
echo "tcp: $tcp"
echo“*********************************“
;;
#当用户输入exit时退出程序
quit)
exit 1
;;
*)
echo "input error,please input again ! "
esac
done
- 测试:
(1)CPU:
(2)内存:
(3)硬盘:
(4)tcp: