1,备份家目录
#!/bin/bash
BACKUP_DIR=/share/nas5/wanghj/data
TIMESTAMP=$(date +%Y%m%d%H%M%S)
tar -zcvf $BACKUP_DIR/backup_$TIMESTAMP.tar.gz /home/wanghj
2,监控系统内存,i/o及存储等
#!/bin/bash
# Set the threshold values
DISK_THRESHOLD=90
LOAD_THRESHOLD=20
MEMORY_THRESHOLD=50
IO_THRESHOLD=50
# User to write the message
USERNAME="wanghj"
# Log file
LOGFILE="/home/wanghj/logfile.log"
while true; do
# Check disk usage
DISK_USAGE=$(df /share/backup01/DATA/ | grep / | awk '{ print $5}' | sed 's/%//g')
if [ "$DISK_USAGE" -gt "$DISK_THRESHOLD" ] ; then
DISK_MESSAGE="Your root partition remaining free space is critically low. Used: $DISK_USAGE%"
echo "$(date) - $DISK_MESSAGE" >> $LOGFILE
echo "$DISK_MESSAGE" | write $USERNAME
else
DISK_MESSAGE="Your root partition disk usage is normal. Used: $DISK_USAGE%"
echo "$(date) - $DISK_MESSAGE" >> $LOGFILE
fi
# Check system load
LOAD=$(uptime | awk -F'load average: ' '{ print $2 }' | cut -d, -f1)
if (( $(echo "$LOAD > $LOAD_THRESHOLD" |bc -l) )); then
LOAD_MESSAGE="High load on server. Current load is: $LOAD"
echo "$(date) - $LOAD_MESSAGE" >> $LOGFILE
echo "$LOAD_MESSAGE" | write $USERNAME
else
LOAD_MESSAGE="Server load is normal. Current load is: $LOAD"
echo "$(date) - $LOAD_MESSAGE" >> $LOGFILE
fi
# Check memory usage
MEMORY_USAGE=$(free | grep Mem | awk '{print $3/$2 * 100.0}')
if (( $(echo "$MEMORY_USAGE > $MEMORY_THRESHOLD" |bc -l) )); then
MEMORY_MESSAGE="High memory usage on server. Current usage is: $MEMORY_USAGE%"
echo "$(date) - $MEMORY_MESSAGE" >> $LOGFILE
echo "$MEMORY_MESSAGE" | write $USERNAME
else
MEMORY_MESSAGE="Server memory usage is normal. Current usage is: $MEMORY_USAGE%"
echo "$(date) - $MEMORY_MESSAGE" >> $LOGFILE
fi
# Check IO usage
IO_USAGE=$(iostat -dx | awk 'NR==4 {print $14}')
if (( $(echo "$IO_USAGE > $IO_THRESHOLD" |bc -l) )); then
IO_MESSAGE="High I/O usage on server. Current usage is: $IO_USAGE%"
echo "$(date) - $IO_MESSAGE" >> $LOGFILE
echo "$IO_MESSAGE" | write $USERNAME
else
IO_MESSAGE="Server I/O usage is normal. Current usage is: $IO_USAGE%"
echo "$(date) - $IO_MESSAGE" >> $LOGFILE
fi
# Wait for 10 minutes
sleep 600
done
可以把该脚本用&挂在后台,该脚本可以让系统资源异常占用或磁盘快满的时候通知你
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!