python监控网页状态
最近公司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的支持和超时时间
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | #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) #邮件发送后半小时再后再次监控网页 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】