Sha256: 38f79f406c1d3101aa2b2429dc2f6d57335564ab2585edaa4b3c5020ce2a4151

Contents?: true

Size: 1.74 KB

Versions: 2

Compression:

Stored size: 1.74 KB

Contents

module MyNagios
  class Check < ActiveRecord::Base
    belongs_to :group

    enum status:  [ :info, :success, :critical ]
    enum state:   [ :completed, :running ]

    scope :enabled, -> { where(enabled: true) }

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

        Net::SSH.start( self.host, self.user, config: true, keys: [self.pem_key], non_interactive: true ) do| ssh |
          result = ssh.exec! self.command
          self.update(status: determinate_status_by_response(result), latest_state: result, latest_updated_at: Time.now)
        end
      rescue => e
        self.update(status: :info, latest_state: e, latest_updated_at: Time.now)
      ensure
        self.update(state: :completed)
      end
    end

    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'], config: true, keys: [config['pem_key']], non_interactive: true ) do |ssh|
          check_list.each do |check|
            result = ssh.exec! check.command
            check.update(status: determinate_status_by_response(result), latest_state: result, latest_updated_at: Time.now)
          end
        end

      rescue => e
        check_list.update_all(status: :info, latest_state: e, latest_updated_at: Time.now)
      ensure
        check_list.update_all(state: :completed)
      end
    end

    private

    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 not response.scan('No such file or directory').blank?
      :success
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
my_nagios-0.0.10 app/models/my_nagios/check.rb
my_nagios-0.0.9 app/models/my_nagios/check.rb