Custom Plugin Error in Sensu Checks

Hi All,

Seeking your opinion on an issue in developing a custom plugin.

What we are trying to achieve is create a plugin to convert an XML file to JSON.

If the code block below is run in the command line (ruby environment), it successfully returns the desired JSON output.

#! /usr/bin/env ruby

require ‘active_support/all’

require ‘json’

file_name="/home/oracle/test/"

sortname=Dir["/home/oracle/test/*"].sort_by { |file_name| File.mtime(file_name)}

result = Hash.from_xml(File.read(sortname[-1])).to_json

puts result

return result

``

However, when executed by Sensu through checks it returns an error:

/opt/sensu/embedded/bin/event-queues.rb:6:in `initialize': no implicit conversion of nil into String (TypeError) from /opt/sensu/embedded/bin/event-queues.rb:6:in `<main>'

``

Any response is greatly appreciated. Thanks!

Regards,

Peter

Peter,

Before iterating over something you should always check that its empty. I was able to replicate by creating an empty dir and doing what you did. From my quick tests of using Dir it will always return an empty array when using a blob even if the parent dir does not exist, this is probably your issue if I had to guess.

Here is a slightly more defensive approach that worked in my test: https://gist.github.com/majormoses/2e57e74ea68c2e6c8ba10b555102c802

Thanks.

···

On Wednesday, July 5, 2017 at 11:37:00 PM UTC-7, Peter wrote:

Hi All,

Seeking your opinion on an issue in developing a custom plugin.

What we are trying to achieve is create a plugin to convert an XML file to JSON.

If the code block below is run in the command line (ruby environment), it successfully returns the desired JSON output.

#! /usr/bin/env ruby

require ‘active_support/all’

require ‘json’

file_name="/home/oracle/test/"

sortname=Dir["/home/oracle/test/*"].sort_by { |file_name| File.mtime(file_name)}

result = Hash.from_xml(File.read(sortname[-1])).to_json

puts result

return result

``

However, when executed by Sensu through checks it returns an error:

/opt/sensu/embedded/bin/event- queues.rb:6:in initialize': no implicit conversion of nil into String (TypeError) from /opt/sensu/embedded/bin/event-queues.rb:6:in

``

Any response is greatly appreciated. Thanks!

Regards,

Peter

Hi Ben,

Thank you for responding to this thread.

The thing is the dir I am referring to is not empty. When I execute your script in the command line using the following I get the exact results I want. But when sensu client picks it up, it is seeing an empty DIR.

./ruby xmltojson.rb

``

Any ideas on this? thanks!

Regards,

Peter

···

On Monday, July 10, 2017 at 2:35:56 AM UTC+8, Ben Abrams wrote:

Peter,

Before iterating over something you should always check that its empty. I was able to replicate by creating an empty dir and doing what you did. From my quick tests of using Dir it will always return an empty array when using a blob even if the parent dir does not exist, this is probably your issue if I had to guess.

Here is a slightly more defensive approach that worked in my test: https://gist.github.com/majormoses/2e57e74ea68c2e6c8ba10b555102c802

Thanks.

On Wednesday, July 5, 2017 at 11:37:00 PM UTC-7, Peter wrote:

Hi All,

Seeking your opinion on an issue in developing a custom plugin.

What we are trying to achieve is create a plugin to convert an XML file to JSON.

If the code block below is run in the command line (ruby environment), it successfully returns the desired JSON output.

#! /usr/bin/env ruby

require ‘active_support/all’

require ‘json’

file_name="/home/oracle/test/"

sortname=Dir["/home/oracle/test/*"].sort_by { |file_name| File.mtime(file_name)}

result = Hash.from_xml(File.read(sortname[-1])).to_json

puts result

return result

``

However, when executed by Sensu through checks it returns an error:

/opt/sensu/embedded/bin/event- queues.rb:6:in initialize': no implicit conversion of nil into String (TypeError) from /opt/sensu/embedded/bin/event-queues.rb:6:in

``

Any response is greatly appreciated. Thanks!

Regards,

Peter

Also, the dir and the files has all the permissions needed. So I don’t understand why Sensu sees this as empty.

/home/oracle/test
drwxrwxrwx. 2 oracle oinstall 36864 Jul 4 05:23 test

``

/home/oracle/test/*

-rwxrwxrwx. 1 oracle oinstall 60056 Jun 28 01:55 performance.20170628015548.xml

``

···

On Wednesday, July 12, 2017 at 9:43:22 AM UTC+8, Peter wrote:

Hi Ben,

Thank you for responding to this thread.

The thing is the dir I am referring to is not empty. When I execute your script in the command line using the following I get the exact results I want. But when sensu client picks it up, it is seeing an empty DIR.

./ruby xmltojson.rb

``

Any ideas on this? thanks!

Regards,

Peter

On Monday, July 10, 2017 at 2:35:56 AM UTC+8, Ben Abrams wrote:

Peter,

Before iterating over something you should always check that its empty. I was able to replicate by creating an empty dir and doing what you did. From my quick tests of using Dir it will always return an empty array when using a blob even if the parent dir does not exist, this is probably your issue if I had to guess.

Here is a slightly more defensive approach that worked in my test: https://gist.github.com/majormoses/2e57e74ea68c2e6c8ba10b555102c802

Thanks.

On Wednesday, July 5, 2017 at 11:37:00 PM UTC-7, Peter wrote:

Hi All,

Seeking your opinion on an issue in developing a custom plugin.

What we are trying to achieve is create a plugin to convert an XML file to JSON.

If the code block below is run in the command line (ruby environment), it successfully returns the desired JSON output.

#! /usr/bin/env ruby

require ‘active_support/all’

require ‘json’

file_name="/home/oracle/test/"

sortname=Dir["/home/oracle/test/*"].sort_by { |file_name| File.mtime(file_name)}

result = Hash.from_xml(File.read(sortname[-1])).to_json

puts result

return result

``

However, when executed by Sensu through checks it returns an error:

/opt/sensu/embedded/bin/event- queues.rb:6:in initialize': no implicit conversion of nil into String (TypeError) from /opt/sensu/embedded/bin/event-queues.rb:6:in

``

Any response is greatly appreciated. Thanks!

Regards,

Peter

What version of sensu are you running? Wondering if its maybe something different caused by different ruby versions. When I have some time I will see if I can replicate this locally outside of the context of sensu and if not then I will try reproducing it in a sensu context.

···

On Tuesday, July 11, 2017 at 7:17:24 PM UTC-7, Peter wrote:

Also, the dir and the files has all the permissions needed. So I don’t understand why Sensu sees this as empty.

/home/oracle/test
drwxrwxrwx. 2 oracle oinstall 36864 Jul 4 05:23 test

``

/home/oracle/test/*

-rwxrwxrwx. 1 oracle oinstall 60056 Jun 28 01:55 performance.20170628015548.xml

``

On Wednesday, July 12, 2017 at 9:43:22 AM UTC+8, Peter wrote:

Hi Ben,

Thank you for responding to this thread.

The thing is the dir I am referring to is not empty. When I execute your script in the command line using the following I get the exact results I want. But when sensu client picks it up, it is seeing an empty DIR.

./ruby xmltojson.rb

``

Any ideas on this? thanks!

Regards,

Peter

On Monday, July 10, 2017 at 2:35:56 AM UTC+8, Ben Abrams wrote:

Peter,

Before iterating over something you should always check that its empty. I was able to replicate by creating an empty dir and doing what you did. From my quick tests of using Dir it will always return an empty array when using a blob even if the parent dir does not exist, this is probably your issue if I had to guess.

Here is a slightly more defensive approach that worked in my test: https://gist.github.com/majormoses/2e57e74ea68c2e6c8ba10b555102c802

Thanks.

On Wednesday, July 5, 2017 at 11:37:00 PM UTC-7, Peter wrote:

Hi All,

Seeking your opinion on an issue in developing a custom plugin.

What we are trying to achieve is create a plugin to convert an XML file to JSON.

If the code block below is run in the command line (ruby environment), it successfully returns the desired JSON output.

#! /usr/bin/env ruby

require ‘active_support/all’

require ‘json’

file_name="/home/oracle/test/"

sortname=Dir["/home/oracle/test/*"].sort_by { |file_name| File.mtime(file_name)}

result = Hash.from_xml(File.read(sortname[-1])).to_json

puts result

return result

``

However, when executed by Sensu through checks it returns an error:

/opt/sensu/embedded/bin/event- queues.rb:6:in initialize': no implicit conversion of nil into String (TypeError) from /opt/sensu/embedded/bin/event-queues.rb:6:in

``

Any response is greatly appreciated. Thanks!

Regards,

Peter

Can you verify the same script on the server has the same results when you run /opt/sensu/embedded/bin/ruby $PATH_TO_SCRIPT and calling $PATH_TO_SCRIPT directly?

···

On Tuesday, July 11, 2017 at 9:15:15 PM UTC-7, Ben Abrams wrote:

What version of sensu are you running? Wondering if its maybe something different caused by different ruby versions. When I have some time I will see if I can replicate this locally outside of the context of sensu and if not then I will try reproducing it in a sensu context.

On Tuesday, July 11, 2017 at 7:17:24 PM UTC-7, Peter wrote:

Also, the dir and the files has all the permissions needed. So I don’t understand why Sensu sees this as empty.

/home/oracle/test
drwxrwxrwx. 2 oracle oinstall 36864 Jul 4 05:23 test

``

/home/oracle/test/*

-rwxrwxrwx. 1 oracle oinstall 60056 Jun 28 01:55 performance.20170628015548.xml

``

On Wednesday, July 12, 2017 at 9:43:22 AM UTC+8, Peter wrote:

Hi Ben,

Thank you for responding to this thread.

The thing is the dir I am referring to is not empty. When I execute your script in the command line using the following I get the exact results I want. But when sensu client picks it up, it is seeing an empty DIR.

./ruby xmltojson.rb

``

Any ideas on this? thanks!

Regards,

Peter

On Monday, July 10, 2017 at 2:35:56 AM UTC+8, Ben Abrams wrote:

Peter,

Before iterating over something you should always check that its empty. I was able to replicate by creating an empty dir and doing what you did. From my quick tests of using Dir it will always return an empty array when using a blob even if the parent dir does not exist, this is probably your issue if I had to guess.

Here is a slightly more defensive approach that worked in my test: https://gist.github.com/majormoses/2e57e74ea68c2e6c8ba10b555102c802

Thanks.

On Wednesday, July 5, 2017 at 11:37:00 PM UTC-7, Peter wrote:

Hi All,

Seeking your opinion on an issue in developing a custom plugin.

What we are trying to achieve is create a plugin to convert an XML file to JSON.

If the code block below is run in the command line (ruby environment), it successfully returns the desired JSON output.

#! /usr/bin/env ruby

require ‘active_support/all’

require ‘json’

file_name="/home/oracle/test/"

sortname=Dir["/home/oracle/test/*"].sort_by { |file_name| File.mtime(file_name)}

result = Hash.from_xml(File.read(sortname[-1])).to_json

puts result

return result

``

However, when executed by Sensu through checks it returns an error:

/opt/sensu/embedded/bin/event- queues.rb:6:in initialize': no implicit conversion of nil into String (TypeError) from /opt/sensu/embedded/bin/event-queues.rb:6:in

``

Any response is greatly appreciated. Thanks!

Regards,

Peter

Hi Ben,

For some environment reasons I can only execute the script with the correct results when I go to the directory ‘/opt/sensu/embedded/bin’ and then execute the following

#/opt/sensu/embedded/bin> ./ruby event-queues.rb

``

When I execute it outside the directory by using the following the result is “No such file or directory”

#/home/oracle> /opt/sensu/embedded/bin/ruby event-queues.rb

``

Regards,

Peter

···

On Wednesday, July 12, 2017 at 12:23:34 PM UTC+8, Ben Abrams wrote:

Can you verify the same script on the server has the same results when you run /opt/sensu/embedded/bin/ruby $PATH_TO_SCRIPT and calling $PATH_TO_SCRIPT directly?

On Tuesday, July 11, 2017 at 9:15:15 PM UTC-7, Ben Abrams wrote:

What version of sensu are you running? Wondering if its maybe something different caused by different ruby versions. When I have some time I will see if I can replicate this locally outside of the context of sensu and if not then I will try reproducing it in a sensu context.

On Tuesday, July 11, 2017 at 7:17:24 PM UTC-7, Peter wrote:

Also, the dir and the files has all the permissions needed. So I don’t understand why Sensu sees this as empty.

/home/oracle/test
drwxrwxrwx. 2 oracle oinstall 36864 Jul 4 05:23 test

``

/home/oracle/test/*

-rwxrwxrwx. 1 oracle oinstall 60056 Jun 28 01:55 performance.20170628015548.xml

``

On Wednesday, July 12, 2017 at 9:43:22 AM UTC+8, Peter wrote:

Hi Ben,

Thank you for responding to this thread.

The thing is the dir I am referring to is not empty. When I execute your script in the command line using the following I get the exact results I want. But when sensu client picks it up, it is seeing an empty DIR.

./ruby xmltojson.rb

``

Any ideas on this? thanks!

Regards,

Peter

On Monday, July 10, 2017 at 2:35:56 AM UTC+8, Ben Abrams wrote:

Peter,

Before iterating over something you should always check that its empty. I was able to replicate by creating an empty dir and doing what you did. From my quick tests of using Dir it will always return an empty array when using a blob even if the parent dir does not exist, this is probably your issue if I had to guess.

Here is a slightly more defensive approach that worked in my test: https://gist.github.com/majormoses/2e57e74ea68c2e6c8ba10b555102c802

Thanks.

On Wednesday, July 5, 2017 at 11:37:00 PM UTC-7, Peter wrote:

Hi All,

Seeking your opinion on an issue in developing a custom plugin.

What we are trying to achieve is create a plugin to convert an XML file to JSON.

If the code block below is run in the command line (ruby environment), it successfully returns the desired JSON output.

#! /usr/bin/env ruby

require ‘active_support/all’

require ‘json’

file_name="/home/oracle/test/"

sortname=Dir["/home/oracle/test/*"].sort_by { |file_name| File.mtime(file_name)}

result = Hash.from_xml(File.read(sortname[-1])).to_json

puts result

return result

``

However, when executed by Sensu through checks it returns an error:

/opt/sensu/embedded/bin/event- queues.rb:6:in initialize': no implicit conversion of nil into String (TypeError) from /opt/sensu/embedded/bin/event-queues.rb:6:in

``

Any response is greatly appreciated. Thanks!

Regards,

Peter

If you are not currently in the same folder as the script, you’ll need the full (or correct relative) path for the script to work too.

#/home/oractle>/opt/sensu/embedded/bin/ruby/ /opt/sensu/embedded/bin/event-queues.rb

``

The reason you do not need the full path when, say, writing your JSON files is that during normal operation sensu-client assumes it is already in /opt/sensu/embedded/bin when executing scripts.

···

On Thursday, July 13, 2017 at 9:28:34 AM UTC+2, Peter wrote:

Hi Ben,

For some environment reasons I can only execute the script with the correct results when I go to the directory ‘/opt/sensu/embedded/bin’ and then execute the following

#/opt/sensu/embedded/bin> ./ruby event-queues.rb

``

When I execute it outside the directory by using the following the result is “No such file or directory”

#/home/oracle> /opt/sensu/embedded/bin/ruby event-queues.rb

``

Hi Alexander,

Thank you for this and apologies for the late response. Been quite busy these past few days :slight_smile:

However, this still doesn’t explain why sensu client reads the directory as empty?

Also, the sensu client version is Version: 0.29.0. Any ideas on this?

Regards,

Peter

···

On Thursday, July 13, 2017 at 5:49:25 PM UTC+8, Alexander Skiba wrote:

On Thursday, July 13, 2017 at 9:28:34 AM UTC+2, Peter wrote:

Hi Ben,

For some environment reasons I can only execute the script with the correct results when I go to the directory ‘/opt/sensu/embedded/bin’ and then execute the following

#/opt/sensu/embedded/bin> ./ruby event-queues.rb

``

When I execute it outside the directory by using the following the result is “No such file or directory”

#/home/oracle> /opt/sensu/embedded/bin/ruby event-queues.rb

``

If you are not currently in the same folder as the script, you’ll need the full (or correct relative) path for the script to work too.

#/home/oractle>/opt/sensu/embedded/bin/ruby/ /opt/sensu/embedded/bin/event-queues.rb

``

The reason you do not need the full path when, say, writing your JSON files is that during normal operation sensu-client assumes it is already in /opt/sensu/embedded/bin when executing scripts.

Hi Everyone,

This is solved. I think there’s a permission issue in the directory that I am using. I just moved my files under /opt/sensu/embedded/bin directory which solved my problem.

Thanks for your assistance!

Regards,
Peter

···

On Tuesday, July 18, 2017 at 11:42:43 AM UTC+8, Peter wrote:

Hi Alexander,

Thank you for this and apologies for the late response. Been quite busy these past few days :slight_smile:

However, this still doesn’t explain why sensu client reads the directory as empty?

Also, the sensu client version is Version: 0.29.0. Any ideas on this?

Regards,

Peter

On Thursday, July 13, 2017 at 5:49:25 PM UTC+8, Alexander Skiba wrote:

On Thursday, July 13, 2017 at 9:28:34 AM UTC+2, Peter wrote:

Hi Ben,

For some environment reasons I can only execute the script with the correct results when I go to the directory ‘/opt/sensu/embedded/bin’ and then execute the following

#/opt/sensu/embedded/bin> ./ruby event-queues.rb

``

When I execute it outside the directory by using the following the result is “No such file or directory”

#/home/oracle> /opt/sensu/embedded/bin/ruby event-queues.rb

``

If you are not currently in the same folder as the script, you’ll need the full (or correct relative) path for the script to work too.

#/home/oractle>/opt/sensu/embedded/bin/ruby/ /opt/sensu/embedded/bin/event-queues.rb

``

The reason you do not need the full path when, say, writing your JSON files is that during normal operation sensu-client assumes it is already in /opt/sensu/embedded/bin when executing scripts.