Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/interferon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,15 @@ def build_alerts_queue(hosts, alerts, groups)
people += (groups[g] || [])
end

# Convenience for alerts that select groups dynamically, since
# those groups may not exist. Allow configuration of one or more
# fallback_groups, that are static and are known to exist
if people.empty?
alert[:notify][:fallback_groups].each do |g|
people += (groups[g] || [])
end
end

# queue the alert up for creation; we clone the alert to save the current state
alerts_generated[alert[:name]] = [alert.clone, people]
break if alert[:applies] == :once
Expand Down
4 changes: 4 additions & 0 deletions lib/interferon/alert_dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ def groups(v = nil, &block)
get_or_set(:@groups, v, block, [])
end

def fallback_groups(v = nil, &block)
get_or_set(:@fallback_groups, v, block, [])
end

def audit(v = nil, &block)
get_or_set(:@audit, v, block, false)
end
Expand Down
50 changes: 50 additions & 0 deletions spec/lib/interferon_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,42 @@
end
end

context '#build_alerts_queue(hosts, alerts, groups)' do
let(:interferon) { Interferon::Interferon.new({ 'processes' => 0 }, true) }

before do
allow_any_instance_of(MockAlert).to receive(:evaluate)
end

it 'adds people to alerts when notify.groups{} is used' do
added = create_test_alert('name1', 'testquery3', '')
groups = { 'a' => %w[foo bar] }
result = interferon.build_alerts_queue(['host'], [added], groups)
expect(result[0]['name1'][1]).to eq(%w[foo bar].to_set)
end

context 'when notify.fallback_groups{} is used' do
it 'adds fallback people to alerts when no other groups are found' do
added = create_test_alert_with_groups_and_fallback_groups(
['nonexistent_group'],
['fallback_group']
)
groups = { 'fallback_group' => %w[biz baz] }
result = interferon.build_alerts_queue(['host'], [added], groups)
expect(result[0]['name1'][1]).to eq(%w[biz baz].to_set)
end

it 'does not add fallback people to alerts when other groups are found' do
added = create_test_alert_with_groups_and_fallback_groups(['group'], ['fallback_group'])
groups = {}
groups['group'] = %w[foo bar]
groups['fallback_groups'] = %w[biz baz]
result = interferon.build_alerts_queue(['host'], [added], groups)
expect(result[0]['name1'][1]).to eq(%w[foo bar].to_set)
end
end
end

context 'dry_run_update_alerts_on_destination' do
let(:interferon) { Interferon::Interferon.new({ 'processes' => 0 }, true) }

Expand Down Expand Up @@ -436,4 +472,18 @@ def create_test_alert(name, datadog_query, message, options = {})

MockAlert.new(alert_dsl)
end

def create_test_alert_with_groups_and_fallback_groups(groups = [], fallback_groups = [])
alert_dsl = AlertDSL.new({})

notify_dsl = NotifyDSL.new({})
notify_dsl.groups(groups)
notify_dsl.fallback_groups(fallback_groups)
alert_dsl.instance_variable_set(:@notify, notify_dsl)

alert_dsl.name('name1')
alert_dsl.applies(true)

MockAlert.new(alert_dsl)
end
end