Class: RedisAlerting::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_alerting/engine.rb

Instance Method Summary collapse

Constructor Details

#initialize(config, redis, log) ⇒ Engine

Returns a new instance of Engine.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/redis_alerting/engine.rb', line 3

def initialize(config, redis, log)
  @config     = config
  @active_set = "#{@config[:namespace]}#{@config[:separator]}active"
  @redis      = redis
  @log        = log
  @extrema    = {}

  check_redis
  build_extrema

  @log.info "Redis Alerting Engine Initialized"
  @log.info "Publishing alert information on channel: #{@config[:channel]}"
  @log.info "Currently active alerts are in the key: #{@active_set}"

  @log.info "Working with sources:"
  @config[:sources].each do |name, source|
    @log.info "  #{name}: #{source}"
  end

  @log.info "Working with extrema:"
  @extrema.each do |name, ex|
    @log.info "  #{name}:"
    @log.info "    min: #{ex[:min]}"
    @log.info "    max: #{ex[:max]}"
  end

end

Instance Method Details

#runObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/redis_alerting/engine.rb', line 31

def run
  @config[:sources].each do |name, source|

    # get the readings and alert ranges
    min = @redis.get(@extrema[name][:min]).to_i
    max = @redis.get(@extrema[name][:max]).to_i
    value = @redis.get(source).to_i

    @log.debug "Checking #{name} (min #{min}) (max #{max}): #{value}"

    # silently ignore
    next if max.nil? or min.nil? or value.nil?

    # check for alert conditions
    if condition = out_of_range?(value, min, max)
      add_alert_for(name, condition, value, min, max)
      next
    end

    # if we got to here the alert conditions are not present anymore
    # so we should remove the alert if it exists
    remove_if_alert_exists name, value, min, max
  end
end