利用python批量檢查網(wǎng)站的可用性
前言
隨著站點(diǎn)的增多,管理復(fù)雜性也上來(lái)了,俗話說(shuō):人多了不好帶,我發(fā)現(xiàn)站點(diǎn)多了也不好管,因?yàn)檫@些站點(diǎn)里有重要的也有不重要的,重要核心的站點(diǎn)當(dāng)然就管理的多一些,像一些萬(wàn)年都不出一次問(wèn)題的,慢慢就被自己都淡忘了,冷不丁那天出個(gè)問(wèn)題,還的手忙腳亂的去緊急處理,所以規(guī)范的去管理這些站點(diǎn)是很有必要的,今天我們就做第一步,不管大站小站,先統(tǒng)一把監(jiān)控做起來(lái),先不說(shuō)業(yè)務(wù)情況,最起碼那個(gè)站點(diǎn)不能訪問(wèn)了,要第一時(shí)間報(bào)出來(lái),別等著業(yè)務(wù)方給你反饋,就顯得我們不夠?qū)I(yè)了,那接下來(lái)我們看看如果用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é)
以上就是這篇文章的全部?jī)?nèi)容,有需要的朋友們可以參考借鑒。
相關(guān)文章
Python 使用threading+Queue實(shí)現(xiàn)線程池示例
今天小編就為大家分享一篇Python 使用threading+Queue實(shí)現(xiàn)線程池示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
在Keras中實(shí)現(xiàn)保存和加載權(quán)重及模型結(jié)構(gòu)
這篇文章主要介紹了在Keras中實(shí)現(xiàn)保存和加載權(quán)重及模型結(jié)構(gòu),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06
Python中json.load()和json.loads()有哪些區(qū)別
json.loads()用于解析一個(gè)有效的JSON字符串并將其轉(zhuǎn)換為Python字典,json.load——()用于從一個(gè)文件讀取JSON類型的數(shù)據(jù),然后轉(zhuǎn)轉(zhuǎn)換成Python字典,本文講解下python中兩者的使用2021-06-06
Python探針完成調(diào)用庫(kù)的數(shù)據(jù)提取
這篇文章主要介紹了Python探針完成調(diào)用庫(kù)的數(shù)據(jù)提取,Python中可以通過(guò)sys.meta_path來(lái)實(shí)現(xiàn)import?hook的功能,下文詳細(xì)資料介紹,需要的小伙伴可以參考一下2022-05-05
基于Django signals 信號(hào)作用及用法詳解
這篇文章主要介紹了基于Django signals 信號(hào)作用及用法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
通過(guò)Python OpenGL的point sprite技術(shù)繪制雪花
通常,點(diǎn)精靈(point sprite)技術(shù)被用于描述大量粒子在屏幕上的運(yùn)動(dòng),自然也可以用于繪制雪花。本文將通過(guò)Python OpenGL繪制雪花,感興趣的可以動(dòng)手試一試2022-02-02

