forked from mathewmeconry/icinga2-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicinga2-aws.py
74 lines (56 loc) · 2.43 KB
/
icinga2-aws.py
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
import boto3
import argparse
import os
import shutil
import subprocess
import configparser
def writeTemplate(template, targetFile, targetFolder):
with open(icinga2ConfigDir + 'conf.d/hosts/' + targetFolder + '/' + targetFile + '.conf', 'w') as out:
template = template
template = template.replace('{HOST}', instance.id)
template = template.replace('{IP}', instance.public_ip_address)
out.write(template)
out.flush()
out.close()
def walklevel(some_dir, level=1):
some_dir = some_dir.rstrip(os.path.sep)
assert os.path.isdir(some_dir)
num_sep = some_dir.count(os.path.sep)
for root, dirs, files in os.walk(some_dir):
yield root, dirs, files
num_sep_this = root.count(os.path.sep)
if num_sep + level <= num_sep_this:
del dirs[:]
def cleanupHosts(instances):
for x in walklevel(icinga2ConfigDir + "conf.d/hosts/"):
instanceActive = False
if (x[0].find('i-') > -1):
for instance in instances:
if x[0].split("/")[-1] == instance.id:
instanceActive = True
if instanceActive == False:
shutil.rmtree(x[0])
config = configparser.ConfigParser()
config.read('config.ini')
icinga2ConfigDir = config['Default']['icinga2ConfigDir']
parser = argparse.ArgumentParser(description='AWS for Icinga 2')
parser.add_argument('-t', '--tags', nargs='+', required=True, help='Tag Filter: Key value pair')
parser.add_argument('-th', '--template-host', dest='templateHost', type=open, required=True, help='Which Template should be used for the host')
parser.add_argument('-tc', '--template-check', dest='templateCheck', type=open, required=True, help='Which Template should be used for the checks')
args = parser.parse_args()
filters = []
for tag in args.tags:
tag = tag.split(':')
filters.append({"Name": "tag:"+tag[0], "Values": [tag[1]]})
ec2 = boto3.resource('ec2')
templateHost = args.templateHost.read()
templateCheck = args.templateCheck.read()
instances = ec2.instances.filter(
Filters=filters)
for instance in instances:
if not os.path.exists(icinga2ConfigDir + "conf.d/hosts/" + instance.id):
os.makedirs(icinga2ConfigDir + "conf.d/hosts/" + instance.id)
writeTemplate(templateHost, instance.id, instance.id)
writeTemplate(templateCheck, 'checks', instance.id)
cleanupHosts(instances)
subprocess.call(config['Default']['icinga2ReloadCommand'], shell=True)