首页 > 系统服务 > 详细

使用Shell脚本监控Linux主机

时间:2020-05-24 19:49:03      阅读:56      评论:0      收藏:0      [点我收藏+]
编写如下脚本(根据实际情况来修改邮件发送目的地址):

[root@centos02 ~]# cat system.sh 
#!/bin/bash
dug=$(df -h | grep "/$" | awk ‘{print $5}‘ | awk -F% ‘{print $1}‘)
cug=$(expr 100 - $(mpstat | tail -1 | awk ‘{print $12}‘ | awk -F. ‘{print $1}‘))
mug=$(expr $(free | grep "Mem:" | awk ‘{print $3}‘) \* 100 / $(free | grep "Mem:" | awk ‘{print $2}‘))
alog="/tmp/alert.txt"
email="root"                   <!--邮件发送目的地址变量-->
if [ $dug -gt 90 ]
then
        echo " 磁盘占用率:$dug % " >> $alog
fi
if [ $cug -gt 80 ]
then
         echo " cpu使用率: $cug % " >> $alog
fi
if [ $mug -gt 90 ]
then
        echo " 内存使用率:$mug % " >> $alog
fi
if [ -f $alog ]
then
 cat $alog | mail -s "Host Alert" $email      <!--有关发送邮件的配置-->
        rm -rf $alog
rm -rf $alog
fi
[root@centos02 ~]# systemctl status crond  <!--查询crond服务是否为启动状态-->
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since 三 2020-05-20 01:20:56 CST; 57min ago
 Main PID: 829 (crond)
   CGroup: /system.slice/crond.service
           └─829 /usr/sbin/crond -n
         ...................  <!--此处省略部分内容-->

[root@centos02 ~]# crontab -e                  <!--创建新的任务计划-->
*/15          *      *      *           *       /root/system.sh          
[root@centos02 ~]# crontab -l              <!--查看任务计划-->
*/15          *      *      *           *       /root/system.sh      

————————————本文到此结束,感谢观看——————————————

使用Shell脚本监控Linux主机

原文:https://blog.51cto.com/14156658/2498080

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!