|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
如果您觉得本篇CentOSLinux教程讲得好,请记得点击右边漂浮的分享程序,把好文章分享给你的好朋友们!剧本功效:
监控多台Web办事器状况,一旦产生成绩就发送邮件
运转情况:
Python2.7/2.4皆可运转
剧本应用办法:
可应用Crontab或许筹划义务来指准时间运转,例如:
*/10****剧本路径
剧本运转后果以下:
剧本内容以下:
- #!/usr/bin/envpython#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)LEO2013#Python:2.4/2.7#----------------------------------------------------------fromsmtplibimportSMTPfromemailimportMIMETextfromemailimportHeaderfromdatetimeimportdatetimeimporthttplib#界说要检测的办事器,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.comsender=xxxx@xxx.compassword=passwordreceiver=(收件人1,收件人2)subject=uWEB办事器告警邮件From=uWeb办事器To=u办事器治理员#界说日记文件地位error_log=/tmp/web_server_status.txtdefsend_mail(context):发送邮件#界说邮件的头部信息header=Header.Headermsg=MIMEText.MIMEText(context,plain,utf-8)msg[From]=header(From)msg[To]=header(To)msg[Subject]=header(subject+
- )#衔接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()ifstatusin[200,301]andcontent_length!=0:returnTrueelse:returnFalseif__name__==__main__:logfile=open(error_log,a)problem_server_list=[]forhostinweb_servers:host_url=host[0]check=check_webserver(host_url,host[1],host[2])ifnotcheck:temp_string=TheServer[%s]mayappearproblemat%s
- %(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
如果您觉得本篇CentOSLinux教程讲得好,请记得点击右边漂浮的分享程序,把好文章分享给你的小伙伴们! |
|