利用python批量檢查網(wǎng)站的可用性
前言
隨著站點(diǎn)的增多,管理復(fù)雜性也上來了,俗話說:人多了不好帶,我發(fā)現(xiàn)站點(diǎn)多了也不好管,因?yàn)檫@些站點(diǎn)里有重要的也有不重要的,重要核心的站點(diǎn)當(dāng)然就管理的多一些,像一些萬年都不出一次問題的,慢慢就被自己都淡忘了,冷不丁那天出個(gè)問題,還的手忙腳亂的去緊急處理,所以規(guī)范的去管理這些站點(diǎn)是很有必要的,今天我們就做第一步,不管大站小站,先統(tǒng)一把監(jiān)控做起來,先不說業(yè)務(wù)情況,最起碼那個(gè)站點(diǎn)不能訪問了,要第一時(shí)間報(bào)出來,別等著業(yè)務(wù)方給你反饋,就顯得我們不夠?qū)I(yè)了,那接下來我們看看如果用python實(shí)現(xiàn)多網(wǎng)站的可用性監(jiān)控,腳本如下:
#!/usr/bin/env python import pickle, os, sys, logging from httplib import HTTPConnection, socket from smtplib import SMTP def email_alert(message, status): fromaddr = 'xxx@163.com' toaddrs = 'xxxx@qq.com' server = SMTP('smtp.163.com:25') server.starttls() server.login('xxxxx', 'xxxx') server.sendmail(fromaddr, toaddrs, 'Subject: %s\r\n%s' % (status, message)) 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.error('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_status_changed(url): status = get_site_status(url) friendly_status = '%s is %s' % (url, status) print friendly_status if url in prev_results and 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): pickledata = {} 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.dump(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) map(status_checker, urls) else: logging.error('Either the world ended or we are not connected to the net.') store_results(pickle_file, pickledata) if __name__ == '__main__': main(sys.argv[1:])
腳本核心點(diǎn)解釋:
1、getattr()
是python的內(nèi)置函數(shù),接收一個(gè)對(duì)象,可以根據(jù)對(duì)象屬性返回對(duì)象的值。
2、compare_site_status()
函數(shù)是返回的是一個(gè)內(nèi)部定義的函數(shù)。
3、map()
,需要2個(gè)參數(shù),一個(gè)是函數(shù),一個(gè)是序列,功能就是將序列中的每個(gè)元素應(yīng)用函數(shù)方法。
總結(jié)
以上就是這篇文章的全部內(nèi)容,有需要的朋友們可以參考借鑒。
相關(guān)文章
numpy 產(chǎn)生隨機(jī)數(shù)的幾種方法
本文主要介紹了numpy 產(chǎn)生隨機(jī)數(shù)的幾種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02在PyTorch中實(shí)現(xiàn)可解釋的神經(jīng)網(wǎng)絡(luò)模型的方法詳解
這篇文章主要為大家介紹在PyTorch如何中實(shí)現(xiàn)可解釋的神經(jīng)網(wǎng)絡(luò)模型,并為您提供使用簡單的 PyTorch 接口實(shí)現(xiàn)最先進(jìn)的基于概念的模型的工具,需要的朋友可以參考下2023-06-06python字符串,元組,列表,字典互轉(zhuǎn)代碼實(shí)例詳解
這篇文章主要介紹了python字符串,元組,列表,字典互轉(zhuǎn)代碼實(shí)例詳解,需要的朋友可以參考下2020-02-02Keras構(gòu)建神經(jīng)網(wǎng)絡(luò)踩坑(解決model.predict預(yù)測值全為0.0的問題)
這篇文章主要介紹了Keras構(gòu)建神經(jīng)網(wǎng)絡(luò)踩坑(解決model.predict預(yù)測值全為0.0的問題),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07