1 #!/usr/bin/env python 2 #coding=utf8 3 import time,os,sched,urllib,httplib 4 import smtplib 5 import string 6 7 schedule = 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 def timming_exe(self, inc = 60): 13 schedule.enter(inc, 0, perform_command, (self, inc)) 14 schedule.run() 15 16 def monitoring(self): 17 print("开始监控...") 18 httpClient = None 19 try: 20 params = urllib.urlencode({‘name‘: ‘tom‘, ‘age‘: 22}) 21 headers = {"Content-type": "application/x-www-form-urlencoded" 22 , "Accept": "text/plain"} 23 24 httpClient = httplib.HTTPConnection("2xx.x9.2x1.x", 8800, timeout=30) 25 httpClient.request("POST", "/path/pathxxxxxxx", params, headers) 26 27 response = httpClient.getresponse() 28 print (response.status) 29 print (response.reason) 30 #print response.read() 31 #print response.getheaders() #获取头信息 32 33 if response.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(): 46 FROM="xxx.com" 47 TO="xxx.com" 48 PASS="xxx" 49 HOST="smtp.sina.com" 50 PORT="25" 51 SUBJECT="Interface alarm " 52 TEXT="The alarm information !" 53 BODY= string.join(( 54 "From: %s" % FROM, 55 "To: %s" % TO, 56 "Subject: %s" % SUBJECT, 57 "", 58 TEXT 59 ), "\r\n") 60 server=smtplib.SMTP() 61 server.connect(HOST,"25") 62 server.login(FROM,PASS) 63 server.sendmail(FROM,TO,BODY) 64 server.quit() 65 66 print("服务监控>>> 一分钟后开始执行(每10秒):") 67 timming_exe("echo %time%", 10)
时间: 2024-10-13 12:02:04