Skip to content

Instantly share code, notes, and snippets.

@thoraxe
Created January 5, 2013 20:04
Show Gist options
  • Save thoraxe/4463366 to your computer and use it in GitHub Desktop.
Save thoraxe/4463366 to your computer and use it in GitHub Desktop.
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
@thoraxe
Copy link
Author

thoraxe commented Jan 5, 2013

rake aborted!
undefined method `empty?' for :needs:Symbol
/home/riding/.rvm/gems/ruby-1.8.7-p371@global/gems/rake-10.0.3/lib/rake/task_manager.rb:29:in `define_task'
/home/riding/.rvm/gems/ruby-1.8.7-p371@global/gems/rake-10.0.3/lib/rake/task.rb:333:in `define_task'
/home/riding/.rvm/gems/ruby-1.8.7-p371@global/gems/rake-10.0.3/lib/rake/dsl_definition.rb:32:in `task'
/home/riding/Riding-Resource/lib/tasks/ipblocker.rake:7
/home/riding/.rvm/gems/ruby-1.8.7-p371@global/gems/rake-10.0.3/lib/rake/task_manager.rb:196:in `in_namespace'
/home/riding/.rvm/gems/ruby-1.8.7-p371@global/gems/rake-10.0.3/lib/rake/dsl_definition.rb:104:in `namespace'
/home/riding/Riding-Resource/lib/tasks/ipblocker.rake:2
/home/riding/.rvm/gems/ruby-1.8.7-p371@rr-2.3/gems/activesupport-2.3.14/lib/active_support/dependencies.rb:171:in `load_without_new_constant_marking'
/home/riding/.rvm/gems/[email protected]/gems/activesupport-2.3.14/lib/active_support/dependencies.rb:171:in `load'
/home/riding/.rvm/gems/[email protected]/gems/activesupport-2.3.14/lib/active_support/dependencies.rb:547:in `new_constants_in'
/home/riding/.rvm/gems/ruby-1.8.7-p371@rr-2.3/gems/activesupport-2.3.14/lib/active_support/dependencies.rb:171:in `load'
/home/riding/.rvm/gems/[email protected]/gems/rails-2.3.14/lib/tasks/rails.rb:14
/home/riding/.rvm/gems/[email protected]/gems/rails-2.3.14/lib/tasks/rails.rb:14:in `each'
/home/riding/.rvm/gems/[email protected]/gems/rails-2.3.14/lib/tasks/rails.rb:14
/home/riding/.rvm/rubies/ruby-1.8.7-p371/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require'
/home/riding/.rvm/rubies/ruby-1.8.7-p371/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `require'
/home/riding/Riding-Resource/Rakefile:9
/home/riding/.rvm/gems/ruby-1.8.7-p371@global/gems/rake-10.0.3/lib/rake/rake_module.rb:25:in `load'
/home/riding/.rvm/gems/ruby-1.8.7-p371@global/gems/rake-10.0.3/lib/rake/rake_module.rb:25:in `load_rakefile'
/home/riding/.rvm/gems/ruby-1.8.7-p371@global/gems/rake-10.0.3/lib/rake/application.rb:583:in `raw_load_rakefile'
/home/riding/.rvm/gems/ruby-1.8.7-p371@global/gems/rake-10.0.3/lib/rake/application.rb:89:in `load_rakefile'
/home/riding/.rvm/gems/ruby-1.8.7-p371@global/gems/rake-10.0.3/lib/rake/application.rb:160:in `standard_exception_handling'
/home/riding/.rvm/gems/ruby-1.8.7-p371@global/gems/rake-10.0.3/lib/rake/application.rb:88:in `load_rakefile'
/home/riding/.rvm/gems/ruby-1.8.7-p371@global/gems/rake-10.0.3/lib/rake/application.rb:72:in `run'
/home/riding/.rvm/gems/ruby-1.8.7-p371@global/gems/rake-10.0.3/lib/rake/application.rb:160:in `standard_exception_handling'
/home/riding/.rvm/gems/ruby-1.8.7-p371@global/gems/rake-10.0.3/lib/rake/application.rb:70:in `run'
/home/riding/.rvm/gems/ruby-1.8.7-p371@global/gems/rake-10.0.3/bin/rake:33
/home/riding/.rvm/gems/[email protected]/bin/rake:19:in `load'
/home/riding/.rvm/gems/ruby-1.8.7-p371@rr-2.3/bin/rake:19
/home/riding/.rvm/gems/ruby-1.8.7-p371@rr-2.3/bin/ruby_noexec_wrapper:14

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment