Python monitors page status

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.