Sensu check using Sensu::Plugin::Utils

I am attempting to write a a few plugins for AWS that will check various services we have set up and would like to push a set of credentials out with the Sensu checks that are used by all plugins. The easiest way to me seems like it would be to use settings method that is part of the Sensu::Plugin::Utils module similar to what the Sensu::Handler class does.

The code I’m testing with is:

require ‘rubygems’

require ‘sensu-plugin/check/cli’

require ‘sensu-plugin/utils’

class SQSMsgs < Sensu::Plugin::Check::CLI

include Sensu::Plugin::Utils

option :aws_access_key,

:short => ‘-a PARAM’,

:long => ‘–abcd PARAM’,

:description => “Stuff”,

:default => settings[‘abcd’][‘something’]

def run

puts “do something neat”

end

end

Running it results in:

$ ruby check-test.rb

Sensu::Plugin::CLI: Not implemented! You should override Sensu::Plugin::CLI#run.

check-test.rb:12: undefined method `’ for nil:NilClass (NoMethodError)

I am obviously overriding the run method, and removing the :default parameter in my test code works as expected. I can’t figure out why including the util module is either a) not including the utility module’s methods, or b) somehow breaking the Check::Cli class. Any thoughts?

Snipets of the code I was using to generate this:

graphite-tcp.rb

require ‘sensu-handler’

class Graphite < Sensu::Handler

def handle

graphite_server = settings[‘graphite’][‘server’]

graphite_port = settings[‘graphite’][‘port’]

sensu-plugin/lib/sensu-handler.rb

require ‘net/http’

require ‘json’

require ‘sensu-plugin/utils’

module Sensu

class Handler

include Sensu::Plugin::Utils

Have you experimented with Sensu check command token substitution for this use case?

Wiki page https://github.com/sensu/sensu/wiki/Checks#check-command-token-substitution

Allows you to have client specific plugin arguments. The custom client attributes are also included in event data.

As for the error you have encountered, I recall Sensu::Plugin using at_exit() to call run() after doing some setup, there is likely a silent killer. I will take a look at the library shortly.

Sean.

···

On Thursday, 21 March 2013 19:32:15 UTC-7, jlam...@letsevenup.com wrote:

I am attempting to write a a few plugins for AWS that will check various services we have set up and would like to push a set of credentials out with the Sensu checks that are used by all plugins. The easiest way to me seems like it would be to use settings method that is part of the Sensu::Plugin::Utils module similar to what the Sensu::Handler class does.

The code I’m testing with is:

require ‘rubygems’

require ‘sensu-plugin/check/cli’

require ‘sensu-plugin/utils’

class SQSMsgs < Sensu::Plugin::Check::CLI

include Sensu::Plugin::Utils

option :aws_access_key,

:short => ‘-a PARAM’,

:long => ‘–abcd PARAM’,

:description => “Stuff”,

:default => settings[‘abcd’][‘something’]

def run

puts “do something neat”

end

end

Running it results in:

$ ruby check-test.rb

Sensu::Plugin::CLI: Not implemented! You should override Sensu::Plugin::CLI#run.

check-test.rb:12: undefined method `’ for nil:NilClass (NoMethodError)

I am obviously overriding the run method, and removing the :default parameter in my test code works as expected. I can’t figure out why including the util module is either a) not including the utility module’s methods, or b) somehow breaking the Check::Cli class. Any thoughts?

Snipets of the code I was using to generate this:

graphite-tcp.rb

require ‘sensu-handler’

class Graphite < Sensu::Handler

def handle

graphite_server = settings[‘graphite’][‘server’]

graphite_port = settings[‘graphite’][‘port’]

sensu-plugin/lib/sensu-handler.rb

require ‘net/http’

require ‘json’

require ‘sensu-plugin/utils’

module Sensu

class Handler

include Sensu::Plugin::Utils

Thanks for the response Sean. I haven’t used the token substitution, but that looks like it might work. It’s a bit harder for testing or if someone wants to run a check manually since the variables have to be looked up and applied rather than just running the command. (or is there a trick I’m not aware of?)

I do see how at_exit is being used to call run (https://github.com/sensu/sensu-plugin/blob/master/lib/sensu-plugin/cli.rb#L53). While I think I understand how that works when using the Sensu::Plugin::Check::Cli, I’m not a strong enough ruby programmer to figure out why including and using the Util module causes an issue with that.

···

On Thu, Mar 21, 2013 at 9:07 PM, portertech portertech@gmail.com wrote:

Have you experimented with Sensu check command token substitution for this use case?

Wiki page https://github.com/sensu/sensu/wiki/Checks#check-command-token-substitution

Allows you to have client specific plugin arguments. The custom client attributes are also included in event data.

As for the error you have encountered, I recall Sensu::Plugin using at_exit() to call run() after doing some setup, there is likely a silent killer. I will take a look at the library shortly.

Sean.

On Thursday, 21 March 2013 19:32:15 UTC-7, jlam...@letsevenup.com wrote:

I am attempting to write a a few plugins for AWS that will check various services we have set up and would like to push a set of credentials out with the Sensu checks that are used by all plugins. The easiest way to me seems like it would be to use settings method that is part of the Sensu::Plugin::Utils module similar to what the Sensu::Handler class does.

The code I’m testing with is:

require ‘rubygems’

require ‘sensu-plugin/check/cli’

require ‘sensu-plugin/utils’

class SQSMsgs < Sensu::Plugin::Check::CLI

include Sensu::Plugin::Utils

option :aws_access_key,

:short => ‘-a PARAM’,

:long => ‘–abcd PARAM’,

:description => “Stuff”,

:default => settings[‘abcd’][‘something’]

def run

puts “do something neat”

end

end

Running it results in:

$ ruby check-test.rb

Sensu::Plugin::CLI: Not implemented! You should override Sensu::Plugin::CLI#run.

check-test.rb:12: undefined method `’ for nil:NilClass (NoMethodError)

I am obviously overriding the run method, and removing the :default parameter in my test code works as expected. I can’t figure out why including the util module is either a) not including the utility module’s methods, or b) somehow breaking the Check::Cli class. Any thoughts?

Snipets of the code I was using to generate this:

graphite-tcp.rb

require ‘sensu-handler’

class Graphite < Sensu::Handler

def handle

graphite_server = settings[‘graphite’][‘server’]

graphite_port = settings[‘graphite’][‘port’]

sensu-plugin/lib/sensu-handler.rb

require ‘net/http’

require ‘json’

require ‘sensu-plugin/utils’

module Sensu

class Handler

include Sensu::Plugin::Utils