Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ This CHANGELOG follows the format listed [here](https://github.com/sensu-plugins


## [Unreleased]
### Added
- check-cmd.rb: added `--echo_stdout` which allows grabbing the processes stdout (@lahdekorpi)

### Changed
- check-cmd.rb: refactored enhancing the response output into a helper function to reduce branch complexity and other misc rubocop appeasing (@majormoses)

## [2.7.0] - 2018-01-06
### Changed
Expand Down
37 changes: 28 additions & 9 deletions bin/check-cmd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,43 @@ class CheckCmdStatus < Sensu::Plugin::Check::CLI
short: '-o',
long: '--check_output REGEX'

option :echo_stdout,
description: 'Output the stdout of the command',
short: '-e',
long: '--echo_stdout',
boolean: true,
default: false

# 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}"
if $CHILD_STATUS.exitstatus.to_s == config[:status]
check_cmd_output(stdout)
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
status = "#{config[:command]} exited with #{$CHILD_STATUS.exitstatus}"
status += "\nOutput: #{stdout}" if config[:echo_stdout]
critical status
end
end

def check_cmd_output(stdout)
if config[:check_output]
if Regexp.new(config[:check_output]).match(stdout)
status = "#{config[:command]} matched #{config[:check_output]} and exited with #{$CHILD_STATUS.exitstatus}"
status += "\nOutput: #{stdout}" if config[:echo_stdout]
ok status
else
ok "#{config[:command]} exited with #{$CHILD_STATUS.exitstatus}"
status = "#{config[:command]} output didn't match #{config[:check_output]} (exit #{$CHILD_STATUS.exitstatus})"
status += "\nOutput: #{stdout}" if config[:echo_stdout]
critical status
end
else
status = "#{config[:command]} exited with #{$CHILD_STATUS.exitstatus}"
status += "\nOutput: #{stdout}" if config[:echo_stdout]
ok status
end
end

Expand Down