Skip to content

Instantly share code, notes, and snippets.

@hdoedens
Last active February 27, 2017 06:05
Show Gist options
  • Save hdoedens/df55ad940857f911793d to your computer and use it in GitHub Desktop.
Save hdoedens/df55ad940857f911793d to your computer and use it in GitHub Desktop.
dashing foreman job

#Dashing job to retrieve Foreman data

This job executes a list of queries to the Foreman API so you can optionally filter out the interesting stuff.

Sample job to retrieve foreman data. Data can be send to any widget. The example widget below is a slight modification of the default List widget.

#!/usr/bin/env ruby
require 'net/http'
require 'json'
require 'time'
FOREMAN_API_ENDPOINT = "https://url_to_foreman/api/"
# Set user and password if you want to enable authentication.
# Otherwise, leave them blank.
FOREMAN_API_USER = ''
FOREMAN_API_PASSWORD = ''
queries = {
'disabled' => 'status.enabled = false',
'error' => 'last_report > "35 minutes ago" and (status.failed > 0 or status.failed_restarts > 0 or status.skipped > 0)',
'out of sync' => 'last_report < "30 minutes ago" and status.enabled = true',
}
logger = Logger.new "#{Dir.pwd}/log/foreman.log"
logger.formatter = Logger::Formatter.new
logger.formatter.datetime_format = "%Y-%m-%d %H:%M:%S.%3N"
logger.level = Logger::ERROR
SCHEDULER.every '30s', :first_in => 0 do |job|
data = []
queries.each { |name, query|
auth = (FOREMAN_API_USER.empty? || FOREMAN_API_PASSWORD.empty?) ? false : true
uri = URI.parse(URI.escape(FOREMAN_API_ENDPOINT+"hosts/?format=json&search=#{query}"))
req = Net::HTTP::Get.new(uri)
req.basic_auth FOREMAN_API_USER, FOREMAN_API_PASSWORD if auth
logger.info "Started querying foreman"
response = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https', :verify_mode => OpenSSL::SSL::VERIFY_NONE) {|http|
http.request(req)
}
logger.debug("Response: #{response.code} - #{response.message}")
hosts = JSON.parse(response.body)
logger.info("#{hosts['subtotal']} #{name} hosts found, out of a total of #{hosts['total']}")
# hosts['results'].each do |host|
# // do some more magic here
# end
data.push({ label: name, value: hosts['subtotal']})
}
send_event('foreman', {items: data})
end
class Dashing.Foreman_host_list extends Dashing.Widget
ready: ->
if @get('unordered')
$(@node).find('ol').remove()
else
$(@node).find('ul').remove()
onData: (data) ->
background_color = '#12b0c5'
for item in data['items']
console.log(item['label'])
if item['label'] == 'Error' && item['value'] > 10
background_color = 'red'
else if item['label'] == 'Disabled' && item['value'] > 0
background_color = 'orange'
else if item['label'] == 'Out of sync' && item['value'] > 0
background_color = 'orange'
$(@node).fadeOut().css('background-color', background_color).fadeIn()
<h1 class="title" data-bind="title"></h1>
<ol>
<li data-foreach-item="items">
<span class="label" data-bind="item.label"></span>
<span class="value" data-bind="item.value"></span>
</li>
</ol>
<ul class="list-nostyle">
<li data-foreach-item="items">
<span class="label" data-bind="item.label"></span>
<span class="value" data-bind="item.value"></span>
</li>
</ul>
<p class="more-info" data-bind="moreinfo"></p>
<p class="updated-at" data-bind="updatedAtMessage"></p>
// ----------------------------------------------------------------------------
// 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-foreman-host-list {
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment