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

Returns a new instance of 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/nagiosharder/cli.rb', line 17

def run
  return_value = case command
  when 'status'
    status_checks = []
    hostnames_and_check_names = [param,*the_rest].compact

    # for each hostname or checkname we got
    hostnames_and_check_names.each do |check|
      host,service = check.split("/")
      check_info = client.host_status(host)

      # If we were asked about a specific service, get rid of the ones
      # that they didn't ask for
      check_info.reject! { |name,s| service and service != name }

      # accumulate our results
      status_checks += check_info.map { |name,service| service }
    end

    service_table do
      status_checks
    end
    true
  when /^(ack|acknowledged)$/
    if service.nil?
      client.acknowledge_host(host, the_rest.join(' '))
    else
      client.acknowledge_service(host, service, the_rest.join(' '))
    end
  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 the_rest == []
      puts "Downtime requires duration in minutes."
      false
    else
      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
    end
  when 'problems'
    service_table do
      params = {
        :service_status_types => [:critical, :warning, :unknown],
      }
      params[:group] = param
      client.service_status(params)
    end
    true
  when /^(critical|warning|unknown)/
    service_table do
      params = {
        :service_status_types => [
          $1,
        ],
        :service_props => [
          :hard_state,
          :no_scheduled_downtime,
          :state_unacknowledged,
          :notifications_enabled
        ]
      }
      params[:group] = param
      client.service_status(params)
    end
    true
  when /^(triage|unhandled)/
    service_table do
      params = {
        :service_status_types => [
          :critical,
          :warning,
          :unknown
        ],
        :host_props => [
          :no_scheduled_downtime,
          :state_unacknowledged,
          :notifications_enabled
        ],
        :service_props => [
          :no_scheduled_downtime,
          :state_unacknowledged,
          :notifications_enabled
        ]
      }
      params[:group] = param
      client.service_status(params)
    end
    true
  when /^servicegroups/
    groups_table do
      client.servicegroups_summary()
    end
    true
  when /^hostgroups/
    groups_table do
      client.hostgroups_summary()
    end
    true
  when /^muted/
    service_table do
      params = {
        :service_props => [
          :notifications_disabled,
        ]
      }
      params[:group] = param
      client.service_status(params)
    end
    true
  when /^scheduled_downtime/
    service_table do
      params = {
        :service_props => [
          :scheduled_downtime,
        ]
      }
      params[:group] = param
      client.service_status(params)
    end
    true
  when /^(acked|acknowledged)/
    service_table do
      params = {
        :service_status_types => [
          :critical,
          :warning,
          :unknown
        ],
        :service_props => [
          :state_acknowledged,
        ]
      }
      params[:group] = param
      client.service_status(params)
    end
    true
  else
    debug "'#{command}'"
    raise ArgumentError, help
  end
  if return_value
    0
  else
    puts "Sorry yo, nagios didn't like that."
    1
  end
end