Created
February 24, 2015 18:58
-
-
Save jbehrends/9ba401964c5a98e06c46 to your computer and use it in GitHub Desktop.
Sensu CLI script to set/remove stashes and check status of checks.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
# DESCRIPTION: | |
# This script should be templated to a node via puppet/chef/other. This will | |
# allow you to silence/unsilence/check stashes/check sensu checks for the host | |
# the script is being ran from. | |
# | |
require 'rubygems' | |
require 'json' | |
require 'net/http' | |
require 'optparse' | |
require 'uri' | |
options = { } | |
parser = OptionParser.new do|opts| | |
opts.banner = "Usage: sensu COMMAND [options]" | |
opts.separator "" | |
opts.separator "Commands:" | |
opts.separator " silence - silence the current host" | |
opts.separator " unsilence - unsilence the current host" | |
opts.separator " status - display stash status (takes no options)" | |
opts.separator " checks - display all checks for this host (takes no options)" | |
opts.separator "" | |
opts.separator "Options:" | |
opts.on('-c', '--check <checkname>', 'Check name (example -c check_disk)') do |check| | |
options[:check] = check; | |
end | |
opts.on('-e', '--expire <seconds>', Numeric, 'Seconds till the silenced host or check will expire') do |expire| | |
options[:expire] = expire; | |
end | |
opts.on('-r', '--reason <message>', 'An explination of why you created the stash') do |reason| | |
options[:reason] = reason; | |
end | |
opts.on('-H', '--host <hostname>', 'Enter a fqdn to override the local hosts fqdn') do |host| | |
options[:host] = host; | |
end | |
opts.on('-F', '--FORCE', 'Force the command without user verification') do |force| | |
options[:force] = force; | |
end | |
opts.on('-h', '--help', 'Displays Help') do | |
puts opts | |
exit | |
end | |
end | |
begin | |
parser.parse! | |
rescue OptionParser::InvalidOption | |
puts "Warning: Invalid option" | |
end | |
options[:host] = '<%= @fqdn %>' if options[:host] == nil | |
def submit_request(request) | |
uri = URI.parse("http://<%= @sensu_hieradata['client']['server'] %>:4567") | |
http = Net::HTTP.new(uri.host, uri.port) | |
begin | |
Net::HTTP.start(uri.host, uri.port) do |http| | |
response = http.request(request) | |
return response | |
end | |
rescue StandardError, Timeout::Error => e | |
puts "error: " + e | |
end | |
end | |
def user_verify(force) | |
if force | |
return true | |
else | |
print "Continue? [y/n]: " | |
answer = $stdin.gets.chomp | |
if answer.downcase == 'y' || answer.downcase == 'yes' | |
return true | |
else | |
return false | |
end | |
end | |
end | |
case ARGV[0] | |
when 'silence' | |
content = { :timestamp => Time.now.to_i } | |
content.merge!(:source => "CLI - " + ENV['USER']) | |
content.merge!(:reason => options[:reason]) if options[:reason] | |
payload = { :content => content } | |
payload.merge!(:expire => options[:expire]) if options[:expire] | |
silence_path = 'silence' | |
silence_path << "/#{options[:host]}" | |
silence_path << "/#{options[:check]}" if options[:check] | |
payload = payload.merge!(:path => silence_path).to_json | |
puts "The following stash will be submitted:" | |
puts JSON.pretty_generate(JSON.parse(payload)) | |
if user_verify(options[:force]) | |
req = Net::HTTP::Post.new("/stashes", {'Content-Type' => 'application/json'}) | |
req.body = payload | |
submit_request(req) | |
exit 0 | |
end | |
puts "Exited without submitting a stash" | |
exit 1 | |
when 'unsilence' | |
puts "unsilence" | |
unsilence_path ='/silence' | |
unsilence_path << "/#{options[:host]}" | |
unsilence_path << "/#{options[:check]}" if options[:check] | |
puts "The following object will be unsilenced:" | |
puts unsilence_path | |
if user_verify(options[:force]) | |
req = Net::HTTP::Delete.new("/stash#{unsilence_path}") | |
submit_request(req) | |
exit 0 | |
end | |
exit 1 | |
when 'status' | |
req = Net::HTTP::Get.new("/stashes") | |
response = submit_request(req) | |
if response.body.to_s.include? options[:host].to_s | |
puts "The following stashes were found for: #{options[:host]}" | |
JSON.parse(response.body).each { |a| | |
if a["path"].to_s.include? options[:host].to_s | |
puts JSON.pretty_generate(a) # TODO: Figure out how to sort this damn json object for consistency | |
end | |
} | |
else | |
puts "No stashes found for this host!" | |
end | |
exit 0 | |
when 'checks' | |
req = Net::HTTP::Get.new("/clients/#{options[:host]}/history") | |
response = submit_request(req) | |
JSON.parse(response.body).each { |a| | |
status = case a["last_status"]; when 0 then "OK"; when 1 then "Warning"; when 2 then "Critical"; else "Unknown" end | |
puts "#{status} - #{a["check"]}" | |
} | |
exit 0 | |
else | |
if ARGV[0] == nil | |
puts "ERROR - No arguments passed!" | |
else | |
puts "ERROR - #{ARGV[0]} is not a valid argument!" | |
end | |
puts "try sensu --help for help." | |
exit 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment