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:
650) this. width = 650; "src =" http://img1.51cto.com/attachment/201306/132120320.png "title =" QQ20130605132048.png "/>
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 from smtplib import SMTPfrom email im Port MIMETextfrom email import Headerfrom datetime import datetimeimport httplib # defines the server to be detected. The URL port Number 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 log file location error_log ='/tmp/web_server_status.txt 'def send_mail (context ): '''send mail ''' # defines the header information of the mail = Header. header msg = 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 () def Get_now_date_time (): ''' get the current date ''' now = datetime. now () return str (now. year) + "-" + str (now. month) + "-" \ + str (now. day) + "" + str (now. hour) + ":" \ + str (now. minute) + ":" + str (now. second) def check_webserver (host, port, resource): ''' checks the WEB server status ''' if not resource. startswith ('/'): resource = '/' + resource try: connection = httplib. HTTPConnection (host, port) connection. request ('get', r Esource) response = connection. getresponse () status = response. status content_length = response. length limit T: return False finally: connection. close () if status in [200,301] and content_length! = 0: return True else: return Falseif _ name _ = '_ main _': logfile = open (error_log, 'A ') problem_server_list = [] for host in web_servers: host_url = host [0] check = check_webserver (host_url, host [1], host [2]) if not check: temp_string = 'the Server [% s] may appear problem at % s \ n' % (host_url, get_now_date_time () print> logfile, temp_string problem_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 mail if problem_server_list: send_mail (''. join (problem_server_list ))
This article is from the blog of reelcos, please be sure to keep this source http://linux5588.blog.51cto.com/65280/1216417