sensu-plugin 0.0.6 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/sensu-plugin.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module Sensu
2
2
  module Plugin
3
- VERSION = "0.0.6"
3
+ VERSION = "0.1.0"
4
4
  EXIT_CODES = {
5
5
  'OK' => 0,
6
6
  'WARNING' => 1,
@@ -15,8 +15,12 @@ module Sensu
15
15
  end
16
16
  end
17
17
 
18
- def format_output(status, output)
19
- "#{self.class.check_name}: #{status} - #{output}"
18
+ def message(msg)
19
+ @message = msg
20
+ end
21
+
22
+ def output(msg=@message)
23
+ puts "#{self.class.check_name} #{@status}" + (msg ? ": #{msg}" : "")
20
24
  end
21
25
 
22
26
  end
@@ -11,8 +11,7 @@ module Sensu
11
11
  # Implementing classes should override this to produce appropriate
12
12
  # output for their handler.
13
13
 
14
- def format_output(status, output)
15
- output
14
+ def output(*args)
16
15
  end
17
16
 
18
17
  # This will define 'ok', 'warning', 'critical', and 'unknown'
@@ -20,7 +19,8 @@ module Sensu
20
19
 
21
20
  Sensu::Plugin::EXIT_CODES.each do |status, code|
22
21
  define_method(status.downcase) do |*args|
23
- puts format_output(status, *args)
22
+ @status = status
23
+ output(*args)
24
24
  exit(code)
25
25
  end
26
26
  end
@@ -52,7 +52,7 @@ module Sensu
52
52
  rescue SystemExit => e
53
53
  exit e.status
54
54
  rescue Exception => e
55
- self.new.critical "Check failed to run: #{e}"
55
+ self.new.critical "Check failed to run: #{e.message}, #{e.backtrace}"
56
56
  end
57
57
  self.new.warning "Check did not exit! You should call an exit code method."
58
58
  end
@@ -4,11 +4,23 @@ require 'json'
4
4
  module Sensu
5
5
  module Plugin
6
6
  class Metric
7
- class CLI < Sensu::Plugin::CLI
7
+ class CLI
8
8
 
9
- def format_output(status, output)
10
- output[:timestamp] ||= Time.now.to_i
11
- ::JSON.generate(output)
9
+ class JSON < Sensu::Plugin::CLI
10
+ def output(obj=nil)
11
+ unless obj.nil? || obj.is_a?(String) || obj.is_a?(Exception)
12
+ obj['timestamp'] ||= Time.now.to_i
13
+ puts ::JSON.generate(obj)
14
+ end
15
+ end
16
+ end
17
+
18
+ class Graphite < Sensu::Plugin::CLI
19
+ def output(path=nil, value=nil, timestamp=Time.now.to_i)
20
+ unless path.nil? || path.is_a?(Exception) || value.nil?
21
+ puts [path, value, timestamp].join("\t")
22
+ end
23
+ end
12
24
  end
13
25
 
14
26
  end
@@ -6,17 +6,6 @@ module Sensu
6
6
  module Procs
7
7
 
8
8
  class << self
9
- def get_procs
10
- `which tasklist`; $? == 0 ? `tasklist` : `ps aux`
11
- end
12
-
13
- def find_proc(name)
14
- get_procs.split("\n").find {|ln| ln.include?(name) }
15
- end
16
-
17
- def find_proc_regex(pat)
18
- get_procs.split("\n").find {|ln| ln =~ pat }
19
- end
20
9
  end
21
10
 
22
11
  end
@@ -1,18 +1,11 @@
1
- #!/usr/bin/env ruby
2
-
3
1
  require 'rubygems'
4
2
  require 'minitest/autorun'
5
3
 
6
4
  class TestCheckExternal < MiniTest::Unit::TestCase
5
+ include SensuPluginTestHelper
7
6
 
8
7
  def setup
9
- @script = File.join(File.dirname(__FILE__), 'external/check-options')
10
- end
11
-
12
- def run_script(*args)
13
- IO.popen([@script] + args, 'r+') do |child|
14
- child.read
15
- end
8
+ set_script 'external/check-options'
16
9
  end
17
10
 
18
11
  def test_ok
@@ -35,6 +28,11 @@ class TestCheckExternal < MiniTest::Unit::TestCase
35
28
  assert $?.exitstatus == 3
36
29
  end
37
30
 
31
+ def test_override
32
+ output = run_script '-O'
33
+ assert $?.exitstatus == 0 && !output.include?('argv =')
34
+ end
35
+
38
36
  def test_fallthrough
39
37
  run_script
40
38
  assert $?.exitstatus == 1
@@ -1,21 +1,12 @@
1
- #!/usr/bin/env ruby
2
-
3
1
  require 'rubygems'
4
2
  require 'minitest/autorun'
5
3
  require 'json'
6
4
 
7
5
  class TestHandlerExternal < MiniTest::Unit::TestCase
6
+ include SensuPluginTestHelper
8
7
 
9
8
  def setup
10
- @script = File.join(File.dirname(__FILE__), 'external/handle-nofilter')
11
- end
12
-
13
- def run_script(event)
14
- output = IO.popen(@script, 'r+') do |child|
15
- child.puts JSON.generate(event)
16
- child.close_write
17
- child.read
18
- end
9
+ set_script 'external/handle-nofilter'
19
10
  end
20
11
 
21
12
  def test_handled
@@ -24,13 +15,13 @@ class TestHandlerExternal < MiniTest::Unit::TestCase
24
15
  'check' => { 'name' => 'test' },
25
16
  'occurrences' => 1,
26
17
  }
27
- output = run_script(event)
18
+ output = run_script_with_input(JSON.generate(event))
28
19
  assert $?.exitstatus == 0 && output =~ /Event:.*test/
29
20
  end
30
21
 
31
22
  def test_missing_keys
32
23
  event = {}
33
- output = run_script(event)
24
+ output = run_script_with_input(JSON.generate(event))
34
25
  assert $?.exitstatus == 0 && output =~ /Event:/
35
26
  end
36
27
 
@@ -0,0 +1,30 @@
1
+ require 'rubygems'
2
+ require 'minitest/autorun'
3
+
4
+ class TestMetricExternal < MiniTest::Unit::TestCase
5
+ include SensuPluginTestHelper
6
+
7
+ def setup
8
+ set_script 'external/trivial-metric'
9
+ end
10
+
11
+ def test_ok
12
+ output = JSON.parse(run_script)
13
+ assert $?.exitstatus == 0 && output.key?('timestamp')
14
+ end
15
+
16
+ end
17
+
18
+ class TestGraphiteMetricExternal < MiniTest::Unit::TestCase
19
+ include SensuPluginTestHelper
20
+
21
+ def setup
22
+ set_script 'external/multi-output'
23
+ end
24
+
25
+ def test_multi
26
+ lines = run_script.split("\n")
27
+ assert lines.size == 2 && lines.all? {|line| line.split("\t").size == 3 }
28
+ end
29
+
30
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,24 @@
1
+ require 'rubygems'
2
+ require 'minitest/autorun'
3
+
4
+ module SensuPluginTestHelper
5
+
6
+ def set_script(script)
7
+ @script = File.join(File.dirname(__FILE__), script)
8
+ end
9
+
10
+ def run_script(*args)
11
+ IO.popen([@script] + args, 'r+') do |child|
12
+ child.read
13
+ end
14
+ end
15
+
16
+ def run_script_with_input(input, *args)
17
+ IO.popen([@script] + args, 'r+') do |child|
18
+ child.puts input
19
+ child.close_write
20
+ child.read
21
+ end
22
+ end
23
+
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: 0.0.6
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-30 00:00:00.000000000 Z
12
+ date: 2012-01-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
16
- requirement: &70176373322360 !ruby/object:Gem::Requirement
16
+ requirement: &70146182369580 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70176373322360
24
+ version_requirements: *70146182369580
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: mixlib-cli
27
- requirement: &70176373321840 !ruby/object:Gem::Requirement
27
+ requirement: &70146182367580 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: 1.1.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70176373321840
35
+ version_requirements: *70146182367580
36
36
  description: Plugins and helper libraries for Sensu, a monitoring framework
37
37
  email:
38
38
  - decklin@red-bean.com
@@ -48,6 +48,8 @@ files:
48
48
  - lib/sensu-plugin.rb
49
49
  - test/external_check_test.rb
50
50
  - test/external_handler_test.rb
51
+ - test/external_metric_test.rb
52
+ - test/helper.rb
51
53
  homepage: https://github.com/sonian/sensu-plugin
52
54
  licenses:
53
55
  - MIT
@@ -76,3 +78,5 @@ summary: Sensu Plugins
76
78
  test_files:
77
79
  - test/external_check_test.rb
78
80
  - test/external_handler_test.rb
81
+ - test/external_metric_test.rb
82
+ - test/helper.rb