Class: Fluent::MongoKpiOutput
- Inherits:
-
BufferedOutput
- Object
- BufferedOutput
- Fluent::MongoKpiOutput
- Defined in:
- lib/fluent/plugin/out_mongokpi.rb
Instance Attribute Summary collapse
-
#collections_opts ⇒ Object
readonly
Returns the value of attribute collections_opts.
-
#connection_opts ⇒ Object
readonly
Returns the value of attribute connection_opts.
Instance Method Summary collapse
-
#configure(conf) ⇒ Object
This method is called before starting.
- #convert_collection_name(collection_name, yyyymmdd) ⇒ Object
- #count_up(kpi_type, doc, record, count_name, time) ⇒ Object
-
#format(tag, time, record) ⇒ Object
This method is called when an event is reached.
- #get_client(address, connection_opts) ⇒ Object
- #get_collection(collection_name, yyyymmdd) ⇒ Object
- #get_collection_from_db(client, db_name, collection_name, collections_opts) ⇒ Object
- #get_doc(kpi_type, count_key, count_key_value, count_name, time) ⇒ Object
- #get_insert_doc_hash(kpi_type, chunk, time_key, time_format, count_key, count_name) ⇒ Object
-
#initialize ⇒ MongoKpiOutput
constructor
A new instance of MongoKpiOutput.
- #insert(kpi_type, collection_name, count_key, count_name, doc_hash) ⇒ Object
-
#shutdown ⇒ Object
This method is called when shutting down.
-
#start ⇒ Object
This method is called when starting.
-
#write(chunk) ⇒ Object
This method is called every flush interval.
Constructor Details
#initialize ⇒ MongoKpiOutput
Returns a new instance of MongoKpiOutput.
34 35 36 37 38 39 |
# File 'lib/fluent/plugin/out_mongokpi.rb', line 34 def initialize super require 'date' require 'mongo' require 'msgpack' end |
Instance Attribute Details
#collections_opts ⇒ Object (readonly)
Returns the value of attribute collections_opts.
32 33 34 |
# File 'lib/fluent/plugin/out_mongokpi.rb', line 32 def collections_opts @collections_opts end |
#connection_opts ⇒ Object (readonly)
Returns the value of attribute connection_opts.
32 33 34 |
# File 'lib/fluent/plugin/out_mongokpi.rb', line 32 def connection_opts @connection_opts end |
Instance Method Details
#configure(conf) ⇒ Object
This method is called before starting. ‘conf’ is a Hash that includes configuration parameters. If the configuration is invalid, raise Fluent::ConfigError.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/fluent/plugin/out_mongokpi.rb', line 44 def configure(conf) super @connection_opts = {} @connection_opts[:w] = @write_concern unless @write_concern.nil? @connection_opts[:name] = @name unless @name.nil? @connection_opts[:read] = @read unless @read.nil? @connection_opts[:refresh_mode] = @refresh_mode unless @refresh_mode.nil? @connection_opts[:refresh_interval] = @refresh_interval unless @refresh_interval.nil? @collections_opts = {} if @capped_size > 0 @collections_opts[:capped] = true @collections_opts[:size] = Config.size_value(conf['capped_size']) @collections_opts[:max] = Config.size_value(conf['capped_max']) if @capped_max else @collections_opts[:capped] = false end end |
#convert_collection_name(collection_name, yyyymmdd) ⇒ Object
132 133 134 |
# File 'lib/fluent/plugin/out_mongokpi.rb', line 132 def convert_collection_name(collection_name, yyyymmdd) return collection_name.sub('yyyymmdd', yyyymmdd) end |
#count_up(kpi_type, doc, record, count_name, time) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/fluent/plugin/out_mongokpi.rb', line 192 def count_up(kpi_type, doc, record, count_name, time) doc[count_name] += 1 if 'access' == kpi_type response_time = record.key?(@f_response_time) ? record[@f_response_time].to_i : 0 if response_time > @responseThreshold doc['countOver'] += 1 end case record[@f_code].to_i / 100 when 1 then doc['count1xx'] += 1 when 2 then doc['count2xx'] += 1 when 3 then doc['count3xx'] += 1 when 4 then doc['count4xx'] += 1 when 5 then doc['count5xx'] += 1 end if doc['responseTimeMax'] < response_time doc['responseTimeMax'] = response_time end if doc['responseTimeMin'] > response_time doc['responseTimeMin'] = response_time end doc['responseTimeSum'] += response_time doc['counter'][time.strftime('%S').to_i] += 1 end return doc end |
#format(tag, time, record) ⇒ Object
This method is called when an event is reached. Convert event to a raw string. def format(tag, time, record)
[tag, time, record].to_json + "\n"
end optionally, you can use to_msgpack to serialize the object. def format(tag, time, record)
[tag, time, record].to_msgpack
end
85 86 87 |
# File 'lib/fluent/plugin/out_mongokpi.rb', line 85 def format(tag, time, record) [tag, time, record].to_msgpack end |
#get_client(address, connection_opts) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/fluent/plugin/out_mongokpi.rb', line 106 def get_client(address, connection_opts) begin if address.include?(',') return Mongo::MongoReplicaSetClient.new(address.split(','), connection_opts) else host_port = address.split(':', 2) return Mongo::MongoClient.new(host_port[0], host_port[1], collections_opts) end rescue Mongo::ConnectionFailure => e $log.fatal "Failed to connect to 'mongod'. Please restart 'fluentd' after 'mongod' started: #{e}" exit! rescue Mongo::OperationFailure => e $log.fatal "Operation failed. Probably, 'mongod' needs an authentication: #{e}" exit! end end |
#get_collection(collection_name, yyyymmdd) ⇒ Object
123 124 125 126 127 128 129 130 |
# File 'lib/fluent/plugin/out_mongokpi.rb', line 123 def get_collection(collection_name, yyyymmdd) converted_collection_name = convert_collection_name(collection_name, yyyymmdd) if @current_collection.nil? || @current_collection.name != converted_collection_name $log.info "Start using collection: #{converted_collection_name}" @current_collection = get_collection_from_db(@client, @db, converted_collection_name, @collections_opts) end return @current_collection end |
#get_collection_from_db(client, db_name, collection_name, collections_opts) ⇒ Object
136 137 138 |
# File 'lib/fluent/plugin/out_mongokpi.rb', line 136 def get_collection_from_db(client, db_name, collection_name, collections_opts) return client.db(db_name).collection(collection_name, @collections_opts) end |
#get_doc(kpi_type, count_key, count_key_value, count_name, time) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/fluent/plugin/out_mongokpi.rb', line 165 def get_doc(kpi_type, count_key, count_key_value, count_name, time) doc = {} doc[count_key] = count_key_value doc['yyyymmdd'] = time.strftime('%Y%m%d') doc['hh'] = time.strftime('%H') doc['mm'] = time.strftime('%M') doc[count_name] = 0 if 'access' == kpi_type doc['countOver'] = 0 doc['count1xx'] = 0 doc['count2xx'] = 0 doc['count3xx'] = 0 doc['count4xx'] = 0 doc['count5xx'] = 0 doc['responseTimeAve'] = 0 doc['responseTimeMax'] = 0 doc['responseTimeMin'] = 100000000.00 doc['responseTimeSum'] = 0 doc['qpsAve'] = 0 doc['qpsMax'] = 0 doc['qpsMin'] = 100000000 doc['okRatio'] = 0.00 doc['counter'] = Array.new(60, 0) end return doc end |
#get_insert_doc_hash(kpi_type, chunk, time_key, time_format, count_key, count_name) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/fluent/plugin/out_mongokpi.rb', line 140 def get_insert_doc_hash(kpi_type, chunk, time_key, time_format, count_key, count_name) hash_counter = {} chunk.msgpack_each { |tag, time, record| $log.debug record tmp_time = time_key.nil? ? Time.at(time) : time_format.nil? ? DateTime.parse(record[time_key]) : DateTime.strptime(record[time_key], time_format) # with count_key if 'none' != count_key count_key_value = '' count_key.split(',').each { |x| count_key_value += record[x].to_s } key_str = count_key_value + tmp_time.strftime('%Y%m%d%H%M') doc = hash_counter.key?(key_str) ? hash_counter[key_str] : get_doc(kpi_type, count_key, count_key_value, count_name, tmp_time) hash_counter[key_str] = count_up(kpi_type, doc, record, count_name, tmp_time) end # total total_key_str = 'total' + tmp_time.strftime('%Y%m%d%H%M') total = hash_counter.key?(total_key_str) ? hash_counter[total_key_str] : get_doc(kpi_type, count_key, 'total', count_name, tmp_time) hash_counter[total_key_str] = count_up(kpi_type, total, record, count_name, tmp_time) } return hash_counter end |
#insert(kpi_type, collection_name, count_key, count_name, doc_hash) ⇒ Object
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/fluent/plugin/out_mongokpi.rb', line 218 def insert(kpi_type, collection_name, count_key, count_name, doc_hash) begin doc_hash.each { |key, doc| $log.debug doc collection = get_collection(collection_name, doc['yyyymmdd']) # 2.5 or less # http://stackoverflow.com/questions/8508663/calculate-max-value-in-an-atomic-findandmodify-operation # TODO improve for Mongo 2.6 # $min, $max field update operators # https://jira.mongodb.org/browse/SERVER-1534 # https://jira.mongodb.org/browse/DOCS-2012 if 'access' == kpi_type collection.update( {'_id' => key, count_key => doc[count_key], 'yyyymmdd' => doc['yyyymmdd'], 'hh' => doc['hh'], 'mm' => doc['mm']}, {'$setOnInsert' => {'counter' => Array.new(60, 0)}}, {:upsert => true} ) collection.update( {'_id' => key, count_key => doc[count_key], 'yyyymmdd' => doc['yyyymmdd'], 'hh' => doc['hh'], 'mm' => doc['mm']}, {'$inc' => { count_name => doc[count_name], 'countOver' => doc['countOver'], 'count1xx' => doc['count1xx'], 'count2xx' => doc['count2xx'], 'count3xx' => doc['count3xx'], 'count4xx' => doc['count4xx'], 'count5xx' => doc['count5xx'], 'responseTimeSum' => doc['responseTimeSum'], 'counter.0' => doc['counter'][0], 'counter.1' => doc['counter'][1], 'counter.2' => doc['counter'][2], 'counter.3' => doc['counter'][3], 'counter.4' => doc['counter'][4], 'counter.5' => doc['counter'][5], 'counter.6' => doc['counter'][6], 'counter.7' => doc['counter'][7], 'counter.8' => doc['counter'][8], 'counter.9' => doc['counter'][9], 'counter.10' => doc['counter'][10], 'counter.11' => doc['counter'][11], 'counter.12' => doc['counter'][12], 'counter.13' => doc['counter'][13], 'counter.14' => doc['counter'][14], 'counter.15' => doc['counter'][15], 'counter.16' => doc['counter'][16], 'counter.17' => doc['counter'][17], 'counter.18' => doc['counter'][18], 'counter.19' => doc['counter'][19], 'counter.20' => doc['counter'][20], 'counter.21' => doc['counter'][21], 'counter.22' => doc['counter'][22], 'counter.23' => doc['counter'][23], 'counter.24' => doc['counter'][24], 'counter.25' => doc['counter'][25], 'counter.26' => doc['counter'][26], 'counter.27' => doc['counter'][27], 'counter.28' => doc['counter'][28], 'counter.29' => doc['counter'][29], 'counter.30' => doc['counter'][30], 'counter.31' => doc['counter'][31], 'counter.32' => doc['counter'][32], 'counter.33' => doc['counter'][33], 'counter.34' => doc['counter'][34], 'counter.35' => doc['counter'][35], 'counter.36' => doc['counter'][36], 'counter.37' => doc['counter'][37], 'counter.38' => doc['counter'][38], 'counter.39' => doc['counter'][39], 'counter.40' => doc['counter'][40], 'counter.41' => doc['counter'][41], 'counter.42' => doc['counter'][42], 'counter.43' => doc['counter'][43], 'counter.44' => doc['counter'][44], 'counter.45' => doc['counter'][45], 'counter.46' => doc['counter'][46], 'counter.47' => doc['counter'][47], 'counter.48' => doc['counter'][48], 'counter.49' => doc['counter'][49], 'counter.50' => doc['counter'][50], 'counter.51' => doc['counter'][51], 'counter.52' => doc['counter'][52], 'counter.53' => doc['counter'][53], 'counter.54' => doc['counter'][54], 'counter.55' => doc['counter'][55], 'counter.56' => doc['counter'][56], 'counter.57' => doc['counter'][57], 'counter.58' => doc['counter'][58], 'counter.59' => doc['counter'][59] }}, {:upsert => true} ) updated_doc_array = collection.find({'_id' => key}).to_a $log.debug updated_doc_array continue if updated_doc_array.nil? updated_doc = updated_doc_array[0] response_time_ave = updated_doc['responseTimeSum'] / doc[count_name] if !updated_doc['responseTimeMax'].nil? && updated_doc['responseTimeMax'] > doc['responseTimeMax'] response_time_max = updated_doc['responseTimeMax'] else response_time_max = doc['responseTimeMax'] end if !updated_doc['responseTimeMin'].nil? && updated_doc['responseTimeMin'] < doc['responseTimeMin'] response_time_min = updated_doc['responseTimeMin'] else response_time_min = doc['responseTimeMin'] end qps_ave = (updated_doc['counter'].inject(0.0){|r,i| r+=i } / updated_doc['counter'].size).round qps_max = updated_doc['counter'].max qps_min = updated_doc['counter'].min ok_ratio = ((updated_doc[count_name] - updated_doc['countOver']).to_f / updated_doc[count_name]).round(4) collection.update( {'_id' => key, count_key => doc[count_key], 'yyyymmdd' => doc['yyyymmdd'], 'hh' => doc['hh'], 'mm' => doc['mm']}, { '$set' => { 'responseTimeAve' => response_time_ave, 'responseTimeMax' => response_time_max, 'responseTimeMin' => response_time_min, 'qpsAve' => qps_ave, 'qpsMax' => qps_max, 'qpsMin' => qps_min, 'okRatio' => ok_ratio }} ) else collection.update( {'_id' => key, count_key => doc[count_key], 'yyyymmdd' => doc['yyyymmdd'], 'hh' => doc['hh'], 'mm' => doc['mm']}, {'$inc' => {count_name => doc[count_name]}}, {:upsert => true} ) end } rescue Mongo::OperationFailure => e raise e end end |
#shutdown ⇒ Object
This method is called when shutting down. Shutdown the thread and close sockets or files here.
71 72 73 74 |
# File 'lib/fluent/plugin/out_mongokpi.rb', line 71 def shutdown @client.db.connection.close super end |
#start ⇒ Object
This method is called when starting. Open sockets or files here.
64 65 66 67 |
# File 'lib/fluent/plugin/out_mongokpi.rb', line 64 def start super @client = get_client(@address, @connection_opts) end |
#write(chunk) ⇒ Object
This method is called every flush interval. write the buffer chunk to files or databases here. ‘chunk’ is a buffer chunk that includes multiple formatted events. You can use ‘data = chunk.read’ to get all events and ‘chunk.open {|io| … }’ to get IO object. def write(chunk)
data = chunk.read
print data
end optionally, you can use chunk.msgpack_each to deserialize objects. def write(chunk)
chunk.msgpack_each {|(tag,time,record)|
}
end
101 102 103 104 |
# File 'lib/fluent/plugin/out_mongokpi.rb', line 101 def write(chunk) doc_hash = get_insert_doc_hash(@kpi_type, chunk, @time_key, @time_format, @count_key, @count_name) insert(@kpi_type, @collection, @count_key, @count_name, doc_hash) end |