sensu-plugins-json-mutator 0.1.0 → 0.2.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: 84175624864453c65ec0f511e94cdcfc19c6c360
4
- data.tar.gz: 235b149a1a3dbbf7b682cebf366f973ec41556fa
3
+ metadata.gz: 4bf3678e20709f8edea8527f78613936c67be799
4
+ data.tar.gz: 854350ddc124f082ca742e5b08979275f960c4db
5
5
  SHA512:
6
- metadata.gz: a3eb9db8f3889ef8e7e7cd79566a49e9f9550a44617bd242f712c6c8885ca207c3fd5874b027cb703b4acde990a5acb9feeea05d7643d4232615195a84531b52
7
- data.tar.gz: 6533c92d23fde011ecf8518d98d780ba801217407d1ee39a90cf6d060ea02778c3649b8cb140496a237ab2e611af08b59479ac63c75d59d95f6aff3faf27de75
6
+ metadata.gz: 26ca01d37cbd76995846a265508dd841f6a122ff493537b9d2094a4815abf93c8b0e840dd88c0c5eec5f820c7ccba8cd1023ae4bba671743177ebc03b5fc6b0e
7
+ data.tar.gz: 3498508e951c3655452cdaa9629bb80af2c737998076f4489083d2015ee7c324340a37a433d1a834e973583b46f29ea314ce772c94d2f21599c22741da274cee
data/bin/mutator-json.rb CHANGED
@@ -16,25 +16,30 @@
16
16
  # gem: sensu-plugin
17
17
 
18
18
  require 'json'
19
+ require 'sensu-plugin'
20
+ require 'sensu-plugins-json-mutator/mutator'
19
21
 
20
- GRAPHITE = /([\w\.]+)\s(.+).*/ # sample.key 42.13 1462521982
21
- STATSD = /([\w\.]+):(.*)\|.*/ # sample.key:42.13|kv
22
+ class JSONMutator < Sensu::Mutator
23
+ GRAPHITE = /([\w\.]+)\s(.+).*/ # sample.key 42.13 1462521982
24
+ STATSD = /([\w\.]+):(.*)\|.*/ # sample.key:42.13|kv
22
25
 
23
- def parse_lines(lines, regexp)
24
- kv = {}
26
+ def mutate
27
+ lines = @event['check']['output'].lines
25
28
 
26
- lines.each do |line|
27
- result = line.match(regexp)
28
- kv[result[1]] = result[2].to_f if result
29
+ @event['check']['output'] = parse_lines(lines, GRAPHITE) if lines.first =~ GRAPHITE
30
+ @event['check']['output'] = parse_lines(lines, STATSD) if lines.first =~ STATSD
29
31
  end
30
32
 
31
- kv
32
- end
33
+ private
33
34
 
34
- event = JSON.parse(STDIN.read)
35
- lines = event['check']['output'].lines
35
+ def parse_lines(lines, regexp)
36
+ kv = {}
36
37
 
37
- event['check']['output'] = parse_lines(lines, GRAPHITE) if lines.first =~ GRAPHITE
38
- event['check']['output'] = parse_lines(lines, STATSD) if lines.first =~ STATSD
38
+ lines.each do |line|
39
+ result = line.match(regexp)
40
+ kv[result[1]] = result[2].to_f if result
41
+ end
39
42
 
40
- puts JSON.dump(event)
43
+ kv
44
+ end
45
+ end
@@ -0,0 +1,49 @@
1
+ require 'json'
2
+ require 'sensu-plugin/utils'
3
+ require 'mixlib/cli'
4
+
5
+ module Sensu
6
+ class Mutator
7
+ include Sensu::Plugin::Utils
8
+ include Mixlib::CLI
9
+
10
+ attr_accessor :argv
11
+
12
+ def initialize(argv = ARGV)
13
+ super()
14
+ self.argv = parse_options(argv)
15
+ end
16
+
17
+ def mutate
18
+ ## Override this, be sure any changes are made to @event
19
+ nil
20
+ end
21
+
22
+ def dump
23
+ puts JSON.dump(@event)
24
+ end
25
+
26
+ # This works just like Plugin::CLI's autorun.
27
+ @@autorun = self
28
+ class << self
29
+ def method_added(name)
30
+ if name == :mutate
31
+ @@autorun = self
32
+ end
33
+ end
34
+ end
35
+
36
+ def self.disable_autorun
37
+ @@autorun = false
38
+ end
39
+
40
+ at_exit do
41
+ if @@autorun
42
+ mutator = @@autorun.new
43
+ mutator.read_event(STDIN)
44
+ mutator.mutate
45
+ mutator.dump
46
+ end
47
+ end
48
+ end
49
+ end
@@ -1,3 +1,3 @@
1
1
  module SensuPluginsJSONMutator
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sensu-plugins-json-mutator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sensu-Plugins and contributors
@@ -92,6 +92,7 @@ files:
92
92
  - README.md
93
93
  - bin/mutator-json.rb
94
94
  - lib/sensu-plugins-json-mutator.rb
95
+ - lib/sensu-plugins-json-mutator/mutator.rb
95
96
  - lib/sensu-plugins-json-mutator/version.rb
96
97
  homepage: https://github.com/artemeff/sensu-plugins-json-mutator
97
98
  licenses: