使用Shell腳本監控Linux主機

編寫如下腳本(根據實際情況來修改郵件發送目的地址):

[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      

————————————本文到此結束,感謝觀看——————————————

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章