url监控
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | #!/usr/bin/env python #coding:utf-8 import MySQLdb,requests import time from datetime import datetime global sendtime global flag global monitor_time number = 0 to_email = [] params = {} timedic = {} a = [] b = [] c = [] def sendmail(mail_list, message): url = "http://api.sendcloud.net/apiv2/mail/send" for number in range ( len (mail_list)): params[ 'apiUser' ] = "" params[ 'apiKey' ] = "" params[ 'from' ] = "" params[ 'fromName' ] = "bill" params[ 'subject' ] = "warning" params[ 'html' ] = "your html is abnormal: " + message params[ 'to' ] = mail_list[number] r = requests.post(url, files = {}, data = params) print r.text #get请求 def get_status(url): g = requests.get(url,allow_redirects = False ) return g.status_code,g.content #post请求 def post_status(url): p = requests.post(url,allow_redirects = False ) return p.status_code , p.content if __name__ = = '__main__' : while True : conn = MySQLdb.connect(host = '127.0.0.1' , user = 'root' , passwd = '734815' , db = 'url_monitor' ) cur = conn.cursor() # 查找所有的 reCount = cur.execute( 'select * from url_test;' ) # 获取所有的url及相关参数 all_urls = cur.fetchall() for item in all_urls: #设置初始值 if number = = 0 : timedic[item[ 0 ]] = 0 flag = 0 #监控间隔 monitor_time = int (item[ 5 ]) #判断是post还是get请求 if item[ 1 ] = = 'get' : current_status = get_status(item[ 0 ]) else : current_status = post_status(item[ 0 ]) to_email = item[ 3 ].split( ',' ) #print to_email a = [] b = [] c = [] #此时存在数据库与直接获取的页面都存在空白符,所以需要去掉,用函数去出现问题 #数据库 for n in range ( len (item[ 4 ])): if item[ 4 ][n] ! = '\r' : a.append(item[ 4 ][n]) for n in range ( len (a)): if a[n] ! = '\n' : c.append(a[n]) #直接获取 for n in range ( len (current_status[ 1 ])): if current_status[ 1 ][n] ! = '\n' : b.append(current_status[ 1 ][n]) #请求之后的状态跟预设的状态不一致,或者请求的页面与预设的页面不一致 if current_status[ 0 ] ! = int (item[ 2 ]) or c ! = b: #异常发送 if timedic[item[ 0 ]] = = 0 : # 标志着此时的状态是不正常的 flag = 1 sendtime = datetime.now() timedic[item[ 0 ]] = 1 message = item[ 0 ] + '\t' + str (current_status[ 0 ]) + ' is mismatch with preset! ' + 'The preset is ' + str (item[ 2 ]) + 'or the reponse is error' sendmail(to_email, message) #print message currenttime = datetime.now() if (currenttime - sendtime).seconds > = 3 : timedic[item[ 0 ]] = 0 #如果此时恢复正常 if current_status[ 0 ] = = int (item[ 2 ]) and c = = b: if flag = = 1 : message = item[ 0 ] + '\tChange from abnomaly to normal!' sendmail(to_email, message) flag = 0 #print message else : print item[ 0 ] + '\t is normal!' # 正常状态 number = 1 # 控制监控的间隔,有数据库的监控间隔决定 time.sleep(monitor_time) cur.close() conn.close() |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· DeepSeek火爆全网,官网宕机?本地部署一个随便玩「LLM探索」
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 上周热点回顾(1.20-1.26)
· 【译】.NET 升级助手现在支持升级到集中式包管理