[root@dn3 data]# cat monitor.sh #!/bin/bash cpu_idle=$(top -n2|grep 'Cpu'|tail -n 1|awk '{print $8}') cpu_usage=$(echo "100 - $cpu_idle"|bc|awk '{printf "%.2f", $0}') echo "CPU利用率:$cpu_usage %" mem_free=$(free -m|awk '/Mem:/{print $4 + $6 + $7}') mem_total=$(free -m|awk '/Mem:/{print $2}') mem_used=$(echo "$mem_total-$mem_free"|bc) mem_used_rate=$(echo "$mem_used * 100 / $mem_total"|bc) echo "内存使用量:$mem_used M" echo "内存使用率:$mem_used_rate %" disk_used=$(df -h /|tail -n 1|awk '{print $3}') disk_use=$(df -h /|tail -n 1|awk '{print $5}') echo "磁盘空间使用量:$disk_used" echo "磁盘空间使用率:$disk_use"
执行脚本,可以得到系统的监控信息。
[root@dn3 data]# sh monitor.sh CPU利用率:0.70 % 内存使用量:1317 M 内存使用率:71 % 磁盘空间使用量:4.8G 磁盘空间使用率:6%
来源:博客园
作者:菩提花开
链接:https://www.cnblogs.com/putihuakai/p/11430257.html