Last active
May 17, 2016 15:35
-
-
Save smbambling/05d0bd5b98c6af2145f786d2c8ec1d61 to your computer and use it in GitHub Desktop.
Verify that Puppet Enterprise FileSync clients have updated environments and are on the same GIT commit hash as the Master of Masters
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
#!/opt/puppetlabs/puppet/bin/ruby | |
require "rest-client" | |
require "json" | |
require "rugged" | |
require 'optparse' | |
options = { :verbose => nil, :pretty => nil } | |
parser = OptionParser.new do|opts| | |
opts.banner = "Usage: #{$0} [options]" | |
opts.on('-v', '--verbose', 'Toggle to display verbose output') do | |
options["verbose"] = true | |
end | |
opts.on('-p', '--pretty', 'Toggle JSON Pretty Print(Generate), only used with -v/--verbose') do | |
options["pretty"] = true | |
end | |
opts.on('-h', '--help', 'Displays Help') do | |
puts opts | |
exit | |
end | |
end | |
parser.parse! | |
# Set the return codes states | |
nag_ok = 0 | |
nag_warn = 1 | |
nag_crit = 2 | |
nag_unknown = 3 | |
begin | |
puppet_server_response = RestClient.get "https://localhost:8140/status/v1/services/file-sync-storage-service?level=debug", {:accept => :json} | |
rescue RestClient::ExceptionWithResponse => err | |
puts "Failed to obtain hash from Puppet Server" | |
err.response | |
exit nag_unknown | |
end | |
# Get the response from the services API Call | |
puppet_server_response = JSON.parse(puppet_server_response) | |
# Initialize GIT log entry hash | |
log_entry = Hash.new | |
# Directory where FileSync stores its "Master" GIT Repositories | |
filesync_storage_dir = "/opt/puppetlabs/server/data/puppetserver/filesync/storage/puppet-code" | |
# Get list of repos on the MoM file system | |
repos = Dir.entries("#{filesync_storage_dir}") | |
# Remove hidden directories . & .. | |
repos.delete('.') | |
repos.delete('..') | |
# Create Hash { repo => commit } | |
repos.each do | repo| | |
r = Rugged::Repository.new("#{filesync_storage_dir}/#{repo}") | |
ref = r.head | |
entry = ref.target_id | |
repo = repo.split(".").first | |
log_entry["#{repo}"] = entry | |
end | |
# Get a list of all the FileSync clients reporting to the MoM | |
clients = puppet_server_response["status"]["clients"].keys | |
# Create an empty hash for broken clients => repos | |
@broken_clients = Hash.new | |
clients.each do |client| | |
# Create an empty array for broken client repos | |
broken_repos = Array.new | |
repos = puppet_server_response["status"]["clients"]["#{client}"]["last_sync"]["puppet-code"]["submodules"].keys | |
repos.each do |repo| | |
# Client FileSync Commit | |
cfs_commit = puppet_server_response["status"]["clients"]["#{client}"]["last_sync"]["puppet-code"]["submodules"]["#{repo}"]["latest_commit"]["commit"] | |
# Master FileSync Commit | |
mfs_commit = log_entry["#{repo}"] | |
# Update hash of broken clients => repos | |
unless cfs_commit == mfs_commit then | |
broken_repos.push(repo) | |
@broken_clients["#{client}"] = broken_repos | |
end | |
end | |
end | |
if @broken_clients.empty? then | |
print "OK - All FileSync Clients in sync with MoM \n" | |
exit nag_ok | |
else | |
if options["verbose"] | |
pp_json = @broken_clients.to_json | |
if options["pretty"] | |
pp_json = JSON.parse(pp_json) | |
puts JSON.pretty_generate(pp_json) | |
else | |
puts pp_json | |
end | |
exit nag_crit | |
else | |
print "CRITICAL - Out of sync clients: #{@broken_clients.keys} \n" | |
exit nag_crit | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment