Skip to content

Instantly share code, notes, and snippets.

@pszypowicz
Forked from bitflingr/README.md
Last active April 11, 2016 23:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pszypowicz/bc07528ed7ae79bf49aa to your computer and use it in GitHub Desktop.
Save pszypowicz/bc07528ed7ae79bf49aa to your computer and use it in GitHub Desktop.

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

This config is configured for working with HTTPS, but if you dont want to use it(HTTPS), you could just remove lines 4-16 from puppetdb_stats.rb file.

You have to generate client certs on puppetmaster/puppetca with following line:

$ puppet cert generate puppetdb_dashing

Then you should copy from it generated certs, which should be placed in:

CA.pem:

/var/lib/puppet/ssl/certs/ca.pem

Public cert

/var/lib/puppet/ssl/certs/puppetdb_dashing.pem

Private key

/var/lib/puppet/ssl/private_keys/puppetdb_dashing.pem

Then you have to provide path for dashing to thats certs, and take care for their ownership and permissions in jobs/puppetdb_stats.rb

##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 bc07528ed7ae79bf49aa
  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/https'
require 'json'
#You should provide path for client certs generated and signed by the same CA as puppetdb
cert = File.read("PATH_TO_CERT")
key = File.read("PATH_TO_KEY")
#Following lines are resonsible for HTTPS connection
http.use_ssl = true
http.cert = OpenSSL::X509::Certificate.new(cert)
http.key = OpenSSL::PKey::RSA.new(key)
http.ca_file = 'CA_PATH_HERE'
#Following option will check if your puppetdb host dns name is the same as in its certificate.
#Not secure is to leave it NONE, VERIFY_PEER is preffered.
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
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;
}
}
@Cornellio
Copy link

Hello,

Can you please offer help with this error I'm getting?

/home/vajobs/dashboards/webstats/jobs/puppetdb_stats.rb:35:in<top (required)>': undefined local variable or method http' for main:Object (NameError)

I have the gem http (0.8.4, 0.6.4, 0.6.1) installed locally.

Maybe I am missing a line in my Gemfile? Adding net/https there makes it fail on bundle install. What should be added to my Gemfile?

Thanks

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