class Sensu::Handler

Constants

TRUTHY_VALUES

Attributes

argv[RW]

Public Class Methods

disable_autorun() click to toggle source
# File lib/sensu-handler.rb, line 74
def self.disable_autorun
  @@autorun = false
end
method_added(name) click to toggle source
# File lib/sensu-handler.rb, line 69
def method_added(name)
  @@autorun = self if name == :handle
end
new(argv = ARGV) click to toggle source
Calls superclass method
# File lib/sensu-handler.rb, line 21
def initialize(argv = ARGV)
  super()
  self.argv = parse_options(argv)
end

Public Instance Methods

bail(msg) click to toggle source
# File lib/sensu-handler.rb, line 109
def bail(msg)
  client_name = @event['client']['name'] || 'error:no-client-name'
  check_name = @event['check']['name'] || 'error:no-check-name'
  puts "#{msg}: #{client_name}/#{check_name}"
  exit 0
end
deprecated_filtering_enabled?() click to toggle source

Evaluates whether the event should be processed by any of the filter methods in this library. Defaults to true, i.e. deprecated filters are run by default.

@return [TrueClass, FalseClass]

# File lib/sensu-handler.rb, line 52
def deprecated_filtering_enabled?
  @event['check'].fetch('enable_deprecated_filtering', false).to_s == 'true'
end
deprecated_occurrence_filtering_enabled?() click to toggle source

Evaluates whether the event should be processed by the filter_repeated method. Defaults to true, i.e. filter_repeated will filter events by default.

@return [TrueClass, FalseClass]

# File lib/sensu-handler.rb, line 61
def deprecated_occurrence_filtering_enabled?
  @event['check'].fetch('enable_deprecated_occurrence_filtering', false).to_s == 'true'
end
event_exists?(client, check) click to toggle source
# File lib/sensu-handler.rb, line 168
def event_exists?(client, check)
  api_request(:GET, '/events/' + client + '/' + check).code == '200'
end
event_summary(trim_at = 100) click to toggle source

Helpers and filters.

# File lib/sensu-handler.rb, line 97
def event_summary(trim_at = 100)
  summary = @event['check']['notification'] || @event['check']['description']
  if summary.nil?
    source = @event['check']['source'] || @event['client']['name']
    event_context = [source, @event['check']['name']].join('/')
    output = @event['check']['output'].chomp
    output = output.length > trim_at ? output[0..trim_at] + '...' : output
    summary = [event_context, output].join(' : ')
  end
  summary
end
filter() click to toggle source

Filters exit the proccess if the event should not be handled.

Filtering events is deprecated and will be removed in a future release.

# File lib/sensu-handler.rb, line 36
def filter
  return unless deprecated_filtering_enabled?
  puts 'warning: event filtering in sensu-plugin is deprecated, see http://bit.ly/sensu-plugin'
  filter_disabled
  filter_silenced
  filter_dependencies
  return unless deprecated_occurrence_filtering_enabled?
  puts 'warning: occurrence filtering in sensu-plugin is deprecated, see http://bit.ly/sensu-plugin'
  filter_repeated
end
filter_dependencies() click to toggle source
# File lib/sensu-handler.rb, line 172
def filter_dependencies
  return unless @event['check'].key?('dependencies')
  return unless @event['check']['dependencies'].is_a?(Array)
  @event['check']['dependencies'].each do |dependency|
    begin
      next if dependency.to_s.empty?
      Timeout.timeout(2) do
        check, client = dependency.split('/').reverse
        if event_exists?(client || @event['client']['name'], check)
          bail 'check dependency event exists'
        end
      end
    rescue Errno::ECONNREFUSED
      puts 'connection refused attempting to query the sensu api for an event'
    rescue Timeout::Error
      puts 'timed out while attempting to query the sensu api for an event'
    end
  end
end
filter_disabled() click to toggle source
# File lib/sensu-handler.rb, line 116
def filter_disabled
  bail 'alert disabled' if @event['check']['alert'] == false
end
filter_repeated() click to toggle source
# File lib/sensu-handler.rb, line 120
def filter_repeated
  defaults = {
    'occurrences' => 1,
    'interval' => 30,
    'refresh' => 1800
  }

  if settings['sensu_plugin'].is_a?(Hash)
    defaults.merge!(settings['sensu_plugin'])
  end

  occurrences = (@event['check']['occurrences'] || defaults['occurrences']).to_i
  interval = (@event['check']['interval'] || defaults['interval']).to_i
  refresh = (@event['check']['refresh'] || defaults['refresh']).to_i
  if @event['occurrences'] < occurrences
    bail 'not enough occurrences'
  end
  return unless @event['occurrences'] > occurrences && @event['action'] == 'create'
  number = refresh.fdiv(interval).to_i
  return if number.zero? || ((@event['occurrences'] - occurrences) % number).zero?
  bail 'only handling every ' + number.to_s + ' occurrences'
end
filter_silenced() click to toggle source
# File lib/sensu-handler.rb, line 147
def filter_silenced
  stashes = [
    ['client', '/silence/' + @event['client']['name']],
    ['check', '/silence/' + @event['client']['name'] + '/' + @event['check']['name']],
    ['check', '/silence/all/' + @event['check']['name']]
  ]
  stashes.each do |(scope, path)|
    begin
      Timeout.timeout(5) do
        if stash_exists?(path)
          bail scope + ' alerts silenced'
        end
      end
    rescue Errno::ECONNREFUSED
      puts 'connection refused attempting to query the sensu api for a stash'
    rescue Timeout::Error
      puts 'timed out while attempting to query the sensu api for a stash'
    end
  end
end
handle() click to toggle source

Implementing classes should override this.

# File lib/sensu-handler.rb, line 28
def handle
  puts 'ignoring event -- no handler defined'
end
stash_exists?(path) click to toggle source
# File lib/sensu-handler.rb, line 143
def stash_exists?(path)
  api_request(:GET, '/stash' + path).code == '200'
end