-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.rb
executable file
·57 lines (52 loc) · 1.67 KB
/
run.rb
1
2
3
4
5
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env ruby
# loadpath by bundler
require 'rubygems'
require 'bundler/setup'
# load gems
require 'json'
require 'parallel'
# local libs
require_relative './lib/config.rb'
require_relative './lib/aws/service.rb'
require_relative './lib/icinga/api.rb'
icinga = Icinga2Api.new
raise 'Cannot talk with icinga2 api' unless icinga.check_running
# these are currently active in icinga
origin_hosts = {}
JSON.parse(icinga.hosts)['results'].each do |result|
origin_hosts[result['name']] = Icinga2HostHelper.from_hash result['attrs']
end
begin
hosts = Service.hosts
rescue => e
puts e
raise 'Failed to retrieve Hosts from AWS'
end
# speed up rest requests by parallel processing
# threads is required to modify vars
Parallel.each(hosts, in_threads: 8) do |host|
if origin_hosts[host.name]
if origin_hosts[host.name].to_hash != host.to_hash
# make sure logging the diff does not break normal processing
begin
puts("Got different hashes for host " + host.name.to_s)
puts("Diff detail:")
Icinga2HostHelper.print_hash_diff_detail(origin_hosts[host.name].to_hash, host.to_hash)
rescue => error
puts("Encountered error while printing diff detail: " + error.to_s)
end
# icinga update does not re-evaluate vars, so delete and create
icinga.delete_host(host.name, host)
# give the icinga api time to delete the host, before re-creating it
sleep 3
icinga.create_host(host.name, host)
end
else
icinga.create_host(host.name, host)
end
origin_hosts.delete(host.name)
end
# clean none existing hosts from icinga
Parallel.each(origin_hosts, in_threads: 8) do |host, _attrs|
icinga.delete_host(host, _attrs)
end