ruby脚本 使用goaccess和request-log-analyzer定时分析nginx log和rails log生成报告

参考: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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值