-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sensu Handler shim for Sensu 2.0 event data. (#190)
* Added event_2to1 method to Utils and --enable-2.0-event option to base Handler class. New option makes it possible to use sensu-plugin based handlers with Sensu 2.0 events until handlers provide native 2.0 event support. * handle case when event['client'] is nil * Refactor event_2to1 utility function to take optional event argument * fix bad conditional for expected integer attribute * add test for --enable-2to1-mapping handler argument * fix unreleased changelog entry format * remove unneeded cornercase mapping due to now resolved attribute naming issue * fix for handle 2to1 test * Refactor function name from awkward 2to1 as per pr review * return orig_event if already mapped * fix argument to match refactor * make it possible to set env variable to attempt v2 into v1 mapping automatically * update changelog with more detail on added v2 -> v1 mapping support * add support for v2 -> v1 event mapping to mutator class, and update changelog * remove unneeded confusing unless conditional * fix for stray comma * refactor state to action mapping to a use a 1:1 hash lookup with a fallback if state is not in understood mapping * refactor history handling, save original history as history_v2. * extend 2to1 test coverage to include history
- Loading branch information
1 parent
c3a2508
commit 552f5fa
Showing
8 changed files
with
203 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#!/usr/bin/env ruby | ||
require 'sensu-handler' | ||
|
||
class Helpers < Sensu::Handler | ||
def handle | ||
puts event_summary | ||
end | ||
|
||
def api_request(*_args) | ||
nil | ||
end | ||
|
||
def stash_exists?(*_args) | ||
nil | ||
end | ||
|
||
def event_exists?(*_args) | ||
true | ||
end | ||
|
||
def event_summary | ||
client_name = @event['client']['name'] | ||
check_name = @event['check']['name'] | ||
source = @event['check']['source'] | ||
output = @event['check']['output'] | ||
total_state_change = @event['check']['total_state_change'] | ||
action = @event['action'] | ||
client_subscribers = @event['client']['subscribers'].join('|') | ||
check_subscribers = @event['client']['subscribers'].join('^') | ||
history = @event['check']['history'].join('') | ||
[client_name, check_name, source, output, total_state_change, action, client_subscribers, check_subscribers, history].join(' : ') | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"entity":{"id":"test_entity","subscriptions":["sub1","sub2","sub3"]},"check":{"name":"test_check","output":"test_output","subscriptions":["sub1","sub2","sub3"],"proxy_entity_id":"test_proxy","total_state_change":4,"state":"failing","history":[{"status":0,"executed":0},{"status":1,"executed":1},{"status":2,"executed":2},{"status":3,"executed":3},{"status":0,"executed":4}],"status":0}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require 'test_helper' | ||
require 'English' | ||
|
||
class TestHandle2to1 < MiniTest::Test | ||
include SensuPluginTestHelper | ||
|
||
def setup | ||
set_script 'external/handle-2to1 --map-v2-event-into-v1' | ||
end | ||
|
||
def test_2to1_enabled | ||
event = JSON.parse(fixture('basic_v2_event.json').read) | ||
expected = "test_entity : test_check : test_proxy : test_output : 4 : create : sub1|sub2|sub3 : sub1^sub2^sub3 : 01230\n" | ||
output = run_script_with_input(JSON.generate(event)) | ||
assert_equal(0, $CHILD_STATUS.exitstatus) | ||
assert_match(expected, output) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#!/usr/bin/env ruby | ||
require 'English' | ||
|
||
require 'test_helper' | ||
|
||
# Simple Heper to test mutator | ||
class TestMutatorHelpers < MiniTest::Test | ||
include SensuPluginTestHelper | ||
def test_base_2to1_mutator | ||
set_script 'external/mutator-trivial --map-v2-event-into-v1' | ||
event = JSON.parse(fixture('basic_v2_event.json').read) | ||
output = run_script_with_input(JSON.generate(event)) | ||
assert_equal(0, $CHILD_STATUS.exitstatus) | ||
assert_equal(event['entity']['id'], JSON.parse(output)['client']['name']) | ||
end | ||
|
||
def test_external_2to1_mutator | ||
set_script 'external/mutator-helpers --map-v2-event-into-v1' | ||
event = JSON.parse(fixture('basic_v2_event.json').read) | ||
output = run_script_with_input(JSON.generate(event)) | ||
assert_equal(0, $CHILD_STATUS.exitstatus) | ||
assert_equal(true, JSON.parse(output)['mutated']) | ||
end | ||
end |