Class: Sensu::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/sensu-handler.rb

Constant Summary collapse

CONFIGS =

Unfortunately, we need to reimplement config loading. I’m not sure there’s a good way to allow overriding these paths.

['/etc/sensu/config.json']
@@autorun =

This works just like Plugin::CLI’s autorun.

self

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.method_added(name) ⇒ Object



26
27
28
29
30
# File 'lib/sensu-handler.rb', line 26

def method_added(name)
  if name == :handle
    @@autorun = self
  end
end

Instance Method Details

#api_request(*path) ⇒ Object



72
73
74
75
# File 'lib/sensu-handler.rb', line 72

def api_request(*path)
  http = Net::HTTP.new(settings['api']['host'], settings['api']['port'])
  http.request(Net::HTTP::Get.new(path.join('/')))
end

#bail(msg) ⇒ Object

Helpers and filters



67
68
69
70
# File 'lib/sensu-handler.rb', line 67

def bail(msg)
  puts msg + ': ' + @event['client']['name'] + '/' + @event['check']['name']
  exit 0
end

#filterObject

Filters exit the proccess if the event should not be handled. Implementation of the default filters is below.



16
17
18
19
20
# File 'lib/sensu-handler.rb', line 16

def filter
  filter_disabled
  filter_repeated unless @event['action'] == 'resolve'
  filter_silenced
end

#filter_disabledObject



77
78
79
80
81
# File 'lib/sensu-handler.rb', line 77

def filter_disabled
  if @event['check']['alert'] == false
    bail 'alert disabled'
  end
end

#filter_repeatedObject



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/sensu-handler.rb', line 83

def filter_repeated
  occurrences = @event['check']['occurrences'] || 1
  interval = @event['check']['interval'] || 30
  refresh = @event['check']['refresh'] || 1800
  if @event['occurrences'] < occurrences
    bail 'not enough occurrences'
  end
  if @event['occurrences'] > occurrences
    n = refresh.fdiv(interval).to_i
    bail 'only repeating alert every ' + n.to_s + ' occurrences' unless @event['occurrences'] % n == 0
  end
end

#filter_silencedObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/sensu-handler.rb', line 96

def filter_silenced
  begin
    timeout(3) do
      if api_request('/stash/silence', @event['client']['name']).code == '200'
        bail 'client alerts silenced'
      end
      if api_request('/stash/silence', @event['client']['name'], @event['check']['name']).code == '200'
        bail 'check alerts silenced'
      end
    end
  rescue Timeout::Error
    puts 'Timed out while attempting to query the Sensu API for stashes'
  end
end

#handleObject

Implementing classes should override this.



9
10
11
# File 'lib/sensu-handler.rb', line 9

def handle
  puts 'ignoring event -- no handler defined'
end

#load_config(filename) ⇒ Object



38
39
40
# File 'lib/sensu-handler.rb', line 38

def load_config(filename)
  JSON.parse(File.open(filename, 'r').read) rescue Hash.new
end

#read_event(file) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/sensu-handler.rb', line 46

def read_event(file)
  begin
    @event = ::JSON.parse(file.read)
    @event['occurrences'] ||= 1
    @event['check'] ||= Hash.new
    @event['client'] ||= Hash.new
  rescue => e
    puts 'Error reading event: ' + e.message
    exit 0
  end
end

#settingsObject



42
43
44
# File 'lib/sensu-handler.rb', line 42

def settings
  @settings ||= CONFIGS.map {|f| load_config(f) }.reduce {|a, b| a.deep_merge(b) }
end