Centos7システムサービス監視スクリプト、メール電子メール警告

免責事項:この記事はブロガーオリジナル記事ですが、許可ブロガーなく再生してはなりません。https://blog.csdn.net/tladagio/article/details/88786340

自動的に電子メール警告を送信するようにスケジュールされたタスクと組み合わせスクリプティング検出アプリケーションサービスのステータス、

システムバージョン

[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 

まず、オンデマンドサービスのテストのためにアプリケーションをインストールし、実験設備のhttp

[root@localhost ~]# yum install -y httpd

第二に、使用する必要があるスクリプトのステータスを確認するために注意を払って、サービスを開始

[root@localhost ~]# systemctl start httpd
[root@localhost ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Mon 2019-03-25 09:52:34 CST; 8s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 8922 (httpd)
   Status: "Processing requests..."
   CGroup: /system.slice/httpd.service
           ├─8922 /usr/sbin/httpd -DFOREGROUND
           ├─8923 /usr/sbin/httpd -DFOREGROUND
           ├─8924 /usr/sbin/httpd -DFOREGROUND
           ├─8925 /usr/sbin/httpd -DFOREGROUND
           ├─8926 /usr/sbin/httpd -DFOREGROUND
           └─8927 /usr/sbin/httpd -DFOREGROUND

Mar 25 09:52:34 localhost.localdomain systemd[1]: Starting The Apache HTTP Server...
Mar 25 09:52:34 localhost.localdomain httpd[8922]: AH00558: httpd: Could not reliably determine the server's fully qualified doma...essage
Mar 25 09:52:34 localhost.localdomain systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

ケースI:

まず、検出スクリプトを書きます

[root@localhost ~]# vim httpd.sh
#!/bin/bash
#this is httpd service script
name1="running"
name2="dead"


if systemctl status httpd | grep $name1

then

    echo "system httpd service is start!" | mail -s "Service check OK" 邮箱@qq.com

elif
    systemctl status httpd | grep $name2

then

    echo "system http service is stop!" | mail -s "Service check Warring" 邮箱@qq.com
fi

実行権限を追加します。

[root@localhost ~]# chmod +x httpd.sh 
[root@localhost ~]# ll
-rwxr-xr-x. 1 root root  362 Mar 25 09:50 httpd.sh

第二に、スケジュールされたタスクを追加し、一回2分ごとにテストしました

[root@localhost ~]# crontab -e
*/2 * * * * /root/httpd.sh

通常の第三に、検出

第四に、手動停止httpdが、アラームを表示します

ケースII:

まず、スクリプトを追加し、HTTPと同じ名前を取らないようにしようと、あるいはPSテストの結果を正確にしないでください

[root@localhost ~]# vim test.sh 
#!/bin/bash

#记录日志文件
LOG_FILE="autostart.log"

#系统时间
curtime=$(date "+%Y-%m-%d %H:%M:%S")

#检测进程
pnhttp=`ps -ef | grep http | grep -v "grep" | wc -l`


if [ $pnhttp -eq 0 ]; then
        echo "$curtime 系统检测到http,已挂掉" >> autostart.log;
        echo "system httpd is down,尝试自动恢复" | mail -s "Service check http" 邮箱@qq.com;

#尝试启动httpd
	    systemctl start httpd;
else
        echo "$curtime 系统检测到http运行正常" >> autostart.log;
	echo "system httpd is up" | mail -s "Service check http" 邮箱@qq.com;
fi

実行権限を追加します。

[root@localhost ~]# chmod +x test.sh 

必要に応じて第二に、調整する時間をスケジュールされたタスクを追加

[root@localhost ~]# crontab -e
*/2 * * * * /root/test.sh

通常の第三に、検出

第四に、手動停止httpdが、アラームを表示します

自動回復の成功

 

 

おすすめ

転載: blog.csdn.net/tladagio/article/details/88786340