Skip to content

Commit

Permalink
log_monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
alvin2ye committed Oct 16, 2009
1 parent 8402195 commit 36a22fe
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/deploy_kit.rb
Expand Up @@ -9,3 +9,4 @@
require 'deploy_kit/backup_mysql'
require 'deploy_kit/backup_log'
require 'deploy_kit/s3_storage'
require 'deploy_kit/log_monitor'
51 changes: 51 additions & 0 deletions lib/deploy_kit/log_monitor.rb
@@ -0,0 +1,51 @@
class LogMonitor < DeployKit
def chekc_warning(will_sent = nil)
mail_body = []

lines = `cat #{log_path} | grep "200 OK" | awk '{print $3 " " $0}' | sort -nr | head -n 10`
return if !need_send?(lines)

if !will_sent.blank?
send_mail(lines)
else
puts lines
end
end

def allow_time
@fu_conf[:allow_time_ms].to_i
end

def log_path
File.join(RAILS_ROOT, "log", "production.log")
end

def need_send?(lines)
result = nil
time = lines[0].to_s.split(" ").first

return false if time.blank?
if is_ms?(time)
if time.to_f > allow_time * 1000
result = true
end
else
if time.to_f > allow_time
result = true
end
end

result
end

def send_mail(lines)
cmd = "echo \"#{lines}\" | mail #{@fu_conf[:to_mail]} -s \"#{@timestamp} #{@fu_conf[:app_name]} log \" -a \"From: #{@fu_conf[:from_mail]}\""
puts cmd if @verbose
`#{cmd}`
end

private
def is_ms?(time)
time.index("ms")
end
end
7 changes: 7 additions & 0 deletions tasks/deploy_kit_tasks.rake
Expand Up @@ -30,3 +30,10 @@ namespace :s3 do
puts "..." # TODO impl
end
end

namespace :monitor do
desc "or rake monitor:slow_log SEND_MAIL=true"
task :slow_log do
LogMonitor.new.chekc_warning(ENV["SEND_MAIL"])
end
end

0 comments on commit 36a22fe

Please sign in to comment.