python服务器状态监控并发短信和邮件
简单的Python检测web服务状态的程序,根据返回的状态码来鉴别服务是否正常,并使用第三方发短信服务平台来发送短信,发送邮件及时通知管理人员.没学过Python,只看了点儿文档写出来的,代码写的不是很好,但能跑起来,凑合着用吧...
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 123 124 125 126 127 |
#!/usr/bin/env python # -*- coding: UTF-8 -*- #author libertyspy #link http://www.lastme.com import socket import smtplib import urllib mail_options = { 'server':'smtp.qq.com',#使用了QQ的SMTP服务,需要在邮箱中设置开启SMTP服务 'port':25, #端口 'user':'hacker@qq.com',#发送人 'pwd':'hacker', #发送人的密码 'send_to':'sniper@qq.com', #收件者 } msg_options={ 'user':'hacker', #短信平台的用户名 'pwd':'74110', #短信平台的密码 'phone':'12345678910', #需要发短信的电话号码 } test_host = 'http://www.lastme.com/' def url_request(host,port=80): try: response = urllib.urlopen(host) response_code = response.getcode() if 200 != response_code: return response_code else: return True except IOError,e: return False def send_message(msg,host,status): send_msg='服务器:%s挂了!状态码:%s' % (host,status) request_api="http://www.uoleem.com.cn/api/uoleemApi?username=%s&pwd=%s&mobile=%s&content=%s" \ % (msg['user'],msg['pwd'],msg['phone'],send_msg) return url_request(request_api) def send_email(mail,host,status): smtp = smtplib.SMTP() smtp.connect(mail['server'], mail['port']) smtp.login(mail['user'],mail['pwd']) msg="From:%s\rTo:%s\rSubject:服务器: %s 挂了 !状态码:%s\r\n" \ % (mail['user'],mail['send_to'],host,status) smtp.sendmail(mail['user'],mail['send_to'], msg) smtp.quit() """ def check_status(host,port=80): s = socket.socket() ret_msg = [] try: s.connect((host,port)) return True except socket.error,e: return False """ if __name__=='__main__': status = url_request(test_host) if status is not True and status is not None: send_email(mail_options,test_host,status) send_message(msg_options,test_host,status) else: pass |
No Comments