Skip to content

Commit 4884650

Browse files
committed
Finished the reconfigure using an existing config file (if found)
1 parent 47ead33 commit 4884650

File tree

1 file changed

+38
-14
lines changed

1 file changed

+38
-14
lines changed

probe_installer.py

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,23 @@ def logrotation(rotation_path):
7070
return rotate_tpl.read() % rotation_path
7171

7272

73+
def read_config(path):
74+
"""
75+
read configuration file and write data to dict
76+
"""
77+
config = {}
78+
try:
79+
conf_file = open(path)
80+
for line in conf_file:
81+
if not (line == '\n'):
82+
if not (line.startswith('#')):
83+
config[line.split(':')[0]] = line.split(':')[1].rstrip()
84+
conf_file.close()
85+
return config
86+
except Exception as read_error:
87+
print bcolor.RED + "No config found! Error Message: %s Exiting!" + bcolor.END % read_error
88+
sys.exit()
89+
7390
def init_script(script_path, user):
7491
init_script_tpl = open("./scripts/probe.tpl")
7592
return init_script_tpl.read() % (script_path, user)
@@ -180,13 +197,17 @@ def get_config_gid(default=str(uuid.uuid4())):
180197

181198
def get_config_ip(default=""):
182199
tmpIP = "%s" % str(raw_input(bcolor.GREEN + "Please provide the IP/DNS name of the PRTG Core Server [" + default + "]: " + bcolor.END)).rstrip().lstrip()
183-
if not tmpIP == "":
200+
if not (tmpIP == "") or not (default == ""):
201+
if (tmpIP == "") and not (default == ""):
202+
tmpIP = default
184203
response = os.system("ping -c 1 " + tmpIP + " > /dev/null")
185204
if not response == 0:
186205
print bcolor.YELLOW + "PRTG Server can not be reached. Please make sure the server is reachable." + bcolor.END
187206
go_on = "%s" % str(raw_input(bcolor.YELLOW + "Do you still want to continue using this server [y/N]: " + bcolor.END)).rstrip().lstrip()
188207
if not go_on.lower() == "y":
189208
return get_config_ip()
209+
else:
210+
print bcolor.GREEN + "PRTG Server can be reached. Continuing..." + bcolor.END
190211
return tmpIP
191212
else:
192213
print bcolor.YELLOW + "You have not provided an IP/DNS name of the PRTG Core Server." + bcolor.END
@@ -208,7 +229,9 @@ def get_config_base_interval(default="60"):
208229

209230
def get_config_access_key(default=""):
210231
tmpAccessKey = "%s" % str(raw_input(bcolor.GREEN + "Please provide the Probe Access Key as defined on the PRTG Core [" + default + "]: " + bcolor.END)).rstrip().lstrip()
211-
if not tmpAccessKey == "":
232+
if not (tmpAccessKey == "") or not (default == ""):
233+
if (tmpAccessKey == "") and not (default == ""):
234+
tmpAccessKey = default
212235
return tmpAccessKey
213236
else:
214237
print bcolor.YELLOW + "You have not provided the Probe Access Key as defined on the PRTG Core." + bcolor.END
@@ -247,7 +270,7 @@ def get_config_debug(default=""):
247270
else:
248271
return "False"
249272

250-
def get_config():
273+
def get_config(config_old = {}):
251274
print ""
252275
print bcolor.YELLOW + "Checking for necessary modules and Python Version" + bcolor.END
253276
try:
@@ -274,17 +297,17 @@ def get_config():
274297
print ""
275298
try:
276299
probe_user = get_config_user()
277-
probe_conf['name'] = get_config_name()
278-
probe_conf['gid'] = get_config_gid()
279-
probe_conf['server'] = get_config_ip()
280-
probe_conf['port'] = get_config_port()
281-
probe_conf['baseinterval'] = get_config_base_interval()
282-
probe_conf['key'] = get_config_access_key()
300+
probe_conf['name'] = get_config_name(config_old['name'])
301+
probe_conf['gid'] = get_config_gid(config_old['gid'])
302+
probe_conf['server'] = get_config_ip(config_old['server'])
303+
probe_conf['port'] = get_config_port(config_old['port'])
304+
probe_conf['baseinterval'] = get_config_base_interval(config_old['baseinterval'])
305+
probe_conf['key'] = get_config_access_key(config_old['key'])
283306
probe_path = get_config_path()
284-
probe_conf['cleanmem'] = get_config_clean_memory()
285-
probe_conf['announced'] = get_config_announced()
286-
probe_conf['protocol'] = get_config_protocol()
287-
probe_conf['debug'] = get_config_debug()
307+
probe_conf['cleanmem'] = get_config_clean_memory(config_old['cleanmem'])
308+
probe_conf['announced'] = get_config_announced(config_old['announced'])
309+
probe_conf['protocol'] = get_config_protocol(config_old['protocol'])
310+
probe_conf['debug'] = get_config_debug(config_old['debug'])
288311
print ""
289312
file_create(path)
290313
write_config(probe_conf)
@@ -328,7 +351,8 @@ def remove_config():
328351
print ""
329352
probe_config_exists = "%s" % str(raw_input(bcolor.YELLOW + "A config file was already found. Do you want to reconfigure [y/N]: " + bcolor.END)).rstrip().lstrip()
330353
if probe_config_exists.lower() == "y":
331-
get_config()
354+
config_old = read_config(path)
355+
get_config(config_old)
332356
else:
333357
print ""
334358
uninstall = "%s" % str(raw_input(bcolor.YELLOW + "Do you want to Uninstall or Restart the service [u/R]: " + bcolor.END)).rstrip().lstrip()

0 commit comments

Comments
 (0)