Recently, the company's ERP server irregular, uninterrupted, occasional convulsions, often hung up a few hours after the other departments of the people call to say the server hung. So I wrote a simple web-monitoring with Python. The program mainly monitors the page status code, 200 is normal, otherwise as the server hangs. Every 70 seconds, if you find three consecutive queries reported errors, the pre-set mailbox sent warning messages. The settings page is monitored again 30 minutes after the message is sent.
#Coding:utf-8#AUTHOR:LJC#python verson 2.7.9ImportSmtplibImportUrllibImport Timedefsendmail (): Mail_to= Smtplib. SMTP ('smtp.126.com', 25)#set up a mail sending serverMail_to.login ("[email protected]","[Email protected]#")#set the account number to send the message, passwordmsg ="""From:system <[email protected]> to: <[email protected]>subject:webserver_downweb server was down"""Mail_to.sendmail ('[email protected]','[email protected]', msg) mail_to.close ()if __name__=='__main__':
Print Time.strftime ("%y-%m-%d%h:%m:%s", Time.localtime ()), ' Server monitor is running ' while1: Count=0 Error_status_count=0 whileCount<3: Time.sleep (90g#monitor servers every 70 seconds Try: Status=urllib.urlopen ("http://192.168.0.8"). Code#Collect page status codes for monitoring URLs ifstatus==200: PrintTime.strftime ("%y-%m-%d%h:%m:%s", Time.localtime ()),'Web server is functional' ifstatus<>200: Error_status_count+=1PrintTime.strftime ("%y-%m-%d%h:%m:%s", Time.localtime ()),'Web Servier is down, error status count:', Error_status_count,'Status number', Statusexcept: Error_status_count+=1#Page Status Error count cumulative PrintTime.strftime ("%y-%m-%d%h:%m:%s", Time.localtime ()),'Web Servier is down, error status count:', Error_status_count Count+=1ifError_status_count>=3:#Web page status error more than 3 automatically send alert messages Print 'Error status count is:', Error_status_count,'sending Email,the program Wiil try to monint the server after half an hour'SendMail () time.sleep (1800)#The message is sent half an hour after the page is again monitored
Python monitors page status