class Flapjack::Data::Event

Constants

OPTIONAL_KEYS
REQUIRED_KEYS

type was a required key in v1, but is superfluous tags are now ignored, tags on the checks are used for rule matching

VALIDATIONS

Attributes

acknowledgement_id[R]
counter[RW]
details[R]
id[R]
id_hash[RW]
perfdata[R]
summary[R]

Public Class Methods

create_acknowledgements(queue, checks, opts = {}) click to toggle source
# File lib/flapjack/data/event.rb, line 153
def self.create_acknowledgements(queue, checks, opts = {})
  raise "Check(s) must be provided" if checks.nil?
  checks.each do |check|
    self.push(queue, 'state'              => 'acknowledgement',
                     'check'              => check.name,
                     'summary'            => opts[:summary],
                     'duration'           => opts[:duration],
                     'acknowledgement_id' => opts[:acknowledgement_id])
  end
end
new(attrs = {}) click to toggle source
# File lib/flapjack/data/event.rb, line 178
def initialize(attrs = {})
  @id = if attrs['entity'].nil?
    attrs['check']
  else
    "#{attrs['entity']}:#{attrs['check']}"
  end
  [:state, :time, :initial_failure_delay, :repeat_failure_delay,
   :initial_recovery_delay, :summary, :details, :perfdata,
   :acknowledgement_id, :duration].each do |key|

    instance_variable_set("@#{key.to_s}", attrs[key.to_s])
  end
  # summary, details and perfdata are optional. set to nil if they only contain whitespace
  ['@summary', '@details', '@perfdata'].each do |inst|
    value = instance_variable_get(inst)
    v = if value.is_a?(String)
      vs = value.strip
      vs.empty? ? nil : vs
    else
      nil
    end
    instance_variable_set(inst, v)
  end
end
parse_and_validate(raw, opts = {}) click to toggle source
# File lib/flapjack/data/event.rb, line 79
def self.parse_and_validate(raw, opts = {})
  errors = []
  if parsed = Flapjack.load_json(raw)
    if parsed.is_a?(Hash)
      errors = validation_errors_for_hash(parsed, opts)
    else
      errors << "Event must be a JSON hash, see http://flapjack.io/docs/1.0/development/DATA_STRUCTURES#event-queue"
    end
    return [parsed, errors]
  end
  [nil, errors]
rescue JSON::JSONError => e
  errors << "Error deserialising event json: #{e}, raw json: #{raw.inspect}"
  [nil, errors]
end
pending_count(queue) click to toggle source

Provide a count of the number of events on the queue to be processed.

# File lib/flapjack/data/event.rb, line 149
def self.pending_count(queue)
  Flapjack.redis.llen(queue)
end
push(queue, event) click to toggle source

creates, or modifies, an event object and adds it to the events list in redis

'entity'                => entity,
'check'                 => check,
'time'                  => timestamp,
'initial_failure_delay' => initial_failure_delay,
'repeat_failure_delay'  => repeat_failure_delay,
'initial_recovery_delay' => initial_recovery_delay,
'type'                  => 'service',
'state'                 => state,
'summary'               => check_output,
'details'               => check_long_output,
'perfdata'              => perf_data
# File lib/flapjack/data/event.rb, line 130
def self.push(queue, event)
  event['time'] = Time.now.to_i if event['time'].nil?

  begin
    event_json = Flapjack.dump_json(event)
  rescue JSON::JSONError => e
    Flapjack.logger.warn("Error serialising event json: #{e}, event: #{event.inspect}")
    event_json = nil
  end

  if event_json
    Flapjack.redis.multi do
      Flapjack.redis.lpush(queue, event_json)
      Flapjack.redis.lpush("#{queue}_actions", "+")
    end
  end
end
test_notifications(queue, checks, opts = {}) click to toggle source
# File lib/flapjack/data/event.rb, line 164
def self.test_notifications(queue, checks, opts = {})
  raise "Check(s) must be provided" if checks.nil?
  condition = opts[:condition] || 'critical'
  unless Flapjack::Data::Condition.unhealthy.keys.include?(condition)
    raise "Condition must be a problem"
  end
  checks.each do |check|
    self.push(queue, 'state'              => "test_notifications #{condition}",
                     'check'              => check.name,
                     'summary'            => opts[:summary],
                     'details'            => opts[:details])
  end
end
validation_errors_for_hash(hash, opts = {}) click to toggle source
# File lib/flapjack/data/event.rb, line 95
def self.validation_errors_for_hash(hash, opts = {})
  errors = []
  missing_keys = REQUIRED_KEYS.select {|k|
    !hash.has_key?(k) || hash[k].nil? || hash[k].empty?
  }
  unless missing_keys.empty?
    errors << "Event hash has missing keys '#{missing_keys.join('\', \'')}'"
  end

  unknown_keys =  hash.keys - (REQUIRED_KEYS + OPTIONAL_KEYS)
  unless unknown_keys.empty?
    errors << "Event hash has unknown key(s) '#{unknown_keys.join('\', \'')}'"
  end

  if errors.empty?
    errors += VALIDATIONS.keys.inject([]) {|ret,vk|
      ret << "Event #{VALIDATIONS[vk]}" unless vk.call(hash)
      ret
    }
  end
  errors
end

Public Instance Methods

dump() click to toggle source
# File lib/flapjack/data/event.rb, line 217
def dump
  return @dump unless @dump.nil?
  @dump = "#{id}, #{state}"
  @dump << ", #{summary}" unless summary.nil?
  @dump << ", #{Time.at(time).to_s}" unless time.nil?
end
state() click to toggle source
# File lib/flapjack/data/event.rb, line 203
def state
  return unless @state
  @state.downcase
end