Skip to content

Instantly share code, notes, and snippets.

@gswallow
Created May 24, 2018 18:17
Show Gist options
  • Save gswallow/45da69b7367ed17bd98af89e81128039 to your computer and use it in GitHub Desktop.
Save gswallow/45da69b7367ed17bd98af89e81128039 to your computer and use it in GitHub Desktop.
For Red Hat.
#!/usr/bin/ruby
require 'time'
require 'rest-client'
require 'json'
require 'yaml'
require 'mail'
ME = 'satellite.ivytech.edu'
url = "https://#{ME}"
foreman_url = "#{url}/api/v2"
$email_to = 'its-infra@lists.ivytech.edu'
$username = 'admin'
$password = 'password'
$expire = '864000'.to_i
PER_PAGE = 100
org_name = 'Ivy Tech Community College'
mailer_settings = YAML.load(File.read('/etc/foreman/email.yaml'))['production']
Mail.defaults do
delivery_method mailer_settings['delivery_method'], mailer_settings['smtp_settings'].inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
end
# Performs a GET using the passed URL location
def get_json(location, page = 1)
location = "#{location}?page=#{page}&per_page=#{PER_PAGE}"
response = RestClient::Request.new(
:method => :get,
:url => location,
:user => $username,
:password => $password,
:headers => { :accept => :json,
:content_type => :json }
).execute
JSON.parse(response.to_str)
end
# Has the host checked in within ten days?
def too_late?(host)
last = host.fetch('subscription_facet_attributes', {}).fetch('last_checkin', nil)
if last.nil?
true
else
Time.now.to_i - Time.parse(last).to_i > $expire
end
end
def lineitem(host, style)
red = "border: none; background-color: rgba(255, 99, 71, 0.5); padding: 8px; font-family: Arial, Helvetica, sans-serif"
if too_late?(host)
style = red
end
<<-"EOF"
<tr>
<td style="#{style}">#{host['name']}</td>
<td style="#{style}">#{host.fetch('subscription_facet_attributes', {}).fetch('last_checkin', 'N/A')}</td>
<td style="#{style}">#{host.fetch('content_facet_attributes', {}).fetch('errata_counts', {}).fetch('security', "N/A").to_s}</td>
<td style="#{style}">#{host.fetch('content_facet_attributes', {}).fetch('errata_counts', {}).fetch('bugfix', "N/A").to_s}</td>
<td style="#{style}">#{host.fetch('content_facet_attributes', {}).fetch('upgradable_package_count', "N/A").to_s}</td>
</tr>
EOF
end
def table_def
th_style = "border: none; background-color: DodgerBlue; color: white; font-size: 18px; font-family: Arial, Helvetica, sans-serif; texgt-align: center"
<<-"EOF"
<h1 style="font-family: Arial, Helvetica, sans-serif">Satellite Host Report</h1>
<table style="border-collapse: collapse; width: 100%">
<tr>
<th style="#{th_style}">Host</th>
<th style="#{th_style}">Last Checkin</th>
<th style="#{th_style}">Security</th>
<th style="#{th_style}">Bugfixes</th>
<th style="#{th_style}">Upgradeable</th>
</tr>
EOF
end
def table_end
" </table>"
end
###### Do the things ######
grey = "border: none; background-color: #f2f2f2; padding: 8px; font-family: Arial, Helvetica, sans-serif"
nocolor = "border: none; padding: 8px; font-family: Arial, Helvetica, sans-serif"
color_counter=0
body = table_def
hostcount = get_json("#{foreman_url}/hosts")['total'].to_f
pages = (hostcount / PER_PAGE).ceil
1.upto(pages) do |page|
hosts = get_json("#{foreman_url}/hosts", page)['results']
hosts.each do |host|
next if host['name'].eql? ME
color_counter % 2 == 0 ? body.concat(lineitem(host, nocolor)) : body.concat(lineitem(host, grey))
color_counter += 1
end
end
body.concat(table_end)
mail = Mail.new do
from mailer_settings['smtp_settings']['user_name']
to $email_to
subject "Satellite Host Report"
text_part do
body 'This message is in html format.'
end
html_part do
content_type 'text/html; charset=UTF-8'
body body
end
end
mail.deliver!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment