sensu-plugin 2.6.1 → 2.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b1dfcce5f32c8ed80fd9da0ff34dfac0b6b3a999
4
- data.tar.gz: 19da9ae23b0a0706f05c84ffb50ad4415433c383
3
+ metadata.gz: 3933344846102d33bd69622c4134b3a53c687cce
4
+ data.tar.gz: 1cd15f9096d8e72250b667d425317739f30a0ed3
5
5
  SHA512:
6
- metadata.gz: 3eb2f697572579bf4af26638f38a1e849ca873150412ccff731f6f770f4342b5d98919c4b7bc6bcd0a9c488c938ebd67e042bfd143dcc3c157084a6a7cf4789a
7
- data.tar.gz: 0bb69e9bb18ddf8eaea85f5f07643ff45cc230b4ca2510e84814c20dd7b1f87f52c6ea524cd62d5f7a06c11b2f835d90dcb45d8338e25780455cadb592ff7c62
6
+ metadata.gz: 4205c698be3548741f7942566b00453ed58c3f3550d5dc51a98644ec6fcca092f2a0b418baa95c463d4d84123344260cd5a85ecbccfcb0d8ab182f534a2a70d7
7
+ data.tar.gz: 591c9f4bce7d7e178c2bb6c5f99e7e2682625f56f9efb5b663661b2beb4a665ede9f9a130cb3133db1e0d9795e0f3686d6fc616dd97c05ad34ae968129d29bf6
data/lib/sensu-handler.rb CHANGED
@@ -9,6 +9,10 @@ module Sensu
9
9
  class Handler
10
10
  include Sensu::Plugin::Utils
11
11
  include Mixlib::CLI
12
+ option :map_v2_event_into_v1,
13
+ description: 'Enable 2.x to 1.4 event mapping. Alternatively set envvar SENSU_MAP_V2_EVENT_INTO_V1=1.',
14
+ boolean: true,
15
+ long: '--map-v2-event-into-v1'
12
16
 
13
17
  attr_accessor :argv
14
18
 
@@ -73,6 +77,14 @@ module Sensu
73
77
  if @@autorun
74
78
  handler = @@autorun.new
75
79
  handler.read_event(STDIN)
80
+
81
+ TRUTHY_VALUES = %w[1 t true yes y].freeze
82
+ automap = ENV['SENSU_MAP_V2_EVENT_INTO_V1'].to_s.downcase
83
+
84
+ if handler.config[:map_v2_event_into_v1] || TRUTHY_VALUES.include?(automap)
85
+ new_event = handler.map_v2_event_into_v1
86
+ handler.event = new_event
87
+ end
76
88
  handler.filter
77
89
  handler.handle
78
90
  end
data/lib/sensu-mutator.rb CHANGED
@@ -34,6 +34,10 @@ module Sensu
34
34
  class Mutator
35
35
  include Sensu::Plugin::Utils
36
36
  include Mixlib::CLI
37
+ option :map_v2_event_into_v1,
38
+ description: 'Enable 2.x to 1.4 event mapping. Alternatively set envvar SENSU_MAP_V2_EVENT_INTO_V1=1.',
39
+ boolean: true,
40
+ long: '--map-v2-event-into-v1'
37
41
 
38
42
  attr_accessor :argv
39
43
 
@@ -67,6 +71,15 @@ module Sensu
67
71
  return unless @@autorun
68
72
  mutator = @@autorun.new
69
73
  mutator.read_event(STDIN)
74
+
75
+ TRUTHY_VALUES = %w[1 t true yes y].freeze
76
+ automap = ENV['SENSU_MAP_V2_EVENT_INTO_V1'].to_s.downcase
77
+
78
+ if mutator.config[:map_v2_event_into_v1] || TRUTHY_VALUES.include?(automap)
79
+ new_event = mutator.map_v2_event_into_v1
80
+ mutator.event = new_event
81
+ end
82
+
70
83
  mutator.mutate
71
84
  mutator.dump
72
85
  end
data/lib/sensu-plugin.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module Sensu
2
2
  module Plugin
3
- VERSION = '2.6.1'.freeze
3
+ VERSION = '2.7.0'.freeze
4
4
  EXIT_CODES = {
5
5
  'OK' => 0,
6
6
  'WARNING' => 1,
@@ -23,6 +23,14 @@ module Sensu
23
23
  @settings ||= config_files.map { |f| load_config(f) }.reduce { |a, b| deep_merge(a, b) }
24
24
  end
25
25
 
26
+ def event
27
+ @event
28
+ end
29
+
30
+ def event=(value)
31
+ @event = value
32
+ end
33
+
26
34
  def read_event(file)
27
35
  @event = ::JSON.parse(file.read)
28
36
  @event['occurrences'] ||= 1
@@ -33,6 +41,92 @@ module Sensu
33
41
  exit 0
34
42
  end
35
43
 
44
+ ##
45
+ # Helper method to convert Sensu 2.0 event into Sensu 1.4 event
46
+ # This is here to help keep Sensu Plugin community handlers working
47
+ # until they natively support 2.0
48
+ # Takes 2.0 event json object as argument
49
+ # Returns event with 1.4 mapping included
50
+ #
51
+ # Note:
52
+ # The 1.4 mapping overwrites some attributes so the resulting event cannot
53
+ # be used in a 2.0 workflow. The top level boolean attribute "v2_event_mapped_into_v1"
54
+ # will be set to true as a hint to indicate this is a mapped event object.
55
+ #
56
+ ##
57
+ def map_v2_event_into_v1(orig_event = nil)
58
+ orig_event ||= @event
59
+
60
+ # return orig_event if already mapped
61
+ return orig_event if orig_event['v2_event_mapped_into_v1']
62
+
63
+ # Deep copy of orig_event
64
+ event = Marshal.load(Marshal.dump(orig_event))
65
+
66
+ # Trigger mapping code if enity exists and client does not
67
+ client_missing = event['client'].nil? || event['client'].empty?
68
+ if event.key?('entity') && client_missing
69
+ ##
70
+ # create the client hash from the entity hash
71
+ ##
72
+ event['client'] = event['entity']
73
+
74
+ ##
75
+ # Fill in missing client attributes
76
+ ##
77
+ event['client']['name'] ||= event['entity']['id']
78
+ event['client']['subscribers'] ||= event['entity']['subscriptions']
79
+
80
+ ##
81
+ # Fill in renamed check attributes expected in 1.4 event
82
+ # subscribers, source
83
+ ##
84
+ event['check']['subscribers'] ||= event['check']['subscriptions']
85
+ event['check']['source'] ||= event['check']['proxy_entity_id']
86
+
87
+ ##
88
+ # Mimic 1.4 event action based on 2.0 event state
89
+ # action used in logs and fluentd plugins handlers
90
+ ##
91
+ action_state_mapping = {
92
+ 'flapping' => 'flapping',
93
+ 'passing' => 'resolve',
94
+ 'failing' => 'create'
95
+ }
96
+
97
+ state = event['check']['state'] || 'unknown::2.0_event'
98
+
99
+ # Attempt to map 2.0 event state to 1.4 event action
100
+ event['action'] ||= action_state_mapping[state.downcase]
101
+ # Fallback action is 2.0 event state
102
+ event['action'] ||= state
103
+
104
+ ##
105
+ # Mimic 1.4 event history based on 2.0 event history
106
+ # Note: This overwrites the same history attribute
107
+ # 2.x history is an array of hashes, each hash includes status
108
+ # 1.x history is an array of statuses
109
+ ##
110
+ if event['check']['history']
111
+ # Let's save the original history
112
+ history_v2 = Marshal.load(Marshal.dump(event['check']['history']))
113
+ event['check']['history_v2'] = history_v2
114
+ legacy_history = []
115
+ event['check']['history'].each do |h|
116
+ legacy_history << h['status'].to_i.to_s || '3'
117
+ end
118
+ event['check']['history'] = legacy_history
119
+ end
120
+
121
+ ##
122
+ # Setting flag indicating this function has already been called
123
+ ##
124
+ event['v2_event_mapped_into_v1'] = true
125
+ end
126
+ # return the updated event
127
+ event
128
+ end
129
+
36
130
  def net_http_req_class(method)
37
131
  case method.to_s.upcase
38
132
  when 'GET'
@@ -0,0 +1,18 @@
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
@@ -0,0 +1,24 @@
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
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: 2.6.1
4
+ version: 2.7.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: 2018-08-28 00:00:00.000000000 Z
12
+ date: 2018-09-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
@@ -116,9 +116,11 @@ files:
116
116
  - test/external_handler_argument_test.rb
117
117
  - test/external_handler_test.rb
118
118
  - test/external_metric_test.rb
119
+ - test/handle_2to1_test.rb
119
120
  - test/handle_api_request_test.rb
120
121
  - test/handle_filter_test.rb
121
122
  - test/handle_helper_test.rb
123
+ - test/mutator_2to1_test.rb
122
124
  - test/mutator_test.rb
123
125
  - test/test_helper.rb
124
126
  homepage: https://github.com/sensu-plugins/sensu-plugin
@@ -152,7 +154,9 @@ test_files:
152
154
  - test/test_helper.rb
153
155
  - test/handle_filter_test.rb
154
156
  - test/handle_helper_test.rb
157
+ - test/handle_2to1_test.rb
155
158
  - test/external_handler_argument_test.rb
159
+ - test/mutator_2to1_test.rb
156
160
  - test/deep_merge_test.rb
157
161
  - test/cast_bool_value_test.rb
158
162
  - test/external_handler_test.rb