Skip to content

Commit cc1dfd2

Browse files
committed
feat: export templates for zabbix-agent according to plugin-type option
1 parent b287f8d commit cc1dfd2

File tree

2 files changed

+35
-29
lines changed

2 files changed

+35
-29
lines changed

mamonsu/lib/runner.py

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,7 @@ def quit_handler(_signo=None, _stack_frame=None):
2525
signal.signal(signal.SIGTERM, quit_handler)
2626
if platform.LINUX:
2727
signal.signal(signal.SIGQUIT, quit_handler)
28-
# temporal list to keep names of all refactored classes
29-
#refactored_classes = ["Oldest", "PgBufferCache", "ArchiveCommand", "BgWriter", "Checkpoint", "Connections",
30-
# "Databases", "PgHealth", "Instance", "PgLocks", "Xlog",
31-
# "PgStatProgressVacuum", "PgStatStatement", "PgWaitSampling", "La", "OpenFiles",
32-
# "SystemUptime", "ProcStat", "Net", "Memory", "DiskStats", "DiskSizes", "DefConfTest",
33-
# "Health"]
28+
3429
commands = sys.argv[1:]
3530
if len(commands) > 0:
3631
tool = commands[0]
@@ -61,10 +56,6 @@ def quit_handler(_signo=None, _stack_frame=None):
6156
# get PG version
6257
version_args = args.pg_version.split('_')
6358
define_pg_version(version_args)
64-
# print(Plugin.VersionPG['type'])
65-
# print(Plugin.VersionPG['number'])
66-
# print("this is args", args)
67-
# print("this is commands", commands)
6859
cfg = Config(args.config_file, args.plugins_dirs)
6960
if not len(commands) == 3:
7061
print_total_help()
@@ -91,8 +82,6 @@ def quit_handler(_signo=None, _stack_frame=None):
9182
if args.plugin_type == 'pg' or args.plugin_type == 'sys' or args.plugin_type == 'all':
9283
# check if conf file has a path
9384
len_path = commands[2].rfind("/")
94-
# print(len_path)
95-
# print(len(commands[2]))
9685
# get path for conf file and scripts
9786
if len_path != -1:
9887
path = commands[2][:len_path] + "/scripts"
@@ -112,9 +101,10 @@ def quit_handler(_signo=None, _stack_frame=None):
112101
# write bash scripts for zabbix - agent to a file
113102
for key in Scripts.Bash:
114103
with codecs.open(path + "/" + key + ".sh", 'w+', 'utf-8') as f:
104+
# configuration file for zabbix-agent is generated for selected plugin-type
115105
f.write(Scripts.Bash[key]) # pass script itself
116106
os.chmod(path + "/" + key + ".sh", 0o755)
117-
print("Bash scripts for native zabbix agent have been saved to {0}".format(path))
107+
print("Bash scripts for native zabbix-agent have been saved to {0}".format(path))
118108
else:
119109
print_total_help()
120110
sys.exit(0)
@@ -132,22 +122,35 @@ def quit_handler(_signo=None, _stack_frame=None):
132122
plugins.append(klass(cfg))
133123
template = ZbxTemplate(args.template, args.application)
134124
with codecs.open(commands[2], 'w', 'utf-8') as f:
135-
f.write(template.xml(plugins))
125+
# template for mamonsu (zabbix-trapper) is generated for all available plugins
126+
f.write(template.xml("all", plugins)) # set type to 'all' for mamonsu
136127
sys.exit(0)
137128
elif commands[1] == 'zabbix-template':
138129
Plugin.Type = 'agent' # change plugin type for template generator
139130
plugins = []
140-
for klass in Plugin.only_child_subclasses():
141-
if klass.__name__ == "PgWaitSampling": # check if plugin is for EE
142-
if Plugin.VersionPG['type'] == 'PGEE':
143-
plugins.append(klass(cfg))
144-
else:
145-
if klass.__name__ != "Cfs":
146-
plugins.append(klass(cfg))
147-
template = ZbxTemplate(args.template, args.application)
148-
with codecs.open(commands[2], 'w', 'utf-8') as f:
149-
f.write(template.xml(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'
139+
if args.plugin_type == 'pg' or args.plugin_type == 'sys' or args.plugin_type == 'all':
140+
for klass in Plugin.only_child_subclasses():
141+
if klass.__name__ == "PgWaitSampling": # check if plugin is for EE
142+
if Plugin.VersionPG['type'] == 'PGEE':
143+
plugins.append(klass(cfg))
144+
else:
145+
if klass.__name__ != "Cfs":
146+
plugins.append(klass(cfg))
147+
template = ZbxTemplate(args.template, args.application)
148+
with codecs.open(commands[2], 'w', 'utf-8') as f:
149+
# template for zabbix-agent is generated for selected plugin-type
150+
f.write(template.xml(args.plugin_type, plugins))
150151
sys.exit(0)
152+
else:
153+
print_total_help()
151154
else:
152155
print_total_help()
153156

mamonsu/lib/zbx_template.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77

88
class ZbxTemplate(object):
9+
plg_type = 'all'
910
mainTemplate = u"""<?xml version="1.0" encoding="UTF-8"?>
1011
<zabbix_export>
1112
<version>2.0</version>
@@ -111,9 +112,10 @@ def turn_agent_type(self, xml):
111112
xml = re.sub(r"<type>2", "<type>0", xml)
112113
return xml
113114

114-
def xml(self, plugins=[]):
115+
def xml(self, type, plugins=[]):
115116
# sort plugins!
116117
plugins.sort(key=lambda x: x.__class__.__name__)
118+
self.plg_type = type
117119
# create template
118120
template_data = {}
119121
template_data['template'] = self.Template
@@ -136,10 +138,11 @@ def xml(self, plugins=[]):
136138
def _get_all(self, items='items', plugins=[]):
137139
result = ''
138140
for plugin in plugins:
139-
row = getattr(plugin, items)(self) # get Items of this particular plugin
140-
if row is None:
141-
continue
142-
result += row
141+
if plugin.AgentPluginType == self.plg_type or self.plg_type == 'all':
142+
row = getattr(plugin, items)(self) # get Items of this particular plugin
143+
if row is None:
144+
continue
145+
result += row
143146
return result
144147

145148
def _macro(self, xml_key='macro'):

0 commit comments

Comments
 (0)