require 'net/http' require 'json' # Currently do not have this working with client side SSL, perhaps someone can help with that in the comments. http = Net::HTTP.new(host, port) nodes_uri = '/metrics/v1/mbeans/puppetlabs.puppetdb.query.population:type=default,name=num-nodes' resources_uri = '/metrics/v1/mbeans/puppetlabs.puppetdb.query.population:type=default,name=num-resources' avg_resources_uri = '/metrics/v1/mbeans/puppetlabs.puppetdb.query.population:type=default,name=avg-resources-per-node' agg_eventcounts_uri = '/pdb/query/v4/aggregate-event-counts?query=%5B%22%3D%22%2C%22latest_report%3F%22%2Ctrue%5D&summarize_by=certname' one_hour_ago = (Time.now - 60*60).utc.iso8601 reported_in_uri = "/pdb/query/v4/nodes?query=%5B%22extract%22%2C%5B%5B%22function%22%2C%22count%22%5D%5D%2C%5B%22%3C%22%2C%22report_timestamp%22%2C%22#{one_hour_ago}%22%5D%5D" SCHEDULER.every '5m', :first_in => 0 do |job| num_nodes = JSON.parse(http.get(nodes_uri).body) num_resources = JSON.parse(http.get(resources_uri).body) avg_resource = JSON.parse(http.get(avg_resources_uri).body) agg_eventcounts = JSON.parse(http.get(agg_eventcounts_uri).body) unreported = JSON.parse(http.get(reported_in_uri).body).first['count'].to_i send_event('puppetdb_stats', { num_nodes: num_nodes['Value'], num_resources: num_resources['Value'], avg_resource: avg_resource['Value'].to_f.round(2), unreported: unreported, successes: agg_eventcounts.first['successes'], failures: agg_eventcounts.first['failures'] }) end