foreman_monitoring 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3d9186b9fa48b695bea2617e3c589b22d0df22bd
4
- data.tar.gz: f3dd5db766d37990e59d09ffdf9881a830abc134
3
+ metadata.gz: 6ec30b6e40aad99c7da94b66100590381d300289
4
+ data.tar.gz: ef1bb20065f0c5b11544f64babe2ab0c8faa3b6d
5
5
  SHA512:
6
- metadata.gz: 25700b1a96cebf6cfb1e829b54dfef9f6fd6f934ffbbd1e8a9fa753ee9e51f0a406c478c0bdba8e8cd2b46bbeaf5ae3d2be0a06da9524ea77740b8e042e32f61
7
- data.tar.gz: 1f714e3081f6adeb50b7f01f30c6855dc237f3613210993719fc0f0d97ed34828190f58c784e9ac93a6afb6c54d2049c1bd5b82f7e76d088553b3af7ae8f1479
6
+ metadata.gz: 92fc91db5150b641f8a169fce2468695a4a64e84ec02235259385864deb0c1b7309e49853fb297a88f9e37d143371f88d6aa446c37abebc8d8a2172433a725d1
7
+ data.tar.gz: 9306c61272155529c75cd7c90b9a115a5d931b11455f0313fa057de42ac565a3a301023cbd95fc8fa744d913e570ea8a8ad3c689f042c45aa5a6d4dc00d944e2
data/README.md CHANGED
@@ -17,7 +17,16 @@ The gem name is `foreman_monitoring`.
17
17
 
18
18
  RPM users can install the `tfm-rubygem-foreman_monitoring` package.
19
19
 
20
- This plug-in has not been packaged for Debian, yet.
20
+ Deb users can install the `ruby-foreman_monitoring` package.
21
+
22
+ If using the gem as installation source database enhancement and service restart must
23
+ be done manually.
24
+
25
+ ```
26
+ foreman-rake db:migrate
27
+ foreman-rake db:seed
28
+ touch ~foreman/tmp/restart.txt
29
+ ```
21
30
 
22
31
  # Usage
23
32
 
@@ -37,6 +46,14 @@ panel. You can configure if the sub-status should affect the global status.
37
46
  Furthermore it allows to individually set a downtime at the host detail view
38
47
  or as a bulk action from the host overview.
39
48
 
49
+ # Troubleshooting
50
+
51
+ Logging entries relevant to the plug-in will be located in the Foreman's log
52
+ which is by default `/var/log/foreman/production.log`.
53
+
54
+ Also check the troubleshooting section of the Smart Proxy plug-in if problems
55
+ occure in the underlying communication.
56
+
40
57
  ## Contributing
41
58
 
42
59
  Fork and send a Pull Request. Thanks!
data/Rakefile CHANGED
File without changes
@@ -12,7 +12,8 @@ module Api
12
12
  param :timestamp, String, :desc => N_('Timestamp of the results')
13
13
  param :acknowledged, [true, false], :desc => N_('Is the result acknowledged?')
14
14
  param :downtime, [true, false], :desc => N_('Is the result in downtime?')
15
- param :result, [0, 1, 2, 3], :desc => N_('State of the monitoring result (0 -> ok, 1 -> warning, 2 -> critical, 3 -> unknown)')
15
+ param :result, [0, 1, 2, 3],
16
+ :desc => N_('State of the monitoring result (0 -> ok, 1 -> warning, 2 -> critical, 3 -> unknown)')
16
17
 
17
18
  def create
18
19
  begin
@@ -1,18 +1,17 @@
1
1
  module ForemanMonitoring
2
2
  module HostsControllerExtensions
3
- extend ActiveSupport::Concern
4
-
5
- included do
6
- before_action :find_resource_with_monitoring, :only => [:downtime]
7
- before_action :find_multiple_with_monitoring, :only => [:select_multiple_downtime, :update_multiple_downtime,
8
- :select_multiple_monitoring_proxy, :update_multiple_monitoring_proxy]
9
- before_action :validate_host_downtime_params, :only => [:downtime]
10
- before_action :validate_hosts_downtime_params, :only => [:update_multiple_downtime]
11
- before_action :validate_multiple_monitoring_proxy, :only => :update_multiple_monitoring_proxy
12
-
13
- alias_method :find_resource_with_monitoring, :find_resource
14
- alias_method :find_multiple_with_monitoring, :find_multiple
15
- alias_method_chain :update_multiple_power_state, :monitoring
3
+ def self.prepended(base)
4
+ base.class_eval do
5
+ before_action :find_resource_with_monitoring, :only => [:downtime]
6
+ before_action :find_multiple_with_monitoring, :only => %i[select_multiple_downtime update_multiple_downtime
7
+ select_multiple_monitoring_proxy update_multiple_monitoring_proxy]
8
+ before_action :validate_host_downtime_params, :only => [:downtime]
9
+ before_action :validate_hosts_downtime_params, :only => [:update_multiple_downtime]
10
+ before_action :validate_multiple_monitoring_proxy, :only => :update_multiple_monitoring_proxy
11
+
12
+ alias_method :find_resource_with_monitoring, :find_resource
13
+ alias_method :find_multiple_with_monitoring, :find_multiple
14
+ end
16
15
  end
17
16
 
18
17
  def downtime
@@ -20,11 +19,10 @@ module ForemanMonitoring
20
19
  process_error(:redirect => host_path, :error_msg => @host.errors.full_messages.to_sentence)
21
20
  return false
22
21
  end
23
- process_success :success_msg => _('Created downtime for %s') % (@host), :success_redirect => :back
22
+ process_success :success_msg => _('Created downtime for %s') % @host, :success_redirect => :back
24
23
  end
25
24
 
26
- def select_multiple_downtime
27
- end
25
+ def select_multiple_downtime; end
28
26
 
29
27
  def update_multiple_downtime
30
28
  failed_hosts = {}
@@ -60,21 +58,21 @@ module ForemanMonitoring
60
58
  validate_multiple_proxy(select_multiple_monitoring_proxy_hosts_path)
61
59
  end
62
60
 
63
- def select_multiple_monitoring_proxy
64
- end
61
+ def select_multiple_monitoring_proxy; end
65
62
 
66
63
  def update_multiple_monitoring_proxy
67
64
  update_multiple_proxy(_('Monitoring'), :monitoring_proxy=)
68
65
  end
69
66
 
70
- def update_multiple_power_state_with_monitoring
67
+ def update_multiple_power_state
71
68
  options = {
72
69
  :comment => 'Power state changed in Foreman',
73
70
  :author => "Foreman User #{User.current}",
74
- :start_time => DateTime.now.to_time.to_i,
75
- :end_time => DateTime.now.advance(:minutes => 30).to_time.to_i
71
+ :start_time => Time.current.to_i,
72
+ :end_time => Time.current.advance(:minutes => 30).to_i
76
73
  }
77
- if User.current.allowed_to?(:controller => :hosts, :action => :select_multiple_downtime) && params[:power][:set_downtime]
74
+ set_downtime = Foreman::Cast.to_bool(params[:power][:set_downtime])
75
+ if set_downtime && User.current.allowed_to?(:controller => :hosts, :action => :select_multiple_downtime)
78
76
  @hosts.each do |host|
79
77
  unless host.monitored?
80
78
  logger.debug "Not setting a downtime for #{host} as it is not monitored."
@@ -87,7 +85,7 @@ module ForemanMonitoring
87
85
  end
88
86
  end
89
87
  end
90
- update_multiple_power_state_without_monitoring
88
+ super
91
89
  end
92
90
 
93
91
  private
@@ -96,8 +94,8 @@ module ForemanMonitoring
96
94
  {
97
95
  :comment => params[:downtime][:comment],
98
96
  :author => "Foreman User #{User.current}",
99
- :start_time => DateTime.parse(params[:downtime][:starttime]).to_time.to_i,
100
- :end_time => DateTime.parse(params[:downtime][:endtime]).to_time.to_i
97
+ :start_time => Time.zone.parse(params[:downtime][:starttime]).to_i,
98
+ :end_time => Time.zone.parse(params[:downtime][:endtime]).to_i
101
99
  }
102
100
  end
103
101
 
@@ -118,20 +116,21 @@ module ForemanMonitoring
118
116
  process_error(:redirect => redirect_url, :error_msg => 'No start/endtime for downtime!')
119
117
  return false
120
118
  end
121
- begin
122
- DateTime.parse(params[:downtime][:starttime])
123
- DateTime.parse(params[:downtime][:endtime])
124
- rescue ArgumentError
119
+ starttime = Time.zone.parse(params[:downtime][:starttime])
120
+ endtime = Time.zone.parse(params[:downtime][:endtime])
121
+ if starttime.nil? || endtime.nil? || starttime >= endtime
125
122
  process_error(:redirect => redirect_url, :error_msg => 'Invalid start/endtime for downtime!')
126
123
  return false
127
124
  end
125
+ true
128
126
  end
129
127
 
130
128
  def action_permission
131
129
  case params[:action]
132
- when 'downtime', 'select_multiple_downtime', 'update_multiple_downtime',
133
- 'select_multiple_monitoring_proxy', 'update_multiple_monitoring_proxy'
130
+ when 'downtime', 'select_multiple_downtime', 'update_multiple_downtime'
134
131
  :downtime
132
+ when 'select_multiple_monitoring_proxy', 'update_multiple_monitoring_proxy'
133
+ :edit
135
134
  else
136
135
  super
137
136
  end
@@ -1,18 +1,17 @@
1
1
  module ForemanMonitoring
2
2
  module HostsHelperExt
3
- extend ActiveSupport::Concern
4
-
5
- included do
6
- alias_method_chain :host_title_actions, :monitoring
7
- alias_method_chain :multiple_actions, :monitoring
8
- end
9
-
10
- def multiple_actions_with_monitoring
11
- return multiple_actions_without_monitoring unless authorized_for(:controller => :hosts, :action => :select_multiple_downtime)
12
- multiple_actions_without_monitoring + [[_('Set downtime'), select_multiple_downtime_hosts_path], [_('Change Monitoring Proxy'), select_multiple_monitoring_proxy_hosts_path]]
3
+ def multiple_actions
4
+ actions = super
5
+ if authorized_for(:controller => :hosts, :action => :select_multiple_downtime)
6
+ actions << [_('Set downtime'), select_multiple_downtime_hosts_path]
7
+ end
8
+ if authorized_for(:controller => :hosts, :action => :select_multiple_monitoring_proxy)
9
+ actions << [_('Change Monitoring Proxy'), select_multiple_monitoring_proxy_hosts_path]
10
+ end
11
+ actions
13
12
  end
14
13
 
15
- def host_title_actions_with_monitoring(host)
14
+ def host_title_actions(host)
16
15
  title_actions(
17
16
  button_group(
18
17
  display_link_if_authorized(_('Downtime'),
@@ -24,12 +23,10 @@ module ForemanMonitoring
24
23
  :title => _('Set a downtime for this host'),
25
24
  :id => 'host-downtime',
26
25
  :data => { :toggle => 'modal',
27
- :target => '#set_host_downtime'
28
- }
29
- )
26
+ :target => '#set_host_downtime' })
30
27
  )
31
28
  )
32
- host_title_actions_without_monitoring(host)
29
+ super
33
30
  end
34
31
 
35
32
  def host_monitoring_result_icon_class(result)
@@ -18,19 +18,19 @@ module ProxyAPI
18
18
  end
19
19
 
20
20
  def create_host(host, attributes = {})
21
- parse(put({:attributes => attributes}, "host/#{host}"))
21
+ parse(put({ :attributes => attributes }, "host/#{host}"))
22
22
  rescue => e
23
23
  raise ProxyException.new(url, e, N_('Unable to create monitoring host object for %s') % host)
24
24
  end
25
25
 
26
26
  def update_host(host, attributes = {})
27
- parse(post({:attributes => attributes}, "host/#{host}"))
27
+ parse(post({ :attributes => attributes }, "host/#{host}"))
28
28
  rescue => e
29
29
  raise ProxyException.new(url, e, N_('Unable to update monitoring host object for %s') % host)
30
30
  end
31
31
 
32
32
  def delete_host(host)
33
- raise Foreman::Exception.new('Missing hostname.') if host.blank?
33
+ raise Foreman::Exception, 'Missing hostname.' if host.blank?
34
34
  parse(delete("host/#{host}"))
35
35
  rescue RestClient::ResourceNotFound
36
36
  true
@@ -43,7 +43,7 @@ module ProxyAPI
43
43
  rescue RestClient::ResourceNotFound
44
44
  nil
45
45
  rescue => e
46
- raise ProxyException.new(url, e, N_('Unable to query monitoring host object for %s') % host)
46
+ raise ProxyException.new(url, e, N_('Unable to query monitoring host object for %{host}: %{message}') % { :host => host, :message => e.try(:response) || e.try(:message) })
47
47
  end
48
48
  end
49
49
  end
@@ -1,15 +1,13 @@
1
1
  module ForemanMonitoring
2
2
  module HostExtensions
3
- extend ActiveSupport::Concern
4
- included do
5
- include Orchestration::Monitoring
3
+ def self.prepended(base)
4
+ base.class_eval do
5
+ include Orchestration::Monitoring
6
6
 
7
- after_build :downtime_host_build
7
+ after_build :downtime_host_build
8
8
 
9
- alias_method_chain :smart_proxy_ids, :monitoring_proxy
10
- alias_method_chain :hostgroup_inherited_attributes, :monitoring
11
-
12
- has_many :monitoring_results, :dependent => :destroy, :foreign_key => 'host_id'
9
+ has_many :monitoring_results, :dependent => :destroy, :foreign_key => 'host_id'
10
+ end
13
11
  end
14
12
 
15
13
  def monitoring_status(options = {})
@@ -42,12 +40,12 @@ module ForemanMonitoring
42
40
  monitoring_proxy.present?
43
41
  end
44
42
 
45
- def hostgroup_inherited_attributes_with_monitoring
46
- hostgroup_inherited_attributes_without_monitoring + ['monitoring_proxy_id']
43
+ def hostgroup_inherited_attributes
44
+ super + ['monitoring_proxy_id']
47
45
  end
48
46
 
49
- def smart_proxy_ids_with_monitoring_proxy
50
- ids = smart_proxy_ids_without_monitoring_proxy
47
+ def smart_proxy_ids
48
+ ids = super
51
49
  [monitoring_proxy, hostgroup.try(:monitoring_proxy)].compact.each do |proxy|
52
50
  ids << proxy.id
53
51
  end
@@ -11,48 +11,55 @@ module Orchestration::Monitoring
11
11
  def queue_monitoring
12
12
  return unless monitored? && errors.empty?
13
13
  clear_monitoring_object
14
- !monitoring_object.has_key?(:attrs) ? queue_monitoring_create : queue_monitoring_update
14
+ !monitoring_object.key?(:attrs) ? queue_monitoring_create : queue_monitoring_update
15
15
  end
16
16
 
17
17
  def queue_monitoring_create
18
- queue.create(:name => _("Create monitoring object for %s") % self, :priority => 20,
19
- :action => [self, :setMonitoring]) if ::Monitoring.create_action?(:create)
18
+ return true unless ::Monitoring.create_action?(:create)
19
+ queue.create(:name => _('Create monitoring object for %s') % self, :priority => 20,
20
+ :action => [self, :setMonitoring])
20
21
  end
21
22
 
22
23
  def queue_monitoring_update
23
24
  return unless monitoring_update_required?(monitoring_object[:attrs], monitoring_attributes)
24
- Rails.logger.debug("Detected a change to the monitoring object is required.")
25
- queue.create(:name => _("Monitoring update for %s") % old, :priority => 2,
26
- :action => [self, :setMonitoringUpdate]) if ::Monitoring.create_action?(:create)
25
+ Rails.logger.debug('Detected a change to the monitoring object is required.')
26
+ if ::Monitoring.create_action?(:create)
27
+ queue.create(:name => _('Monitoring update for %s') % old, :priority => 2,
28
+ :action => [self, :setMonitoringUpdate])
29
+ end
27
30
  end
28
31
 
29
32
  def queue_monitoring_destroy
30
33
  return unless monitored? && errors.empty?
31
- queue.create(:name => _("Removing monitoring object for %s") % self, :priority => 2,
32
- :action => [self, :delMonitoring]) if ::Monitoring.delete_action?(:delete)
33
- queue.create(:name => _("Set monitoring downtime for %s") % self, :priority => 2,
34
- :action => [self, :setMonitoringDowntime]) if ::Monitoring.delete_action?(:downtime)
34
+ if ::Monitoring.delete_action?(:delete)
35
+ queue.create(:name => _('Removing monitoring object for %s') % self, :priority => 2,
36
+ :action => [self, :delMonitoring])
37
+ end
38
+ if ::Monitoring.delete_action?(:downtime)
39
+ queue.create(:name => _('Set monitoring downtime for %s') % self, :priority => 2,
40
+ :action => [self, :setMonitoringDowntime])
41
+ end
35
42
  end
36
43
 
37
44
  def setMonitoring
38
45
  Rails.logger.info "Adding Monitoring object for #{name}"
39
46
  monitoring.create_host(self)
40
47
  rescue => e
41
- failure _("Failed to create a monitoring object %{name}: %{message}\n ") % { :name => name, :message => e.message }, e
48
+ failure format(_("Failed to create a monitoring object %{name}: %{message}\n "), :name => name, :message => e.message), e
42
49
  end
43
50
 
44
51
  def delMonitoring
45
52
  Rails.logger.info "Deleting Monitoring object for #{name}"
46
53
  monitoring.delete_host(self)
47
54
  rescue => e
48
- failure _("Failed to delete a monitoring object %{name}: %{message}\n ") % { :name => name, :message => e.message }, e
55
+ failure format(_("Failed to delete a monitoring object %{name}: %{message}\n "), :name => name, :message => e.message), e
49
56
  end
50
57
 
51
58
  def setMonitoringUpdate
52
59
  Rails.logger.info "Updating Monitoring object for #{name}"
53
60
  monitoring.update_host(self)
54
61
  rescue => e
55
- failure _("Failed to update a monitoring object %{name}: %{message}\n ") % { :name => name, :message => e.message }, e
62
+ failure format(_("Failed to update a monitoring object %{name}: %{message}\n "), :name => name, :message => e.message), e
56
63
  end
57
64
 
58
65
  def delMonitoringUpdate; end
@@ -61,14 +68,14 @@ module Orchestration::Monitoring
61
68
  Rails.logger.info "Setting Monitoring downtime for #{name}"
62
69
  monitoring.set_downtime_host(self, monitoring_downtime_defaults)
63
70
  rescue => e
64
- failure _("Failed to set a monitoring downtime for %{name}: %{message}\n ") % { :name => name, :message => e.message }, e
71
+ failure format(_("Failed to set a monitoring downtime for %{name}: %{message}\n "), :name => name, :message => e.message), e
65
72
  end
66
73
 
67
74
  def delMonitoringDowntime
68
75
  Rails.logger.info "Deleting Monitoring downtime for #{name}"
69
76
  monitoring.del_downtime_host(self, monitoring_downtime_defaults)
70
77
  rescue => e
71
- failure _("Failed to set a monitoring downtime for %{name}: %{message}\n ") % { :name => name, :message => e.message }, e
78
+ failure format(_("Failed to set a monitoring downtime for %{name}: %{message}\n "), :name => name, :message => e.message), e
72
79
  end
73
80
 
74
81
  def monitoring_object
@@ -99,7 +106,7 @@ module Orchestration::Monitoring
99
106
  end
100
107
  desired_v
101
108
  end
102
- Rails.logger.debug "No monitoring update required."
109
+ Rails.logger.debug 'No monitoring update required.'
103
110
  false
104
111
  end
105
112
  end
@@ -14,6 +14,7 @@ module HostStatus
14
14
  grouped_results.each do |resultset, _count|
15
15
  result, downtime, acknowledged = resultset
16
16
  next if downtime
17
+ result = map_result_to_status(result)
17
18
  result = WARNING if acknowledged || result == UNKNOWN
18
19
  state = result if result > state
19
20
  end
@@ -70,7 +71,21 @@ module HostStatus
70
71
  private
71
72
 
72
73
  def grouped_results
73
- host.monitoring_results.group([:result, :downtime, :acknowledged]).count
74
+ host.monitoring_results.group(%i[result downtime acknowledged]).count
75
+ end
76
+
77
+ def map_result_to_status(result)
78
+ return result if Rails::VERSION::MAJOR < 5
79
+ case result.to_sym
80
+ when :ok
81
+ OK
82
+ when :warning
83
+ WARNING
84
+ when :critical
85
+ CRITICAL
86
+ else
87
+ UNKNOWN
88
+ end
74
89
  end
75
90
  end
76
91
  end
@@ -1,5 +1,5 @@
1
1
  class MonitoringResult < ActiveRecord::Base
2
- enum :result => [:ok, :warning, :critical, :unknown]
2
+ enum :result => %i[ok warning critical unknown]
3
3
 
4
4
  belongs_to_host
5
5
 
@@ -1,17 +1,16 @@
1
1
  class Setting
2
2
  class Monitoring < ::Setting
3
-
4
3
  def self.default_settings
5
4
  [
6
5
  set('monitoring_affect_global_status',
7
6
  _("Monitoring status will affect a host's global status when enabled"),
8
7
  true, N_('Monitoring status should affect global status')),
9
8
  set('monitoring_create_action',
10
- _("What action should be taken when a host is created"),
11
- 'create', N_('Host Create Action'), nil, {:collection => Proc.new {::Monitoring::CREATE_ACTIONS} }),
9
+ _('What action should be taken when a host is created'),
10
+ 'create', N_('Host Create Action'), nil, { :collection => proc { ::Monitoring::CREATE_ACTIONS } }),
12
11
  set('monitoring_delete_action',
13
- _("What action should be taken when a host is deleted"),
14
- 'delete', N_('Host Delete Action'), nil, {:collection => Proc.new {::Monitoring::DELETE_ACTIONS} })
12
+ _('What action should be taken when a host is deleted'),
13
+ 'delete', N_('Host Delete Action'), nil, { :collection => proc { ::Monitoring::DELETE_ACTIONS } })
15
14
  ]
16
15
  end
17
16
 
@@ -20,7 +19,7 @@ class Setting
20
19
  return unless super
21
20
 
22
21
  self.transaction do
23
- default_settings.each { |s| self.create! s.update(:category => "Setting::Monitoring")}
22
+ default_settings.each { |s| self.create! s.update(:category => 'Setting::Monitoring') }
24
23
  end
25
24
 
26
25
  true
@@ -1,11 +1,9 @@
1
1
  Deface::Override.new(:virtual_path => 'hosts/show',
2
2
  :name => 'add_monitoring_result_tab',
3
3
  :insert_bottom => 'ul.nav-tabs',
4
- :partial => 'monitoring_results/host_tab'
5
- )
4
+ :partial => 'monitoring_results/host_tab')
6
5
 
7
6
  Deface::Override.new(:virtual_path => 'hosts/show',
8
7
  :name => 'add_monitoring_result_tab_pane',
9
8
  :insert_bottom => 'div.tab-content',
10
- :partial => 'monitoring_results/host_tab_pane'
11
- )
9
+ :partial => 'monitoring_results/host_tab_pane')
@@ -1,5 +1,4 @@
1
1
  Deface::Override.new(:virtual_path => 'hosts/select_multiple_power_state',
2
2
  :name => 'add_host_multiple_power_set_downtime_checkbox',
3
3
  :insert_before => "erb[silent]:contains('end')",
4
- :partial => 'hosts/host_downtime_checkbox'
5
- )
4
+ :partial => 'hosts/host_downtime_checkbox')
@@ -1,5 +1,4 @@
1
1
  Deface::Override.new(:virtual_path => 'hosts/show',
2
2
  :name => 'add_monitoring_set_downtime_modal',
3
3
  :insert_after => 'div#review_before_build',
4
- :partial => 'hosts/set_host_downtime'
5
- )
4
+ :partial => 'hosts/set_host_downtime')
@@ -27,23 +27,22 @@ class Monitoring
27
27
  end
28
28
 
29
29
  def set_downtime_host(host, options = {})
30
- proxy_api.create_host_downtime(host.fqdn, default_downtime_options.merge(options))
30
+ proxy_api.create_host_downtime(host.name, default_downtime_options.merge(options))
31
31
  end
32
32
 
33
33
  def del_downtime_host(host, options = {})
34
- proxy_api.remove_host_downtime(host.fqdn, default_downtime_options.merge(options))
34
+ proxy_api.remove_host_downtime(host.name, default_downtime_options.merge(options))
35
35
  end
36
36
 
37
37
  def create_host(host)
38
- proxy_api.create_host(host.fqdn, host.monitoring_attributes)
38
+ proxy_api.create_host(host.name, host.monitoring_attributes)
39
39
  end
40
40
 
41
41
  def update_host(host)
42
- proxy_api.update_host(host.fqdn, host.monitoring_attributes)
42
+ proxy_api.update_host(host.name, host.monitoring_attributes)
43
43
  end
44
44
 
45
45
  def delete_host(host)
46
- # We cannot use host.fqdn here as that is delegated to primary interface that does not exist anymore
47
46
  proxy_api.delete_host(host.name)
48
47
  end
49
48
 
@@ -1,3 +1,3 @@
1
1
  <%= text_f f, :comment, :size => "col-md-5", :label => _('Comment'), :help_inline => _('Short description that explains why the downtime was set.'), :required => true %>
2
- <%= datetime_f f, :starttime, :size => "col-md-5", :label => _('Starttime'), :help_inline => _('Time when the downtime should start.'), :value => DateTime.now.strftime("%Y-%m-%dT%H:%M"), :required => true %>
3
- <%= datetime_f f, :endtime, :size => "col-md-5", :label => _('Endtime'), :help_inline => _('Time when the downtime should end.'), :value => DateTime.now.advance(:hours => 2).strftime("%Y-%m-%dT%H:%M"), :required => true %>
2
+ <%= datetime_f f, :starttime, :size => "col-md-5", :label => _('Starttime'), :help_inline => _('Time when the downtime should start.'), :value => Time.current.strftime("%Y-%m-%dT%H:%M"), :required => true %>
3
+ <%= datetime_f f, :endtime, :size => "col-md-5", :label => _('Endtime'), :help_inline => _('Time when the downtime should end.'), :value => Time.current.advance(:hours => 2).strftime("%Y-%m-%dT%H:%M"), :required => true %>
@@ -7,7 +7,6 @@ module ForemanMonitoring
7
7
  config.autoload_paths += Dir["#{config.root}/app/controllers/concerns"]
8
8
  config.autoload_paths += Dir["#{config.root}/app/helpers/concerns"]
9
9
  config.autoload_paths += Dir["#{config.root}/app/models/concerns"]
10
- config.autoload_paths += Dir["#{config.root}/app/overrides"]
11
10
  config.autoload_paths += Dir["#{config.root}/app/services"]
12
11
 
13
12
  # Add any db migrations
@@ -43,10 +42,13 @@ module ForemanMonitoring
43
42
  :resource_type => 'Host'
44
43
  permission :upload_monitoring_results,
45
44
  :'api/v2/monitoring_results' => [:create]
45
+ permission :edit_hosts,
46
+ { :hosts => [:select_multiple_monitoring_proxy, :update_multiple_monitoring_proxy] },
47
+ :resource_type => 'Host'
46
48
  end
47
49
 
48
50
  role 'Monitoring viewer', [:view_monitoring_results]
49
- role 'Monitoring manager', [:view_monitoring_results, :manage_host_downtimes, :upload_monitoring_results]
51
+ role 'Monitoring manager', [:view_monitoring_results, :manage_host_downtimes]
50
52
 
51
53
  register_custom_status HostStatus::MonitoringStatus
52
54
 
@@ -62,15 +64,17 @@ module ForemanMonitoring
62
64
  # add monitoring smart proxy to hosts and hostgroups
63
65
  smart_proxy_for Host::Managed, :monitoring_proxy, monitoring_proxy_options
64
66
  smart_proxy_for Hostgroup, :monitoring_proxy, monitoring_proxy_options
67
+
68
+ add_controller_action_scope(HostsController, :index) { |base_scope| base_scope.includes(:monitoring_proxy) }
65
69
  end
66
70
  end
67
71
 
68
72
  config.to_prepare do
69
73
  begin
70
- ::Host::Managed.send :include, ForemanMonitoring::HostExtensions
71
- ::Hostgroup.send :include, ForemanMonitoring::HostgroupExtensions
72
- ::HostsHelper.send(:include, ForemanMonitoring::HostsHelperExt)
73
- ::HostsController.send :include, ForemanMonitoring::HostsControllerExtensions
74
+ ::Host::Managed.send(:prepend, ForemanMonitoring::HostExtensions)
75
+ ::Hostgroup.send(:include, ForemanMonitoring::HostgroupExtensions)
76
+ ::HostsHelper.send(:prepend, ForemanMonitoring::HostsHelperExt)
77
+ ::HostsController.send(:prepend, ForemanMonitoring::HostsControllerExtensions)
74
78
  rescue => e
75
79
  Rails.logger.warn "ForemanMonitoring: skipping engine hook (#{e})"
76
80
  end
@@ -1,3 +1,3 @@
1
1
  module ForemanMonitoring
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.1.1'.freeze
3
3
  end
@@ -5,6 +5,9 @@ class HostsControllerExtensionsTest < ActionController::TestCase
5
5
  setup do
6
6
  User.current = users(:admin)
7
7
  disable_monitoring_orchestration
8
+ ProxyAPI::Monitoring.stubs(:create_host).returns(true)
9
+ ProxyAPI::Monitoring.stubs(:update_host).returns(true)
10
+ ProxyAPI::Monitoring.stubs(:delete_host).returns(true)
8
11
  @host = FactoryGirl.create(:host, :managed)
9
12
  end
10
13
 
@@ -19,8 +22,8 @@ class HostsControllerExtensionsTest < ActionController::TestCase
19
22
  :id => @host.name,
20
23
  :downtime => {
21
24
  :comment => 'Maintenance work.',
22
- :starttime => DateTime.now,
23
- :endtime => DateTime.now
25
+ :starttime => Time.current,
26
+ :endtime => Time.current.advance(:hours => 2)
24
27
  }
25
28
  }, set_session_user
26
29
  assert_response :found
@@ -52,13 +55,27 @@ class HostsControllerExtensionsTest < ActionController::TestCase
52
55
  :downtime => {
53
56
  :comment => 'Maintenance work.',
54
57
  :starttime => 'invalid',
55
- :endtime => 'invalid' }
58
+ :endtime => 'invalid'
59
+ }
56
60
  }, set_session_user
57
61
  assert_response :found
58
62
  assert_redirected_to host_path(:id => @host)
59
63
  assert_not_nil flash[:error]
60
64
  assert_equal 'Invalid start/endtime for downtime!', flash[:error]
61
65
  end
66
+
67
+ test 'should parse the times in the correct time zone' do
68
+ User.current.update_attribute(:timezone, 'Berlin')
69
+ Host::Managed.any_instance.expects(:downtime_host).with(has_entries(:start_time => 1_492_676_100, :end_time => 1_492_683_300))
70
+ put :downtime, {
71
+ :id => @host.name,
72
+ :downtime => {
73
+ :comment => 'Maintenance work.',
74
+ :starttime => '2017-04-20T10:15',
75
+ :endtime => '2017-04-20T12:15'
76
+ }
77
+ }, set_session_user
78
+ end
62
79
  end
63
80
 
64
81
  describe 'setting a downtime on multiple hosts' do
@@ -69,9 +86,10 @@ class HostsControllerExtensionsTest < ActionController::TestCase
69
86
 
70
87
  test 'show a host selection' do
71
88
  host_ids = @hosts.map(&:id)
72
- xhr :post, :select_multiple_downtime, {:host_ids => host_ids}, set_session_user
89
+ xhr :post, :select_multiple_downtime, { :host_ids => host_ids }, set_session_user
73
90
  assert_response :success
74
- assert response.body =~ /#{@hosts.first.name}.*#{@hosts.last.name}/m
91
+ assert_includes response.body, @hosts.first.name
92
+ assert_includes response.body, @hosts.last.name
75
93
  end
76
94
 
77
95
  test 'should set a downtime' do
@@ -80,8 +98,8 @@ class HostsControllerExtensionsTest < ActionController::TestCase
80
98
  :host_ids => @hosts.map(&:id),
81
99
  :downtime => {
82
100
  :comment => 'Maintenance work.',
83
- :starttime => DateTime.now,
84
- :endtime => DateTime.now
101
+ :starttime => Time.current,
102
+ :endtime => Time.current.advance(:hours => 2)
85
103
  }
86
104
  }
87
105
 
@@ -157,9 +175,11 @@ class HostsControllerExtensionsTest < ActionController::TestCase
157
175
 
158
176
  test 'show a host selection' do
159
177
  host_ids = hosts.map(&:id)
160
- xhr :post, :select_multiple_monitoring_proxy, {:host_ids => host_ids}, set_session_user
178
+ xhr :post, :select_multiple_monitoring_proxy, { :host_ids => host_ids }, set_session_user
161
179
  assert_response :success
162
- assert response.body =~ /#{hosts.first.name}.*#{hosts.last.name}/m
180
+ hosts.each do |host|
181
+ assert response.body =~ /#{host.name}/m
182
+ end
163
183
  end
164
184
 
165
185
  test 'should change the proxy' do
@@ -178,7 +198,7 @@ class HostsControllerExtensionsTest < ActionController::TestCase
178
198
  assert_response :found
179
199
  assert_redirected_to hosts_path
180
200
  assert_nil flash[:error]
181
- assert_equal "The Monitoring proxy of the selected hosts was set to #{monitoring_proxy.name}.", flash[:notice]
201
+ assert_equal "The Monitoring proxy of the selected hosts was set to #{monitoring_proxy.name}", flash[:notice]
182
202
 
183
203
  hosts.each do |host|
184
204
  as_admin do
@@ -27,13 +27,13 @@ class ProxyApiDhcpTest < ActiveSupport::TestCase
27
27
  end
28
28
 
29
29
  test 'create_host should do put' do
30
- @monitoring.expects(:put).with({:attributes => {:ip => '1.1.1.1'}}, 'host/example.com').
30
+ @monitoring.expects(:put).with({ :attributes => { :ip => '1.1.1.1' } }, 'host/example.com').
31
31
  returns(fake_rest_client_response({ 'result' => {} }))
32
32
  assert_equal({ 'result' => {} }, @monitoring.create_host('example.com', :ip => '1.1.1.1'))
33
33
  end
34
34
 
35
35
  test 'update_host should do post' do
36
- @monitoring.expects(:post).with({:attributes => {:ip => '1.1.1.1'}}, 'host/example.com').
36
+ @monitoring.expects(:post).with({ :attributes => { :ip => '1.1.1.1' } }, 'host/example.com').
37
37
  returns(fake_rest_client_response({ 'result' => {} }))
38
38
  assert_equal({ 'result' => {} }, @monitoring.update_host('example.com', :ip => '1.1.1.1'))
39
39
  end
@@ -41,7 +41,7 @@ class HostTest < ActiveSupport::TestCase
41
41
  test 'should queue monitoring update' do
42
42
  fake_host_query_result = {
43
43
  'ip' => '1.1.1.1',
44
- 'ip6' => '2001:db8::1',
44
+ 'ip6' => '2001:db8::1'
45
45
  }
46
46
  ProxyAPI::Monitoring.any_instance.stubs(:query_host).returns(fake_host_query_result)
47
47
  host.save
@@ -79,7 +79,6 @@ class HostTest < ActiveSupport::TestCase
79
79
  Setting[:monitoring_delete_action] = 'downtime'
80
80
  end
81
81
 
82
-
83
82
  test 'should not queue monitoring create actions' do
84
83
  assert_valid host
85
84
  tasks = host.queue.all.map(&:name)
@@ -94,6 +93,14 @@ class HostTest < ActiveSupport::TestCase
94
93
  assert_includes tasks, "Set monitoring downtime for #{host}"
95
94
  assert_equal 1, tasks.size
96
95
  end
96
+
97
+ test 'should set downtime on delete with correct hostname' do
98
+ assert host.save
99
+ host.queue.clear
100
+ host.stubs(:skip_orchestration?).returns(false) # Enable orchestration
101
+ ProxyAPI::Monitoring.any_instance.expects(:create_host_downtime).with(host.name, anything).returns(true).once
102
+ assert host.destroy
103
+ end
97
104
  end
98
105
 
99
106
  test 'setMonitoring' do
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreman_monitoring
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Timo Goebel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-13 00:00:00.000000000 Z
11
+ date: 2017-10-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: 0.49.1
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: 0.49.1
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rdoc
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -125,7 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
125
125
  version: '0'
126
126
  requirements: []
127
127
  rubyforge_project:
128
- rubygems_version: 2.6.11
128
+ rubygems_version: 2.6.12
129
129
  signing_key:
130
130
  specification_version: 4
131
131
  summary: Foreman plugin for monitoring system integration.