Skip to content

Instantly share code, notes, and snippets.

@op-ct
Last active December 4, 2019 20:30
Show Gist options
  • Select an option

  • Save op-ct/94ce40f9dbc48af09c1d1c7ff3dd43cc to your computer and use it in GitHub Desktop.

Select an option

Save op-ct/94ce40f9dbc48af09c1d1c7ff3dd43cc to your computer and use it in GitHub Desktop.
Query latest report metrics from PuppetDB
#!/opt/puppetlabs/puppet/bin/ruby
# -------------------------------------------------------------------
# Report the latest nodes whose catalogs that took over 300s to apply
#
# Setup:
#
# puppet-query --cert CERTNAME.cert.pem --key KEYFILE.key.pem 'reports[certname,status,catalog_uuid,receive_time,start_time,end_time,noop,noop_pending,environment,configuration_version,metrics] { latest_report? = true }' > latest_reports.json
#
# Usage:
#
# /opt/puppetlabs/puppet/bin/ruby puppetdbdb.rb "latest-reports.json"
#
require 'json'
require 'yaml'
require 'irb'
report_metrics_file = File.read(ARGV[0])
report_metrics_data = JSON.parse(report_metrics_file)
metrics_list = report_metrics_data.map do |i|
y = i['metrics']['data'].select{|x| x['name'] =~ /plugin_sync|catalog_application|transaction_evaluation/ }.map{|x| [x['name'], x['value']]}
[i['certname'],Hash[y].merge(i.select{|k,v| k =~ /^(receive_time|catalog_uuid)$/})]
end.sort_by { |a| a.last.fetch("catalog_application",0) }.reverse
reports_metrics = Hash[metrics_list]
slow_catalogs = reports_metrics.select{|k,v| v['catalog_application'].to_f > 300.0 }
puts slow_catalogs.to_yaml
puppet-query --cert CERTNAME.cert.pem --key KEYFILE.key.pem \
'reports[certname,status,catalog_uuid,receive_time,start_time,end_time,noop,noop_pending,environment,configuration_version,metrics] { latest_report? = true }'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment