Project

General

Profile

« Previous | Next » 

Revision d70eecfe

Added by Daniel Lobato Garcia over 10 years ago

fixes #3059 - new mail rake task sends foreman reports to each user

View differences:

app/mailers/host_mailer.rb
# options our host list if required
filter = []
if (@url = Setting[:foreman_url]).empty?
raise ::Foreman::Exception.new N_("':foreman_url:' entry in Foreman configuration file, see http://theforeman.org/projects/foreman/wiki/Mail_Notifications")
end
set_url
if options[:env]
hosts = envhosts = options[:env].hosts
......
raise ::Foreman::Exception.new(N_("unable to find recipients")) if email.empty?
time = options[:time] || 1.day.ago
host_data = Report.summarise(time, hosts.all).sort
total_metrics = {"failed"=>0, "restarted"=>0, "skipped"=>0, "applied"=>0, "failed_restarts"=>0}
host_data.flatten.delete_if { |x| true unless x.is_a?(Hash) }.each do |data_hash|
total_metrics["failed"] += data_hash[:metrics]["failed"]
total_metrics["restarted"] += data_hash[:metrics]["restarted"]
total_metrics["skipped"] += data_hash[:metrics]["skipped"]
total_metrics["applied"] += data_hash[:metrics]["applied"]
total_metrics["failed_restarts"] += data_hash[:metrics]["failed_restarts"]
end
total_metrics = load_metrics(host_data)
total = 0 ; total_metrics.values.each { |v| total += v }
subject = _("Summary Puppet report from Foreman - F:%{failed} R:%{restarted} S:%{skipped} A:%{applied} FR:%{failed_restarts} T:%{total}") % {
:failed => total_metrics["failed"],
......
@out_of_sync = hosts.out_of_sync
@disabled = hosts.alerts_disabled
@filter = filter
mail(:to => email, :subject => subject)
mail(:to => email,
:from => Setting["email_reply_address"],
:subject => subject,
:date => Time.now )
end
def error_state(report)
host = report.host
email = host.owner.recipients if SETTINGS[:login] and not host.owner.nil?
email = host.owner.recipients if SETTINGS[:login] && host.owner.present?
email = Setting[:administrator] if email.empty?
raise ::Foreman::Exception.new(N_("unable to find recipients")) if email.empty?
@report = report
@host = host
mail(:to => email, :subject => (_("Puppet error on %s") % host.to_label))
end
def failed_runs(user, options = {})
set_url
time = options[:time] || 1.day.ago
host_data = Report.summarise(time, user.hosts).sort
total_metrics = load_metrics(host_data)
total = 0 ; total_metrics.values.each { |v| total += v }
@hosts = host_data.sort_by { |h| h[1][:metrics]['failed'] }.reverse
@timerange = time
@out_of_sync = Host.out_of_sync.select { |h| h.owner == user }
@disabled = Host.alerts_disabled.select { |h| h.owner == user }
mail(:to => user.mail,
:from => Setting["email_reply_address"],
:subject => _("Summary Puppet report from Foreman - F:#{total_metrics["failed"]} R:#{total_metrics["restarted"]} S:#{total_metrics["skipped"]} A:#{total_metrics["applied"]} FR:#{total_metrics["failed_restarts"]} T:#{total}"),
:date => Time.now )
end
def set_url
if (@url = Setting[:foreman_url]).empty?
raise ":foreman_url is not set, please configure in the Foreman Web UI (More -> Settings -> General)"
end
end
def load_metrics(host_data)
total_metrics = {"failed"=>0, "restarted"=>0, "skipped"=>0, "applied"=>0, "failed_restarts"=>0}
host_data.flatten.delete_if { |x| true unless x.is_a?(Hash) }.each do |data_hash|
total_metrics["failed"] += data_hash[:metrics]["failed"]
total_metrics["restarted"] += data_hash[:metrics]["restarted"]
total_metrics["skipped"] += data_hash[:metrics]["skipped"]
total_metrics["applied"] += data_hash[:metrics]["applied"]
total_metrics["failed_restarts"] += data_hash[:metrics]["failed_restarts"]
end
total_metrics
end
end
app/views/host_mailer/_active_hosts.html.erb
text-align:center;
background-color: #FF9933;">Hostname
</th>
<th style="border: 1px solid #FF9933;
border-collapse: collapse;
padding: 4px;
text-align:center;
background-color: #FF9933;">Host group
</th>
<th style="border: 1px solid #FF9933;
border-collapse: collapse;
padding: 4px;
text-align:center;
background-color: #FF9933;">Environment
</th>
<% list.first.last[:metrics].keys.each do |header| -%>
<th style="border: 1px solid #FF9933;
border-collapse: collapse;
......
</tr>
<% list.each do |host,params| -%>
<tr>
<% host_object = Host.find_by_name(host) %>
<%= render 'link_to_host', :host => host %>
<td style="border:1px solid #FF9933;border-collapse:collapse;padding:4px;background-color:#FFFFFF;">
<%= host_object.hostgroup %>
</td>
<td style="border:1px solid #FF9933;border-collapse:collapse;padding:4px;background-color:#FFFFFF;">
<%= host_object.environment %>
</td>
<% params[:metrics].each do |m,v| -%>
<% if m =~ /failed|failed_restart/ and v > 0 -%>
<td style="color:red;font-weight:bold;border:1px solid #FF9933;border-collapse:collapse;padding:4px;background-color:#FFFFFF;">
......
<%= v %>
<% end -%>
</td>
<% end -%>
</tr>
<% end -%>
</tr>
<% end -%>
app/views/host_mailer/_nonactive_hosts.html.erb
padding: 4px;
background-color: #FF0000;">Hostname
</th>
<th style="border: 1px solid #FF9933;
border-collapse: collapse;
padding: 4px;
text-align:center;
background-color: #FF0000;">Host group
</th>
<th style="border: 1px solid #FF9933;
border-collapse: collapse;
padding: 4px;
text-align:center;
background-color: #FF0000;">Environment
</th>
<th style="border: 1px solid #FF9933;
border-collapse: collapse;
text-align:center;
......
<% list.each do |host| -%>
<tr>
<%= render 'link_to_host', :host => host %>
<td style="border:1px solid #FF9933;border-collapse:collapse;padding:4px;background-color:#FFFFFF;">
<%= host.hostgroup %>
</td>
<td style="border:1px solid #FF9933;border-collapse:collapse;padding:4px;background-color:#FFFFFF;">
<%= host.environment %>
</td>
<% if host.last_report -%>
<td style="border: 1px solid #FF9933;
border-collapse: collapse;
app/views/host_mailer/failed_runs.html.erb
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%# this view contains style as some email clients ignore css files -%>
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
<title> Summary report for Puppet activity from Foreman </title>
</head>
<body style="background-color: #ffffff;">
<h1 style='font-family: Arial,"Times new roman"; font-size: 90%;'>Summary from <%= distance_of_time_in_words (Time.now - @timerange) %> ago to now</h1>
<% unless @filter.empty? -%>
<h2 style='font-family: Arial,"Times new roman"; font-size: 90%;'>Filtering resulted based on <%= @filter.join(" and ") -%></h2>
<% end -%>
<%= render :partial => 'list', :locals => {:state => 'active', :title => "Hosts with interesting values (changed, failures etc)", :list => @hosts} %>
<%= render :partial => 'list', :locals => {:state => 'nonactive', :title => "Hosts which are currently not running puppet", :list => @out_of_sync } %>
<%= render :partial => 'list', :locals => {:state => 'nonactive', :title => "Hosts which Foreman reporting is disabled", :list => @disabled } %>
</body>
</html>
lib/tasks/reports.rake
HostMailer.summary(options).deliver
end
end
desc <<-END_DESC
Sends a periodic digest to every user with a list of hosts with failed puppet runs
Available conditions:
* days => number of days to scan backwards (defaults to 1)
* hours => number of hours to scan backwards (defaults to disabled)
Example:
# Sends out a summary email for the last 3 days.
rake reports:failed_runs days=3 RAILS_ENV="production" # Sends out a summary email for the last 3 days.
# Sends out a summary email for the last 12 hours.
rake reports:failed_runs hours=12 RAILS_ENV="production" # Sends out a summary email for the last 12 hours.
END_DESC
namespace :reports do
task :failed_runs => :environment do
options = {}
time = ENV['hours'].to_i.hours.ago if ENV['hours']
time = ENV['days'].to_i.days.ago if ENV['days']
options[:time] = time if time
users = User.select { |u| u.hosts.length > 0 }
users.each do |user|
HostMailer.failed_runs(user, options).deliver
end
end
end

Also available in: Unified diff