Skip to content

Commit 6d91cbd

Browse files
committed
feat: create message with discription of a mistake for input command
1 parent 5b9a6d1 commit 6d91cbd

File tree

1 file changed

+61
-21
lines changed

1 file changed

+61
-21
lines changed

mamonsu/lib/runner.py

Lines changed: 61 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,44 +57,41 @@ def quit_handler(_signo=None, _stack_frame=None):
5757
version_args = args.pg_version.split('_')
5858
define_pg_version(version_args)
5959
cfg = Config(args.config_file, args.plugins_dirs)
60-
if not len(commands) == 3:
60+
if not len(commands) == 2 and not len(commands) == 3:
6161
print_total_help()
6262
if commands[1] == 'zabbix-parameters':
6363
# zabbix agent keys generation
6464
Plugin.Type = 'agent' # change plugin type for template generator
6565
plugins = []
66+
if len(commands) == 2:
67+
commands.append('postgrespro_agent.conf')
68+
print('Configuration file for zabbix-agent have been saved in postgrespro_agent.conf file')
6669
for klass in Plugin.only_child_subclasses():
6770
if klass.__name__ == "PgWaitSampling": # check if plugin is for EE
6871
if Plugin.VersionPG['type'] == 'PGEE':
6972
plugins.append(klass(cfg))
70-
7173
else:
7274
if klass.__name__ != "Cfs":
7375
plugins.append(klass(cfg))
74-
types = args.plugin_type.split(',')
75-
# check if any plugin types is equal
76-
if len(types) > 1:
77-
if is_any_equal(types):
78-
print_total_help()
79-
# if number of plugin types is more than 1 => plugin type should be 'all'
80-
if len(types) > 1:
81-
args.plugin_type = 'all'
76+
args.plugin_type = correct_plugin_type(args.plugin_type)
8277
if args.plugin_type == 'pg' or args.plugin_type == 'sys' or args.plugin_type == 'all':
8378
# check if conf file has a path
8479
len_path = commands[2].rfind("/")
8580
# get path for conf file and scripts
8681
if len_path != -1:
87-
path = commands[2][:len_path] + "/scripts"
88-
Plugin.PATH = path
82+
path = commands[2][:len_path] + "/scripts"
83+
Plugin.PATH = path
8984
else:
9085
path = os.getcwd() + "/scripts"
9186
Plugin.PATH = path
92-
print(path)
9387
# create directory for scripts along the path of conf file if needed
9488
if not os.path.exists(path):
9589
os.makedirs(path)
9690
print("directory for scripts has created")
9791
template = GetKeys()
92+
# if no name for zabbix-parameters save to postgrespro.conf
93+
if commands[2].rfind("/") == len(commands[2]) - 1:
94+
commands[2] = commands[2][:-1] + "/postgrespro.conf"
9895
# write conf file
9996
with codecs.open(commands[2], 'w', 'utf-8') as f:
10097
f.write(template.txt(args.plugin_type, plugins)) # pass command type
@@ -106,36 +103,42 @@ def quit_handler(_signo=None, _stack_frame=None):
106103
os.chmod(path + "/" + key + ".sh", 0o755)
107104
print("Bash scripts for native zabbix-agent have been saved to {0}".format(path))
108105
else:
106+
print("Got wrong plugin types. For help, see the message below")
109107
print_total_help()
110108
sys.exit(0)
111109
elif commands[1] == 'config':
110+
if len(commands) == 2:
111+
commands.append('postgrespro.conf')
112+
print('Configuration file for mamonsu have been saved in postgrespro.conf file')
112113
with open(commands[2], 'w') as fd:
113114
cfg.config.write(fd)
114115
sys.exit(0)
115116
elif commands[1] == 'template':
116117
plugins = []
118+
if len(commands) == 2:
119+
commands.append('postgrespro.xml')
120+
print('Template for mamonsu have been saved in postgrespro.conf file')
117121
for klass in Plugin.only_child_subclasses():
118122
if klass.__name__ == "PgWaitSampling": # check if plugin is for EE
119123
if Plugin.VersionPG['type'] == 'PGEE':
120124
plugins.append(klass(cfg))
121125
else:
122126
plugins.append(klass(cfg))
123127
template = ZbxTemplate(args.template, args.application)
128+
# if no name for template save to postgrespro.xml
129+
if commands[2].rfind("/") == len(commands[2]) - 1:
130+
commands[2] = commands[2][:-1] + "/postgrespro.xml"
124131
with codecs.open(commands[2], 'w', 'utf-8') as f:
125132
# template for mamonsu (zabbix-trapper) is generated for all available plugins
126133
f.write(template.xml("all", plugins)) # set type to 'all' for mamonsu
127134
sys.exit(0)
128135
elif commands[1] == 'zabbix-template':
129136
Plugin.Type = 'agent' # change plugin type for template generator
137+
if len(commands) == 2:
138+
commands.append('postgrespro_agent.xml')
139+
print('Template for zabbix-agent have been saved in postgrespro_agent.xml file')
130140
plugins = []
131-
types = args.plugin_type.split(',')
132-
# check if any plugin types is equal
133-
if len(types) > 1:
134-
if is_any_equal(types):
135-
print_total_help()
136-
# if number of plugin types is more than 1 => plugin type should be 'all'
137-
if len(types) > 1:
138-
args.plugin_type = 'all'
141+
args.plugin_type = correct_plugin_type(args.plugin_type)
139142
if args.plugin_type == 'pg' or args.plugin_type == 'sys' or args.plugin_type == 'all':
140143
for klass in Plugin.only_child_subclasses():
141144
if klass.__name__ == "PgWaitSampling": # check if plugin is for EE
@@ -145,11 +148,15 @@ def quit_handler(_signo=None, _stack_frame=None):
145148
if klass.__name__ != "Cfs":
146149
plugins.append(klass(cfg))
147150
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"
148154
with codecs.open(commands[2], 'w', 'utf-8') as f:
149155
# template for zabbix-agent is generated for selected plugin-type
150156
f.write(template.xml(args.plugin_type, plugins))
151157
sys.exit(0)
152158
else:
159+
print("Got wrong plugin types. For help, see the message below")
153160
print_total_help()
154161
else:
155162
print_total_help()
@@ -200,25 +207,58 @@ def define_pg_version(version_args):
200207
LooseVersion(version_args[0]) == "9.6" or LooseVersion(version_args[0]) == "9.5":
201208
version_number = version_args[0].split('.')
202209
if len(version_number) > 3:
210+
print("Version number is too long. For help, see the message below")
203211
print_total_help()
204212
else:
205213
for num in version_number:
206214
if not num.isdigit():
215+
print("Version number contains only digits. For help, see the message below")
207216
print_total_help()
208217
Plugin.VersionPG['number'] = version_args[0]
209218
else:
219+
print("Version number is not valid. For help, see the message below")
210220
print_total_help()
211221
elif len(version_args) == 2 and (version_args[0] == "PGEE" or version_args[0] == "PGPRO"):
212222
version_number = version_args[1].split('.')
213223
if len(version_number) > 3:
224+
print("Version number is too long. For help, see the message below")
214225
print_total_help()
215226
else:
216227
for num in version_number[1:]:
217228
if not num.isdigit():
229+
print("Version number contains only digits. For help, see the message below")
218230
print_total_help()
219231
if version_args[1] == "11" or LooseVersion(version_args[1]) == "10" or \
220232
LooseVersion(version_args[1]) == "9.6" or LooseVersion(version_args[1]) == "9.5":
221233
Plugin.VersionPG['number'] = version_args[1]
222234
Plugin.VersionPG['type'] = version_args[0]
223235
else:
236+
print("Version number is not valid. For help, see the message below")
224237
print_total_help()
238+
else:
239+
print("Version number is not valid. For help, see the message below")
240+
print_total_help()
241+
242+
243+
# check if plugin type is valid
244+
def correct_plugin_type(plugin_type):
245+
types = plugin_type.split(',')
246+
# if number of plugin types is more than 1 and plugin types are valid => plugin type should be 'all'
247+
valid_plugin_types = ('pg', 'sys', 'all')
248+
if len(types) == 2 or len(types) == 3:
249+
# check if any plugin types is equal
250+
if is_any_equal(types):
251+
print("Got equal plugin types. For help, see the message below")
252+
print_total_help()
253+
# if plugin type name is wrong
254+
if False in [type in valid_plugin_types for type in types]:
255+
print("Got wrong plugin types. For help, see the message below")
256+
print_total_help()
257+
else:
258+
plugin_type = 'all'
259+
return plugin_type
260+
elif len(types) > 3:
261+
print("Got more than 3 plugin types. For help, see the message below")
262+
print_total_help()
263+
else:
264+
return plugin_type

0 commit comments

Comments
 (0)