Module: LanGrove::LogMonitor

Defined in:
lib/langrove/ext/log_monitor.rb,
lib/langrove/ext/log_monitor.rb

Defined Under Namespace

Modules: Sampler

Instance Method Summary collapse

Instance Method Details

#log_monitor_config(root, hash) ⇒ Object



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
# File 'lib/langrove/ext/log_monitor.rb', line 229

def log_monitor_config( root, hash )

  @@sampler = Sampler.init( root )
  
  # 
  # <hash> as the state storage (capsule)
  #
  
  #
  # <config> as hash, as loaded from the
  # relevant cofig branch.
  # 
  # :success: regex match
  # :error: regex match
  # :known_errors:
  #    :short_name: regex_match
  #    :nother_name: nother match
  # :versions:
  #    :version_name: regex match
  # 
  # Errors that match :error: but no :known_error: are
  # tracked as unknown errors.
  # 
  # Errors that match a :known_errors: are tracked by name
  # 
  # Success match will reset all errors.... (GONE!)
  # and set :success: to true on the capsule.
  # 
  # last_run_at is updated with each log message. 
  # 

  raise LanGrove::DaemonConfigException.new(
  
    "#{self.class} requires :success:, :error:, and :known_errors: config."
  
  ) unless (
  
    config.has_key?( :success ) and
    config.has_key?( :error ) and
    config.has_key?( :known_errors )
  
  )
  
  # 
  # Mandatory config 
  # 
  @success_match = config[:success]
  @error_match = config[:error]
  
  
  #
  # Optional config
  #
  @known_error_matchers = config[:known_errors]
  @known_error_matchers ||= {}
  
  @version_matchers = config[:versions]
  @version_matchers ||= {}
  
  @known_error_matchers.each do |name, matcher|
    
    raise LanGrove::DaemonConfigException.new(

      "#{self.class} :known_error:#{name}: has no regex matcher"

    ) if matcher.nil?
    
  end
  
  #
  # Move unknown_errors that are now in config
  # to known_errors.
  #
  
  hash['unknown_errors'].each do |error_name, count|
    
    known_error = known_error?( error_name )
    
    unless known_error.nil?
      
      hash['known_errors'] = {} if hash['known_errors'].nil?
      
      hash['known_errors'][known_error] = count
      
      hash['unknown_errors'].delete error_name
      
    end
    
  end if (
    
    not hash.nil? and 
    hash.has_key?( 'unknown_errors' )
  
  )
  
  
  #
  # Clear known_errors from state that
  # are no longer in config
  # 
  
  hash['known_errors'].each_key do |error_name|
    
    unless @known_error_matchers.has_key?( error_name )
    
      hash['known_errors'].delete error_name
    
    end
    
  end if (
  
    not hash.nil? and 
    hash.has_key?( 'known_errors' )
    
  )
  
end

#log_monitor_event(hash, event) ⇒ Object



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/langrove/ext/log_monitor.rb', line 347

def log_monitor_event( hash, event )
  
  changed = false
  
  hash['known_errors'] = {} if hash['known_errors'].nil?
  hash['unknown_errors'] = {} if hash['unknown_errors'].nil?
  
  hash['last_ran_at'] = event[:timestamp]
  
  changed = update_versions( hash, event )
  
  success_match = /#{@success_match}/.match( event[:event] )
  
  unless success_match.nil?
    
    hash['success'] = true
    
    hash['success_at'] = event[:timestamp]
    
    changed = log_monitor_reset( hash )
    
    return changed
    
  end
  
  error_match = /#{@error_match}/.match( event[:event] )
  error_type = 'unknown_errors'
  error_name = nil
  
  unless error_match.nil?
    
    #
    # Success to false on error
    #
    
    hash['success'] = false
    
    error_name = event[:event]
    
    known_error = known_error?( error_name )
    
    unless known_error.nil?
      
      error_type = 'known_errors'
      error_name = known_error
      
    end
  
  end
  
  unless error_name.nil?
    
    if hash[ error_type ].nil?
      
      hash[ error_type ] = {}
      
    end
    
    if hash[ error_type ][ error_name ].nil?
      
      hash[ error_type ][ error_name ] = 0
      
    end
    
    hash[ error_type ][ error_name ] = 
      hash[ error_type ][ error_name ] + 1
    
    changed = true
    
  end
  
  return changed
  
end

#log_monitor_reset(hash) ⇒ Object



422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/langrove/ext/log_monitor.rb', line 422

def log_monitor_reset( hash )
  
  hash.delete 'unknown_errors'
  
  hash['unknown_errors'] = {}
  
  hash.delete 'known_errors'
  
  hash['known_errors'] = {}
  
  return true
  
end