diff --git a/lib/interferon.rb b/lib/interferon.rb index 4957abb..2c44753 100644 --- a/lib/interferon.rb +++ b/lib/interferon.rb @@ -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 diff --git a/lib/interferon/alert_dsl.rb b/lib/interferon/alert_dsl.rb index f8436d6..0a59af4 100644 --- a/lib/interferon/alert_dsl.rb +++ b/lib/interferon/alert_dsl.rb @@ -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 diff --git a/spec/lib/interferon_spec.rb b/spec/lib/interferon_spec.rb index 9fc04bb..a0bbd57 100644 --- a/spec/lib/interferon_spec.rb +++ b/spec/lib/interferon_spec.rb @@ -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) } @@ -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