实用的shell 脚本监控 cpu_mem_disk.sh
#!/bin/bash
DATE=$(date +%F" "%H:%M)
IP=$(ifconfig eth0 |awk '/netmask/ {print $2}')
if ! which vmstat &> /dev/null; then
echo "vmstat command no found, Please install procps package."
exit 1
fi
## CPU
US=$(vmstat |awk 'NR==3 {print $13}')
SY=$(vmstat |awk 'NR==3 {print $14}')
IDLE=$(vmstat |awk 'NR==3 {print $15}')
WAIT=$(vmstat |awk 'NR==3 {print $15}')
USE=$(($US+$SY))
if [ $USE -ge 50 ];then
echo "
Date: $DATE
Host: $IP
Problem: CPU utilization $USE"
fi
## Mem
TOTAL=$(free -m |awk '/Mem/ {print $2}')
USE=$(free -m |awk '/Mem/ {print $3}')
FREE=$(free -m |awk '/Mem/ {print $4+$6}')
if [ $FREE -lt 1024 ]; then
echo "
Date: $DATE
Host: $IP
Problem: Total=$TOTAL,Use=$USE,Free=$FREE"
fi
#disk
PART_USE=$(df -h |awk -F'[% ]+' 'BEGIN{OFS="="} /^\/dev/ {print $1,$2,$5,$6}')
for i in $PART_USE; do
PART=$(echo $i |cut -d"=" -f1)
TOTAL=$(echo $i |cut -d "=" -f2)
USE=$(echo $i |cut -d"=" -f3)
MOUNT=$(echo $i |cut -d"=" -f4)
if [ $USE -gt 80 ]; then
echo "
Date: $DATE
Host: $IP
Total: $TOTAL
Problem: $PART=$USE($MOUNT)"
fi
done