Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Goldschrafe committed Nov 6, 2014
1 parent 7fa7321 commit 1efba2f
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source 'https://rubygems.org'

gem 'sensu'
4 changes: 4 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Usage
=====

./sensu-run -d /etc/sensu/conf.d '/etc/sensu/checks/check_disk.rb -w :::check_disk.warn::: -c :::check_disk.crit:::'
70 changes: 70 additions & 0 deletions sensu-run
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env ruby

require 'rubygems'
require 'json'
require 'open3'
require 'sensu/cli'
require 'sensu/client'
require 'sensu/logger'
require 'sensu/settings'

module Sensu
class MockClient < Client
def execute_check_command(check)
command, unmatched_tokens = substitute_command_tokens(check)
if unmatched_tokens.empty?
start = Time.now.to_f
stdout, stderr, status = Open3.capture3(command)
finish = Time.now.to_f

check[:executed] = start
check[:duration] = finish - start
check[:status] = status.exitstatus
else
check[:output] = 'Unmatched command tokens: ' + unmatched_tokens.join(',')
check[:status] = 3
check[:handle] = false
end
publish_result(check)
end

def publish_result(check)
puts check.to_json
end
end

class Runner
def initialize(options={})
setup_logger(options)
@settings = Settings.get(options)
end

def run(client, command)
if @settings[:checks] && @settings[:checks][command]
@logger.info "Executing check: '#{command}'"
check = @settings[:checks][command]
client.execute_check_command(check)
else
@logger.warn "No such check: '#{command}'; interpreting as arbitrary command"
client.execute_check_command(:command => command, :name => command)
end
end

def setup_logger(options)
@logger = Logger.get(options)
end
end
end

options = Sensu::CLI.read
runner = Sensu::Runner.new(options)
client = Sensu::MockClient.new(options)

if ARGV.length == 0
puts 'No check command specified! Aborting.'
exit 1
end

ARGV.each do |check_name|
runner.run client, check_name
end

0 comments on commit 1efba2f

Please sign in to comment.