Skip to content

Instantly share code, notes, and snippets.

@faja
Created March 11, 2013 09:34
Show Gist options
  • Select an option

  • Save faja/5133064 to your computer and use it in GitHub Desktop.

Select an option

Save faja/5133064 to your computer and use it in GitHub Desktop.
sensu-status.rb

sensu-status.rb prints 'nagios' like list of all hosts and checks with status, like on screenshot below: sensu-status.rb

required gems:

  • json
  • redis
  • rainbow
#!/usr/bin/env ruby
# [email protected]
require 'json'
require 'redis'
require 'rainbow'
if ARGV.length == 0 or ! File.exist?(ARGV[0])
puts 'Usage: ./redis.rb /path/to/sensu-server/config.json [client_name]'
exit 1
end
begin
settings = JSON.parse(File.open(ARGV[0],'r').read, :symbolize_names => true)
rescue
puts "#{ARGV[0]} is not a valid json file"
exit 1
end
def print_state(arg)
return '['+'ok'.color(:green)+']' if arg == '0'
return '['+'warning'.color(:yellow)+']' if arg == '1'
return '['+'critical'.color(:red)+']' if arg == '2'
return '['+'silenced'.color(:magenta)+']' if arg == 's'
return '['+'unknow'.color(:blue)+']'
end
begin
r=Redis.new(settings[:redis])
if ARGV[1] and ! r.get("client:#{ARGV[1]}")
puts "No such client: #{ARGV[1]}"
puts "List of valid clients: "+r.smembers("clients").sort.join(', ')
exit 1
end
stashes = r.smembers("stashes")
r.smembers("clients").each do |client|
next if client != ARGV[1] and ARGV[1]
keepalive_state = r.hexists("events:#{client}","keepalive") ? print_state(r.lindex("history:#{client}:keepalive",-1)) : print_state('0')
printf "%-45s %25s %25s%s\n", client, "keepalive", keepalive_state, stashes.include?("silence/#{client}") ? print_state('s') : nil
r.keys("history:#{client}:*").each do |check|
next if check == "history:#{client}:keepalive"
check_name = check[/history:.+:(.+)/,1]
printf "%71s %25s%s\n", check_name, print_state(r.lindex(check,-1)), stashes.include?("silence/#{client}/#{check_name}") ? print_state('s') : nil
end
puts
end
rescue => error
puts error
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment