Last active
November 20, 2018 17:27
-
-
Save teknofire/a56ef31bbd5dc5023a547435e1cd559b to your computer and use it in GitHub Desktop.
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/delivery/embedded/bin/ruby | |
# Usage: | |
# 1. download this file as automate_compare_status.rb | |
# 2. make it executable `chmod u+x find_missing_compliance_nodes.rb` | |
# 3. run the report `./find_missing_compliance_nodes.rb > missing_nodes_report.txt` | |
# | |
# This will print information to the console about the number of nodes found and | |
# how many are missing. It will filter out all nodes that doesn't have the | |
# wgb_audit::default recipe. | |
# | |
# It will also create a report file that contains the list of nodes that were | |
# not found in the compliance data reports. This matching is done via FQDN. | |
require 'json' | |
def read_data(filename = nil, verbose = false) | |
if filename.nil? | |
data = yield | |
else | |
data = File.read(filename) | |
end | |
unique_nodes(JSON.parse(data), verbose) | |
end | |
def info(msg) | |
STDERR.puts(msg) | |
end | |
$stats = { | |
total: 0, | |
missing: 0, | |
no_audit: 0, | |
audit: 0, | |
no_status: 0 | |
} | |
def inc_stat(name) | |
$stats[name] += 1 | |
nil | |
end | |
def get_node_info(hit, verbose) | |
source = hit['_source'] | |
info source if verbose | |
if source['node_uuid'] | |
# compliance state data | |
source['node_name'].downcase | |
else | |
# node state data | |
inc_stat(:total) | |
return inc_stat(:no_status) unless %w{success failure missing}.include?(source['status']) | |
return inc_stat(:missing) if source['status'] == 'missing' | |
return inc_stat(:missing) if source['name'].nil? | |
return inc_stat(:no_audit) unless source['exists'] == 'true' | |
return inc_stat(:no_audit) if source['recipes'].nil? | |
return inc_stat(:no_audit) unless source['recipes'].include?('audit::default') | |
inc_stat(:audit) | |
source['name'].downcase | |
end | |
end | |
def unique_nodes(results, verbose = false) | |
results['hits']['hits'].inject([]) do |collector, node| | |
data = get_node_info(node, verbose) | |
unless data.nil? | |
collector << data unless collector.include?(data) | |
end | |
collector.uniq | |
end | |
end | |
info "Reading node data" | |
nodes = read_data(ARGV.shift) do | |
`curl -s -k -XGET 'localhost:9200/node-state-2/_search?size=10000'` | |
end | |
info "Reading compliance data" | |
compliance = read_data(ARGV.shift) do | |
`curl -s -k -XGET 'localhost:9200/compliance-latest-1/_search?size=10000'` | |
end | |
missing = nodes - compliance | |
puts missing | |
info "" | |
info "Node Stats" | |
info '-' * 40 | |
info sprintf ' %11s %6s', 'With Audit:', $stats[:audit] | |
info sprintf ' %11s %6s', 'No Audit:', $stats[:no_audit] | |
info sprintf ' %11s %6s', 'Missing:', $stats[:missing] | |
info sprintf ' %11s %6s', 'No Status:', $stats[:no_status] | |
info sprintf ' %11s %6s', 'Total:', $stats[:total] | |
info "" | |
info "Found #{compliance.count} compliance-state records" | |
info "#{missing.count} nodes missing from compliance data" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment