Skip to content

Instantly share code, notes, and snippets.

@johnwards
Last active December 18, 2015 00:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnwards/5697369 to your computer and use it in GitHub Desktop.
Save johnwards/5697369 to your computer and use it in GitHub Desktop.

##Preview

Description

Simple Dashing widget (and associated job) to display current alerts from your server density account.

##Dependencies

httparty

Add it to dashing's gemfile:

gem 'httparty'

and run bundle install. Everything should work now :)

##Usage

To use this widget, copy server_density_alerts.html, server_density_alerts.coffee, and server_density_alerts.scss into the /widgets/server_density_alerts directory. Put the server_density_alerts.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="server-density-alerts" data-view="ServerDensityAlerts" data-title="Server Density" ></div>
</li>

##Settings

You'll need to add the your username, password and server density account url to your environment. Use the following names: SERVER_DENSITY_USERNAME, SERVER_DENSITY_PASSWORD, SERVER_DENSITY_ACCOUNT

class Dashing.ServerDensityAlerts extends Dashing.Widget
<h1 class="title" data-bind="title"></h1>
<img class="background" data-hideif="items.length" src="/assets/donotpanic.jpg">
<img class="background" data-showif="items.length" src="/assets/wearealldoomed.jpg">
<h2 data-hideif="items.length">Don't panic</h2>
<ul class="items list-nostyle" data-showif="items">
<li class="item" data-foreach-item="items">
<h3 data-bind="item.server"></h3>
<span class="label alert" data-bind="item.alert"></span>
<span class="label time" data-bind="item.time"></span>
<div class="clearfix" />
</li>
</ul>
require 'httparty'
def update_alerts(username, password, account)
api_url = 'https://api.serverdensity.com/1.4/alerts/getOpen?account=%s'
api_url = api_url % [account]
api_response = HTTParty.get(api_url, :headers => { "Accept" => "application/json" }, :basic_auth => {:username => username, :password => password} )
api_json = JSON.parse(api_response.body)
data = []
if api_json['data']
api_json['data']['alerts'].each do |a|
alert = {
server: a["device"]["name"],
time: a["alert"]["timeAlertedText"],
alert: a["alert"]["checkTypeText"]
}
data.push(alert);
end
end
return data
end
SCHEDULER.every '10s', :first_in => 0 do
items = update_alerts(ENV['SERVER_DENSITY_USERNAME'], ENV['SERVER_DENSITY_PASSWORD'], ENV['SERVER_DENSITY_ACCOUNT'])
send_event('server-density-alerts', { items: items })
end
$value-color: #FFF;
$background-color: #444;
$background-error-color: #A31F1F;
$background-passed-color: #8fb347;
$background-pending-color: #47bbb3;
$title-color: rgba(255, 255, 255, 1);
$label-color: rgba(255, 255, 255, 0.7);
.widget.widget-server-density_alerts{
background-color: $background-color;
padding: 0px;
vertical-align: top;
img.background {
width: 100% !important;
position: absolute;
left: 0;
top: 30px;
opacity: 0.15;
}
.title {
color: $title-color;
}
ol, ul {
margin: 0px;
text-align: left;
color: $label-color;
}
ol {
list-style-position: inside;
}
li {
margin-bottom: 5px;
}
h2 {
text-align: center;
display: block;
font-weight: 600;
font-size: 40px;
word-wrap: break-word;
}
.list-nostyle {
}
.items{
list-style: none;
li {
margin-top: 5px;
background-color: $background-error-color;
h3 {
padding-left: 5px;
}
.label {
display: block;
color: $label-color;
font-size: 20px;
word-wrap: break-word;
&.alert {
text-align: left;
font-size: 14px;
padding: 5px 0px 0 5px;
font-weight: bold;
}
&.time {
text-align: left;
font-size: 12px;
padding: 0px 0px 5px 5px;
}
}
}
}
}
@guizello
Copy link

Hello, I can't see my alerts into the dashing widget ... I've added

puts username
puts password
puts account

and i see that they are ok. But nothing when i try to see what is written into data[].

Would you have an idea ?

Thank you
Guillaume

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