Class: SensuRunCheck::Runner
- Inherits:
-
Sensu::Client::Process
- Object
- Sensu::Client::Process
- SensuRunCheck::Runner
- Defined in:
- lib/sensu-run-check/runner.rb
Overview
Class Method Summary collapse
Instance Method Summary collapse
-
#execute_check_command(check) ⇒ Object
Execute a check command, capturing its output (STDOUT/ERR), exit status code, execution duration, timestamp, and publish the result.
-
#get_all_checks ⇒ Array
Get all Sensu checks.
-
#get_check(checkname) ⇒ Hash
Find a Sensu check by name.
-
#initialize(options = {}) ⇒ Runner
constructor
Create a new impostor.
- #publish_check_result(check) ⇒ Object
-
#run_check(checkname) ⇒ Array
Run a Sensu check by name.
Constructor Details
#initialize(options = {}) ⇒ Runner
Create a new impostor.
58 59 60 61 62 63 |
# File 'lib/sensu-run-check/runner.rb', line 58 def initialize(={}) @checks_in_progress = [] @settings = Sensu::Settings.get() @settings.validate @logger = SensuRunCheck::NilLog.new end |
Class Method Details
.run(options = {}) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/sensu-run-check/runner.rb', line 30 def self.run(={}) s = SensuRunCheck::Runner.new() if [:list_checks] puts s.get_all_checks.collect{ |check| check[:name] }.sort.join(",") elsif [:run_all_checks] statuses = [] s.get_all_checks.collect{ |check| check[:name] }.sort.each do |checkname| stdout, status = s.run_check(checkname) status ||= 3 statuses << status puts "#{checkname} #{status} #{stdout}" end # Exit with critical if there is any critical, otherwise exit mith the highest. exit(2) if statuses.include?(2) exit(statuses.max) else stdout, status = s.run_check([:run_check]) puts stdout status ||= 3 exit(status) end end |
Instance Method Details
#execute_check_command(check) ⇒ Object
Execute a check command, capturing its output (STDOUT/ERR), exit status code, execution duration, timestamp, and publish the result. This method guards against multiple executions for the same check. Check command tokens are substituted with the associated client attribute values. If there are unmatched check command tokens, the check command will not be executed, instead a check result will be published reporting the unmatched tokens.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/sensu-run-check/runner.rb', line 75 def execute_check_command(check) @logger.debug("attempting to execute check command", :check => check) unless @checks_in_progress.include?(check[:name]) @checks_in_progress << check[:name] command, unmatched_tokens = substitute_check_command_tokens(check) if unmatched_tokens.empty? check[:executed] = Time.now.to_i started = Time.now.to_f output, status = Open3.capture2(command) check[:duration] = ("%.3f" % (Time.now.to_f - started)).to_f check[:output] = output check[:status] = status.exitstatus @checks_in_progress.delete(check[:name]) publish_check_result(check) else check[:output] = "Unmatched command tokens: " + unmatched_tokens.join(", ") check[:status] = 3 check[:handle] = false @checks_in_progress.delete(check[:name]) publish_check_result(check) end else @logger.warn("previous check command execution in progress", :check => check) end end |
#get_all_checks ⇒ Array
Get all Sensu checks.
117 118 119 |
# File 'lib/sensu-run-check/runner.rb', line 117 def get_all_checks @settings.checks end |
#get_check(checkname) ⇒ Hash
Find a Sensu check by name.
110 111 112 |
# File 'lib/sensu-run-check/runner.rb', line 110 def get_check(checkname) @settings.checks.select{ |c| c[:name] == checkname }.first end |
#publish_check_result(check) ⇒ Object
102 103 104 |
# File 'lib/sensu-run-check/runner.rb', line 102 def publish_check_result(check) return check[:output], check[:status] end |
#run_check(checkname) ⇒ Array
Run a Sensu check by name.
125 126 127 128 129 130 131 132 |
# File 'lib/sensu-run-check/runner.rb', line 125 def run_check(checkname) check = @settings.checks.select{ |c| c[:name] == checkname }.first if check == nil return "No such check: #{checkname}", 3 else execute_check_command(check) end end |