Class: NagiosResque::Check

Inherits:
Object
  • Object
show all
Defined in:
lib/nagios_resque/check.rb

Defined Under Namespace

Classes: ResqueRedis

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Check

Returns a new instance of Check.



3
4
5
6
7
8
9
10
11
12
# File 'lib/nagios_resque/check.rb', line 3

def initialize(options)
  @host      = options[:host]      || "localhost"
  @port      = options[:port]      || 6379
  @namespace = options[:namespace] || "resque"
  @job       = options[:job]       || 'NagiosResque::Job'
  @warning   = options[:warn]
  @critical  = options[:crit]

  @redis = ResqueRedis.new(:host => @host, :port => @port, :namespace => @namespace)
end

Instance Method Details

#critical?Boolean

Returns:

  • (Boolean)


30
31
32
33
# File 'lib/nagios_resque/check.rb', line 30

def critical?
  return true unless passed_time
  @critical && (passed_time.nil? || @critical.first < passed_time)
end

#critical_messageObject



84
85
86
# File 'lib/nagios_resque/check.rb', line 84

def critical_message
  "haven't run in #{@critical.first} seconds"
end

#last_run_atObject



35
36
37
38
39
# File 'lib/nagios_resque/check.rb', line 35

def last_run_at
  if time = @redis.get(NAGIOS_RESQUE_TIMESTAMP_KEY)
    Integer(time)
  end
end

#ok_messageObject



76
77
78
# File 'lib/nagios_resque/check.rb', line 76

def ok_message
  "last run at #{Time.at(last_run_at).strftime('%Y-%m-%d %H:%M:%S %z')}"
end

#passed_timeObject



41
42
43
44
45
46
47
48
49
50
# File 'lib/nagios_resque/check.rb', line 41

def passed_time
  # need to cache because there is small time difference between checks
  return @passed_time if defined?(@passed_time)
  @passed_time =
    if time = last_run_at
      Integer(Time.now - time)
    else
      nil
    end
end

#payloadObject

It is just (:class => @job, :args => []).to_json Don’t want to depend on resque gem which itself has many dependecies



16
17
18
# File 'lib/nagios_resque/check.rb', line 16

def payload
  %Q({"class":"#{@job}","args":[]})
end

#requeueObject



20
21
22
23
24
# File 'lib/nagios_resque/check.rb', line 20

def requeue
  @redis.sadd 'queues', 'high'
  @redis.lrem 'queue:high', 0, payload
  @redis.rpush 'queue:high', payload
end

#warning?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/nagios_resque/check.rb', line 26

def warning?
  @warning && (passed_time.nil? || @warning.include?(passed_time))
end

#warning_messageObject



80
81
82
# File 'lib/nagios_resque/check.rb', line 80

def warning_message
  "haven't run in #{@warning.last} seconds"
end