WEB Server inspection script phthon script

Source: Internet
Author: User

Script Function:

Monitors the status of multiple Web servers and sends emails if any problem occurs.


Running environment:

Python2.7/2.4 can be run


Script usage:

You can use Crontab or scheduled task to run the task at a specified time. For example:

*/10 * script path


The script runs as follows:


The script content is as follows:

#! /Usr/bin/env python # coding = UTF-8 # -------------------------------------------------------- # Name: WEB Server inspection script # Purpose: monitors the status of multiple Web servers and sends an email if any problem occurs: 1.0 # Author: LEO # BLOG: http://linux5588.blog.51cto.com # EMAIL: chanyipiaomiao@163.com # Created: 2013-06-04 # Copyright: (c) LEO 2013 # Python: 2.4/2.7 # export fromsmtplib importSMTPfromemail impor TMIMETextfromemail importHeaderfromdatetime importdatetimeimporthttplib # defines the server to be detected. The URL port number indicates the Resource Name web_servers = [('123. 168.1.254 ', 80, 'index.html'), ('www .xxx.com ', 80, 'index.html'), ('2017. 114.114.114 ', 9000,'/main/login.html '),] # define host account password recipient email subject smtpserver = 'smtp .163.com 'sender = 'xxxx @ xxx.com' password = 'Password' receiver = ('recipient 1', 'recipient 2 ') subject = u'web server alert mail' From = u'web Server' To = u'server Postmaster '# define the location of the log file err Or_log = '/tmp/web_server_status.txt' defsend _ mail (context): ''' send mail ''' # define the mail header information Header = header. headermsg = MIMEText. MIMEText (context, 'plain ', 'utf-8') msg ['from'] = header (From) msg ['to'] = header () msg ['subobject'] = header (Subject + '\ n') # connect to the SMTP server and send the smtp = SMTP (smtpserver) smtp message. login (sender, password) smtp. sendmail (sender, receiver, msg. as_string () smtp. close () defget_now_date_time (): ''' get current date ''' now = dat Etime. now () returnstr (now. year) + "-" + str (now. month) + "-" \ + str (now. day) + "" + str (now. hour) + ":" \ + str (now. minute) + ":" + str (now. second) defcheck_webserver (host, port, resource): ''' checks the WEB server status ''' ifnotresource. startswith ('/'): resource = '/' + resourcetry: try: connection = httplib. HTTPConnection (host, port) connection. request ('get', resource) response = connection. getresponse () status = response. statuscontent_length = Response. lengthexcept: returnFalsefinally: connection. close () ifstatus in [200,301] andcontent_length! = 0: returnTrueelse: returnFalseif _ name _ = '_ main _': logfile = open (error_log, 'A') problem_server_list = [] forhost inweb_servers: host_url = host [0] check = check_webserver (host_url, host [1], host [2]) ifnotcheck: temp_string = 'the Server [% s] may appear problem at % s \ n' % (host_url, get_now_date_time () print> logfile, temp_stringproblem_server_list.append (temp_string) logfile. close () # If problem_server_list is not empty, it indicates that there is a problem with the server. Then, send the ifproblem_server_list: send_mail (''. join (problem_server_list ))


From the blog of reelcos, please be sure to keep this source http://linux5588.blog.51cto.com/65280/1216417


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.