GoAccess 是一款开源的且具有交互视图界面的实时 Web 日志分析工具,通过你的 Web 浏览器或者 *nix 系统下的终端程序(terminal)即可访问。
能为系统管理员提供快速且有价值的 HTTP 统计,并以在线可视化服务器的方式呈现。
Python实现goaccess分析nginx日志,结果报告发邮件通知运维。
#!/usr/bin/python
#-*- coding:utf-8 -*-
#import psycopg2
import os
import time
import datetime
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
today = datetime.date.today()
yesterday = today - datetime.timedelta(days=1)
beforeday = today - datetime.timedelta(days=2)
daily = str(yesterday)
ddaily = str(beforeday)
#logsfile = 'app.access-'+'str(yesterday)'+'.log'
os.system("goaccess -f /opt/nginx/logs/analysis/app.access-$(date -d '1 days ago' +%Y-%m-%d).log -p /opt/nginx/logs/analysis/goaccess_nginx.conf -a -g -o /opt/nginx/logs/analysis/daily-export-$(date -d '1 days ago' +%Y-%m-%d).html")
# 邮件发送
mail_host="smtp.126.com" #邮件服务器
mail_port='465' #邮件服务器使用的端口
mail_user="oolpower@126.com" #发送邮件用户名
mail_pass="bOOL123" #口令
#mailto_list=str(sys.argv[1])
mailto_list=["ool_power@aliyun.com"]
mailcc_list=["oolpower@163.com"]
msg = MIMEMultipart()
##如名字所示 Multipart 就是分多个部分
#msg = MIMEText(mailContent,_subtype='html',_charset='utf-8')
msg.attach(MIMEText('生产环境,每天Nginx日志分析报告,Yesterday !', 'plain', 'utf-8'))
#msg["Subject"] = u"生产环境Nginx日志分析报告_" + str(yesterday)
msg["Subject"] = '生产环境Nginx日志分析报告_' + daily
os.system("cd /opt/nginx/logs/analysis")
analyseFileName = '/opt/nginx/logs/analysis/daily-export-' + daily + '.html'
part = MIMEApplication(open(analyseFileName,'rb').read())
part.add_header('Content-Disposition', 'attachment', filename='daily-export-' + daily + '.html')
msg.attach(part)
msg["From"] = mail_user
msg["To"] = ";".join(mailto_list)
msg['Cc'] = ";".join(mailcc_list)
print 'mailing...'
try:
server = smtplib.SMTP_SSL(mail_host,mail_port,timeout=30)
# server.starttls()
server.login(mail_user,mail_pass)
server.sendmail(mail_user, mailto_list+mailcc_list, msg.as_string())
# server.sendmail(mail_user, mailto_list, msg.as_string())
server.close()
except Exception, e:
print str(e)
server.close()
#crontab del old file
delfilename1 = '/opt/nginx/logs/analysis/app.access-' + ddaily + '.log'
if os.path.exists(delfilename1):
os.system("rm -r /opt/nginx/logs/analysis/app.access-$(date -d '2 days ago' +%Y-%m-%d).log")
else:
message = "Sorry, I cannot find the file"
delfilename2 = '/opt/nginx/logs/analysis/daily-export-' + ddaily + '.html'
if os.path.exists(delfilename2):
os.system("rm -r /opt/nginx/logs/analysis/daily-export-$(date -d '2 days ago' +%Y-%m-%d).html")
else:
message = "Sorry, I cannot find the file"
#del app.access.log
current_time = time.localtime(time.time())
if((current_time.tm_tm_mday == 1) and (current_time.tm_hour == 0) and (current_time.tm_min >= 0) and (current_time.tm_sec >= 0)):
os.system(">/opt/nginx/logs/analysis/app.access.log")
else:
message = "Sorry, I cannot find the file"