Linux監控web服務並郵件提醒

2021-09-24 09:19:08 字數 3391 閱讀 2703

原文首發於:

本文出自

rebornchang的部落格

首先說下在基於linux作業系統的郵件傳送。

簡單的說使用mailx:

yum  install  mailx -y
命令列: mail -s "theme" emailaddress,回車後輸入內容按ctrl+d傳送郵件.

管道符: echo "mail main content" | mail -s "theme" emailaddress

檔案內容作為郵件內容: mail -s "theme" emailaddress < /tmp/t.txt

具體的關於sendemail的設定之類的這裡就不說了,可以參考下面兩個博文:

vim url
www.zhangruibin.com www.92cnb.com
#!/bin/bash

#監控web服務並郵件提醒

while true

domail="[email protected]"

failcount=0

retval=0

geturlstatus() >/dev/null 2>&1

#-t超時時間,--tries嘗試1次,--spider蜘蛛

[ $? -ne 0 ] && let failcount+=1; #訪問超時時,$?不等於0,則failcount加1

done

date=`date +%f" "%h:%m`

if [ $failcount -gt 1 ];then

retval=1

echo "check fail!!!,sendmail....."

## 使用mutt

#echo -e "date : $date\nproblem : $url is not running." | mutt -s "web server monitor" $mail

echo "date : $date \n problem :$url服務掛了" | mailx -v -s "服務監控提醒" $mail

else

retval=0

echo "date : $date $url is running."

fireturn $retval #如果返回值為0,就正常退出迴圈,不為0則繼續迴圈

}for url in `cat url | sed '/^#/d'`

do#geturlstatus $url && echo yes || echo no

geturlstatus $url

done

sleep 600 #死迴圈,設定每600s執行一次

done

nohup sh monitor.sh >>monitor.log &
ps -ef|grep monitor

結果:root 454 328 0 03:00 pts/2 00:00:00 grep --color=auto monitor

root 27535 1 0 jun10 ? 00:00:00 sh monitor.sh

cat monitor.log

結果:date : 2019-06-10 21:47 www.zhangruibin.com is running.

date : 2019-06-10 21:47 www.92cnb.com is running.

date : 2019-06-10 21:57 www.zhangruibin.com is running.

date : 2019-06-10 21:57 www.92cnb.com is running.

date : 2019-06-10 22:07 www.zhangruibin.com is running.

date : 2019-06-10 22:07 www.92cnb.com is running.

date : 2019-06-10 22:17 www.zhangruibin.com is running.

date : 2019-06-10 22:17 www.92cnb.com is running.

date : 2019-06-10 22:27 www.zhangruibin.com is running.

date : 2019-06-10 22:27 www.92cnb.com is running.

date : 2019-06-10 22:37 www.zhangruibin.com is running.

date : 2019-06-10 22:37 www.92cnb.com is running.

date : 2019-06-10 22:47 www.zhangruibin.com is running.

date : 2019-06-10 22:47 www.92cnb.com is running.

date : 2019-06-10 22:57 www.zhangruibin.com is running.

date : 2019-06-10 22:57 www.92cnb.com is running.

check fail!!!,sendmail.....

[email protected]... connecting to [127.0.0.1] via relay...

220 zhrb.localdomain esmtp postfix

>>> ehlo zhrb.localdomain

250-zhrb.localdomain

250-pipelining

250-size 10240000

250-vrfy

250-etrn

250-enhancedstatuscodes

250-8bitmime

250 dsn

>>> mail from:size=304

250 2.1.0 ok

>>> rcpt to:>>> data

250 2.1.5 ok

354 end data with .>>> .

250 2.0.0 ok: queued as 5c4001d6e

[email protected]... sent (ok: queued as 5c4001d6e)

closing connection to [127.0.0.1]

>>> quit

221 2.0.0 bye

並且郵箱中收到了郵件,驗證成功。

over!

Linux監控服務並主動重啟

linux查詢後台程序,如果沒有程序號,則重啟服務 bin sh basepath cd dirname 0 pwd while true doprocnum ps ef grep 服務名稱 grep v grep wc l if procnum eq 0 then 啟動命令 echo date y...

linux 郵件服務 mail

郵件服務小常識 centos預設安裝輕量級的sendmail,命令列發郵件較簡單快捷,如果企業應用還需要用postfix合適 rpm qa grep mail rpm qa grep sendmail yum y remove mail sendmail 總之把有關郵件的都刪掉重新安裝 yum y ...

web伺服器監控工具

監控你的web伺服器或者web主機執行是否正常與健康是非常重要的。你要確保使用者始終可以開啟你的 並且網速不慢。伺服器監控工具允許你收集和分析有關你的web伺服器的資料。performance co pilot,簡稱 pcp,是乙個系統效能和分析框架。它從多個主機整理資料並實時的分析,幫你識別不正常...