Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;
}
}
@mgorbsky

This comment has been minimized.

Copy link

commented Sep 18, 2013

Great widget! One suggestion though - perhaps handle a timeout exception as a failure. We just had a server stop responding due to a DNS issue and this script timed out rather than showing the server as "down".

I fixed this by wrapping the request code in a begin/end with a rescue handler setting the result = 0. I'm very new to Ruby, so I'm not sure if this is the proper way to deal with this, but it seems to have worked for me.

@ozdemircili

This comment has been minimized.

Copy link

commented Nov 18, 2013

Hi,

I am trying to send data using curl as:

curl -d '{ "auth_token": "YOUR_AUTH_TOKEN", "value": 10 , "label": server[:Server1], "value": 10 }' http://localhost:3030/widgets/server_status

Yet I get the following error:

JSON::ParserError: 757: unexpected token at '{ "auth_token": "YOUR_AUTH_TOKEN", "value": 10 , "label": server[:Server1], "value": 10 }'

And when I send the following . I don`t get any errors yet widget displays no values:

curl -d '{ "auth_token": "YOUR_AUTH_TOKEN", "label": "server3", "value": "10" ,"arrow": "icon-ok-sign","color": "green"}' http://localhost:3030/widgets/server_status

What would be the correct way to send data to this widget using curl?

Thanks in advance.

@TheAshwanik

This comment has been minimized.

Copy link

commented Mar 16, 2014

Hi there,

I Could not find a way to send data to this widget through Curl or something else. Whatever I tried, the widget does not update. If you can provide me some example it would be great... Thanks a lot in advance...

@ozdemircili,
Were you able to sort this out ?

@derwin12

This comment has been minimized.

Copy link

commented Mar 19, 2014

curl -d '{ "auth_token": "TOKEN", "title": "Prod", "moreinfo": "info", "items": [{"label": "Server1", "arrow": "icon-ok-sign", "color": "green" }, {"label": "Server2", "arrow": "icon-warning-sign", "color": "red" } ] }' http://myserver:3030/widgets/server_status

@MartynKeigher

This comment has been minimized.

Copy link

commented Jun 27, 2014

Anyone else having this issue with their Server Status tile?

The server list groups up in the middle of the tile!? - very odd. If i restart the ubuntu box, then it's fine afor a few minutes, and the randomly groups together!

If you have had this issue.. how did you resolve it??

http://i.imgur.com/MIBFHO2.jpg

Other than this issue... the tile works fine!

Thanks!

@jsturdevant

This comment has been minimized.

Copy link

commented Mar 24, 2015

I had some errors show up when the website wasn't running that said "end of file" on the line response = http.request(request). I added a rescue around the request like this...

begin
  response = http.request(request)
    if response.code == "200"
      result = 1
    else
      result = 0
    end
rescue
  result = 0
end

And the issue is resolved. Additionally, as @mgorbsky pointed out, this should also handle DNS issues.

@mandelbrotmedia

This comment has been minimized.

Copy link

commented Apr 3, 2015

After some changes in the server_status.rb serverlist the widget was freezing.
It doesnt update anymore! It shows server which i allready deleted in server_status.rb.
I restarted dashing and also the server but no change.
Any ideas?

@nwdude44

This comment has been minimized.

Copy link

commented Jul 11, 2015

I'm having some trouble trying to get an up status while using the "ping" method, is there any trick to this as I am following what the example is in server_status.rb?

@mandelbrotmedia

This comment has been minimized.

Copy link

commented Aug 17, 2015

Hi nwdude44,

i solved the ping problem by changing this line in the server_status.rb:

result = ping -n #{ping_count} #{server[:url]}

Works for me on a windows maschine

cheers

@fizerkhan

This comment has been minimized.

Copy link

commented Oct 4, 2015

@jsturdevant Thanks for the fix.

@Skillz0r

This comment has been minimized.

Copy link

commented Dec 22, 2015

Hello everyone. Is there a way of changing this code to be able to import Json data from a url, and update it on a widget? I would really apreciate your help because I can't find any tutorial that show's how to do a Json httprequest to import the data. Really thx if anyone could help

@sebstr1

This comment has been minimized.

Copy link

commented Feb 12, 2016

@willjohnson The icon that shows green or red does not show up at all for me. I am running dashing on a raspberry pi 2. Can somebody help? I am much a beginner with these things and I am unable to find a solution for this..

Thank you!

@rahulkshinde

This comment has been minimized.

Copy link

commented Apr 13, 2016

Hi @willjohnson For the status Icon, how can I use a custom icon? I have different statuses and I was wondering if I can use an icon of my own?

@txaume79

This comment has been minimized.

Copy link

commented Jun 17, 2016

@WorldEd1t i hope you have found the solution about missing icons. Dashing uses among others, google free fonts not just for the text but also for some icons. It seems, but i don't known the cause, that those icons are lost right now on google's open sans.

A fix that i found is to put an other font origin on layout.erb.
Where?
between <header></header>
and just before that line <link href='//fonts.googleapis.com/css?family=Open+Sans:300,400,600,700' rel='stylesheet' type='text/css'>
i use <link href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css" rel="stylesheet" type='text/css'>
but you can use any other font that have the needed character that's missing on google's. Hope that workaround can help you.
Cheers!

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8"/>
  <meta name="description" content="">
  <meta name="viewport" content="width=device-width">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">


  <title><%= yield_content(:title) %></title>

  <!-- The javascript and css are managed by sprockets. The files can be found in the /assets folder-->
  <script type="text/javascript" src="/assets/application.js"></script>
  <link rel="stylesheet" href="/assets/application.css">

  <link href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css" rel="stylesheet" type='text/css'>
  <link href='//fonts.googleapis.com/css?family=Open+Sans:300,400,600,700' rel='stylesheet' type='text/css'>
  <link rel="icon" href="/assets/favicon.ico">
</head>
  <body>
    <div id="container">
      <%= yield %>
    </div>

    <% if development? %>
      <div id="saving-instructions">
        <p>Paste the following at the top of <i><%= params[:dashboard] %>.erb</i></p>
        <textarea id="gridster-code"></textarea>
      </div>
      <a href="#saving-instructions" id="save-gridster">Save this layout</a>
    <% end %>
  </body>
</html>
@joergrech

This comment has been minimized.

Copy link

commented Jun 23, 2016

Is there a solution to checking HTTPS servers with redirects? The comments mention that "redirects from http to https" will result in a "false" return value and I've tried many combinations with different protocols and with and without port numbers. (Note: our servers cannot be pinged)

@Karatyus

This comment has been minimized.

Copy link

commented Jul 12, 2016

Hi everybody.
I'm trying to reach an http URL with port like this
http://myadresse.com:port
In this case, the test is always failed, but I know that this URL is reachable. Any idea?

Thanks a lot

@joshuafernandes

This comment has been minimized.

Copy link

commented Aug 3, 2016

@txaume79, thanks for the little snippet - worked perfectly :)

Hi @joergrech & @Karatyus,
I've forked this to use 'typhoeus' for the http requests bit and cleaned up the job file a bit. Have a look and see if it works for what you want to do?

https://gist.github.com/joshuafernandes

Cheers

@Thibzy

This comment has been minimized.

Copy link

commented Dec 20, 2016

Hi,
@txaume79 , I have meet the same problem of @WorldEd1t.
And I have try your solution. But my icons are steel hidden.
I work with a Raspberry pi 3 in local with a Windows 7 computer (no link to internet).
I can ping him by the command line. But in Dashing I have no icons at all. But the name of server was apparent.
What can I do for solve this ?

Thank for your attention

@Thibzy

This comment has been minimized.

Copy link

commented Jan 5, 2017

@txaume79 I have already try to add a new font manualy to the project by my self but I dont know if I do it properly... maybe somebody have a good tutorial to do it. ?
Because like I am not connected to internet, I am certainly not able to catch the font you advice in your solution.
I'm pretty sure the problem comes of the font because in the source code widget of my dashboard, I can see the variable color changed with the status of my server..
plz any suggestion will be realy appreciate.

@devopsstart

This comment has been minimized.

Copy link

commented Jul 10, 2017

Hi,
I'm having some trouble while trying to executing the server_status.rb.
Getting below error.
in `

': uninitialized constant SCHEDULER (NameError)
What can I do for solving this?

@henryp007

This comment has been minimized.

Copy link

commented Nov 13, 2017

Edit: Looks like I've got it working now from the fix above.

Hi Guys - wondering if I could have some help?
I've loaded the widget and files into Smashing but it doesn't load any of the ping data just the display names of the servers. Any ideas?
I'm currently running the server on Ubuntu

image

@mandelbrotmedia

This comment has been minimized.

Copy link

commented Mar 20, 2018

Hi,

is it possible to switch backgroundcolor to red if one of the serverstats is warning?

@jok0

This comment has been minimized.

Copy link

commented Apr 12, 2018

Hi guys, i have this working although its ticking the servers/computers that are offline. Has anyone had this before?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.