Skip to content

Instantly share code, notes, and snippets.

@jwalton
Forked from james/REAMDE.md
Last active March 27, 2020 10:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jwalton/6625777 to your computer and use it in GitHub Desktop.
Save jwalton/6625777 to your computer and use it in GitHub Desktop.
Pingdom widget for Dashing
class Dashing.Pingdom extends Dashing.Widget
<h1 class="title" data-bind="title"></h1>
<ul>
<li class="check" data-foreach-check="checks" data-bind-data-state="check.state">
<span class="checkName" data-bind="check.name"></span><span class="checkPing" data-bind="check.lastRepsonseTime"></span>
</li>
</ul>
require "rest-client"
require "cgi"
require "json"
api_key = ENV['PINGDOM_API_KEY'] || ''
user = ENV['PINGDOM_USER'] || ''
password = ENV['PINGDOM_PASSWORD'] || ''
SCHEDULER.every '1m', :first_in => 0 do
# Get checks
url = "https://#{CGI::escape user}:#{CGI::escape password}@api.pingdom.com/api/2.0/checks"
response = RestClient.get(url, {"App-Key" => api_key})
response = JSON.parse(response.body, :symbolize_names => true)
if response[:checks]
checks = response[:checks].map { |check|
if check[:status] == 'up'
state = 'up'
last_response_time = "#{check[:lastresponsetime]}ms"
else
state = 'down'
last_response_time = "DOWN"
end
{ name: check[:name], state: state, lastRepsonseTime: last_response_time }
}
else
checks = [name: "pingdom", state: "down", lastRepsonseTime: "-"]
end
checks.sort_by { |check| check['name'] }
send_event('pingdom', { checks: checks })
end
// ----------------------------------------------------------------------------
// Sass declarations
// ----------------------------------------------------------------------------
$background-color: #A0BC3B;
// ----------------------------------------------------------------------------
// Widget-clock styles
// ----------------------------------------------------------------------------
.widget-pingdom {
background-color: $background-color;
.check {
float: left;
padding: 4px 0px;
width:100%
}
.check[data-state="up"] {
}
.check[data-state="down"] {
background-color: #B72126;
}
.check span {
padding: 0px 4px;
}
.checkName {
float: left
}
.checkPing {
float: right
}
}

Description

Simple Dashing widget (and associated job) to display Pingdom checks.

##Dependencies

rest-client

Add it to dashing's gemfile:

gem 'rest-client'

and run bundle install. Everything should work now :)

##Usage

To use this widget, copy pingdom.html, pingdom.coffee, and pingdom.scss into the /widgets/pingdom directory. Put the pingdom.rb file in your /jobs folder.

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="1" data-sizey="1">
  <div data-id="pingdom" data-view="Pingdom" data-title="Pingdom" data-cols="1"></div>
</li>

##Settings

You'll need to add your pingdom API key and login settings. You can either add it straight to the source code on lines 4-6 of pingdom.rb, or set the variables in the ENV. For heroku, you can do this with

heroku config:set PINGDOM_API_KEY=
heroku config:set PINGDOM_USER=
heroku config:set PINGDOM_PASSWORD=

Pingdom is checked every minute, but you can change that by editing the job schedule.

Contributions

This has been extracted from a dashboard for the UK Ministry of Justice, with minimal generalising. Feel free to submit patches to @james.

Modified by @jwalton to use rest-client instead of pingdom-client.

@OliPassey
Copy link

This is brilliant! Thank you!
I am unable to move the widget however, it just loads at the bottom and cant be changed.
Any ideas?

@travism
Copy link

travism commented Nov 4, 2016

Sending user and password in plain text is not secure. Pingdom supports an authentication header. This would encrypt the headers via SSL and takes it out of the URL.

I'm updating my local copy, I'm not a ruby developer but if I get it looking nice I'll contribute back.

@travism
Copy link

travism commented Nov 4, 2016

Making this change:

url = "https://api.pingdom.com/api/2.0/checks"

  response = RestClient::Request.execute(
      method: :get,
      url: url,
      user: user,
      password: password,
      headers: {"App-Key" => api_key}
    )

  response = JSON.parse(response.body, :symbolize_names => true)

Works and seems more secure.

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