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
|