Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

logging overhaul

  • Loading branch information...
commit e83f4d177d7874ac3f4ae067b10fd2885b492ac4 1 parent 53ded70
Pete Fritchman authored
2  cepmon.cfg
@@ -3,6 +3,8 @@
3 3
4 4 listen "127.0.0.1", 8989
5 5
  6 +#log :path => cepmon.log, :debug => false
  7 +
6 8 amqp_input :host => "127.0.0.1",
7 9 :port => "5672",
8 10 :user => "user",
6 lib/cepmon/alert.rb
@@ -36,12 +36,6 @@ def reason
36 36 end
37 37
38 38 public
39   - def to_s
40   - "#{Time.at(@data[:timestamp])} [ALERT] cluster=#{@data[:cluster]}/host=#{@data[:host]} " +
41   - "| name=#{@data[:name]} | value=#{@data[:value]} | statement=#{@data[:statement]}"
42   - end # def to_s
43   -
44   - public
45 39 def to_json(*args)
46 40 @data.merge({
47 41 :started => @started,
25 lib/cepmon/config.rb
... ... @@ -1,8 +1,10 @@
  1 +require "logger"
  2 +
1 3 class CEPMon
2 4 class Config
3 5 attr_reader :amqp
4 6 attr_reader :host
5   - attr_reader :log_path
  7 + attr_reader :logger
6 8 attr_reader :port
7 9 attr_reader :statements
8 10
@@ -12,12 +14,26 @@ def initialize(cfg_file="cepmon.cfg")
12 14 @statements = {}
13 15 @host = "0.0.0.0"
14 16 @port = 8989
15   - @log_path = nil
  17 + @logger = Logger.new(STDOUT)
  18 + @logger.progname = "cepmon"
  19 + @logger.level = Logger::INFO
16 20
17 21 instance_eval(File.read(cfg_file))
18 22 end # def initialize
19 23
20 24 private
  25 + def log(params)
  26 + if params[:path]
  27 + @logger = Logger.new(params[:path])
  28 + @logger.level = Logger::INFO
  29 + end
  30 +
  31 + if params[:debug]
  32 + @logger.level = Logger::DEBUG
  33 + end
  34 + end
  35 +
  36 + private
21 37 def verify_present(params, required)
22 38 required.each do |key|
23 39 return false unless params.member?(key)
@@ -200,10 +216,5 @@ def listen(host, port = 8989)
200 216 @port = port.to_i
201 217 end
202 218
203   - private
204   - def logfile(path)
205   - @log_path = path
206   - end
207   -
208 219 end # class Config
209 220 end # class CEPMon
14 lib/cepmon/eventlistener.rb
@@ -28,12 +28,6 @@ def initialize(engine, config)
28 28 else
29 29 @exchange = nil
30 30 end
31   -
32   - if @config.log_path
33   - @log = File.open(@config.log_path, "a+")
34   - else
35   - @log = nil
36   - end
37 31 end
38 32
39 33 public
@@ -47,8 +41,7 @@ def update(new_events, old_events, statement, provider)
47 41 vars[:statement] = statement.getName
48 42 vars[:timestamp] = timestamp
49 43
50   - puts "event: #{statement.getName} @#{timestamp} (#{Time.at(timestamp.to_i)}) (engine.uptime=#{@engine.uptime}): #{vars.collect { |h, k| [h, k.inspect].join("=") }.join(" ")}"
51   - $stderr.puts "event vars=#{vars.inspect}"
  44 + @config.logger.debug("CEP event: #{statement.getName}: @#{timestamp} (#{Time.at(timestamp.to_i)}) (engine.uptime=#{@engine.uptime}): #{vars.inspect}")
52 45 record_alert(vars, statement.getName, @exchange)
53 46 end
54 47 end # def update
@@ -69,8 +62,9 @@ def record_alert(vars, statement_name, exchange)
69 62 @alerts[key].update(vars)
70 63 else
71 64 alert = CEPMon::Alert.new(vars)
  65 + @config.logger.info("NEW ALERT: #{statement_name}: cluster=#{alert.data[:cluster]} host=#{alert.data[:host]} reason=#{alert.reason}")
  66 + @config.logger.debug("NEW ALERT: json: #{alert.to_json}")
72 67 exchange.publish(alert.to_json) if exchange
73   - @log.puts alert.to_json if @log
74 68 @alerts[key] = alert
75 69 @history << alert
76 70 end
@@ -81,7 +75,7 @@ def expire_alerts
81 75 now = @engine.runtime.getCurrentTime / 1000
82 76 @alerts.each do |key, alert|
83 77 next unless alert.expired?(now)
84   - puts "ALERT EXPIRING: #{alert.to_json}"
  78 + @config.logger.info("ALERT EXPIRING: #{alert.data[:statement]} cluster=#{alert.data[:cluster]} host=#{alert.data[:host]}")
85 79 @alerts.delete(key)
86 80 end
87 81 end
3  lib/cepmon/mon.rb
@@ -46,13 +46,14 @@ def run(args)
46 46 end
47 47 end
48 48
49   - puts "connecting to rabbitmq..."
  49 + @config.logger.info("connecting to rabbitmq...")
50 50 amqp.start
51 51 queue = amqp.queue("cepmon-#{Process.pid}", :auto_delete => true)
52 52 exchange = amqp.exchange(@config.amqp[:exchange_metrics],
53 53 :type => :topic,
54 54 :durable => true)
55 55 queue.bind(exchange)
  56 + @config.logger.info("connected; bound queue cepmon-#{Process.pid} to topic #{config.amqp[:exchange_metrics]}")
56 57
57 58 Thread.new do
58 59 begin
2  test.cfg
@@ -3,6 +3,8 @@
3 3
4 4 listen "127.0.0.1", 8989
5 5
  6 +log :debug => true
  7 +
6 8 threshold('test_host_gt', 'test.foo',
7 9 :operator => '>',
8 10 :threshold => 1.0,

0 comments on commit e83f4d1

Please sign in to comment.
Something went wrong with that request. Please try again.