使用shell脚本监控Linux主机
作者:互联网
编写如下脚本(根据实际情况来修改邮件发送目的地址):
[root@localhost ~]# cat sysmon.sh
#!/bin/bash
dug=(df−h∣grep"/" | awk ‘{print $5}’ | awk -F% '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 1}̲')
cug=(expr 100 - $(mpstat | tail -1 | awk ‘{print $12}’ | awk -F. '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 1}̲'))
mug=(expr $(free | grep “Mem:” | awk ‘{print $3}’) * 100 / $(free | grep “Mem:” | awk ‘{print $2}’))
alog="/tmp/alert.txt"
email=“root” #邮件发送目的地址变量
if [ dug−gt90]thenecho"磁盘占用率:dug % " >> $alog
fi
if [ $cug -gt 80 ]
then
echo " cpu使用率: $cug % " >> $alog
fi
if [ mug−gt90]thenecho"内存使用率:mug % " >> $alog
fi
if [ -f $alog ]
then
cat $alog | mail -s “Host Alert” $email #有关发送邮件的配置
rm -rf $alog
rm -rf $alog
fi
[root@localhost ~]# systemctl status crond #查询crond服务是否为启动状态
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: active (running) since 四 2019-05-23 05:03:09 CST; 1h 24min ago
Main PID: 1339 (crond)
Tasks: 1
CGroup: /system.slice/crond.service
└─1339 /usr/sbin/crond -n
…
[root@localhost ~]# crontab -e #创建新的任务计划
*/30 * * * * /root/sysmon.sh
[root@localhost ~]# crontab -l #查看任务计划
*/30 * * * * /root/sysmon.sh
标签:shell,alog,quot,主机,awk,Linux,print,root,crond 来源: https://blog.csdn.net/weixin_44907813/article/details/98848882