shell脚本监控linux,cpu,内存,磁盘资源

shell脚本如下:

#!/bin/bash
#CPU
cpu_idle=$(top -n1 | grep 'Cpu' | tail -n 1 | awk '{print $5}')
cpu_idle_num=`echo $cpu_idle | awk -F"%" '{print $1}'`
cpu_usage=$(echo "100 - $cpu_idle_num" | bc)

#Mem
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_rate=$(echo "$mem_used * 100 / $mem_total" | bc)


#disk
disk_usage=$(df -hP / | tail -n 1 | awk '{print $5}')
disk_used=$(df -hP / | tail -n 1 | awk '{print $3}')



echo "cpu利用率: $cpu_usage %"
echo "内存使用量: $mem_used M"
echo "内存使用率: $mem_rate %"
echo "磁盘空间使用量: $disk_used"
echo "磁盘空间利用率: $disk_usage"

 

执行效果:

 

posted @ 2020-12-16 10:19  紫电清霜  阅读(413)  评论(0编辑  收藏  举报