最近公司erp服務器無規律、不間斷、時不時抽風,往往都是掛了快個把小時后其它部門的人才打電話過來說服務器掛了。於是用python寫了一個簡單的網頁監控。程序主要監控網頁狀態碼,200為正常,否則視為服務器掛了。每隔70秒查詢一次,若發現三次連續的查詢中都報錯誤,則通過預先設定的郵箱發送警告郵件。郵件發送后隔30分鍾再次監控設定網頁。
verson 1
直接將日志直接通過屏幕輸出
#coding:utf-8 #author:ID404 #python verson 2.7.9 import smtplib import urllib import time def sendmail(): mail_to = smtplib.SMTP('smtp.126.com',25) #設置郵件發送服務器 mail_to.login("send_mail@126.com","123456") #設置發送郵件的帳號,密碼 msg = """From: system <send_mail@126.com> To: <receive_mail@126.com> Subject: webserver_down web server is down """ mail_to.sendmail('send_mail@126.com','receive_mail@126.com',msg) mail_to.close() if __name__ == '__main__':
print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),'server monitor is running' while 1: count=0 error_status_count=0 while count<3: time.sleep(70) #每隔70秒監控一次服務器 try: status=urllib.urlopen("http://192.168.0.8").code #收集監控網址的網頁狀態碼 if status==200: print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),'web server is functional' if status<>200: error_status_count+=1 print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),'web servier is down ,error status count:',error_status_count,'status number',status except: error_status_count+=1 #網頁狀態錯誤次數進行累加 print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),'web servier is down ,error status count:',error_status_count count+=1 if error_status_count>=3: #網頁狀態錯誤超過3次自動發送報警郵件 print 'error status count is :',error_status_count,'sending email,the program wiil try to monint the server after half an hour' sendmail() time.sleep(1800) #郵件發送后半小時再后再次監控網頁
verson 2
日志將在同目錄下生成logs.txt
#coding:utf-8 #author:ID404 #python verson 2.7.9 #程序主要監控網頁狀態碼,200為正常,否則視為服務器掛了。每隔70秒查詢一次,若發現三次連續的查詢中都報錯誤,則通過預先設定的郵箱發送警告郵件。郵件發送后隔30分鍾再次監控設定網頁。 import smtplib import urllib from datetime import * import time
from tkMessageBox import *
def sendmail():
send_mail="send_system@126.com"
send_mail_passwd="123456"
receive_mail='rec@126.com'
send_mail_server='smtp.126.com'
mail_to = smtplib.SMTP(send_mail_server,25)
mail_to.login(send_mail,send_mail_passwd)
msg ="From: send_system <"+send_mail+""">
To: <"""+receive_mail+""">
Subject: web server is down
web server is down
"""
mail_to.sendmail(send_mail,receive_mail,msg)
mail_to.close()
if __name__ == '__main__': logs=open('./logs.txt','a+') logs.writelines(['\n',str(datetime.now()),' the program is running']) logs.close() while 1: count=0 error_status_count=0 while count<3: time.sleep(70) #每隔70秒監控一次服務器 try: status=urllib.urlopen("http://192.168.0.8").code #收集監控網址的網頁狀態碼 if status==200: logs=open('./logs.txt','a+') logs.writelines(['\n',str(datetime.now()),' web server is functional']) logs.close() if status<>200: error_status_count+=1 logs=open('./logs.txt','a+') logs.writelines(['\n',str(datetime.now()),' web servier is down ,error status count:',str(error_status_count),' status number:',str(status)]) logs.close() except: error_status_count+=1 #網頁狀態錯誤次數進行累加 logs=open('./logs.txt','a+') logs.writelines(['\n',str(datetime.now()),' web servier is down ,error status count:',str(error_status_count)]) logs.close() count+=1 if error_status_count>=3: #網頁狀態錯誤超過3次自動發送報警郵件 logs=open('./logs.txt','a+') logs.writelines(['\n','error status count is :',str(error_status_count),' sending email,the program wiil try to monint the server after half an hour']) logs.close()
showwarning('attention',['tzx webserver is down!',str(datetime.now())]) sendmail() time.sleep(1800) #郵件發送后半小時再后再次監控網頁
vsersion 3 增加對https的支持和超時時間
#coding:utf-8 #author:ID404 #python verson 2.7.9 #程序主要監控網頁狀態碼,200為正常,否則視為服務器掛了。每隔70秒查詢一次,若發現三次連續的查詢中都報錯誤,則通過預先設定的郵箱發送警告郵件。郵件發送后隔30分鍾再次監控設定網頁。 import smtplib import urllib2 from datetime import * import time from tkMessageBox import * import ssl def sendmail(): send_mail="send_system@126.com" send_mail_passwd="123456" receive_mail='rec@126.com' send_mail_server='smtp.126.com' mail_to = smtplib.SMTP(send_mail_server,25) mail_to.login(send_mail,send_mail_passwd) msg ="From: send_system <"+send_mail+"""> To: <"""+receive_mail+"""> Subject: web server is down web server is down """ mail_to.sendmail(send_mail,receive_mail,msg) mail_to.close() if __name__ == '__main__': logs=open('./logs.txt','a+') logs.writelines(['\n',str(datetime.now()),' the program is running']) logs.close() while 1: count=0 error_status_count=0 while count<3: time.sleep(70) #每隔70秒監控一次服務器 context = ssl._create_unverified_context() try: status=urllib2.urlopen("https://192.168.0.8",timeout=5,context=context).code #收集監控網址的網頁狀態碼 if status==200: logs=open('./logs.txt','a+') logs.writelines(['\n',str(datetime.now()),' web server is functional']) logs.close() if status<>200: error_status_count+=1 logs=open('./logs.txt','a+') logs.writelines(['\n',str(datetime.now()),' web servier is down ,error status count:',str(error_status_count),' status number:',str(status)]) logs.close() except: error_status_count+=1 #網頁狀態錯誤次數進行累加 logs=open('./logs.txt','a+') logs.writelines(['\n',str(datetime.now()),' web servier is down ,error status count:',str(error_status_count)]) logs.close() count+=1 if error_status_count>=3: #網頁狀態錯誤超過3次自動發送報警郵件 logs=open('./logs.txt','a+') logs.writelines(['\n','error status count is :',str(error_status_count),' sending email,the program wiil try to monint the server after half an hour']) logs.close() showwarning('attention',['tzx webserver is down!',str(datetime.now())]) sendmail() time.sleep(1800) #郵件發送后半小時再后再次監控網頁