Class: Naginata::CLI::ActiveCheck

Inherits:
RemoteAbstract show all
Defined in:
lib/naginata/cli/active_check.rb

Instance Method Summary collapse

Methods inherited from RemoteAbstract

#configure_backend, #execute, #initialize, #load_configuration, #load_remote_objects, #set_log_level, #set_nagios_filter

Constructor Details

This class inherits a constructor from Naginata::CLI::RemoteAbstract

Instance Method Details

#runObject



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
# File 'lib/naginata/cli/active_check.rb', line 10

def run
  if !@options[:enable] and !@options[:disable]
    abort "Either --enable or --disable options is required"
  end
  if @options[:enable] and @options[:disable]
    abort "Both --enable and --disable options can not be given"
  end

  if @options[:all_hosts]
    ::Naginata::Configuration.env.add_filter(:host, :all)
  elsif @options[:patterns].empty?
    abort "At least one hostpattern must be given or use --all-hosts option"
  else
    ::Naginata::Configuration.env.add_filter(:host, @options[:patterns])
  end
  if @options[:services]
    ::Naginata::Configuration.env.add_filter(:service, @options[:services])
  end

  command_file = ::Naginata::Configuration.env.fetch(:nagios_server_options)[:command_file]

  if !@options[:force]
    Naginata.ui.info "Following active checks will be #{@options[:enable] ? 'enabled' : 'disabled'}"
    Naginata::Runner.run_locally do |nagios_server, services|
      services.group_by{ |s| s.hostname }.each do |hostname, svcs|
        Naginata.ui.info hostname
        svcs.each do |service|
          Naginata.ui.info "  - #{service.description}"
        end
      end
    end
    abort unless Naginata.ui.yes?("Are you sure? [y|N]")
  end

  Naginata::Runner.run do |backend, nagios_server, services|
    path = nagios_server.fetch(:command_file) || command_file

    services.each do |service|
      opts = {path: (nagios_server.fetch(:command_file) || command_file), host_name: service.hostname}
      opts.merge!(service_description: service.description) if service.description != :ping
      action = @options[:enable] ? 'enable' : 'disable'
      host_or_svc = service.description == :ping ? 'host' : 'svc'
      command_arg = Naginata::Command::ExternalCommand.send("#{action}_#{host_or_svc}_check".to_sym, opts).split(/\s+/, 2)
      command = command_arg.shift.to_sym
      backend.execute command, command_arg
    end
  end
  Naginata.ui.info "Done"
end