Skip to content

Commit f323085

Browse files
committed
Added several checks and moved some code around to a function.
1 parent d752a12 commit f323085

File tree

1 file changed

+83
-83
lines changed

1 file changed

+83
-83
lines changed

probe_installer.py

Lines changed: 83 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@
2525
import time
2626
import subprocess
2727

28+
probe_conf = {}
2829
if sys.version_info < (2, 7):
29-
print "Python version too old! Please install at least version 2.7"
30+
print "\033[91mPython version too old! Please install at least version 2.7\033[0m"
3031
print "Exiting"
3132
sys.exit(2)
3233

@@ -68,15 +69,81 @@ def init_script(script_path, user):
6869
init_script_tpl = open("./scripts/probe.tpl")
6970
return init_script_tpl.read() % (script_path, user)
7071

72+
def get_config():
73+
print ""
74+
print "No configuration file found. Creating."
75+
print ""
76+
try:
77+
probe_user = "%s" % str(
78+
raw_input("Please provide the username the script should run under "
79+
"(please use 'root' for now): ")).rstrip().lstrip()
80+
probe_conf['name'] = "%s" % str(
81+
raw_input("Please provide the desired name of your Mini Probe [Python MiniProbe]: ")).rstrip().lstrip()
82+
probe_conf['gid'] = "%s" % str(
83+
raw_input("Please provide the Probe GID (any unique alphanumerical string): ")).rstrip().lstrip()
84+
probe_conf['server'] = "%s" % str(
85+
raw_input("Please provide the IP/DNS name of the PRTG Core Server: ")).rstrip().lstrip()
86+
probe_conf['port'] = "%s" % str(raw_input(
87+
"Please provide the port the PRTG web server is listening to "
88+
"(IMPORTANT: Only SSL is supported)[443]: ")).rstrip().lstrip()
89+
probe_conf['baseinterval'] = "%s" % str(
90+
raw_input("Please provide the base interval for your sensors [60]: ")).rstrip().lstrip()
91+
probe_conf['key'] = "%s" % str(
92+
raw_input("Please provide the Probe Access Key as defined on the PRTG Core: ")).rstrip().lstrip()
93+
probe_path = "%s" % str(
94+
raw_input("Please provide the path the probe files are located: ")).rstrip().lstrip()
95+
probe_cleanmem = "%s" % str(
96+
raw_input("Do you want the mini probe flushing buffered and cached memory [y/N]: ")).rstrip().lstrip()
97+
probe_conf['announced'] = "0"
98+
probe_conf['protocol'] = "1"
99+
probe_conf['debug'] = ""
100+
# Handling some default values if nothing is provided
101+
if probe_cleanmem == "y":
102+
probe_conf['cleanmem'] = "True"
103+
else:
104+
probe_conf['cleanmem'] = "False"
105+
if not probe_conf['baseinterval']:
106+
probe_conf['baseinterval'] = "60"
107+
if not probe_conf['name']:
108+
probe_conf['name'] = "Python MiniProbe"
109+
if not probe_conf['port']:
110+
probe_conf['port'] = "443"
111+
response = os.system("ping -c 1 " + probe_conf['server'] + " > /dev/null")
112+
if not response == 0:
113+
print "\033[91mPRTG Server can not be reached. Please make sure the server is reachable.\033[0m"
114+
go_on = "%s" % str(
115+
raw_input("Do you still want to continue using this server [y/N]: ")).rstrip().lstrip()
116+
if not go_on == "y":
117+
sys.exit()
118+
if not (probe_conf['gid'] or probe_conf['server']):
119+
print "No values for GID or CORE SERVER given. Script will now exit"
120+
sys.exit()
121+
else:
122+
file_create(path)
123+
write_config(probe_conf)
124+
print "Config file successfully created"
125+
logpath = "%s/logs" % probe_path
126+
if not (file_check(logpath)):
127+
os.makedirs(logpath)
128+
return True
129+
path_rotate = "/etc/logrotate.d/MiniProbe"
130+
path_init = "/etc/init.d/probe.sh"
131+
print "Creating Logrotation Config"
132+
write_file(path_rotate, logrotation(probe_path))
133+
print "Setting up runlevel"
134+
write_file(path_init, init_script(probe_path, probe_user))
135+
print "Changing File Permissions"
136+
os.chmod('%s/probe.py' % probe_path, 0755)
137+
os.chmod('/etc/init.d/probe.sh', 0755)
138+
except Exception, e:
139+
print "%s. Exiting!" % e
140+
return False
141+
71142
if __name__ == '__main__':
72143
conf_avail = False
73-
print"""
74-
Welcome to the Miniprobe (Python) for PRTG installer
75-
"""
76-
print """
77-
Checking for necessary modules and Python Version
78-
"""
79-
print "."
144+
print "Welcome to the Miniprobe (Python) for PRTG installer"
145+
print ""
146+
print "Checking for necessary modules and Python Version"
80147
try:
81148
sys.path.append('./')
82149
import hashlib
@@ -91,86 +158,19 @@ def init_script(script_path, user):
91158
print "%s.Please install the same" % e
92159
print "Exiting"
93160
sys.exit(1)
94-
time.sleep(1)
95-
print "."
96-
time.sleep(1)
97-
print "."
161+
print "Successfully imported modules."
98162
path = './probe.conf'
99-
print """
100-
Successfully imported modules.
101-
"""
102163
if file_check(path):
103-
print "Config file found. Skipping Configuration."
164+
probe_config_exists = "%s" % str(
165+
raw_input("A config file was already found. Do you want to reconfigure [y/N]: ")).rstrip().lstrip()
166+
if probe_config_exists == "y":
167+
get_config()
168+
conf_avail = True
104169
else:
105-
print "No configuration file found. Creating."
106-
try:
107-
probe_conf = {}
108-
probe_user = "%s" % str(
109-
raw_input("Please provide the username the script should run under "
110-
"(please use 'root' for now): ")).rstrip().lstrip()
111-
probe_conf['name'] = "%s" % str(
112-
raw_input("Please provide the desired name of your Mini Probe [Python MiniProbe]: ")).rstrip().lstrip()
113-
probe_conf['gid'] = "%s" % str(
114-
raw_input("Please provide the Probe GID (any unique alphanumerical string): ")).rstrip().lstrip()
115-
probe_conf['server'] = "%s" % str(
116-
raw_input("Please provide the IP/DNS name of the PRTG Core Server: ")).rstrip().lstrip()
117-
probe_conf['port'] = "%s" % str(raw_input(
118-
"Please provide the port the PRTG web server is listening to "
119-
"(IMPORTANT: Only SSL is supported)[443]: ")).rstrip().lstrip()
120-
probe_conf['baseinterval'] = "%s" % str(
121-
raw_input("Please provide the base interval for your sensors [60]: ")).rstrip().lstrip()
122-
probe_conf['key'] = "%s" % str(
123-
raw_input("Please provide the Probe Access Key as defined on the PRTG Core: ")).rstrip().lstrip()
124-
probe_path = "%s" % str(
125-
raw_input("Please provide the path the probe files are located: ")).rstrip().lstrip()
126-
probe_cleanmem = "%s" % str(
127-
raw_input("Do you want the mini probe flushing buffered and cached memory [y/N]: ")).rstrip().lstrip()
128-
probe_conf['announced'] = "0"
129-
probe_conf['protocol'] = "1"
130-
probe_conf['debug'] = ""
131-
# Handling some default values if nothing is provided
132-
if probe_cleanmem == "y":
133-
probe_conf['cleanmem'] = "True"
134-
else:
135-
probe_conf['cleanmem'] = "False"
136-
if not probe_conf['baseinterval']:
137-
probe_conf['baseinterval'] = "60"
138-
if not probe_conf['name']:
139-
probe_conf['name'] = "Python MiniProbe"
140-
if not probe_conf['port']:
141-
probe_conf['port'] = "443"
142-
response = os.system("ping -c 1 " + probe_conf['server'] + " > /dev/null")
143-
if not response == 0:
144-
print "PRTG Server can not be reached. Please make sure the server is reachable."
145-
go_on = "%s" % str(
146-
raw_input("Do you still want to continue using this server [y/N]: ")).rstrip().lstrip()
147-
if not go_on == "y":
148-
sys.exit()
149-
if not (probe_conf['gid'] or probe_conf['server']):
150-
print "No values for GID or CORE SERVER given. Script will now exit"
151-
sys.exit()
152-
else:
153-
file_create(path)
154-
write_config(probe_conf)
155-
print "Config file successfully created"
156-
logpath = "%s/logs" % probe_path
157-
if not (file_check(logpath)):
158-
os.makedirs(logpath)
159-
conf_avail = True
160-
except Exception, e:
161-
print "%s. Exiting!" % e
162-
conf_avail = False
170+
conf_avail = get_config()
171+
163172

164173
if conf_avail:
165-
path_rotate = "/etc/logrotate.d/MiniProbe"
166-
path_init = "/etc/init.d/probe.sh"
167-
print "Creating Logrotation Config"
168-
write_file(path_rotate, logrotation(probe_path))
169-
print "Setting up runlevel"
170-
write_file(path_init, init_script(probe_path, probe_user))
171-
print "Changing File Permissions"
172-
os.chmod('%s/probe.py' % probe_path, 0755)
173-
os.chmod('/etc/init.d/probe.sh', 0755)
174174
print subprocess.call("update-rc.d probe.sh defaults", shell=True)
175175
print "Starting Mini Probe"
176176
print subprocess.call("/etc/init.d/probe.sh start", shell=True)

0 commit comments

Comments
 (0)