Repository navigation
Expand file tree
/
Copy pathcorewizard.py
More file actions
277 lines (213 loc) · 8.38 KB
/
Copy pathcorewizard.py
File metadata and controls
277 lines (213 loc) · 8.38 KB
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
270
271
272
273
274
275
276
277
import os, sys, argparse, yaml
from jinja2 import Environment, FileSystemLoader
cname = "corewizard"
cversion = "0.0.6"
appPath = os.path.dirname(os.path.realpath(__file__))
#GENERATE COREWIZARD TEMPLATES
def generateNagiosTemplate(appPath,template):
#TEMPLATE DATA
tempdata = {}
#COREWIZARD TEMPLATE YAML
tyml = "{}/object_yml/{}.yml".format(appPath,template)
#COREWIZARD JINJA TEMPLATE LOCATION
#TODO SET APP PATH FOR TEMPLATES
env = Environment(loader=FileSystemLoader(appPath+"/object_templates/"))
#COREWIZARD NAGIOS FILE TEMPLATE TO PROCESS
tempj2 = "{}.j2".format(template)
temp = env.get_template(tempj2)
#GET NAGIOS FILE CONTENT FROM CORE WIZARD TEMPLATE YAML
with open(tyml, 'r') as f:
data_loaded = yaml.safe_load(f)
tempdata = data_loaded[0]["template"]
#VERSIONING
tempdata["version"] = cversion
#RENDER TEMPLATE
rendered = temp.render(tempdata)
#NAGFILE
nagfile = "{}.cfg".format(template)
#CREATE THE NAGIOS COREWIZARD TEMPLATE
with open(nagfile, mode="wt") as f:
f.write(rendered)
f.flush()
f.close()
#GENERATE COREWIZARD TEMPLATES
def generateNagiosCommands(appPath,template):
#TEMPLATE DATA
tempdata = {}
commands = {}
commands["cmd"] = {}
#COREWIZARD TEMPLATE YAML
tyml = "{}/object_yml/{}.yml".format(appPath,template)
#COREWIZARD JINJA TEMPLATE LOCATION
#TODO SET APP PATH FOR TEMPLATE
env = Environment(loader=FileSystemLoader(appPath+"/object_templates/"))
#COREWIZARD NAGIOS FILE TEMPLATE TO PROCESS
#TODO CHANGE NAME TO *.J2 ONLY
tempj2 = "{}.j2".format(template)
temp = env.get_template(tempj2)
#GET NAGIOS FILE CONTENT FROM CORE WIZARD TEMPLATE YAML
with open(tyml, 'r') as f:
data_loaded = yaml.safe_load(f)
tempdata = data_loaded[0]["commands"]
commands["cmd"] = tempdata
#VERSIONING
commands["version"] = cversion
#RENDER TEMPLATE
rendered = temp.render(commands)
#NAGFILE
nagfile = "{}.cfg".format(template)
#CREATE THE NAGIOS COREWIZARD TEMPLATE
with open(nagfile, mode="wt") as f:
f.write(rendered)
f.flush()
f.close()
#GENERATE NAGIOS OBJECT CONFIGS
def generateNagiosCfg(appPath,type,nhd):
#CORWIZARD OBJECTS TEMPLATE
tyml = "{}/object_yml/corewizard-objects.yml".format(appPath)
#COREWIZARD JINJA TEMPLATE LOCATION
#TODO SET APP PATH FOR TEMPLATES
env = Environment(loader=FileSystemLoader(appPath+"/object_templates/"))
#COREWIZARD NAGIOS FILE TEMPLATE TO PROCESS
tempj2 = "object_template.j2"
temp = env.get_template(tempj2)
#DICT STUFF FOR BUILDING UNIFIED CONFIG FILE
cfg = {}
cfg["services"] = {}
cfg["host"] = {}
#SET THE HOSTNAME
cfg["host"]["host_name"] = nhd[0]
#CHECK TO SEE IF WE GOT A SPLIT ADDRESS
if len(nhd) > 1:
cfg["host"]["address"] = nhd[1]
else:
cfg["host"]["address"] = nhd[0]
#GET NAGIOS FILE CONTENT FROM CORE WIZARD TEMPLATE YAML
with open(tyml, 'r') as f:
data_loaded = yaml.safe_load(f)
#GET REQUIRED FIELDS FROM YML
for item in data_loaded[0][type]["host"]:
cfg["host"][item] = data_loaded[0][type]["host"][item]
#GET ALL LINUX SERVICES DEFINED FOR THE TYPE IN THE YML
for service in data_loaded[0][type]["services"]:
cfg["services"][service] = data_loaded[0][type]["services"][service]
#VERSIONING
cfg["version"] = cversion
#RENDER TEMPLATE
rendered = temp.render(cfg)
#NAGFILE
nagfile = "{}.cfg".format(nhd[0])
#CREATE THE NAGIOS COREWIZARD TEMPLATE
with open(nagfile, mode="wt") as f:
f.write(rendered)
f.flush()
f.close()
#MAIN
if __name__ == "__main__" :
# 5 NEED INPUT
args = argparse.ArgumentParser(prog=cname+" v:"+cversion, formatter_class=argparse.ArgumentDefaultsHelpFormatter)
args.add_argument(
"-a","--action",
required=True,
default="deploy",
choices=["cmd", "template", "monitor"],
help="String(wizard action): The action for the wizard to perform."
),
#HOSTNAME:ADDRESS LIST
args.add_argument(
"-H","--hostlist",
required=False,
default=None,
help="String(hostname:hostaddress,): Comma seperated list of hostname:hostaddesss combinations for which to create object cfgs."
),
#HOST OS TYPE
args.add_argument(
"-T","--ostype",
required=False,
default=None,
choices=["linux","windows"],
help="String(OSType): The target host operating system type."
),
#OVERWRITE
args.add_argument(
"-o","--overwrite",
required=False,
action="store_true",
help="boolean(True/False): If set, script wil overwrite any existing cfg files."
)
#GIVE 5 INPUT
meta = args.parse_args()
#TODO ADD COREWIZARD YAML
#NAGIOS USER
#NAGIOS GROUP
#NAGIOS OBJECTS PATH
#DEFAULT CONTACT/CONTACT GROUP
#CREATE TEMPLATES
if meta.action.lower() == "template":
#TODO ADD COREWIZARD CONFIG YAML
templates = {"corewizard-generic-service","corewizard-generic-host"}
#GENERATE ALL NEEDED TEMPLATES
for template in templates:
# CLOBBER
if meta.overwrite:
print("OVERWRITING EXISTING {} TEMPLATE".format(template.upper()))
generateNagiosTemplate(appPath,template)
#GUARD
else:
#TODO NAGIOS OBJECT PATH FROM YML
tpath = "{}/{}.cfg".format(appPath,template)
#CHECK FILE EXISTS
if os.path.exists(tpath):
print("Error: FILE {} ALREADY EXISTS, EXITING.".format(tpath))
sys.exit()
#NEW FILE
else:
print("GENERATING {} TEMPLATE".format(template.upper()))
generateNagiosTemplate(appPath,template)
#CREATE TEMPLATES
elif meta.action.lower() == "cmd":
#TODO ADD COREWIZARD CONFIG YAML
templates = {"corewizard_commands"}
#GENERATE ALL NEEDED TEMPLATES
for template in templates:
# CLOBBER
if meta.overwrite:
print("OVERWRITING EXISTING {}".format(template.upper()))
generateNagiosCommands(appPath,template)
#GUARD
else:
#TODO NAGIOS OBJECT PATH FROM YML
tpath = "{}/{}.cfg".format(appPath,template)
#CHECK FILE EXISTS
if os.path.exists(tpath):
print("Error: FILE {} ALREADY EXISTS, EXITING.".format(tpath))
sys.exit()
#NEW FILE
else:
print("GENERATING {} ".format(template.upper()))
generateNagiosCommands(appPath,template)
#CREATE MONITORING OBJECT CONFIG FILES
elif meta.action.lower() == "monitor":
#LIST OF HOST OBJECTS
hostlist = meta.hostlist.split(",")
#LOOP THROUGH LIST OF HOSTS
for i in hostlist:
#CHECK TO SEE IF SPLIT ADDRESS
nhd = i.split(":")
# CLOBBER
if meta.overwrite:
print("OVERWRITING {}.cfg ".format(nhd[0]))
generateNagiosCfg(appPath,meta.ostype.lower(),nhd)
#GUARD
else:
#THIS WILL BE NAGIOS WORKING DIRECTORY
cfgpath = "{}/{}.cfg".format(appPath,nhd[0])
if os.path.exists(cfgpath):
print("SKIPPED: \"{}\" File Exists.".format(cfgpath))
else:
print("GENERATING {}.cfg".format(nhd[0]))
generateNagiosCfg(appPath,meta.ostype.lower(),nhd)
#UNKNOWN COMMAND
else:
print("THESE ARE NOT THE DROIDS YOU ARE LOOKING FOR!")
sys.exit()