监听磁盘占有率,用户登录数,内存使用率
#!/bin/bash
# 监听磁盘占有率
listen_path='/boot'
# 监听的比例
ratio=1
if [ `df | grep $listen_path | awk '{print $5}' | awk -F'%' '{print $1}'` -ge $ratio ];then
# 将磁盘使用率写入日志
echo -e " `df -h` \n Warning! system disk($listen_path) use to over 80%,please hande! \n $(date +%F) Disk_Use_Ratio>>> \n ===========$(date +%F) Disk Log=============" >> /logs/disk.log
echo "$(date +%F) Warning DIsk!!! system disk($listen_path) use to over 80%,please hande!" | mail -s "Warning DIsk" root #root 可以换成邮箱号,memory checked 可以换成标题
else
echo "good"
fi
#!/bin/bash
#############################################
# 监听用户登录数
# 用户登录数
user=0
if [ `who | wc -l` -ge $user ];then
#将警告写入日志,同时发送邮件给管理员
# TODO 此处可以写一个邮件发送脚本,或者引入邮件发送
echo "$(date +%F) Warning Concurrent!!! system users login over $user ,please hande" >> /logs/disk.log
echo "$(date +%F) Warning Concurrent!!! system users login over $user ,please hande" | mail -s "Warning Concurrent" root #root 可以换成邮箱号,memory checked 可以换成标题
else
echo "users login is good,OK!"
fi
#!/bin/bash
###############################################
# 监听内存使用率
total=$(free -m | awk 'NR==2{print $2}')
used=$(free -m | awk 'NR==2{print $3}')
syl=$[$used*100/$total]
use_ratio=1
if [ $syl -gt $use_ratio ];then
#将警告写入日志,同时发送邮件给管理员
# TODO 此处可以写一个邮件发送脚本,或者引入邮件发送
echo "$(date +%F) Warning Memory!!! memory is used 80%,please close some not_use software" >> /logs/menory.log #将警告写入日志文件
echo "$(date +%F) Warning Memory!!! memory is used 80%,please close some not_use software" | mail -s "Warning Memory" root #root 可以换成邮箱号,memory checked 可以换成标题
else
echo "$(date +%F) menory is used Good" | mail -s "memory is OK" root
fi
# 定期清理日志(单独写一个定时任务,默认定期清理15天以前的日志)
find /logs/ -mtime +15 -name "*.log" -exec rm -rf {} \;
echo ""> /var/spool/mail/root