- 1 #!/usr/bin/env python
- 2 #coding=utf8
- 3 import time,os,sched,urllib,httplib
- 4 import smtplib
- 5 import string
- 6
- 7schedule = sched.scheduler(time.time, time.sleep)
- 8 def perform_command(self, inc):
- 9 schedule.enter(inc, 0, perform_command, (self, inc))
- 10 #os.system(cmd)
- 11 monitoring(self)
- 12 deftimming_exe(self, inc = 60):
- 13 schedule.enter(inc, 0, perform_command, (self, inc))
- 14 schedule.run()
- 15
- 16 def monitoring(self):
- 17 print("开始监控...")
- 18httpClient = None
- 19 try:
- 20params = urllib.urlencode({'name':'tom','age': 22})
- 21headers = {"Content-type":"application/x-www-form-urlencoded"
- 22,"Accept":"text/plain"}
- 23
- 24httpClient = httplib.HTTPConnection("2xx.x9.2x1.x", 8800, timeout=30)
- 25httpClient.request("POST","/path/pathxxxxxxx", params, headers)
- 26
- 27response = httpClient.getresponse()
- 28 print (response.status)
- 29 print (response.reason)
- 30 #print response.read()
- 31 #print response.getheaders() #获取头信息
- 32
- 33 ifresponse.status == 200:
- 34 print(u"正常")
- 35 else:
- 36 print(u"异常")
- 37 sendmsg
- 38 print '邮件已发送....'
- 39 except Exception,e:
- 40 print e
- 41 finally:
- 42 if httpClient:
- 43 httpClient.close()
- 44
- 45 def sendmsg():
- 46FROM="xxx.com"
- 47TO="xxx.com"
- 48PASS="xxx"
- 49HOST="smtp.sina.com"
- 50PORT="25"
- 51SUBJECT="Interface alarm "
- 52TEXT="The alarm information !"
- 53BODY= string.join((
- 54 "From: %s"% FROM,
- 55 "To: %s"% TO,
- 56 "Subject: %s"% SUBJECT,
- 57 "",
- 58 TEXT
- 59),"\r\n")
- 60server=smtplib.SMTP()
- 61server.connect(HOST,"25")
- 62 server.login(FROM,PASS)
- 63 server.sendmail(FROM,TO,BODY)
- 64 server.quit()
- 65
- 66 print("服务监控>>> 一分钟后开始执行(每10秒):")
- 67timming_exe("echo %time%", 10)
来源: http://www.bubuko.com/infodetail-2071023.html