Class: MyNagios::Check

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/my_nagios/check.rb

Constant Summary collapse

INFO_STATES =
['AuthenticationFailed message', 'No such file or directory']
ERROR_STATES =
['!ruby/exception', 'packet_write_wait: Connection to UNKNOWN: Broken pipe']

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#additional_command_resultObject

Returns the value of attribute additional_command_result.



11
12
13
# File 'app/models/my_nagios/check.rb', line 11

def additional_command_result
  @additional_command_result
end

Class Method Details

.multiple_run!(check_ids, config) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/my_nagios/check.rb', line 33

def self.multiple_run!(check_ids, config)
  check_list = MyNagios::Check.where(id: check_ids)

  begin
    check_list.update_all(state: :running)

    Net::SSH.start( config['host'], config['user'], MyNagios::Check.ssh_config(config['pem_key']) ) do |ssh|
      check_list.each do |check|
        result = ssh.exec! check.command
        status = check.determinate_status_by_response(result)

        check.additional_command_result = ssh.exec!(check.additional_command) if not check.additional_command.blank? and status.eql?(:critical)

        check.update(state: :completed, status: status, latest_state: result, latest_updated_at: Time.now, retry_count: 0)
      end
    end

  rescue => e
    check_list.each { |check| check.update(state: :completed, status: :info, latest_state: e, latest_updated_at: Time.now, retry_count: (check.retry_count + 1)) }
  end
end

.ssh_config(pem_key = nil) ⇒ Object



62
63
64
65
66
67
# File 'app/models/my_nagios/check.rb', line 62

def self.ssh_config(pem_key = nil)
  config = { config: true, non_interactive: true, auth_methods: ['publickey', 'hostbased'] }
  config[:keys] = [pem_key] unless pem_key.blank?

  config
end

Instance Method Details

#determinate_status_by_response(response) ⇒ Object



55
56
57
58
59
60
# File 'app/models/my_nagios/check.rb', line 55

def determinate_status_by_response(response)
  return :critical if not regexp.blank? and response =~ /#{regexp}/
  return :critical if not response.scan('CRITICAL').blank?
  return :info     if response.nil? or MyNagios::Check::INFO_STATES.any? { |msg| response.include?(msg) }
  :success
end

#run!Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/models/my_nagios/check.rb', line 16

def run!
  begin
    self.update(state: :running)

    Net::SSH.start( self.host, self.user, MyNagios::Check.ssh_config(self.pem_key) ) do |ssh|
      result = ssh.exec! self.command
      status = self.determinate_status_by_response(result)

      self.additional_command_result = ssh.exec!(self.additional_command) if not self.additional_command.blank? and status.eql?(:critical)

      self.update(state: :completed, status: status, latest_state: result, latest_updated_at: Time.now, retry_count: 0)
    end
  rescue => e
    self.update(state: :completed, status: :info, latest_state: e, latest_updated_at: Time.now, retry_count: (self.retry_count + 1))
  end
end