Class: NagiosHarder::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/nagiosharder/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Cli



10
11
12
13
14
15
# File 'lib/nagiosharder/cli.rb', line 10

def initialize(argv)
  @options          = parse_connection_options(argv)
  @command, @param  = argv
  @host, @service   = param.split("/") if param
  @the_rest         = argv[2..-1]
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



8
9
10
# File 'lib/nagiosharder/cli.rb', line 8

def command
  @command
end

#hostObject (readonly)

Returns the value of attribute host.



8
9
10
# File 'lib/nagiosharder/cli.rb', line 8

def host
  @host
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/nagiosharder/cli.rb', line 8

def options
  @options
end

#paramObject (readonly)

Returns the value of attribute param.



8
9
10
# File 'lib/nagiosharder/cli.rb', line 8

def param
  @param
end

#serviceObject (readonly)

Returns the value of attribute service.



8
9
10
# File 'lib/nagiosharder/cli.rb', line 8

def service
  @service
end

#the_restObject (readonly)

Returns the value of attribute the_rest.



8
9
10
# File 'lib/nagiosharder/cli.rb', line 8

def the_rest
  @the_rest
end

Instance Method Details

#runObject



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
67
68
69
70
71
72
73
# File 'lib/nagiosharder/cli.rb', line 17

def run
  return_value = case command
  when 'status'
    service_table do
      client.host_status(host).select do |name, s|
        service.nil? || service == name
      end.map { |name, s| s }
    end
    true
  when /^ack/
    client.acknowledge_service(host, service, the_rest.join(' '))
  when /^unack/
    client.unacknowledge_service(host, service)
  when /^(mute|disable_notifications)$/
    client.disable_service_notifications(host, service)
  when /^(unmute|enable_notifications)$/
    client.enable_service_notifications(host, service)
  when 'check'
    if service
      client.schedule_service_check(host, service)
    else
      client.schedule_host_check(host)
    end
  when 'downtime'
    if service
      client.schedule_service_downtime(host, service, :type => :fixed, :start_time => Time.now, :end_time => Time.now + the_rest.first.to_i.minutes)
    else
      client.schedule_host_downtime(host, :type => :fixed, :start_time => Time.now, :end_time => Time.now + the_rest.first.to_i.minutes)
    end
  when 'problems'
    service_table do
      if param
        client.service_status(:all_problems, :group => param)
      else
        client.service_status(:all_problems)
      end
    end
    true
  when /^(triage|unhandled)/
    service_table do
      if param
        client.service_status(:all_problems, :group => param, :hoststatustypes => 3, :serviceprops => 42, :servicestatustypes => 28)
      else
        client.service_status(:all_problems, :hoststatustypes => 3, :serviceprops => 42, :servicestatustypes => 28)
      end
    end
    true
  else
    raise ArgumentError, help
  end
  if return_value
    0
  else
    puts "Sorry, bro, nagios didn't like that."
    1
  end
end