Class: HostMailer

Inherits:
ActionMailer::Base
  • Object
show all
Defined in:
app/models/host_mailer.rb

Instance Method Summary collapse

Instance Method Details

#error_state(report) ⇒ Object


68
69
70
71
72
73
74
75
76
# File 'app/models/host_mailer.rb', line 68

def error_state(report)
  host = report.host
  email = host.owner.recipients if SETTINGS[:login] and not host.owner.nil?
  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

#summary(options = {}) ⇒ Object


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/models/host_mailer.rb', line 8

def summary(options = {})
  # currently we send to all registered users or to the administrator (if LDAP is disabled).
  # TODO add support to host / group based emails.

  # 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

  if options[:env]
    hosts = envhosts = options[:env].hosts
    raise (_("unable to find any hosts for puppet environment=%s") % env) if envhosts.size == 0
    filter << "Environment=#{options[:env].name}"
  end
  name,value = options[:factname],options[:factvalue]
  if name and value
    facthosts = Host.search_for("facts.#{name}=#{value}")
    raise (_("unable to find any hosts with the fact name=%{name} and value=%{value}") % { :name => name, :value => value }) if facthosts.empty?
    filter << "Fact #{name}=#{value}"
    # if environment and facts are defined together, we use a merge of both
    hosts = envhosts.empty? ? facthosts : envhosts & facthosts
  end

  if hosts.empty?
    # print out an error if we couldn't find any hosts that match our request
    raise ::Foreman::Exception.new(N_("unable to find any hosts that match your request")) if options[:env] or options[:factname]
    # we didnt define a filter, use all hosts instead
    hosts = Host::Managed
  end
  email = options[:email] || Setting[:administrator]
  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 = 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"],
    :restarted => total_metrics["restarted"],
    :skipped => total_metrics["skipped"],
    :applied => total_metrics["applied"],
    :failed_restarts => total_metrics["failed_restarts"],
    :total => total_metrics["total"]
  }
  @hosts = host_data
  @timerange = time
  @out_of_sync = hosts.out_of_sync
  @disabled = hosts.alerts_disabled
  @filter = filter
  mail(:to => email, :subject => subject)
end