WEB伺服器巡檢指令碼phthon指令碼

來源:互聯網
上載者:User

指令碼功能:

監控多台Web伺服器狀態,一旦發生問題就發送郵件


運行環境:

Python2.7/2.4皆可運行


指令碼使用方法:

可利用Crontab或者計劃任務來指定時間運行,例如:

*/10 * * * * 指令碼路徑


指令碼運行效果如下:


指令碼內容如下:

#!/usr/bin/env python# coding=utf-8#----------------------------------------------------------# Name: WEB伺服器巡檢指令碼# Purpose: 監控多台Web伺服器狀態,一旦出現問題就發送郵件# Version: 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#----------------------------------------------------------fromsmtplib importSMTPfromemail importMIMETextfromemail importHeaderfromdatetime importdatetimeimporthttplib#定義要檢測的伺服器,URL 連接埠號碼 資源名稱web_servers =[('192.168.1.254', 80, 'index.html'),('www.xxx.com', 80, 'index.html'),('114.114.114.114', 9000, '/main/login.html'),]#定義主機 帳號 密碼 收件者 郵件主題smtpserver ='smtp.163.com'sender ='xxxx@xxx.com'password ='password'receiver =('收件者1','收件者2')subject =u'WEB伺服器警示郵件'From =u'Web伺服器'To =u'伺服器管理員'#定義記錄檔位置error_log ='/tmp/web_server_status.txt'defsend_mail(context):'''發送郵件'''#定義郵件的頭部資訊header =Header.Headermsg =MIMEText.MIMEText(context,'plain','utf-8')msg['From'] =header(From)msg['To'] =header(To)msg['Subject'] =header(subject +'\n')#串連SMTP伺服器,然後發送資訊smtp =SMTP(smtpserver)smtp.login(sender, password)smtp.sendmail(sender, receiver, msg.as_string())smtp.close()defget_now_date_time():'''擷取當前的日期'''now =datetime.now()returnstr(now.year) +"-"+str(now.month) +"-"\+str(now.day) +" "+str(now.hour) +":"\+str(now.minute) +":"+str(now.second)defcheck_webserver(host, port, resource):'''檢測WEB伺服器狀態'''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()#如果problem_server_list不為空白,就說明伺服器有問題,那就發送郵件ifproblem_server_list:send_mail(''.join(problem_server_list))


出自 “雷納科斯的部落格” 部落格,請務必保留此出處http://linux5588.blog.51cto.com/65280/1216417


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.