Sha256: 15a564c618801a2c34a07fef5ca93ae281fd8c0174dcc57667d2c3f34f4adb65

Contents?: true

Size: 1.85 KB

Versions: 21

Compression:

Stored size: 1.85 KB

Contents

#! /usr/bin/env ruby
#
# check-cmd
#
# DESCRIPTION:
#   Generic check raising an error if exit code of command is not N.
#
# OUTPUT:
#   plain text
#
# PLATFORMS:
#   Linux
#
# DEPENDENCIES:
#   gem: sensu-plugin
#   gem: english
#
# USAGE:
#
# NOTES:
#
# LICENSE:
#   Jean-Francois Theroux <failshell@gmail.com>
#   Released under the same terms as Sensu (the MIT license); see LICENSE
#   for details.
#

require 'sensu-plugin/check/cli'
require 'English'

#
# Check Command Status
#
class CheckCmdStatus < Sensu::Plugin::Check::CLI
  option :command,
         description: 'command to run (might need quotes)',
         short: '-c',
         long: '--command COMMAND',
         required: true

  option :status,
         description: 'exit status code the check should get',
         short: '-s',
         long: '--status STATUS',
         default: '0'

  option :check_output,
         description: 'Optionally check the process stdout against a regex',
         short: '-o',
         long: '--check_output REGEX'

  # Acquire the exit code and/or output of a command and alert if it is not
  # what is expected.
  #
  def acquire_cmd_status
    stdout = `#{config[:command]}`
    # #YELLOW
    unless $CHILD_STATUS.exitstatus.to_s == config[:status] # rubocop:disable UnlessElse
      critical "#{config[:command]} exited with #{$CHILD_STATUS.exitstatus}"
    else
      if config[:check_output]
        if Regexp.new(config[:check_output]).match(stdout)
          ok "#{config[:command]} matched #{config[:check_output]} and exited with #{$CHILD_STATUS.exitstatus}"
        else
          critical "#{config[:command]} output didn't match #{config[:check_output]} (exit #{$CHILD_STATUS.exitstatus})"
        end
      else
        ok "#{config[:command]} exited with #{$CHILD_STATUS.exitstatus}"
      end
    end
  end

  # main function
  #
  def run
    acquire_cmd_status
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
sensu-plugins-process-checks-4.2.1 bin/check-cmd.rb
sensu-plugins-process-checks-4.2.0 bin/check-cmd.rb
sensu-plugins-process-checks-4.1.0 bin/check-cmd.rb
sensu-plugins-process-checks-4.0.1 bin/check-cmd.rb
sensu-plugins-process-checks-4.0.0 bin/check-cmd.rb
sensu-plugins-process-checks-3.2.0 bin/check-cmd.rb
sensu-plugins-process-checks-3.1.0 bin/check-cmd.rb
sensu-plugins-process-checks-3.0.2 bin/check-cmd.rb
sensu-plugins-process-checks-3.0.1 bin/check-cmd.rb
sensu-plugins-process-checks-3.0.0 bin/check-cmd.rb
sensu-plugins-process-checks-2.7.0 bin/check-cmd.rb
sensu-plugins-process-checks-2.6.0 bin/check-cmd.rb
sensu-plugins-process-checks-2.5.0 bin/check-cmd.rb
sensu-plugins-process-checks-2.4.0 bin/check-cmd.rb
sensu-plugins-process-checks-2.3.0 bin/check-cmd.rb
sensu-plugins-process-checks-2.2.0 bin/check-cmd.rb
sensu-plugins-process-checks-2.1.0 bin/check-cmd.rb
sensu-plugins-process-checks-2.0.0 bin/check-cmd.rb
sensu-plugins-process-checks-1.0.0 bin/check-cmd.rb
sensu-plugins-process-checks-0.0.6 bin/check-cmd.rb