Class: Blackhole::Hole

Inherits:
Object
  • Object
show all
Defined in:
lib/hole.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Hole

Returns a new instance of Hole.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/hole.rb', line 39

def initialize(options = {})

  self.host = options[:host] || "localhost"
  self.port = options[:port] || 8889
  self.mongo_db_name = options[:mongo_db_name] || "stepper"
  hostports = options[:mongo_hostports] || [["localhost", EM::Mongo::DEFAULT_PORT]]
  self.mongo_hostports = hostports.collect do |hp|
    if hp.is_a?(String)
      host, port = hp.split(":")
      [host, port || EM::Mongo::DEFAULT_PORT]
    else
      hp
    end
  end
  
  self.udp_packets_received = 0
  self.mongo_flushes = 0
  self.logs = []
  self.info = []
end

Instance Attribute Details

#hostObject

Host/Port to suck UDP packets on



19
20
21
# File 'lib/hole.rb', line 19

def host
  @host
end

#infoObject

sender info



36
37
38
# File 'lib/hole.rb', line 36

def info
  @info
end

#logsObject

Received Log



34
35
36
# File 'lib/hole.rb', line 34

def logs
  @logs
end

#mongoObject

Actual EM::Mongo connection



23
24
25
# File 'lib/hole.rb', line 23

def mongo
  @mongo
end

#mongo_db_nameObject

Info to connect to mongo



26
27
28
# File 'lib/hole.rb', line 26

def mongo_db_name
  @mongo_db_name
end

#mongo_flushesObject

Returns the value of attribute mongo_flushes.



31
32
33
# File 'lib/hole.rb', line 31

def mongo_flushes
  @mongo_flushes
end

#mongo_hostportsObject

Returns the value of attribute mongo_hostports.



27
28
29
# File 'lib/hole.rb', line 27

def mongo_hostports
  @mongo_hostports
end

#portObject

Returns the value of attribute port.



20
21
22
# File 'lib/hole.rb', line 20

def port
  @port
end

#udp_packets_receivedObject

Stats



30
31
32
# File 'lib/hole.rb', line 30

def udp_packets_received
  @udp_packets_received
end

Instance Method Details

#drain!Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/hole.rb', line 100

def drain!

  temp_logs = self.logs.clone
  self.logs = []
  temp_info = self.info.clone
  self.info = []
  temp_info.uniq!
  
  if temp_info.size > 0
    collection_name = Blackhole.mongo_collection_name("info") 
    temp_info.each do |info|
      info_tmp = {}
      info_tmp[:host] = info[:hostname].to_s
      info_tmp[:filename] = info[:filename].to_s
      info_tmp[:port] = self.port
      self.mongo.collection(collection_name).update(info_tmp, info_tmp, { upsert: true })
    end
  end

  if temp_logs.size > 0
    # sanitize collection name
    collection_name = Blackhole.mongo_collection_name(self.port) 

    self.mongo.collection(collection_name).insert(temp_logs)
    
  end

  Blackhole.logger.info "Saved logs : #{temp_logs}" 

  self.mongo_flushes += 1
end

#set_procline!Object



142
143
144
# File 'lib/hole.rb', line 142

def set_procline!
  $0 = "blackhole [#{self.port}] [UDP Recv: #{self.udp_packets_received}] [Mongo saves: #{self.mongo_flushes}]"
end

#store!(data) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/hole.rb', line 83

def store!(data)
  if /(?<seq>^<\d+>)(?<date>.{15})\s(?<hostname>[\w-]+)\s(?<filename>[^:]+):\s(?<log>.+)/ =~ data
    unless log == ""
      packet = {}
      packet[:hostname] = hostname
      packet[:filename] = filename
      self.info << packet
      packet[:date] = date
      packet[:log] = log
      packet[:time] = Time.now.to_i
      self.logs << packet
    end
  end

  self.udp_packets_received += 1
end

#tug!Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/hole.rb', line 60

def tug!
  EM.run do
    # Connect to mongo now
    self.mongo

    EM.open_datagram_socket host, port, HoleConnection do |conn|
      conn.hole = self
    end
    
    EM.add_periodic_timer(5) do
      #Blackhole.logger.info "Stepping: #{self.stepping.inspect}"
      #self.flush!
      self.drain!
      self.set_procline!
    end

    EM.next_tick do
      Blackhole.logger.info "Blackhost started to tug logs..."
      self.set_procline!
    end
  end
end