-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_503.rb
More file actions
62 lines (49 loc) · 1.87 KB
/
Copy pathfind_503.rb
File metadata and controls
62 lines (49 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
require 'net/smtp'
require 'rubygems'
require 'mysql2'
client = Mysql2::Client.new(:host => 'db.buildzoom.com', :database => 'bzdb', :username => "new", :password => ARGV.first, :flags => Mysql2::Client::MULTI_STATEMENTS)
def send_email(to,opts={})
opts[:server] ||= 'localhost'
opts[:from] ||= 'david@buildzoom.com'
opts[:subject] ||= "Nginx errors found"
opts[:body] ||= "503 errors found in nginx log!"
msg = <<END_OF_MESSAGE
From: #{opts[:from_alias]} <#{opts[:from]}>
To: <#{to}>
Subject: #{opts[:subject]}
#{opts[:body]}
END_OF_MESSAGE
Net::SMTP.start(opts[:server]) do |smtp|
smtp.send_message msg, opts[:from], to
end
end
log_file = `tail -n 5000 /media/drvf/logs/nginx/buildzoom.access.log | grep '1.1" 503'`
#log_file2 = `tail -n 10000 /media/drvf/logs/nginx/buildzoom.access.log | grep '1.0" 503'`
if (log_file.length > 1)
log_file.each_line do |line|
if (line =~ /\[(.*?)\] "GET (.*?) HTTP.*?503 \d+ "-" "(.*?)"/)
puts line
error_date = client.escape($1)
url = client.escape($2)
user_agent = client.escape($3)
query_string = "insert ignore into 503_errors (error_date, error_ua, error_url) values ('#{error_date}','#{user_agent}','#{url}')"
client.query(query_string)
end
end
end
query = "select id, error_date, error_ua, error_ip, error_url from 503_errors where emailed = 0"
counter = 0
my_string = ""
results = client.query(query)
results.each do |row|
counter = counter + 1
query2 = "update 503_errors set emailed = 1 where id = #{row['id']}"
client.query(query2)
my_string = my_string + "error_date: " + row['error_date'] + " error_url: " + row['error_ua'] + "error_url: " + row['error_url'] + "\n"
end
if counter > 1
puts "sending email\n"
send_email "dpetersen@gmail.com", :body => my_string
send_email "gerard@buildzoom.com", :body => my_string
send_email "alitvak@buildzoom.com", :body => my_string
end