Created
June 25, 2012 22:09
-
-
Save joemiller/2991677 to your computer and use it in GitHub Desktop.
script to check your sensu playbook url coverage
This file contains hidden or 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 | |
# | |
# Sensu Check Playbook Audit | |
# ========================== | |
# | |
# All monitors should have a documentation page (eg: wiki) so that team members | |
# know what to do when they're paged to deal with a particular alert. This | |
# script is intendend to help you find the 'playbook coverage' of your | |
# sensu infrastructure. | |
# | |
# Run this on your sensu-server and it will load all check definitions from | |
# /etc/sensu/config.json and /etc/sensu/conf.d/*.json and give you a count | |
# of how many checks are missing the `playbook` attribute. | |
# | |
require 'rubygems' | |
require 'json' | |
# TODO: use mixlib-cli and make these command line args | |
CONFIG_FILE = '/etc/sensu/config.json' | |
CONF_DIR = '/etc/sensu/conf.d' | |
# TODO: This Settings class is a hacked up version of Sensu::Settings from | |
# sensu >= 0.9.6. We should switch to Sensu::Settings after we upgrade. | |
class Settings | |
def initialize | |
@settings = Hash.new | |
@loaded_files = Array.new | |
end | |
def [](key) | |
@settings[key.to_sym] | |
end | |
def deep_merge(hash_one, hash_two) | |
merged = hash_one.dup | |
hash_two.each do |key, value| | |
merged[key] = case | |
when hash_one[key].is_a?(Hash) && value.is_a?(Hash) | |
deep_merge(hash_one[key], value) | |
when hash_one[key].is_a?(Array) && value.is_a?(Array) | |
hash_one[key].concat(value).uniq | |
else | |
value | |
end | |
end | |
merged | |
end | |
def load_file(file) | |
if File.readable?(file) | |
begin | |
contents = File.open(file, 'r').read | |
config = JSON.parse(contents, :symbolize_names => true) | |
merged = deep_merge(@settings, config) | |
@settings = merged | |
@loaded_files.push(file) | |
rescue JSON::ParserError => error | |
puts "config file #{file} is not valid json: #{error.to_s}" | |
end | |
else | |
puts "config file does not exist or is not readable: #{file}" | |
end | |
end | |
end | |
## main | |
if __FILE__ == $0 | |
# TODO: use Sensu::Settings instead when we upgrade to 0.9.6+ | |
# require 'sensu/settings' | |
# | |
# settings = Sensu::Settings.new | |
# settings.load_file(config_file) | |
# Dir[conf_dir + '/**/*.json'].each do |file| | |
# settings.load_file(file) | |
# end | |
# settings.validate | |
has_playbook = Array.new | |
no_playbook = Array.new | |
settings = Settings.new | |
settings.load_file(CONFIG_FILE) | |
Dir[CONF_DIR + '/**/*.json'].each do |file| | |
settings.load_file(file) | |
end | |
settings[:checks].each_pair do |name,config| | |
if config.has_key?(:playbook) | |
has_playbook << name | |
else | |
no_playbook << name | |
end | |
end | |
total_checks = has_playbook.count + no_playbook.count | |
coverage = (has_playbook.count.to_f / total_checks.to_f) * 100 | |
printf "Playbook coverage: %.2f%% (total: %d, found: %d, missing: %d)\n", | |
coverage, total_checks, has_playbook.count, no_playbook.count | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment