Python Implements batch monitoring website details and examples

Source: Internet
Author: User
This article to share is a very practical, python implementation of multi-site monitoring of the usability of the script, and attached to the core point of interpretation, with the same needs of small partners can refer to the next

">

Recently, a new part of the site, with the increase of the site, management complexity comes up, as the saying goes: People are more bad, I found that the site is also not good tube, because these sites have important and unimportant, the important core of the site of course management more, like some years do not have a problem, Slowly by oneself all forget, sudden that day a problem, also rush to emergency treatment, so the standard to manage these sites is very necessary, today we do the first step, regardless of station station, first unified to monitor do up, first don't say business situation, at least that site can not access, to the first time to report out, Don't wait for the business party to give you feedback, it appears that we are not professional, then we look at if you use Python to implement multi-site usability monitoring, the script is as follows:

#!/usr/bin/env python import pickle, OS, sys, loggingfrom httplib import httpconnection, socketfrom smtplib import SMTP D EF email_alert (Message, status): Fromaddr = ' xxx@163.com ' Toaddrs = ' xxxx@qq.com ' server = SMTP (' smtp.163.com:25 ') s Erver.starttls () server.login (' xxxxx ', ' xxxx ') server.sendmail (fromaddr, Toaddrs, ' Subject:%s\r\n%s '% (status, Messag       e)) Server.quit () def get_site_status (URL): response = get_response (URL) try:if getattr (response, ' status ') = = 200: Return ' up ' except attributeerror:pass return ' down ' def get_response (URL): Try:conn = Httpconnection ( URL) conn.request (' HEAD ', '/') return Conn.getresponse () except Socket.error:return None except:logging.er Ror (' Bad URL: ', URL) exit (1) def get_headers (URL): response = get_response (URL) try:return getattr (response, ' Getheaders ') () except Attributeerror:return ' Headers unavailable ' Def compare_site_status (prev_results): Def is_st Atus_changed (URL): STAtus = get_site_status (URL) friendly_status = '%s is%s '% (URL, status) print Friendly_status if Urlin prev_resul    Tsand Prev_results[url]! = status:logging.warning (status) Email_alert (str (get_headers (URL)), friendly_status) Prev_results[url] = status return is_status_changed def is_internet_reachable (): If Get_site_status (' www.baidu.com ') = = ' Down ' and Get_site_status (' www.sohu.com ') = = ' Down ': Return False return True def load_old_results (file_path): P Ickledata = {} if Os.path.isfile (file_path): picklefile = open (File_path, ' rb ') Pickledata = Pickle.load (picklefile ) Picklefile.close () return pickledata def store_results (File_path, data): output = open (File_path, ' WB ') Pickle.du MP (data, Output) Output.close () def main (URLs): Logging.basicconfig (level=logging. WARNING, filename= ' Checksites.log ', format= '% (asctime) s% (levelname) s:% (message) s ', datefmt= '%y-%m-%d%h:%m:% S ') pickle_file = ' data.pkl ' pickledata = Load_old_resultS (pickle_file) print pickledata if Is_internet_reachable (): Status_checker = Compare_site_status (pickledata) m      AP (Status_checker, URLs) else:logging.error (' Either the world ended or we is not connected to the net. ') Store_results (Pickle_file, pickledata) if __name__ = = ' __main__ ': Main (sys.argv[1:])

Script Core Point Explanation:

1, GetAttr () is a python built-in function that receives an object that can return the value of an object based on the object's properties.

2. The Compare_site_status () function returns an internally defined function.

3, Map (), requires 2 parameters, one is a function, one is a sequence, the function is to apply each element of the sequence to the function method.

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.