参考:http://siwei.me/blog/posts/analyze-your-rails-log
http://siwei.me/blog/posts/analyze-nginx-log-using-goaccess
安装好goaccess和request-log-analyzer后:
date = Time.now.strftime('%Y%m%d')
yesterday_date = (Time.now - (3600 * 24)).strftime('%Y-%m-%d')
# nginx切分出的今天的日志记录昨天的log
nginx_log_file = "access.log-#{date}"
puts "=====start scp #{nginx_log_file}"
scp_command = "scp -P POrt XXX@HOST:/var/log/nginx/#{nginx_log_file} /opt/production_log_reports/"
`#{scp_command}`
puts '=====scp nginx log done'
# analyze nginx log file...
goaccess_command = "goaccess -f /opt/production_log_reports/#{nginx_log_file} -a > /opt/production_log_reports/report_for_nginx-#{yesterday_date}.html"
`#{goaccess_command}`
puts '=====analyze nginx log done'
rm_nginx_log_command = "rm /opt/production_log_reports/#{nginx_log_file}"
`#{rm_nginx_log_command}`
puts '====rm nginx log done'
rails_log_file = "puma\*9:\*#{yesterday_date}.log"
puts "=====start scp #{rails_log_file}"
scp_rails_log_command = "scp -P PORT xxx@HOST:/PROJECT/current/log/#{rails_log_file} /opt/production_log_reports/"
`#{scp_rails_log_command}`
puts '=====scp rails log done'
#analyze rails log file...
rails_analyze_command = "request-log-analyzer /opt/production_log_reports/#{rails_log_file} --format rails3 --output HTML --file /opt/production_log_reports/report_for_rails-#{yesterday_date}.html"
`#{rails_analyze_command}`
puts '=====analyze rails log done'
rm_rails_log_command = "rm /opt/production_log_reports/#{rails_log_file}"
`#{rm_rails_log_command}`
puts '====rm rails log done'
# crontab 内
* 9 * * * cd /xxx/xxx && ruby log_analyze.rb >> /opt/log_analyze.log 2>&1
网友评论