一个简单的python开发的监控程序,当指定网页状态不正常是通过smtp发送通知邮件
转自:http://www.lastme.com/
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 |
<span class="hljs-comment">#!/usr/bin/env python</span> <span class="hljs-comment"># -*- coding: UTF-8 -*-</span> <span class="hljs-comment">#author libertyspy</span> <span class="hljs-comment">#link http://www.lastme.com</span> <span class="hljs-keyword">import</span> socket <span class="hljs-keyword">import</span> smtplib <span class="hljs-keyword">import</span> urllib mail_options = { <span class="hljs-string">'server'</span>:<span class="hljs-string">'smtp.qq.com'</span>,<span class="hljs-comment">#使用了QQ的SMTP服务,需要在邮箱中设置开启SMTP服务</span> <span class="hljs-string">'port'</span>:<span class="hljs-number">25</span>, <span class="hljs-comment">#端口</span> <span class="hljs-string">'user'</span>:<span class="hljs-string">'hacker@qq.com'</span>,<span class="hljs-comment">#发送人</span> <span class="hljs-string">'pwd'</span>:<span class="hljs-string">'hacker'</span>, <span class="hljs-comment">#发送人的密码</span> <span class="hljs-string">'send_to'</span>:<span class="hljs-string">'sniper@qq.com'</span>, <span class="hljs-comment">#收件者</span> } msg_options={ <span class="hljs-string">'user'</span>:<span class="hljs-string">'hacker'</span>, <span class="hljs-comment">#短信平台的用户名</span> <span class="hljs-string">'pwd'</span>:<span class="hljs-string">'74110'</span>, <span class="hljs-comment">#短信平台的密码</span> <span class="hljs-string">'phone'</span>:<span class="hljs-string">'12345678910'</span>, <span class="hljs-comment">#需要发短信的电话号码</span> } test_host = <span class="hljs-string">'http://www.lastme.com/'</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">url_request</span><span class="hljs-params">(host,port=<span class="hljs-number">80</span>)</span>:</span> <span class="hljs-keyword">try</span>: response = urllib.urlopen(host) response_code = response.getcode() <span class="hljs-keyword">if</span> <span class="hljs-number">200</span> != response_code: <span class="hljs-keyword">return</span> response_code <span class="hljs-keyword">else</span>: <span class="hljs-keyword">return</span> <span class="hljs-keyword">True</span> <span class="hljs-keyword">except</span> IOError,e: <span class="hljs-keyword">return</span> <span class="hljs-keyword">False</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">send_message</span><span class="hljs-params">(msg,host,status)</span>:</span> send_msg=<span class="hljs-string">'服务器:%s挂了!状态码:%s'</span> % (host,status) request_api=<span class="hljs-string">"http://www.uoleem.com.cn/api/uoleemApi?username=%s&pwd=%s&mobile=%s&content=%s"</span> \ % (msg[<span class="hljs-string">'user'</span>],msg[<span class="hljs-string">'pwd'</span>],msg[<span class="hljs-string">'phone'</span>],send_msg) <span class="hljs-keyword">return</span> url_request(request_api) <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">send_email</span><span class="hljs-params">(mail,host,status)</span>:</span> smtp = smtplib.SMTP() smtp.connect(mail[<span class="hljs-string">'server'</span>], mail[<span class="hljs-string">'port'</span>]) smtp.login(mail[<span class="hljs-string">'user'</span>],mail[<span class="hljs-string">'pwd'</span>]) msg=<span class="hljs-string">"From:%s\rTo:%s\rSubject:服务器: %s 挂了 !状态码:%s\r\n"</span> \ % (mail[<span class="hljs-string">'user'</span>],mail[<span class="hljs-string">'send_to'</span>],host,status) smtp.sendmail(mail[<span class="hljs-string">'user'</span>],mail[<span class="hljs-string">'send_to'</span>], msg) smtp.quit() <span class="hljs-string">""" def check_status(host,port=80): s = socket.socket() ret_msg = [] try: s.connect((host,port)) return True except socket.error,e: return False """</span> <span class="hljs-keyword">if</span> __name__==<span class="hljs-string">'__main__'</span>: status = url_request(test_host) <span class="hljs-keyword">if</span> status <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">True</span> <span class="hljs-keyword">and</span> status <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">None</span>: send_email(mail_options,test_host,status) send_message(msg_options,test_host,status) <span class="hljs-keyword">else</span>: <span class="hljs-keyword">pass</span> |
