Class: Sensu::Handler
Constant Summary
collapse
- @@autorun =
This works just like Plugin::CLI’s autorun.
self
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#config_files, #load_config, #net_http_req_class, #read_event, #settings
Constructor Details
#initialize(argv = ARGV) ⇒ Handler
Returns a new instance of Handler.
14
15
16
17
|
# File 'lib/sensu-handler.rb', line 14
def initialize(argv = ARGV)
super()
self.argv = parse_options(argv)
end
|
Instance Attribute Details
#argv ⇒ Object
Returns the value of attribute argv.
12
13
14
|
# File 'lib/sensu-handler.rb', line 12
def argv
@argv
end
|
Class Method Details
.method_added(name) ⇒ Object
39
40
41
42
43
|
# File 'lib/sensu-handler.rb', line 39
def method_added(name)
if name == :handle
@@autorun = self
end
end
|
Instance Method Details
#api_request(method, path) {|req| ... } ⇒ Object
60
61
62
63
64
65
66
67
68
|
# File 'lib/sensu-handler.rb', line 60
def api_request(method, path, &blk)
http = Net::HTTP.new(settings['api']['host'], settings['api']['port'])
req = net_http_req_class(method).new(path)
if settings['api']['user'] && settings['api']['password']
req.basic_auth(settings['api']['user'], settings['api']['password'])
end
yield(req) if block_given?
http.request(req)
end
|
#bail(msg) ⇒ Object
55
56
57
58
|
# File 'lib/sensu-handler.rb', line 55
def bail(msg)
puts msg + ': ' + @event['client']['name'] + '/' + @event['check']['name']
exit 0
end
|
#event_exists?(client, check) ⇒ Boolean
114
115
116
|
# File 'lib/sensu-handler.rb', line 114
def event_exists?(client, check)
api_request(:GET, '/event/' + client + '/' + check).code == '200'
end
|
#filter ⇒ Object
Filters exit the proccess if the event should not be handled. Implementation of the default filters is below.
28
29
30
31
32
33
|
# File 'lib/sensu-handler.rb', line 28
def filter
filter_disabled
filter_repeated
filter_silenced
filter_dependencies
end
|
#filter_dependencies ⇒ Object
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/sensu-handler.rb', line 118
def filter_dependencies
if @event['check'].has_key?('dependencies')
if @event['check']['dependencies'].is_a?(Array)
@event['check']['dependencies'].each do |dependency|
begin
timeout(2) do
check, client = dependency.split('/').reverse
if event_exists?(client || @event['client']['name'], check)
bail 'check dependency event exists'
end
end
rescue Timeout::Error
puts 'timed out while attempting to query the sensu api for an event'
end
end
end
end
end
|
#filter_disabled ⇒ Object
70
71
72
73
74
|
# File 'lib/sensu-handler.rb', line 70
def filter_disabled
if @event['check']['alert'] == false
bail 'alert disabled'
end
end
|
#filter_repeated ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/sensu-handler.rb', line 76
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 && @event['action'] == 'create'
number = refresh.fdiv(interval).to_i
unless number == 0 || @event['occurrences'] % number == 0
bail 'only handling every ' + number.to_s + ' occurrences'
end
end
end
|
#filter_silenced ⇒ Object
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/sensu-handler.rb', line 95
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(2) do
if stash_exists?(path)
bail scope + ' alerts silenced'
end
end
rescue Timeout::Error
puts 'timed out while attempting to query the sensu api for a stash'
end
end
end
|
#handle ⇒ Object
Implementing classes should override this.
21
22
23
|
# File 'lib/sensu-handler.rb', line 21
def handle
puts 'ignoring event -- no handler defined'
end
|
#stash_exists?(path) ⇒ Boolean
91
92
93
|
# File 'lib/sensu-handler.rb', line 91
def stash_exists?(path)
api_request(:GET, '/stash' + path).code == '200'
end
|