python 檢測nginx服務(wù)郵件報(bào)警的腳本
$ cat checkserver.py#!/usr/bin/python# -*- coding: utf-8 -*- import osimport socketimport smtplibfrom email.mime.text import MIMETextfrom email.header import Header mail_host = 'smtp.exmail.qq.com'mail_user = 'yunwei-monitor@111.com'mail_pass = 'yNE8dcsx' sender = ’yunwei-monitor@111.com’receivers = [’lixinliang@111.com’] def Checkserverdown(): #三個(gè)參數(shù):第一個(gè)為文本內(nèi)容,第二個(gè) plain 設(shè)置文本格式,第三個(gè) utf-8 設(shè)置編碼 message = MIMEText(’192.168.71.200 nginx is down’,’plain’,’utf-8’) message[’From’] = Header('Nginx is down ', ’utf-8’) # 發(fā)送者 message[’To’] = Header('李鑫亮', ’utf-8’) # 接收者 subject = ’192.168.71.200 nginx is down’ message[’Subject’] = Header(subject,’utf-8’) try:smtpobj = smtplib.SMTP()smtpobj.connect(mail_host,25)smtpobj.login(mail_user,mail_pass)smtpobj.sendmail(sender,receivers,message.as_string())print('郵件發(fā)送成功') except smtplib.SMTPException:print('Error: 無法發(fā)送郵件') def Checkserverstilldown(): #三個(gè)參數(shù):第一個(gè)為文本內(nèi)容,第二個(gè) plain 設(shè)置文本格式,第三個(gè) utf-8 設(shè)置編碼 message = MIMEText(’192.168.71.200 nginx is still down’,’plain’,’utf-8’) message[’From’] = Header('Nginx is still down ', ’utf-8’) # 發(fā)送者 message[’To’] = Header('李鑫亮', ’utf-8’) # 接收者 subject = ’192.168.71.200 nginx is still down’ message[’Subject’] = Header(subject,’utf-8’) try:smtpobj = smtplib.SMTP()smtpobj.connect(mail_host,25)smtpobj.login(mail_user,mail_pass)smtpobj.sendmail(sender,receivers,message.as_string())print('郵件發(fā)送成功') except smtplib.SMTPException:print('Error: 無法發(fā)送郵件') def Checkserverup(): #三個(gè)參數(shù):第一個(gè)為文本內(nèi)容,第二個(gè) plain 設(shè)置文本格式,第三個(gè) utf-8 設(shè)置編碼 message = MIMEText(’192.168.71.200 nginx is up’,’plain’,’utf-8’) message[’From’] = Header('Nginx is up ', ’utf-8’) # 發(fā)送者 message[’To’] = Header('李鑫亮', ’utf-8’) # 接收者 subject = ’192.168.71.200 nginx is up’ message[’Subject’] = Header(subject,’utf-8’) try:smtpobj = smtplib.SMTP()smtpobj.connect(mail_host,25)smtpobj.login(mail_user,mail_pass)smtpobj.sendmail(sender,receivers,message.as_string())print('郵件發(fā)送成功') except smtplib.SMTPException:print('Error: 無法發(fā)送郵件') # 判斷 nginx 進(jìn)程輸出內(nèi)容來確定是否要進(jìn)行進(jìn)程啟動file = '/tmp/nginx.txt'os.system('''ps -ef |grep nginx |grep -Ev 'grep|vim' > %s''' % file) print (os.path.getsize(file))if os.path.getsize(file) == 0:Checkserverdown()os.system('/usr/sbin/nginx')print (os.path.getsize(file))os.system('''ps -ef |grep nginx |grep -Ev 'grep|vim' > %s''' % file)if os.path.getsize(file) == 0:Checkserverstilldown()os.system('/usr/sbin/nginx')else:Checkserverup()
以上就是python 檢測nginx服務(wù)郵件報(bào)警的腳本的詳細(xì)內(nèi)容,更多關(guān)于python 檢測nginx服務(wù)郵件報(bào)警的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. Python獲取抖音關(guān)注列表封號賬號的實(shí)現(xiàn)代碼2. Warning: require(): open_basedir restriction in effect,目錄配置open_basedir報(bào)錯(cuò)問題分析3. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究4. 解決Python 進(jìn)程池Pool中一些坑5. php測試程序運(yùn)行速度和頁面執(zhí)行速度的代碼6. Python如何讀寫CSV文件7. 三個(gè)不常見的 HTML5 實(shí)用新特性簡介8. ajax請求添加自定義header參數(shù)代碼9. python利用os模塊編寫文件復(fù)制功能——copy()函數(shù)用法10. 無線標(biāo)記語言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁
