sensu-plugin 2.7.1 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/mutator-sensu-go-into-ruby.rb +11 -0
- data/lib/sensu-handler.rb +6 -6
- data/lib/sensu-mutator.rb +6 -6
- data/lib/sensu-plugin.rb +1 -1
- data/lib/sensu-plugin/utils.rb +63 -21
- data/test/handle_go_to_1_test.rb +18 -0
- data/test/mutator_go_to_1_test.rb +27 -0
- metadata +13 -11
- data/test/handle_2to1_test.rb +0 -18
- data/test/mutator_2to1_test.rb +0 -24
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9d5b33d233fbb2d5f7713bd9f56f534611a06ff5
|
4
|
+
data.tar.gz: db3da41917254f47c0f3ce0a43e55137178847b2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2cf4e4376598660f9af91a1c99f2484b40c8c642309d9261024b2a5cef672e8b804d0f179b8ee4e994ef7be800fc9198cd8a1cd2ca725f00a2142f5ddbcc3bdf
|
7
|
+
data.tar.gz: cce62ca82e559153d8f8190276696676a22691a73500524b483ec32ddbef660711cb65b167ba71542d29b3dcd7a9df782129cf5492cd67712be2a7e44d6b8770
|
@@ -0,0 +1,11 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'sensu-mutator'
|
4
|
+
|
5
|
+
class Helper < Sensu::Mutator
|
6
|
+
def mutate
|
7
|
+
STDERR.puts 'WARNING: mutator-sensu-go-into-ruby.rb is meant for primarily for development.'
|
8
|
+
STDERR.puts 'Please update your handlers to be compatible with Sensu Go events.'
|
9
|
+
@event = map_go_event_into_ruby
|
10
|
+
end
|
11
|
+
end
|
data/lib/sensu-handler.rb
CHANGED
@@ -9,10 +9,10 @@ module Sensu
|
|
9
9
|
class Handler
|
10
10
|
include Sensu::Plugin::Utils
|
11
11
|
include Mixlib::CLI
|
12
|
-
option :
|
13
|
-
description: 'Enable
|
12
|
+
option :map_go_event_into_ruby,
|
13
|
+
description: 'Enable Sensu Go to Sensu Ruby event mapping. Alternatively set envvar SENSU_MAP_GO_EVENT_INTO_RUBY=1.',
|
14
14
|
boolean: true,
|
15
|
-
long: '--map-
|
15
|
+
long: '--map-go-event-into-ruby'
|
16
16
|
|
17
17
|
attr_accessor :argv
|
18
18
|
|
@@ -79,10 +79,10 @@ module Sensu
|
|
79
79
|
handler.read_event(STDIN)
|
80
80
|
|
81
81
|
TRUTHY_VALUES = %w[1 t true yes y].freeze
|
82
|
-
automap = ENV['
|
82
|
+
automap = ENV['SENSU_MAP_GO_EVENT_INTO_RUBY'].to_s.downcase
|
83
83
|
|
84
|
-
if handler.config[:
|
85
|
-
new_event = handler.
|
84
|
+
if handler.config[:map_go_event_into_ruby] || TRUTHY_VALUES.include?(automap)
|
85
|
+
new_event = handler.map_go_event_into_ruby
|
86
86
|
handler.event = new_event
|
87
87
|
end
|
88
88
|
handler.filter
|
data/lib/sensu-mutator.rb
CHANGED
@@ -34,10 +34,10 @@ module Sensu
|
|
34
34
|
class Mutator
|
35
35
|
include Sensu::Plugin::Utils
|
36
36
|
include Mixlib::CLI
|
37
|
-
option :
|
38
|
-
description: 'Enable
|
37
|
+
option :map_go_event_into_ruby,
|
38
|
+
description: 'Enable Sensu Go to Sensu Ruby event mapping. Alternatively set envvar SENSU_MAP_GO_EVENT_INTO_RUBY=1.',
|
39
39
|
boolean: true,
|
40
|
-
long: '--map-
|
40
|
+
long: '--map-go-event-into-ruby'
|
41
41
|
|
42
42
|
attr_accessor :argv
|
43
43
|
|
@@ -73,10 +73,10 @@ module Sensu
|
|
73
73
|
mutator.read_event(STDIN)
|
74
74
|
|
75
75
|
TRUTHY_VALUES = %w[1 t true yes y].freeze
|
76
|
-
automap = ENV['
|
76
|
+
automap = ENV['SENSU_MAP_GO_EVENT_INTO_RUBY'].to_s.downcase
|
77
77
|
|
78
|
-
if mutator.config[:
|
79
|
-
new_event = mutator.
|
78
|
+
if mutator.config[:map_go_event_into_ruby] || TRUTHY_VALUES.include?(automap)
|
79
|
+
new_event = mutator.map_go_event_into_ruby
|
80
80
|
mutator.event = new_event
|
81
81
|
end
|
82
82
|
|
data/lib/sensu-plugin.rb
CHANGED
data/lib/sensu-plugin/utils.rb
CHANGED
@@ -42,23 +42,26 @@ module Sensu
|
|
42
42
|
end
|
43
43
|
|
44
44
|
##
|
45
|
-
# Helper method to convert Sensu
|
45
|
+
# Helper method to convert Sensu Go event into Sensu Ruby event
|
46
46
|
# This is here to help keep Sensu Plugin community handlers working
|
47
|
-
# until they natively support
|
48
|
-
# Takes
|
49
|
-
# Returns event with
|
47
|
+
# until they natively support Go events
|
48
|
+
# Takes Go event json object as argument
|
49
|
+
# Returns event with Sensu Ruby mapping included
|
50
50
|
#
|
51
51
|
# Note:
|
52
|
-
# The
|
53
|
-
# be used in a
|
52
|
+
# The Sensu Ruby mapping overwrites some attributes so the resulting event cannot
|
53
|
+
# be used in a Sensu Go workflow. The top level boolean attribute "go_event_mapped_into_ruby"
|
54
54
|
# will be set to true as a hint to indicate this is a mapped event object.
|
55
55
|
#
|
56
56
|
##
|
57
|
-
def
|
57
|
+
def map_go_event_into_ruby(orig_event = nil, map_annotation = nil)
|
58
58
|
orig_event ||= @event
|
59
59
|
|
60
|
+
map_annotation ||= ENV['SENSU_MAP_ANNOTATION'] if ENV['SENSU_MAP_ANNOTATION']
|
61
|
+
map_annotation ||= 'sensu.io.json_attributes'
|
62
|
+
|
60
63
|
# return orig_event if already mapped
|
61
|
-
return orig_event if orig_event['
|
64
|
+
return orig_event if orig_event['go_event_mapped_into_ruby']
|
62
65
|
|
63
66
|
# Deep copy of orig_event
|
64
67
|
event = Marshal.load(Marshal.dump(orig_event))
|
@@ -74,18 +77,38 @@ module Sensu
|
|
74
77
|
##
|
75
78
|
# Fill in missing client attributes
|
76
79
|
##
|
77
|
-
|
80
|
+
|
78
81
|
event['client']['subscribers'] ||= event['entity']['subscriptions']
|
79
82
|
|
80
83
|
##
|
81
|
-
#
|
84
|
+
# Map entity metadata into client attributes
|
85
|
+
# Note this is potentially destructive as it may overwrite existing client attributes.
|
86
|
+
##
|
87
|
+
if event['entity'].key?('metadata')
|
88
|
+
##
|
89
|
+
# Map metadata annotation 'name' to client name attribute
|
90
|
+
##
|
91
|
+
event['client']['name'] ||= event['entity']['metadata']['name']
|
92
|
+
|
93
|
+
##
|
94
|
+
# Map special metadata annotation defined in map_annotation as json string and convert to client attributes
|
95
|
+
# Note this is potentially destructive as it may overwrite existing client attributes.
|
96
|
+
##
|
97
|
+
if event['entity']['metadata'].key?('annotations') && event['entity']['metadata']['annotations'].key?(map_annotation)
|
98
|
+
json_hash = JSON.parse(event['entity']['metadata']['annotations'][map_annotation])
|
99
|
+
event['client'].update(json_hash)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
##
|
104
|
+
# Fill in renamed check attributes expected in Sensu Ruby event
|
82
105
|
# subscribers, source
|
83
106
|
##
|
84
107
|
event['check']['subscribers'] ||= event['check']['subscriptions']
|
85
|
-
event['check']['source'] ||= event['check']['
|
108
|
+
event['check']['source'] ||= event['check']['proxy_entity_name']
|
86
109
|
|
87
110
|
##
|
88
|
-
# Mimic
|
111
|
+
# Mimic Sensu Ruby event action based on Go event state
|
89
112
|
# action used in logs and fluentd plugins handlers
|
90
113
|
##
|
91
114
|
action_state_mapping = {
|
@@ -94,23 +117,23 @@ module Sensu
|
|
94
117
|
'failing' => 'create'
|
95
118
|
}
|
96
119
|
|
97
|
-
state = event['check']['state'] || 'unknown::
|
120
|
+
state = event['check']['state'] || 'unknown::go_event'
|
98
121
|
|
99
|
-
# Attempt to map
|
122
|
+
# Attempt to map Go event state to Sensu Ruby event action
|
100
123
|
event['action'] ||= action_state_mapping[state.downcase]
|
101
|
-
# Fallback action is
|
124
|
+
# Fallback action is Go event state
|
102
125
|
event['action'] ||= state
|
103
126
|
|
104
127
|
##
|
105
|
-
# Mimic
|
128
|
+
# Mimic Sensu Ruby event history based on Go event history
|
106
129
|
# Note: This overwrites the same history attribute
|
107
|
-
#
|
108
|
-
#
|
130
|
+
# Go history is an array of hashes, each hash includes status
|
131
|
+
# Sensu Ruby history is an array of statuses
|
109
132
|
##
|
110
133
|
if event['check']['history']
|
111
134
|
# Let's save the original history
|
112
|
-
|
113
|
-
event['check']['
|
135
|
+
original_history = Marshal.load(Marshal.dump(event['check']['history']))
|
136
|
+
event['check']['original_history'] = original_history
|
114
137
|
legacy_history = []
|
115
138
|
event['check']['history'].each do |h|
|
116
139
|
legacy_history << h['status'].to_i.to_s || '3'
|
@@ -118,10 +141,29 @@ module Sensu
|
|
118
141
|
event['check']['history'] = legacy_history
|
119
142
|
end
|
120
143
|
|
144
|
+
##
|
145
|
+
# Map check metadata into client attributes
|
146
|
+
# Note this is potentially destructive as it may overwrite existing check attributes.
|
147
|
+
##
|
148
|
+
if event['check'].key?('metadata')
|
149
|
+
##
|
150
|
+
# Map metadata annotation 'name' to client name attribute
|
151
|
+
##
|
152
|
+
event['check']['name'] ||= event['check']['metadata']['name']
|
153
|
+
|
154
|
+
##
|
155
|
+
# Map special metadata annotation defined in map_annotation as json string and convert to check attributes
|
156
|
+
# Note this is potentially destructive as it may overwrite existing check attributes.
|
157
|
+
##
|
158
|
+
if event['check']['metadata'].key?('annotations') && event['check']['metadata']['annotations'].key?(map_annotation)
|
159
|
+
json_hash = JSON.parse(event['check']['metadata']['annotations'][map_annotation])
|
160
|
+
event['check'].update(json_hash)
|
161
|
+
end
|
162
|
+
end
|
121
163
|
##
|
122
164
|
# Setting flag indicating this function has already been called
|
123
165
|
##
|
124
|
-
event['
|
166
|
+
event['go_event_mapped_into_ruby'] = true
|
125
167
|
end
|
126
168
|
# return the updated event
|
127
169
|
event
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'English'
|
3
|
+
|
4
|
+
class TestHandleGoto1 < MiniTest::Test
|
5
|
+
include SensuPluginTestHelper
|
6
|
+
|
7
|
+
def setup
|
8
|
+
set_script 'external/handle-go-to-ruby --map-go-event-into-ruby'
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_go_to_ruby_enabled
|
12
|
+
event = JSON.parse(fixture('basic_go_event.json').read)
|
13
|
+
expected = "test_entity : top_value : test_check : test_proxy : test_output : 4 : create : sub1|sub2|sub3 : sub1^sub2^sub3 : potato : 01230\n"
|
14
|
+
output = run_script_with_input(JSON.generate(event))
|
15
|
+
assert_equal(0, $CHILD_STATUS.exitstatus)
|
16
|
+
assert_match(expected, output)
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'English'
|
3
|
+
|
4
|
+
require 'test_helper'
|
5
|
+
|
6
|
+
# Simple Heper to test mutator
|
7
|
+
class TestMutatorHelpers < MiniTest::Test
|
8
|
+
include SensuPluginTestHelper
|
9
|
+
def test_base_go_to_1_mutator
|
10
|
+
set_script 'external/mutator-trivial --map-go-event-into-ruby'
|
11
|
+
event = JSON.parse(fixture('basic_go_event.json').read)
|
12
|
+
output = run_script_with_input(JSON.generate(event))
|
13
|
+
assert_equal(0, $CHILD_STATUS.exitstatus)
|
14
|
+
assert_equal(event['entity']['metadata']['name'], JSON.parse(output)['client']['name'])
|
15
|
+
assert_equal('top_value', JSON.parse(output)['client']['top'])
|
16
|
+
assert_equal('nested01_value', JSON.parse(output)['client']['test_json']['nested01'])
|
17
|
+
assert_equal('nested02_value', JSON.parse(output)['client']['test_json']['nested02'])
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_external_go_to_1_mutator
|
21
|
+
set_script 'external/mutator-helpers --map-go-event-into-ruby'
|
22
|
+
event = JSON.parse(fixture('basic_go_event.json').read)
|
23
|
+
output = run_script_with_input(JSON.generate(event))
|
24
|
+
assert_equal(0, $CHILD_STATUS.exitstatus)
|
25
|
+
assert_equal(true, JSON.parse(output)['mutated'])
|
26
|
+
end
|
27
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sensu-plugin
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Decklin Foster
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2018-12-05 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: json
|
@@ -29,16 +29,16 @@ dependencies:
|
|
29
29
|
name: mixlib-cli
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
31
31
|
requirements:
|
32
|
-
- - "
|
32
|
+
- - ">="
|
33
33
|
- !ruby/object:Gem::Version
|
34
|
-
version:
|
34
|
+
version: 1.5.0
|
35
35
|
type: :runtime
|
36
36
|
prerelease: false
|
37
37
|
version_requirements: !ruby/object:Gem::Requirement
|
38
38
|
requirements:
|
39
|
-
- - "
|
39
|
+
- - ">="
|
40
40
|
- !ruby/object:Gem::Version
|
41
|
-
version:
|
41
|
+
version: 1.5.0
|
42
42
|
- !ruby/object:Gem::Dependency
|
43
43
|
name: rake
|
44
44
|
requirement: !ruby/object:Gem::Requirement
|
@@ -99,10 +99,12 @@ description: Plugins and helper libraries for Sensu, a monitoring framework
|
|
99
99
|
email:
|
100
100
|
- decklin@red-bean.com
|
101
101
|
- portertech@gmail.com
|
102
|
-
executables:
|
102
|
+
executables:
|
103
|
+
- mutator-sensu-go-into-ruby.rb
|
103
104
|
extensions: []
|
104
105
|
extra_rdoc_files: []
|
105
106
|
files:
|
107
|
+
- bin/mutator-sensu-go-into-ruby.rb
|
106
108
|
- lib/sensu-handler.rb
|
107
109
|
- lib/sensu-mutator.rb
|
108
110
|
- lib/sensu-plugin.rb
|
@@ -116,11 +118,11 @@ files:
|
|
116
118
|
- test/external_handler_argument_test.rb
|
117
119
|
- test/external_handler_test.rb
|
118
120
|
- test/external_metric_test.rb
|
119
|
-
- test/handle_2to1_test.rb
|
120
121
|
- test/handle_api_request_test.rb
|
121
122
|
- test/handle_filter_test.rb
|
123
|
+
- test/handle_go_to_1_test.rb
|
122
124
|
- test/handle_helper_test.rb
|
123
|
-
- test/
|
125
|
+
- test/mutator_go_to_1_test.rb
|
124
126
|
- test/mutator_test.rb
|
125
127
|
- test/test_helper.rb
|
126
128
|
homepage: https://github.com/sensu-plugins/sensu-plugin
|
@@ -153,11 +155,11 @@ test_files:
|
|
153
155
|
- test/external_check_test.rb
|
154
156
|
- test/test_helper.rb
|
155
157
|
- test/handle_filter_test.rb
|
158
|
+
- test/mutator_go_to_1_test.rb
|
156
159
|
- test/handle_helper_test.rb
|
157
|
-
- test/handle_2to1_test.rb
|
158
160
|
- test/external_handler_argument_test.rb
|
159
|
-
- test/mutator_2to1_test.rb
|
160
161
|
- test/deep_merge_test.rb
|
161
162
|
- test/cast_bool_value_test.rb
|
162
163
|
- test/external_handler_test.rb
|
164
|
+
- test/handle_go_to_1_test.rb
|
163
165
|
- test/mutator_test.rb
|
data/test/handle_2to1_test.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
require 'English'
|
3
|
-
|
4
|
-
class TestHandle2to1 < MiniTest::Test
|
5
|
-
include SensuPluginTestHelper
|
6
|
-
|
7
|
-
def setup
|
8
|
-
set_script 'external/handle-2to1 --map-v2-event-into-v1'
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_2to1_enabled
|
12
|
-
event = JSON.parse(fixture('basic_v2_event.json').read)
|
13
|
-
expected = "test_entity : test_check : test_proxy : test_output : 4 : create : sub1|sub2|sub3 : sub1^sub2^sub3 : 01230\n"
|
14
|
-
output = run_script_with_input(JSON.generate(event))
|
15
|
-
assert_equal(0, $CHILD_STATUS.exitstatus)
|
16
|
-
assert_match(expected, output)
|
17
|
-
end
|
18
|
-
end
|
data/test/mutator_2to1_test.rb
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
require 'English'
|
3
|
-
|
4
|
-
require 'test_helper'
|
5
|
-
|
6
|
-
# Simple Heper to test mutator
|
7
|
-
class TestMutatorHelpers < MiniTest::Test
|
8
|
-
include SensuPluginTestHelper
|
9
|
-
def test_base_2to1_mutator
|
10
|
-
set_script 'external/mutator-trivial --map-v2-event-into-v1'
|
11
|
-
event = JSON.parse(fixture('basic_v2_event.json').read)
|
12
|
-
output = run_script_with_input(JSON.generate(event))
|
13
|
-
assert_equal(0, $CHILD_STATUS.exitstatus)
|
14
|
-
assert_equal(event['entity']['id'], JSON.parse(output)['client']['name'])
|
15
|
-
end
|
16
|
-
|
17
|
-
def test_external_2to1_mutator
|
18
|
-
set_script 'external/mutator-helpers --map-v2-event-into-v1'
|
19
|
-
event = JSON.parse(fixture('basic_v2_event.json').read)
|
20
|
-
output = run_script_with_input(JSON.generate(event))
|
21
|
-
assert_equal(0, $CHILD_STATUS.exitstatus)
|
22
|
-
assert_equal(true, JSON.parse(output)['mutated'])
|
23
|
-
end
|
24
|
-
end
|