Class: Mouth::Sucker
- Inherits:
-
Object
- Object
- Mouth::Sucker
- Defined in:
- lib/mouth/sucker.rb
Instance Attribute Summary collapse
-
#counters ⇒ Object
Accumulators of our data.
-
#host ⇒ Object
Host/Port to suck UDP packets on.
-
#mongo_db ⇒ Object
Actual EM::Mongo connection.
-
#mongo_db_name ⇒ Object
Info to connect to mongo.
-
#mongo_flushes ⇒ Object
Returns the value of attribute mongo_flushes.
-
#mongo_hosts ⇒ Object
Returns the value of attribute mongo_hosts.
-
#port ⇒ Object
Returns the value of attribute port.
-
#timers ⇒ Object
Returns the value of attribute timers.
-
#udp_packets_received ⇒ Object
Stats.
Instance Method Summary collapse
- #flush! ⇒ Object
-
#initialize(options = {}) ⇒ Sucker
constructor
A new instance of Sucker.
- #save_documents!(mongo_docs) ⇒ Object
- #set_procline! ⇒ Object
-
#store!(data) ⇒ Object
counter: gorets:1|c counter w/ sampling: gorets:1|c|@0.1 timer: glork:320|ms (future) gauge: gaugor:333|g.
- #suck! ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Sucker
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/mouth/sucker.rb', line 37 def initialize( = {}) self.host = [:host] || "localhost" self.port = [:port] || 8889 self.mongo_db_name = [:mongo_db_name] || "mouth" self.mongo_hosts = [:mongo_hosts] || ["localhost"] self.udp_packets_received = 0 self.mongo_flushes = 0 self.counters = {} self.timers = {} end |
Instance Attribute Details
#counters ⇒ Object
Accumulators of our data
30 31 32 |
# File 'lib/mouth/sucker.rb', line 30 def counters @counters end |
#host ⇒ Object
Host/Port to suck UDP packets on
19 20 21 |
# File 'lib/mouth/sucker.rb', line 19 def host @host end |
#mongo_db ⇒ Object
Actual EM::Mongo connection
23 24 25 |
# File 'lib/mouth/sucker.rb', line 23 def mongo_db @mongo_db end |
#mongo_db_name ⇒ Object
Info to connect to mongo
26 27 28 |
# File 'lib/mouth/sucker.rb', line 26 def mongo_db_name @mongo_db_name end |
#mongo_flushes ⇒ Object
Returns the value of attribute mongo_flushes.
35 36 37 |
# File 'lib/mouth/sucker.rb', line 35 def mongo_flushes @mongo_flushes end |
#mongo_hosts ⇒ Object
Returns the value of attribute mongo_hosts.
27 28 29 |
# File 'lib/mouth/sucker.rb', line 27 def mongo_hosts @mongo_hosts end |
#port ⇒ Object
Returns the value of attribute port.
20 21 22 |
# File 'lib/mouth/sucker.rb', line 20 def port @port end |
#timers ⇒ Object
Returns the value of attribute timers.
31 32 33 |
# File 'lib/mouth/sucker.rb', line 31 def timers @timers end |
#udp_packets_received ⇒ Object
Stats
34 35 36 |
# File 'lib/mouth/sucker.rb', line 34 def udp_packets_received @udp_packets_received end |
Instance Method Details
#flush! ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 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 |
# File 'lib/mouth/sucker.rb', line 107 def flush! ts = 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: {...} # } # } 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.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
158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/mouth/sucker.rb', line 158 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_#{ns}" doc["t"] = ts.to_i self.mongo_db.collection(collection_name).insert(doc) end self.mongo_flushes += 1 if mongo_docs.any? end |
#set_procline! ⇒ Object
182 183 184 |
# File 'lib/mouth/sucker.rb', line 182 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 (future) gauge: gaugor:333|g
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/mouth/sucker.rb', line 77 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 = 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 end self.udp_packets_received += 1 end |
#suck! ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/mouth/sucker.rb', line 50 def suck! EM.run do # Connect to mongo now self.mongo_db 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}" self.flush! self.set_procline! end EM.next_tick do Mouth.logger.info "Mouth reactor started..." self.set_procline! end end end |