Created
January 5, 2013 20:04
-
-
Save thoraxe/4463366 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :ipblocker do | |
# send email with list of users that have visited more than N times yesterday | |
desc "This task will send an email to the specified address(es) with a list of Visitors who exceeded N visits on date (default is yesterday, format YYYY-MM-DD)" | |
task :email_alerts, :address, :limit, :date, :needs => :environment do |t, args| | |
args.with_defaults(:address => "root@localhost", :limit => 150, :date => Date.yesterday.to_s) | |
puts "Limit: " + args.limit.to_s | |
@limit = args.limit.to_i | |
@address = args.address | |
@date = args.date | |
@visits = Visit.find(:all, :conditions => ["created_at LIKE ?", "%" + @date + "%"]) | |
# group by visitor_id | |
@visits_grouped = @visits.group_by{|v| v.visitor_id} | |
# iterate and check limits | |
@overlimit_visitor_ids = [] | |
@visits_grouped.each do |v| | |
if v[1].length > @limit | |
# over the limit so we will add this user to the email list | |
@overlimit_visitor_ids << v[0] | |
#puts v[0].to_s + ": over" | |
end | |
end | |
@overlimit_visitors = Visitor.find(@overlimit_visitor_ids) | |
# build the mail only if there are visitors worth mailing about | |
if @overlimit_visitors.length > 0 | |
require 'net/smtp' | |
Net::SMTP.start('localhost') do |smtp| | |
smtp.open_message_stream(@address,@address) do |f| | |
f.puts 'From: ' + @address | |
f.puts 'To: ' + @address | |
f.puts 'Subject: IP Blocker violators list' | |
f.puts '' | |
@overlimit_visitors.each do |v| | |
f.puts v.ip_address + " : " + @visits_grouped[v.id].length.to_s + " visits" | |
end | |
end | |
end | |
end | |
end |
Author
thoraxe
commented
Jan 5, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment