forked from Icinga/icinga2-api-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicinga2_ec2
executable file
·269 lines (222 loc) · 8.43 KB
/
icinga2_ec2
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/env ruby
#/******************************************************************************
# * Icinga 2 AWS *
# * Copyright (C) 2015 Icinga Development Team (https://www.icinga.org) *
# * *
# * This program is free software; you can redistribute it and/or *
# * modify it under the terms of the GNU General Public License *
# * as published by the Free Software Foundation; either version 2 *
# * of the License, or (at your option) any later version. *
# * *
# * This program is distributed in the hope that it will be useful, *
# * but WITHOUT ANY WARRANTY; without even the implied warranty of *
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
# * GNU General Public License for more details. *
# * *
# * You should have received a copy of the GNU General Public License *
# * along with this program; if not, write to the Free Software Foundation *
# * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
# ******************************************************************************/
require 'aws-sdk'
require 'rest_client'
require 'colorize'
class AwsInstanceManager
def print_instance(inst)
#puts inst
iid = inst[:instance_id]
istate = inst[:state].name
ipublicdns = inst[:public_dns_name]
ipublicip = inst[:public_ip_address]
if istate != "running"
puts "AWS EC2 instance ID: '#{iid}' State: '#{istate}'".red
else
puts "AWS EC2 instance ID: '#{iid}' State: '#{istate}' Public DNS: '#{ipublicdns}' Public IP: '#{ipublicip}'".green
end
end
def manage_instances(i2)
ec2 = Aws::EC2::Client.new(region: 'us-west-2');
if !i2.check_running()
puts "Icinga 2 API is not available. Please start Icinga 2 with enabled 'api' feature and appropriate api user credentials.".red
exit 1
end
resp = ec2.describe_instances()
resp.reservations.each do |res|
res.instances.each do |inst|
# puts inst
i_id = inst[:instance_id]
i_state = inst[:state].name
i_public_dns = inst[:public_dns_name]
i_public_ip = inst[:public_ip_address]
print_instance(inst)
# if instance is not running, mark it for deletion
# TODO: allow user to define the action
i_to_delete = false
if i_state != "running"
i_to_delete = true
end
# check if the host exists
i2_res = i2.get_host(i_id)
i_exists = false
if i2_res && i2_res.code == 200
#puts i2_res.body
i_exist = true
end
if i_exists == false and i_to_delete == true
# object was marked for deletion
i2.delete_host(i_id)
elsif i_exists == false and i_to_delete == false
# object does not exist
# TODO: add/fetch more tags as custom attributes
tags = { "os" => "Linux", "distribution" => "AWS"}
i2.create_host(i_id, i_public_ip, i_public_dns, tags)
elsif i_exists == true and i_to_delete == false
puts "Found host, doing update only".yellow
# TODO: add/fetch more tags as custom attributes
tags = { "os" => "Linux", "distribution" => "AWS"}
i2.update_host(i_id, i_public_ip, i_public_dns, tags)
end
end
end
end
end
class Icinga2InstanceManager
@@node_name = Socket.gethostbyname(Socket.gethostname).first
def set_node_name(name)
@@node_name = name
end
@@api_username = "root"
def set_api_username(str)
@@api_username = str
end
@@api_password= "icinga"
def set_api_password(str)
@@api_password = str
end
@@api_url_base = "https://192.168.33.5:5665/v1"
def set_api_url_base(url)
@@api_url_base = url
end
@@api_url_obj_hosts = "/objects/hosts"
# prepare the rest client ssl stuff
def prepare_rest_client(api_url)
# check whether pki files are there, otherwise use basic auth
if File.file?("pki/" + @@node_name + ".crt")
#puts "PKI found, using client certificates for connection to Icinga 2 API"
cert_file = File.read("pki/" + @@node_name + ".crt")
key_file = File.read("pki/" + @@node_name + ".key")
ca_file = File.read("pki/ca.crt")
cert = OpenSSL::X509::Certificate.new(cert_file)
key = OpenSSL::PKey::RSA.new(key_file)
options = {:ssl_client_cert => cert, :ssl_client_key => key, :ssl_ca_file => ca_file, :verify_ssl => OpenSSL::SSL::VERIFY_NONE}
else
#puts "PKI not found, using basic auth for connection to Icinga 2 API"
options = { :user => @@api_username, :password => @@api_password, :verify_ssl => OpenSSL::SSL::VERIFY_NONE }
end
res = RestClient::Resource.new(URI.encode(api_url), options)
return res
end
# fetch global status to see if api is available
def check_running()
api_url = @@api_url_base + "/status/IcingaApplication"
rest_client = prepare_rest_client(api_url)
headers = {"Content-Type" => "application/json", "Accept" => "application/json"}
puts "Checking the availability of the Icinga 2 API at #{api_url}."
begin
response = rest_client.get(headers)
rescue => e
puts e
return false
end
puts "Status: " + (JSON.pretty_generate JSON.parse(response.body))
return true
end
# list, GET
def get_host(name)
api_url = @@api_url_base + @@api_url_obj_hosts + "/#{name}"
rest_client = prepare_rest_client(api_url)
headers = {"Content-Type" => "application/json", "Accept" => "application/json"}
puts "Icinga 2: Getting host '#{name}'."
begin
response = rest_client.get(headers)
rescue => e
puts e
e.response
end
#puts response
return response
end
# create, PUT
def create_host(name, address, display_name, tags)
api_url = @@api_url_base + @@api_url_obj_hosts + "/#{name}"
rest_client = prepare_rest_client(api_url)
headers = {"Content-Type" => "application/json", "Accept" => "application/json"}
# hardcode the required check_command attribute
attrs = {
'attrs' =>
{
'check_command' => 'hostalive',
'address' => "#{address}",
'display_name' => "#{display_name}",
'vars' => tags
}
}
puts "Icinga 2: Creating host '#{name}' with attributes: '" + attrs.to_json + "'."
begin
response = rest_client.put(attrs.to_json, headers)
rescue => e
e.response
puts e
end
return response
end
# update, POST
def update_host(name, address, display_name, tags)
api_url = @@api_url_base + @@api_url_obj_hosts + "/#{name}"
rest_client = prepare_rest_client(api_url)
headers = {"Content-Type" => "application/json", "Accept" => "application/json"}
attrs = {
'attrs' =>
{
'check_command' => 'hostalive',
'address' => "#{address}",
'display_name' => "#{display_name}",
'vars' => tags
}
}
puts "Icinga 2: Updating host '#{name}' with attributes: '" + attrs.to_json + "'."
begin
response = rest_client.post(attrs.to_json, headers)
rescue => e
puts e
e.response
end
return response
end
# delete, DELETE
def delete_host(name)
api_url = @@api_url_base + @@api_url_obj_hosts + "/#{name}?cascade=1" #applied services require cascading delete
rest_client = prepare_rest_client(api_url)
headers = {"Content-Type" => "application/json", "Accept" => "application/json"}
puts "Icinga 2: Deleting host '#{name}'."
begin
response = rest_client.delete(headers)
rescue => e
e.response
# silently ignore errors with non-existing objects
#puts "Errors deleting host '#{name}'."
end
# we use cascading delete, but anyways
if response && response.code != 200
puts response.body
end
return response
end
end
if __FILE__ ==$0
aws = AwsInstanceManager.new
i2 = Icinga2InstanceManager.new
#i2.set_node_name("icinga2a")
i2.set_api_username("root")
i2.set_api_password("icinga")
aws.manage_instances(i2)
end