Skip to content

Instantly share code, notes, and snippets.

@smortex
Created August 20, 2022 21:41
Show Gist options
  • Save smortex/d5b1a69b2e68811e5024221a7d53b28c to your computer and use it in GitHub Desktop.
Save smortex/d5b1a69b2e68811e5024221a7d53b28c to your computer and use it in GitHub Desktop.
Gather the value of a Riemann metric for each node in PupppetDB
#!/opt/puppetlabs/puppet/bin/ruby
# Gather the value of a Riemann metric for each node in PupppetDB
require 'optparse'
require 'puppetdb'
require 'riemann'
usage = "usage: #{$PROGRAM_NAME} [metric]"
OptionParser.new do |opts|
opts.banner = usage
end.parse!
if ARGV.size > 1
warn usage
exit 1
end
service = ARGV[0] || "cpu"
pdb = PuppetDB::Client.new({:server => 'http://localhost:8079'})
r = Riemann::Client.new(:ssl => true, :key_file => '/etc/riemann/riemann.pkcs8', :cert_file => '/etc/riemann/riemann.crt', :ca_file => '/etc/riemann/ca.crt', :ssl_verify => true)
hosts = pdb.request('', 'nodes[certname] {}').data.map { |x| x['certname'] }
w = hosts.map { |host| host.length }.max
hosts.map! do |host|
res = r.tcp["host = \"#{host}\" and service = \"#{service}\""]
value = res.size == 1 ? res.first.metric : nil
[host, value]
end
Hash[hosts].sort_by { |host, value| value || Float::INFINITY }.each do |host, value|
puts(format("%-#{w}s %s\n", host, value ? format('%1.3f', value) : 'n/a'))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment