sensu-plugin 0.0.1

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.
@@ -0,0 +1,48 @@
1
+ require 'json'
2
+
3
+ module Sensu
4
+ class Handler
5
+
6
+ # Implementing classes should override this.
7
+
8
+ def handle(event)
9
+ puts 'ignoring event -- no handler defined'
10
+ end
11
+
12
+ # Overriding filtering logic is optional. Returns truthy if the
13
+ # event should be handled and falsy if it should not.
14
+
15
+ def filter(event)
16
+ if event['check']['alert'] == false
17
+ puts 'alert disabled -- filtered event ' + short_name(event)
18
+ exit 0
19
+ end
20
+ refresh = (60.fdiv(event['check']['interval']) * 30).to_i
21
+ event['occurrences'] == 1 || event['occurrences'] % refresh == 0
22
+ end
23
+
24
+ def short_name(event)
25
+ event['client']['name'] + '/' + event['check']['name']
26
+ end
27
+
28
+ # This works just like Plugin::CLI's autorun.
29
+
30
+ @@autorun = self
31
+ class << self
32
+ def method_added(name)
33
+ if name == :handle
34
+ @@autorun = self
35
+ end
36
+ end
37
+ end
38
+
39
+ at_exit do
40
+ handler = @@autorun.new
41
+ event = ::JSON.parse(STDIN.read)
42
+ if handler.filter(event)
43
+ handler.handle(event)
44
+ end
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,11 @@
1
+ module Sensu
2
+ module Plugin
3
+ VERSION = "0.0.1"
4
+ EXIT_CODES = {
5
+ 'OK' => 0,
6
+ 'WARNING' => 1,
7
+ 'CRITICAL' => 2,
8
+ 'UNKNOWN' => 3,
9
+ }
10
+ end
11
+ end
@@ -0,0 +1,25 @@
1
+ require 'sensu-plugin/cli'
2
+
3
+ module Sensu
4
+ module Plugin
5
+ class Check
6
+ class CLI < Sensu::Plugin::CLI
7
+
8
+ class << self
9
+ def check_name(name=nil)
10
+ if name
11
+ @check_name = name
12
+ else
13
+ @check_name || self.to_s
14
+ end
15
+ end
16
+ end
17
+
18
+ def format_output(status, output)
19
+ "#{self.class.check_name}: #{status} - #{output}"
20
+ end
21
+
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,60 @@
1
+ require 'sensu-plugin'
2
+ require 'mixlib/cli'
3
+
4
+ module Sensu
5
+ module Plugin
6
+ class CLI
7
+ include Mixlib::CLI
8
+
9
+ # Implementing classes should override this to produce appropriate
10
+ # output for their handler.
11
+
12
+ def format_output(status, output)
13
+ output
14
+ end
15
+
16
+ # This will define 'ok', 'warning', 'critical', and 'unknown'
17
+ # methods, which the plugin should call to exit.
18
+
19
+ Sensu::Plugin::EXIT_CODES.each do |status, code|
20
+ define_method(status.downcase) do |*args|
21
+ puts format_output(status, *args)
22
+ exit(code)
23
+ end
24
+ end
25
+
26
+ # Implementing classes must override this.
27
+
28
+ def run
29
+ unknown "Not implemented! You should override Sensu::Plugin::CLI#run."
30
+ end
31
+
32
+ # Behind-the-scenes stuff below. If you do something crazy like
33
+ # define two plugins in one script, the last one will 'win' in
34
+ # terms of what gets auto-run.
35
+
36
+ @@autorun = self
37
+ class << self
38
+ def method_added(name)
39
+ if name == :run
40
+ @@autorun = self
41
+ end
42
+ end
43
+ end
44
+
45
+ at_exit do
46
+ begin
47
+ check = @@autorun.new
48
+ check.parse_options
49
+ check.run
50
+ rescue SystemExit => e
51
+ exit e.status
52
+ rescue Exception => e
53
+ self.new.critical "Check failed to run: #{e}"
54
+ end
55
+ self.new.warning "Check did not exit! You should call an exit code method."
56
+ end
57
+
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,17 @@
1
+ require 'sensu-plugin/cli'
2
+ require 'json'
3
+
4
+ module Sensu
5
+ module Plugin
6
+ class Metric
7
+ class CLI < Sensu::Plugin::CLI
8
+
9
+ def format_output(status, output)
10
+ output[:timestamp] ||= Time.now.to_i
11
+ ::JSON.generate(output)
12
+ end
13
+
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,25 @@
1
+ require 'sensu-plugin/check/cli'
2
+
3
+ module Sensu
4
+ module Plugin
5
+ module Util
6
+ module Procs
7
+
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
+ end
21
+
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'minitest/autorun'
5
+
6
+ class TestCheck < MiniTest::Unit::TestCase
7
+
8
+ def setup
9
+ @script = File.join(File.dirname(__FILE__), 'checks/check-options')
10
+ end
11
+
12
+ def test_ok
13
+ system @script, '-o'
14
+ assert $?.exitstatus == 0
15
+ end
16
+
17
+ def test_warning
18
+ system @script, '-w'
19
+ assert $?.exitstatus == 1
20
+ end
21
+
22
+ def test_critical
23
+ system @script, '-c'
24
+ assert $?.exitstatus == 2
25
+ end
26
+
27
+ def test_unknown
28
+ system @script, '-u'
29
+ assert $?.exitstatus == 3
30
+ end
31
+
32
+ def test_fallthrough
33
+ system @script
34
+ assert $?.exitstatus == 1
35
+ end
36
+
37
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sensu-plugin
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Decklin Foster
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: json
16
+ requirement: &70296568612360 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70296568612360
25
+ - !ruby/object:Gem::Dependency
26
+ name: mixlib-cli
27
+ requirement: &70296568611860 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 1.1.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70296568611860
36
+ description: Plugins and helper libraries for Sensu, a monitoring framework
37
+ email:
38
+ - decklin@red-bean.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - lib/sensu-handler.rb
44
+ - lib/sensu-plugin/check/cli.rb
45
+ - lib/sensu-plugin/cli.rb
46
+ - lib/sensu-plugin/metric/cli.rb
47
+ - lib/sensu-plugin/util/procs.rb
48
+ - lib/sensu-plugin.rb
49
+ - test/check.rb
50
+ homepage: https://github.com/sonian/sensu-plugin
51
+ licenses:
52
+ - MIT
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubyforge_project:
71
+ rubygems_version: 1.8.11
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Sensu Plugins
75
+ test_files:
76
+ - test/check.rb