-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathcheck-cmd.rb
More file actions
executable file
·97 lines (89 loc) · 2.38 KB
/
check-cmd.rb
File metadata and controls
executable file
·97 lines (89 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#! /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'
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
if $CHILD_STATUS.exitstatus.to_s == config[:status]
check_cmd_output(stdout)
else
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
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
# main function
#
def run
acquire_cmd_status
end
end