Class: Reader

Inherits:
EventMachine::FileTail
  • Object
show all
Defined in:
lib/block/reader.rb

Instance Method Summary collapse

Constructor Details

#initialize(path, startpos = -1)) ⇒ Reader

Returns a new instance of Reader.



5
6
7
8
# File 'lib/block/reader.rb', line 5

def initialize(path, startpos=-1)
  super(path, startpos)
  @buffer = BufferedTokenizer.new
end

Instance Method Details

#firewall(ip) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/block/reader.rb', line 32

def firewall(ip)
 if ($redis.sismember "ips", "#{ip}")
   puts "Already firewalled"
 else
   unless ($options[:disable] == true)
     puts "Firewalling: #{ip}"
     system "/sbin/iptables -I INPUT -s #{ip} -j DROP"
     $redis.sadd "ips", "#{ip}"
   else
     puts "Adding rules disabled for: #{ip}"
   end
 end
end

#log_search(line, pattern) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/block/reader.rb', line 10

def log_search(line, pattern)
  if line.split(' ').grep(/#{pattern}/).length > 0
    array = line.split(" ")
    count = $redis.incr array.first.to_s
    $redis.expire array.first.to_s, $options[:expiry]
    puts "\nIP: #{array.first.to_s} on #{pattern} (#{count})"
    if (count > $options[:threshold])
      firewall(array.first.to_s)
    end
  else
    print "."
  end
end

#receive_data(data) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/block/reader.rb', line 24

def receive_data(data)
  @buffer.extract(data).each do |line|
    $search.each do |search|
      log_search(line, "#{search}")
    end
  end
end