Class: MonitoringResult

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.import(result) ⇒ Object



6
7
8
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
# File 'app/models/monitoring_result.rb', line 6

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 < updates[:timestamp]
    created.update_attributes(updates)

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

Instance Method Details

#statusObject



42
43
44
45
46
# File 'app/models/monitoring_result.rb', line 42

def status
  return :ok if downtime
  return :warning if acknowledged
  result.to_sym
end

#to_full_labelObject



52
53
54
55
56
57
58
# File 'app/models/monitoring_result.rb', line 52

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



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

def to_label
  label_mapper(status)
end