Class: Nagira::HostStatusController

Inherits:
Object
  • Object
show all
Includes:
OutputTypeable
Defined in:
app/controllers/host_status_controller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from OutputTypeable

#body_with_list, #full?, #list?, #state?

Constructor Details

#initialize(status, output: nil, hostname: nil) ⇒ HostStatusController

Returns a new instance of HostStatusController.



5
6
7
8
9
10
# File 'app/controllers/host_status_controller.rb', line 5

def initialize(status, output: nil, hostname: nil)
  @status = status
  @output = output
  @hostname = hostname
  @http_status = 200
end

Instance Attribute Details

#hostnameObject

optional

hostname, if not given then return data for all hosts



17
18
19
# File 'app/controllers/host_status_controller.rb', line 17

def hostname
  @hostname
end

#http_statusObject

Returns the value of attribute http_status.



11
12
13
# File 'app/controllers/host_status_controller.rb', line 11

def http_status
  @http_status
end

#outputObject

Type of the output for data: full, normal, state or list



14
15
16
# File 'app/controllers/host_status_controller.rb', line 14

def output
  @output
end

Instance Method Details

#getObject

Get host status, depending on the type of output required: full, normal, state or list.



21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/host_status_controller.rb', line 21

def get
  body = case
         when full?  ; then status
         when state? ; then state
         when list?  ; then list

         else normal
         end
  [@http_status, body]
end

#put(params) ⇒ Object

Update host status



33
34
35
36
# File 'app/controllers/host_status_controller.rb', line 33

def put(params)
  Writer.new(:PROCESS_HOST_CHECK_RESULT)
    .put(with_host(params))
end

#statusHash

Status data: for all hosts or single host if hostname provided.

Returns:

  • (Hash)

    Nagios parsed data (Parser.status)



45
46
47
48
49
50
51
52
# File 'app/controllers/host_status_controller.rb', line 45

def status
  if hostname
    { hostname => @status[hostname] }
  else
    @status
  end
    .tap { |x| @http_status = 404 if x.values.compact.empty? }
end

#with_host(params) ⇒ Object



38
39
40
# File 'app/controllers/host_status_controller.rb', line 38

def with_host(params)
  params.merge({'host_name' => hostname})
end