Class: MonitoringResult

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
app/models/monitoring_result.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.import(result) ⇒ Object

rubocop:disable Metrics/AbcSize



9
10
11
12
13
14
15
16
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
# File 'app/models/monitoring_result.rb', line 9

def self.import(result)
  host = Host.find_by(name: result[:host])

  if host.nil?
    logger.error "Unable to find host #{result[:host]}"
    return false
  end

  start_time = Time.now.utc
  logger.info "Processing monitoring result for #{host}"

  updates = {
    :result => result[:result],
    :acknowledged => result[:acknowledged],
    :downtime => result[:downtime],
    :timestamp => (Time.at(result[:timestamp]).utc rescue nil)
  }.compact

  if result[:initial] && result[:service] == 'Host Check'
    logger.info "Removing all monitoring results for #{host} on initial import"
    MonitoringResult.where(:host => host).destroy_all
  end

  created = MonitoringResult.where(:host => host, :service => result[:service]).first_or_create
  if created.timestamp.blank? || updates[:timestamp].blank? || (created.timestamp.to_time - updates[:timestamp].to_time) < 2
    created.update(updates)

    if created.persisted?
      logger.info("Imported monitoring result for #{host} in #{(Time.now.utc - start_time).round(2)} seconds")
      host.get_status(::HostStatus::MonitoringStatus).refresh!
    end
  else
    logger.debug "Skipping monitoring result import for #{host} as it is older than what we have."
  end
end

Instance Method Details

#statusObject

rubocop:enable Metrics/AbcSize



46
47
48
49
50
51
# File 'app/models/monitoring_result.rb', line 46

def status
  return :ok if downtime
  return :warning if acknowledged

  result.to_sym
end

#to_full_labelObject



57
58
59
60
61
62
63
# File 'app/models/monitoring_result.rb', line 57

def to_full_label
  options = []
  options << _('acknowledged') if acknowledged
  options << _('in downtime') if downtime
  suffix = options.any? ? " (#{options.join(', ')})" : ''
  "#{label_mapper(result.to_sym)}#{suffix}"
end

#to_labelObject



53
54
55
# File 'app/models/monitoring_result.rb', line 53

def to_label
  label_mapper(status)
end