-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_nagios_object.py
65 lines (56 loc) · 1.64 KB
/
build_nagios_object.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
#!/usr/bin/env pyhton
import os
import sys
import jinja2
from string import rstrip
# command line argument parse
try:
file_txt = str(sys.argv[1])
except IndexError:
print ""
print "usage: python nagios_object.py [txt_file]"
print ""
sys.exit(0)
# jinja2 environment
loader = jinja2.FileSystemLoader(os.getcwd())
jenv = jinja2.Environment(loader=loader)
define_template = jenv.get_template("templates/object_template.j2")
# Open TXT file
r = open(file_txt, "r")
# Set jinja2 variables
object = ""
host_name = ""
key_value = []
# Function to write final cfg file
def write_file(host_name):
# call jinja2 template
define_file = define_template.render(host_name=host_name,
object=object,
list=key_value)
# write cfg file
w = open("object_file/" + host_name + ".cfg", "a")
w.write(define_file)
w.close()
print define_file
# Read TXT file and populate jinja2 variables
for line in r.readlines():
if line == "\n":
# Write the first object and so on when find a blank line in TXT file
write_file(host_name)
# Reset jinja2 variables
object = ""
host_name = ""
key_value = []
else:
key = line.split(":")[0]
value = line.split(":")[1]
if key == "object":
object = value.rstrip("\n")
elif key == "host_name":
host_name = value.rstrip("\n")
items = {key : value.rstrip("\n")}
key_value.append(items)
else:
items = {key : value.rstrip("\n")}
key_value.append(items)
write_file(host_name)