Skip to content

Commit 586beb6

Browse files
committed
fix: removed PG version dependant parameters from template export and fixed message text for several commands
1 parent 4c0ddbb commit 586beb6

File tree

1 file changed

+66
-98
lines changed

1 file changed

+66
-98
lines changed

mamonsu/lib/runner.py

Lines changed: 66 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ def quit_handler(_signo=None, _stack_frame=None):
5555
args, commands = parse_args()
5656
if not args.zabbix_address:
5757
print('Option --zabbix-address is missing')
58-
exit(125)
58+
sys.exit(2)
5959
if not os.path.isfile(args.zabbix_file):
6060
print('Cannot find zabbix file with metric to upload. Check path in --zabbix-file option.')
61-
exit(125)
61+
sys.exit(2)
6262

6363
cfg = Config(args.config_file, args.plugins_dirs)
6464
cfg.config.set('zabbix', 'address', args.zabbix_address)
@@ -68,13 +68,12 @@ def quit_handler(_signo=None, _stack_frame=None):
6868

6969
supervisor = Supervisor(cfg)
7070
supervisor.send_file_zabbix(cfg, args.zabbix_file)
71-
exit(0)
71+
sys.exit(0)
7272

7373
elif tool == 'export':
7474
args, commands = parse_args()
7575
# get PG version
76-
version_args = args.pg_version.split('_')
77-
define_pg_version(version_args)
76+
Plugin.VersionPG = define_pg_version(args.pg_version)
7877
cfg = Config(args.config_file, args.plugins_dirs)
7978
if args.old_zabbix:
8079
Plugin.old_zabbix = True
@@ -86,102 +85,92 @@ def quit_handler(_signo=None, _stack_frame=None):
8685
plugins = []
8786
if len(commands) == 2:
8887
commands.append('postgrespro_agent.conf')
89-
print('Configuration file for zabbix-agent have been saved in postgrespro_agent.conf file')
9088
for klass in Plugin.only_child_subclasses():
91-
if klass.__name__ == "PgWaitSampling": # check if plugin is for EE
92-
if Plugin.VersionPG['type'] == 'PGEE':
93-
plugins.append(klass(cfg))
94-
else:
95-
if klass.__name__ != "Cfs":
89+
if klass.__name__ != "PgWaitSampling" and klass.__name__ != "Cfs":
9690
plugins.append(klass(cfg))
9791
args.plugin_type = correct_plugin_type(args.plugin_type)
9892
if args.plugin_type == 'pg' or args.plugin_type == 'sys' or args.plugin_type == 'all':
93+
template = GetKeys()
94+
# write conf file
95+
try:
96+
fd = codecs.open(commands[2], 'w', 'utf-8')
97+
fd.write(template.txt(args.plugin_type, plugins)) # pass command type
98+
print("Configuration file for zabbix-agent has been saved as {0}".format(commands[2]))
99+
except (IOError, EOFError) as e:
100+
print(" {0} ".format(e))
101+
sys.exit(2)
102+
# write bash scripts for zabbix - agent to a file
99103
# check if conf file has a path
100104
len_path = commands[2].rfind("/")
101105
# get path for conf file and scripts
102106
if len_path != -1:
103-
path = commands[2][:len_path] + "/scripts"
104-
Plugin.PATH = path
107+
path = commands[2][:len_path] + "/scripts"
108+
Plugin.PATH = path
105109
else:
106110
path = os.getcwd() + "/scripts"
107111
Plugin.PATH = path
108112
# create directory for scripts along the path of conf file if needed
109113
if not os.path.exists(path):
110-
os.makedirs(path)
111-
print("Directory for scripts has created")
112-
template = GetKeys()
113-
# if no name for zabbix-parameters save to postgrespro.conf
114-
if commands[2].rfind("/") == len(commands[2]) - 1:
115-
commands[2] = commands[2][:-1] + "/postgrespro.conf"
116-
# write conf file
117-
with codecs.open(commands[2], 'w', 'utf-8') as f:
118-
f.write(template.txt(args.plugin_type, plugins)) # pass command type
119-
# write bash scripts for zabbix - agent to a file
114+
os.makedirs(path)
120115
for key in Scripts.Bash:
121116
with codecs.open(path + "/" + key + ".sh", 'w+', 'utf-8') as f:
122117
# configuration file for zabbix-agent is generated for selected plugin-type
123118
f.write(Scripts.Bash[key]) # pass script itself
124119
os.chmod(path + "/" + key + ".sh", 0o755)
125120
print("Bash scripts for native zabbix-agent have been saved to {0}".format(path))
126121
else:
127-
print("Got wrong plugin types. For help, see the message below")
128-
print_total_help()
122+
print("Got wrong plugin types. See help 'mamonsu -- help' ")
123+
sys.exit(2)
129124
sys.exit(0)
130125
elif commands[1] == 'config':
126+
# if no name for conf, save to postgrespro.conf
131127
if len(commands) == 2:
132128
commands.append('postgrespro.conf')
133-
print('Configuration file for mamonsu have been saved in postgrespro.conf file')
134-
# if no name for conf, save to mamonsu.conf
135-
if commands[2].rfind("/") == len(commands[2]) - 1:
136-
commands[2] = commands[2][:-1] + "/mamonsu.conf"
137-
with open(commands[2], 'w') as fd:
129+
try:
130+
fd = open(commands[2], 'w')
138131
cfg.config.write(fd)
132+
print("Configuration file for mamonsu has been saved as {0}".format(commands[2]))
139133
sys.exit(0)
134+
except (IOError, EOFError) as e:
135+
print(" {0} ".format(e))
136+
sys.exit(2)
140137
elif commands[1] == 'template':
141138
plugins = []
142139
if len(commands) == 2:
143140
commands.append('postgrespro.xml')
144141
for klass in Plugin.only_child_subclasses():
145-
if klass.__name__ == "PgWaitSampling" or klass.__name__ == "Cfs" : # check if plugin is for EE
146-
if Plugin.VersionPG['type'] == 'PGEE':
147-
plugins.append(klass(cfg))
148-
else:
149142
plugins.append(klass(cfg))
150143
template = ZbxTemplate(args.template, args.application)
151-
# if no name for template save to postgrespro.xml
152-
if commands[2].rfind("/") == len(commands[2]) - 1:
153-
commands[2] = commands[2][:-1] + "/postgrespro.xml"
154-
with codecs.open(commands[2], 'w', 'utf-8') as f:
155-
# template for mamonsu (zabbix-trapper) is generated for all available plugins
156-
f.write(template.xml("all", plugins)) # set type to 'all' for mamonsu
157-
print('Configuration file for mamonsu have been saved in {file} file'.format(file=commands[2]))
144+
try:
145+
fd = codecs.open(commands[2], 'w', 'utf-8')
146+
fd.write(template.xml("all", plugins)) # set type to 'all' for mamonsu
147+
print('Template for mamonsu has been saved as {file}'.format(file=commands[2]))
158148
sys.exit(0)
149+
except (IOError, EOFError) as e:
150+
print(" {0} ".format(e))
151+
sys.exit(2)
159152
elif commands[1] == 'zabbix-template':
160153
Plugin.Type = 'agent' # change plugin type for template generator
161154
if len(commands) == 2:
162155
commands.append('postgrespro_agent.xml')
163-
print('Template for zabbix-agent have been saved in postgrespro_agent.xml file')
164156
plugins = []
165157
args.plugin_type = correct_plugin_type(args.plugin_type)
166158
if args.plugin_type == 'pg' or args.plugin_type == 'sys' or args.plugin_type == 'all':
167159
for klass in Plugin.only_child_subclasses():
168-
if klass.__name__ == "PgWaitSampling": # check if plugin is for EE
169-
if Plugin.VersionPG['type'] == 'PGEE':
170-
plugins.append(klass(cfg))
171-
else:
172-
if klass.__name__ != "Cfs":
160+
if klass.__name__ != "PgWaitSampling" and klass.__name__ != "Cfs": # check if plugin is for EE
173161
plugins.append(klass(cfg))
174162
template = ZbxTemplate(args.template, args.application)
175-
# if no name for template save to postgrespro.xml
176-
if commands[2].rfind("/") == len(commands[2]) - 1:
177-
commands[2] = commands[2][:-1] + "/postgrespro.xml"
178-
with codecs.open(commands[2], 'w', 'utf-8') as f:
179-
# template for zabbix-agent is generated for selected plugin-type
180-
f.write(template.xml(args.plugin_type, plugins))
181-
sys.exit(0)
163+
try:
164+
fd = codecs.open(commands[2], 'w', 'utf-8')
165+
fd.write(template.xml(args.plugin_type, plugins))
166+
print('Template for zabbix-agent has been saved as {file}'.format(file=commands[2]))
167+
sys.exit(0)
168+
except (IOError, EOFError) as e:
169+
print(" {0} ".format(e))
170+
sys.exit(2)
182171
else:
183-
print("Got wrong plugin types. For help, see the message below")
184-
print_total_help()
172+
print("Got wrong plugin types. See help 'mamonsu -- help' ")
173+
sys.exit(2)
185174
else:
186175
print_total_help()
187176

@@ -219,49 +208,28 @@ def quit_handler(_signo=None, _stack_frame=None):
219208

220209

221210
# check if any equal elements in array
222-
def is_any_equal(iterator):
223-
length = len(iterator)
224-
return len(set(iterator)) < length
211+
def is_any_equal(array):
212+
length = len(array)
213+
return len(set(array)) < length
225214

226215

227216
# extract pg version from input
228217
def define_pg_version(version_args):
229-
if len(version_args) == 1:
230-
if version_args[0] == "11" or version_args[0] == "12" or LooseVersion(version_args[0]) == "10" or \
231-
LooseVersion(version_args[0]) == "9.6" or LooseVersion(version_args[0]) == "9.5":
218+
if len(version_args) < 4:
219+
if version_args == "11" or version_args == "12" or version_args == "10" or version_args == "9.6" \
220+
or version_args == "9.5":
232221
version_number = version_args[0].split('.')
233-
if len(version_number) > 3:
234-
print("Version number is too long. For help, see the message below")
235-
print_total_help()
236-
else:
237-
for num in version_number:
238-
if not num.isdigit():
239-
print("Version number contains only digits. For help, see the message below")
240-
print_total_help()
241-
Plugin.VersionPG['number'] = version_args[0]
242-
else:
243-
print("Version number is not valid. For help, see the message below")
244-
print_total_help()
245-
elif len(version_args) == 2 and (version_args[0] == "PGEE" or version_args[0] == "PGPRO"):
246-
version_number = version_args[1].split('.')
247-
if len(version_number) > 3:
248-
print("Version number is too long. For help, see the message below")
249-
print_total_help()
250-
else:
251-
for num in version_number[1:]:
222+
for num in version_number:
252223
if not num.isdigit():
253-
print("Version number contains only digits. For help, see the message below")
254-
print_total_help()
255-
if version_args[1] == "11" or version_args[1] == "12" or LooseVersion(version_args[1]) == "10" or \
256-
LooseVersion(version_args[1]) == "9.6" or LooseVersion(version_args[1]) == "9.5":
257-
Plugin.VersionPG['number'] = version_args[1]
258-
Plugin.VersionPG['type'] = version_args[0]
259-
else:
260-
print("Version number is not valid. For help, see the message below")
261-
print_total_help()
224+
print("Version number contains only digits. See help 'mamonsu -- help' ")
225+
sys.exit(2)
226+
return version_args
227+
else:
228+
print("Version number is not valid. See help 'mamonsu -- help' ")
229+
sys.exit(2)
262230
else:
263-
print("Version number is not valid. For help, see the message below")
264-
print_total_help()
231+
print("Version number is not valid. See help 'mamonsu -- help' ")
232+
sys.exit(2)
265233

266234

267235
# check if plugin type is valid
@@ -272,17 +240,17 @@ def correct_plugin_type(plugin_type):
272240
if len(types) == 2 or len(types) == 3:
273241
# check if any plugin types is equal
274242
if is_any_equal(types):
275-
print("Got equal plugin types. For help, see the message below")
276-
print_total_help()
243+
print("Got equal plugin types. See help 'mamonsu -- help' ")
244+
sys.exit(2)
277245
# if plugin type name is wrong
278246
if False in [type in valid_plugin_types for type in types]:
279-
print("Got wrong plugin types. For help, see the message below")
280-
print_total_help()
247+
print("Got wrong plugin types. See help 'mamonsu -- help' ")
248+
sys.exit(2)
281249
else:
282250
plugin_type = 'all'
283251
return plugin_type
284252
elif len(types) > 3:
285-
print("Got more than 3 plugin types. For help, see the message below")
286-
print_total_help()
253+
print("Got more than 3 plugin types. See help 'mamonsu -- help' ")
254+
sys.exit(2)
287255
else:
288256
return plugin_type

0 commit comments

Comments
 (0)