Module: ForemanMonitoring::HostsControllerExtensions

Defined in:
app/controllers/concerns/foreman_monitoring/hosts_controller_extensions.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/controllers/concerns/foreman_monitoring/hosts_controller_extensions.rb', line 5

def self.prepended(base)
  base.class_eval do
    before_action :find_resource_with_monitoring, :only => [:downtime]
    before_action :find_multiple_with_monitoring, :only => %i[select_multiple_downtime update_multiple_downtime
                                                              select_multiple_monitoring_proxy update_multiple_monitoring_proxy]
    before_action :validate_host_downtime_params, :only => [:downtime]
    before_action :validate_hosts_downtime_params, :only => [:update_multiple_downtime]
    before_action :validate_multiple_monitoring_proxy, :only => :update_multiple_monitoring_proxy

    alias_method :find_resource_with_monitoring, :find_resource
    alias_method :find_multiple_with_monitoring, :find_multiple
  end
end

Instance Method Details

#downtimeObject



19
20
21
22
23
24
25
# File 'app/controllers/concerns/foreman_monitoring/hosts_controller_extensions.rb', line 19

def downtime
  unless @host.downtime_host(downtime_options)
    process_error(:redirect => host_path, :error_msg => @host.errors.full_messages.to_sentence)
    return false
  end
  process_success :success_msg => _('Created downtime for %s') % @host, :success_redirect => :back
end

#select_multiple_downtimeObject



27
# File 'app/controllers/concerns/foreman_monitoring/hosts_controller_extensions.rb', line 27

def select_multiple_downtime; end

#select_multiple_monitoring_proxyObject



63
# File 'app/controllers/concerns/foreman_monitoring/hosts_controller_extensions.rb', line 63

def select_multiple_monitoring_proxy; end

#update_multiple_downtimeObject



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
# File 'app/controllers/concerns/foreman_monitoring/hosts_controller_extensions.rb', line 29

def update_multiple_downtime
  failed_hosts = {}

  @hosts.each do |host|
    unless host.monitored?
      failed_hosts[host.name] = _('is not monitored')
      next
    end
    begin
      unless host.downtime_host(downtime_options)
        error_message = host.errors.full_messages.to_sentence
        failed_hosts[host.name] = error_message
        logger.error "Failed to set a host downtime for #{host}: #{error_message}"
      end
    rescue StandardError => e
      failed_hosts[host.name] = e
      Foreman::Logging.exception(_('Failed to set a host downtime for %s.') % host, e)
    end
  end

  if failed_hosts.empty?
    success _('A downtime was set for the selected hosts.')
  else
    error n_('A downtime clould not be set for host: %s.',
             'A downtime could not be set for hosts: %s.',
             failed_hosts.count) % failed_hosts.map { |h, err| "#{h} (#{err})" }.to_sentence
  end
  redirect_back_or_to hosts_path
end

#update_multiple_monitoring_proxyObject



65
66
67
# File 'app/controllers/concerns/foreman_monitoring/hosts_controller_extensions.rb', line 65

def update_multiple_monitoring_proxy
  update_multiple_proxy(_('Monitoring'), :monitoring_proxy=)
end

#update_multiple_power_stateObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/controllers/concerns/foreman_monitoring/hosts_controller_extensions.rb', line 69

def update_multiple_power_state
  options = {
    :comment => 'Power state changed in Foreman',
    :author => "Foreman User #{User.current}",
    :start_time => Time.current.to_i,
    :end_time => Time.current.advance(:minutes => 30).to_i
  }
  set_downtime = Foreman::Cast.to_bool(params[:power][:set_downtime])
  if set_downtime && User.current.allowed_to?(:controller => :hosts, :action => :select_multiple_downtime)
    @hosts.each do |host|
      unless host.monitored?
        logger.debug "Not setting a downtime for #{host} as it is not monitored."
        next
      end
      if host.downtime_host(options)
        logger.debug "Set a host downtime for #{host}."
      else
        logger.error "Failed to set a host downtime for #{host}: #{host.errors.full_messages.to_sentence}"
      end
    end
  end
  super
end

#validate_multiple_monitoring_proxyObject



59
60
61
# File 'app/controllers/concerns/foreman_monitoring/hosts_controller_extensions.rb', line 59

def validate_multiple_monitoring_proxy
  validate_multiple_proxy(select_multiple_monitoring_proxy_hosts_path)
end