Skip to content

Instantly share code, notes, and snippets.

@ismailmechbal
Forked from willjohnson/README.md
Created January 17, 2016 21:38
Show Gist options
  • Save ismailmechbal/0345b3cc73219a8ccb59 to your computer and use it in GitHub Desktop.
Save ismailmechbal/0345b3cc73219a8ccb59 to your computer and use it in GitHub Desktop.
Server Status Widget for Dashing

Description

A Dashing widget that checks whether a server is responding to either an http or ping request. It displays either a check or alert depending on the response.

Usage

  1. Add the widget HTML to your dashboard
    <li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
      <div data-id="server_status" data-view="ServerStatus" data-title="Server Status"></div>
    </li>
  1. Create a directory titled "server_status" under your widgets directory and move server_status.coffee, server_status.html, and server_status.scss into that directory.

  2. Modify the servers array of the server_status.rb file.

  3. Move server_status.rb into your jobs folder.

class Dashing.ServerStatus extends Dashing.Widget
<li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
<div data-id="server_status" data-view="ServerStatus" data-title="Server Status"></div>
</li>
<h1 class="title" data-bind="title"></h1>
<ul>
<li data-foreach-item="items">
<span class="label" data-bind="item.label"></span>
<span class="value"><div data-bind-class="item.color"><i data-bind-class="item.arrow"></i></div></span>
</li>
</ul>
<p class="more-info" data-bind="moreinfo"></p>
<p class="updated-at" data-bind="updatedAtMessage"></p>
#!/usr/bin/env ruby
require 'net/http'
require 'uri'
# Check whether a server is responding
# you can set a server to check via http request or ping
#
# server options:
# name: how it will show up on the dashboard
# url: either a website url or an IP address (do not include https:// when usnig ping method)
# method: either 'http' or 'ping'
# if the server you're checking redirects (from http to https for example) the check will
# return false
servers = [{name: 'server1', url: 'https://www.test.com', method: 'http'},
{name: 'server2', url: 'https://www.test2.com', method: 'http'},
{name: 'server3', url: '192.168.0.1', method: 'ping'}]
SCHEDULER.every '300s', :first_in => 0 do |job|
statuses = Array.new
# check status for each server
servers.each do |server|
if server[:method] == 'http'
uri = URI.parse(server[:url])
http = Net::HTTP.new(uri.host, uri.port)
if uri.scheme == "https"
http.use_ssl=true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
if response.code == "200"
result = 1
else
result = 0
end
elsif server[:method] == 'ping'
ping_count = 10
result = `ping -q -c #{ping_count} #{server[:url]}`
if ($?.exitstatus == 0)
result = 1
else
result = 0
end
end
if result == 1
arrow = "icon-ok-sign"
color = "green"
else
arrow = "icon-warning-sign"
color = "red"
end
statuses.push({label: server[:name], value: result, arrow: arrow, color: color})
end
# print statuses to dashboard
send_event('server_status', {items: statuses})
end
// ----------------------------------------------------------------------------
// Sass declarations
// ----------------------------------------------------------------------------
$background-color: #12b0c5;
$value-color: #fff;
$title-color: rgba(255, 255, 255, 0.7);
$label-color: rgba(255, 255, 255, 0.7);
$moreinfo-color: rgba(255, 255, 255, 0.7);
// ----------------------------------------------------------------------------
// Widget-list styles
// ----------------------------------------------------------------------------
.widget-server-status {
background-color: $background-color;
vertical-align: top;
.title {
color: $title-color;
}
ol, ul {
margin: 0 15px;
text-align: left;
color: $label-color;
}
ol {
list-style-position: inside;
}
li {
margin-bottom: 5px;
}
.list-nostyle {
list-style: none;
}
.label {
color: $label-color;
}
.value {
float: right;
margin-left: 12px;
font-weight: 600;
color: $value-color;
}
.updated-at {
color: rgba(0, 0, 0, 0.3);
}
.more-info {
color: $moreinfo-color;
}
.red {
color: red;
}
.green {
color: green;
}
}
@sebstr1
Copy link

sebstr1 commented Feb 13, 2016

Why does it not show any icon for me? It doesnt show icon for serverstatus up or down?

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