Skip to content

Instantly share code, notes, and snippets.

@bitflingr
Last active September 11, 2018 07:56
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save bitflingr/a49981b299dff184c04a to your computer and use it in GitHub Desktop.
Save bitflingr/a49981b299dff184c04a to your computer and use it in GitHub Desktop.
Dashing widget for puppetdb

##Description Simple Dashing widget (and associated job) to display general puppetdb status. Pulled the api calls from Nedap's Puppetboard. Excellent tool BTW.

WARNING: This only works if puppetdb is listening on HTTP. I have not gotten around to getting this to work with SSL client certificates yet. If you know how, please leave them at the comments below.

##Screenshot

##Dependencies None

##Usage To use this widget, copy puppetdb_stats.html, puppetdb_stats.coffee, and puppetdb_stats.scss into the /widgets/puppetdb_stats directory. Put the puppetdb_stats.rb file in your /jobs folder. Or you can do this the easy way and run the following in your dashing directory

$> dashing install a49981b299dff184c04a
  create  widgets/puppetdb_stats/puppetdb_stats.coffee
  create  widgets/puppetdb_stats/puppetdb_stats.html
  create  jobs/puppetdb_stats.rb
  create  widgets/puppetdb_stats/puppetdb_stats.scss
Don't forget to edit the Gemfile and run bundle install if needed. More information for this widget can be found at https://gist.github.com/a49981b299dff184c04a

To include the widget in a dashboard, add the following snippet to the dashboard layout file:

<li data-row="1" data-col="1" data-sizex="2" data-sizey="1">
  <div data-id="puppetdb_stats" data-view="PuppetdbStats"></div>
</li>
class Dashing.PuppetdbStats extends Dashing.Widget
ready: ->
# This is fired when the widget is done being rendered
onData: (data) ->
# Handle incoming data
# You can access the html node of this widget with `@node`
# Example: $(@node).fadeOut().fadeIn() will make the node flash each time data comes in.
<h1>Puppet Status</h1>
<div class="row">
<div class="col">
<h2 class="success" data-bind="successes"></h2><span>with status change</span>
</div>
<div class="col" style="float:right;">
<h2 class="failure" data-bind="failures"></h2><span>with status failed</span>
</div>
</div>
<div class="row">
<div class="col" style="width: 33%;">
<h2 data-bind="num_nodes"></h2><span>Population</span>
</div>
<div class="col" style="float:left; width: 33%;">
<h2 data-bind="num_resources"></h2><span>Resources managed</span>
</div>
<div class="col" style="float:left; width: 33%;">
<h2 data-bind="avg_resource"></h2><span>Avg. resource/node</span>
</div>
</div>
require 'net/http'
require 'json'
# Currently do not have this working with client side SSL, perhaps someone can help with that in the comments.
host = 'YOUR_PUPPETDB_HOST_HERE'
port = 'PORT_NUMBER'
http = Net::HTTP.new(host, port)
nodes_uri = '/v3/metrics/mbean/com.puppetlabs.puppetdb.query.population:type=default,name=num-nodes'
resources_uri = '/v3/metrics/mbean/com.puppetlabs.puppetdb.query.population:type=default,name=num-resources'
avg_resources_uri = '/v3/metrics/mbean/com.puppetlabs.puppetdb.query.population:type=default,name=avg-resources-per-node'
agg_eventcounts_uri = '/v3/aggregate-event-counts?query=%5B%22%3D%22%2C%22latest-report%3F%22%2Ctrue%5D&summarize-by=certname'
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)
send_event('puppetdb_stats', { num_nodes: num_nodes['Value'],
num_resources: num_resources['Value'],
avg_resource: avg_resource['Value'].to_f.round(2),
successes: agg_eventcounts['successes'],
failures: agg_eventcounts['failures'] })
end
.widget-puppetdb-stats {
.row {
clear: both;
padding: 10px 0 30px 0;
position: relative;
}
.col {
float: left;
width: 50%;
}
h1 {
text-transform: uppercase;
font-size: 56px;
font-weight: 700;
color: white;
}
h2 {
text-transform: uppercase;
font-size: 48px;
font-weight: 700;
color: gray;
}
.success {
color: green;
}
.failure {
color: red;
}
}
@bitflingr
Copy link
Author

I hope to add ssl client certificates and show how to do that later. If anyone has already done that then please add them in the comments.

@lotherk
Copy link

lotherk commented May 12, 2015

Hey, thanks for your widget.

We're using it modified to support SSL. We do this by using the puppetdb-ruby gem:

https://github.com/puppetlabs/puppetdb-ruby

@MWilkinson
Copy link

Hi, Thanks for this widget.
I managed to get it working with SSL in the following manner - no need for the puppetdb-ruby gem

modify the code around the http=Net::HTTP.new section as follows :-

host = '<your puppetdb host/ip>'
port = '8081'
host_cert = '/var/lib/puppet/ssl/certs/<this servers hostname>.pem'
host_key = '/var/lib/puppet/ssl/private_keys/<this servers hostname>.pem'
http = Net::HTTP.new(host, port)
http.use_ssl = true
http.cert = OpenSSL::X509::Certificate.new(File.read(host_cert))
http.key = OpenSSL::PKey::RSA.new(File.read(host_key))
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

Note - whichever user this runs under will need read access the the puppet ssl directory.

Hope this helps someone

@peelman
Copy link

peelman commented Nov 12, 2015

I had to make some tweaks to get this working under PuppetDB 3.2; I created a "real" git repo here.

@Agarim999
Copy link

Hi, Thanks for this widget.
I'm a newbie and I'll have the following at startup:
image
Also I do not see the status of stations on widget. Say, what I miss?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment