python检测web服务是否正常,并调用金笛短信猫推送异常|python调用短信猫监控服务器|python调用钉钉机器人监控服务器
背景
业务部署在内网,项目运行环境异常时无法及时发送消息到外界。但项目拥有一个短信猫,可以发送短信,编写脚本定时监控业务运行情况,当运行异常时调用短信猫发送短信到指定手机号码。
代码
import requests import datetime import time import os def checkhttml(url,keyword): try: resp=requests.get(url,verify=False).text ##print(resp) if(keyword in resp): return 1 else: return 0 except: return 0 def ding_send(message,at): #如果项目有外网可以使用钉钉机器人 webhook='https://oapi.dingtalk.com/robot/send?access_token=d61xxxxx' data={ "at": { "atDingtalkIds":[ at ], "isAtAll": 'false' }, "text": { "content":"@"+at+" "+message }, "msgtype":"text" } print(data) print(requests.post(webhook,json=data).text) def sleepl(): print("wait 30 seconds for next check") time.sleep(30) def sendsms(phone,device,offlinedate): #本项目没有外网,使用命令行操作JDCheck发送短信 command="JDCheck.1.22.exe 5 "+str(phone)+" "+"\"[设备离线告警] "+offlinedate[:20]+"IP:"+str(device)+"\"" #JDCheck可以去短信猫官网下载 r=os.popen(command) print(r.read()) r.close() def main(): while(1): try: current_time = str(datetime.datetime.now()) deviceIp=['33.157.9.69'] for ip in deviceIp: if(checkhttml('http://'+str(ip),'body')): print(current_time+' '+ip+' Device Online') #ding_send(current_time+' 116.148.185.195:81 服务在线','') else: print(current_time+' '+ip+' Device Offline') sendsms(176xxxxxxxx,ip,current_time) sleepl() except: sleepl() main()