Class: Mouth::Sucker

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Sucker

Returns a new instance of Sucker.



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

def initialize(options = {})
  self.host = options[:host] || "localhost"
  self.port = options[:port] || 8889
  self.mongo_db_name = options[:mongo_db_name] || "mouth"
  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.counters = {}
  self.timers = {}
  self.gauges = {}
end

Instance Attribute Details

#countersObject

Accumulators of our data



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

def counters
  @counters
end

#gaugesObject

Returns the value of attribute gauges.



32
33
34
# File 'lib/mouth/sucker.rb', line 32

def gauges
  @gauges
end

#hostObject

Host/Port to suck UDP packets on



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

def host
  @host
end

#mongoObject

Actual EM::Mongo connection



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

def mongo
  @mongo
end

#mongo_db_nameObject

Info to connect to mongo



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

def mongo_db_name
  @mongo_db_name
end

#mongo_flushesObject

Returns the value of attribute mongo_flushes.



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

def mongo_flushes
  @mongo_flushes
end

#mongo_hostportsObject

Returns the value of attribute mongo_hostports.



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

def mongo_hostports
  @mongo_hostports
end

#portObject

Returns the value of attribute port.



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

def port
  @port
end

#timersObject

Returns the value of attribute timers.



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

def timers
  @timers
end

#udp_packets_receivedObject

Stats



35
36
37
# File 'lib/mouth/sucker.rb', line 35

def udp_packets_received
  @udp_packets_received
end

Instance Method Details

#flush!Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/mouth/sucker.rb', line 121

def flush!
  ts = Mouth.current_timestamp
  limit_ts = ts - 1
  mongo_docs = {}
  
  # We're going to construct mongo_docs which look like this:
  # "mycollections:234234": {  # NOTE: this timpstamp will be popped into .t = 234234
  #   c: {
  #     happenings: 37,
  #     affairs: 3
  #   },
  #   m: {
  #     occasions: {...}
  #   },
  #   g: {things: 3}
  # }
  
  self.counters.each do |cur_ts, counters_to_save|
    if cur_ts <= limit_ts
      counters_to_save.each do |counter_key, value|
        ns, sub_key = Mouth.parse_key(counter_key)
        mongo_key = "#{ns}:#{ts}"
        mongo_docs[mongo_key] ||= {}
        
        cur_mongo_doc = mongo_docs[mongo_key]
        cur_mongo_doc["c"] ||= {}
        cur_mongo_doc["c"][sub_key] = value
      end
      
      self.counters.delete(cur_ts)
    end
  end
  
  self.gauges.each do |cur_ts, gauges_to_save|
    if cur_ts <= limit_ts
      gauges_to_save.each do |gauge_key, value|
        ns, sub_key = Mouth.parse_key(gauge_key)
        mongo_key = "#{ns}:#{ts}"
        mongo_docs[mongo_key] ||= {}
        
        cur_mongo_doc = mongo_docs[mongo_key]
        cur_mongo_doc["g"] ||= {}
        cur_mongo_doc["g"][sub_key] = value
      end
      
      self.gauges.delete(cur_ts)
    end
  end
  
  self.timers.each do |cur_ts, timers_to_save|
    if cur_ts <= limit_ts
      timers_to_save.each do |timer_key, values|
        ns, sub_key = Mouth.parse_key(timer_key)
        mongo_key = "#{ns}:#{ts}"
        mongo_docs[mongo_key] ||= {}
        
        cur_mongo_doc = mongo_docs[mongo_key]
        cur_mongo_doc["m"] ||= {}
        cur_mongo_doc["m"][sub_key] = analyze_timer(values)
      end
      
      self.timers.delete(cur_ts)
    end
  end
  
  save_documents!(mongo_docs)
end

#save_documents!(mongo_docs) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/mouth/sucker.rb', line 189

def save_documents!(mongo_docs)
  Mouth.logger.info "Saving Docs: #{mongo_docs.inspect}"
  
  mongo_docs.each do |key, doc|
    ns, ts = key.split(":")
    collection_name = Mouth.mongo_collection_name(ns)
    doc["t"] = ts.to_i
    
    self.mongo.collection(collection_name).insert(doc)
  end
  
  self.mongo_flushes += 1 if mongo_docs.any?
end

#set_procline!Object



213
214
215
# File 'lib/mouth/sucker.rb', line 213

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

#store!(data) ⇒ Object

counter: gorets:1|c counter w/ sampling: gorets:1|c|@0.1 timer: glork:320|ms gauge: gaugor:333|g



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/mouth/sucker.rb', line 88

def store!(data)
  key_value, command_sampling = data.to_s.split("|", 2)
  key, value = key_value.to_s.split(":")
  command, sampling = command_sampling.to_s.split("|")
  
  return unless key && value && command && key.length > 0 && value.length > 0 && command.length > 0
  
  key = Mouth.parse_key(key).join(".")
  value = value.to_f
  
  ts = Mouth.current_timestamp
  
  if command == "ms"
    self.timers[ts] ||= {}
    self.timers[ts][key] ||= []
    self.timers[ts][key] << value
  elsif command == "c"
    factor = 1.0
    if sampling
      factor = sampling.sub("@", "").to_f
      factor = (factor == 0.0 || factor > 1.0) ? 1.0 : 1.0 / factor
    end
    self.counters[ts] ||= {}
    self.counters[ts][key] ||= 0.0
    self.counters[ts][key] += value * factor
  elsif command == "g"
    self.gauges[ts] ||= {}
    self.gauges[ts][key] = value
  end
  
  self.udp_packets_received += 1
end

#suck!Object



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

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

    EM.open_datagram_socket host, port, SuckerConnection do |conn|
      conn.sucker = self
    end
    
    EM.add_periodic_timer(10) do
      Mouth.logger.info "Counters: #{self.counters.inspect}"
      Mouth.logger.info "Timers: #{self.timers.inspect}"
      Mouth.logger.info "Gauges: #{self.gauges.inspect}"
      self.flush!
      self.set_procline!
    end

    EM.next_tick do
      Mouth.logger.info "Mouth reactor started..."
      self.set_procline!
    end
  end
end