Skip to content

Commit b5fd168

Browse files
committed
feat: add scripts export for zabbix-agent
1 parent f852241 commit b5fd168

File tree

4 files changed

+41
-26
lines changed

4 files changed

+41
-26
lines changed

mamonsu/lib/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def __init__(self, cfg_file=None, plugin_directories=[]):
7777
self.config.readfp(open(cfg_file))
7878

7979
plugins = self.fetch('plugins', 'directory', str)
80-
if not plugins == 'None':
80+
if plugins is not None:
8181
self._load_external_plugins_from_directory(plugins)
8282
self._apply_default_config()
8383
self._check_interval()

mamonsu/lib/parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
--add-plugins <directory>
5757
--template-name <template name>
5858
--application <application name in template>
59-
Default template name = PostgresPro-<platform name>, application = App-PostgresPro-<platform name>, pg-version = 10,
59+
Default template name = PostgresPro-<platform name>, application = App-PostgresPro-<platform name>
6060
6161
6262
Bootstrap DDL for monitoring:

mamonsu/lib/plugin.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class Plugin(object):
4343
is_child = True
4444

4545
# const
46+
PATH = "/etc/zabbix/zabbix_agent.d/scripts"
4647
DELTA = Template.DELTA
4748
GRAPH_TYPE = Template.GRAPH_TYPE
4849
VALUE_TYPE = Template.VALUE_TYPE
@@ -165,7 +166,6 @@ def _loop(self):
165166
self._log_exception(e, trace)
166167
return
167168
# time interval btw sending metrics
168-
print(self.plugin_config('interval'))
169169
sleep_time = int(self.plugin_config('interval')) - int(time.time() - last_start)
170170
if sleep_time > 0:
171171
time.sleep(sleep_time)
@@ -174,6 +174,7 @@ def _loop(self):
174174
'Timeout: {0}s'.format(int(time.time() - last_start)))
175175
return
176176

177+
# convert zabbix key to right type: zabbix-trapper or zabbix-agent
177178
def right_type(self, key, var="", var_discovery=""):
178179
if self.Type == "mamonsu":
179180
new_key = key.format('[{0}{1}]'.format(var, var_discovery[:-1]))

mamonsu/lib/runner.py

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from mamonsu.lib.zbx_template import ZbxTemplate
1515
from mamonsu.lib.get_keys import GetKeys
1616
from distutils.version import LooseVersion
17+
from mamonsu.plugins.system.linux.scripts import Scripts
1718

1819

1920
def start():
@@ -28,7 +29,8 @@ def quit_handler(_signo=None, _stack_frame=None):
2829
refactored_classes = ["Oldest", "PgBufferCache", "ArchiveCommand", "BgWriter", "Checkpoint", "Connections",
2930
"Databases", "PgHealth", "Instance", "PgLocks", "Xlog",
3031
"PgStatProgressVacuum", "PgStatStatement", "PgWaitSampling", "La", "OpenFiles",
31-
"SystemUptime", "ProcStat", "Net", "Memory", "DiskStats", "DiskSizes"]
32+
"SystemUptime", "ProcStat", "Net", "Memory", "DiskStats", "DiskSizes", "DefConfTest",
33+
"Health"]
3234
commands = sys.argv[1:]
3335
if len(commands) > 0:
3436
tool = commands[0]
@@ -71,14 +73,12 @@ def quit_handler(_signo=None, _stack_frame=None):
7173
Plugin.Type = 'agent' # change plugin type for template generator
7274
plugins = []
7375
for klass in Plugin.only_child_subclasses():
74-
if klass.__name__ in refactored_classes:
75-
if klass.__name__ == "PgWaitSampling": # check if plugin is for EE
76-
if Plugin.VersionPG['type'] == 'PGEE':
77-
plugins.append(klass(cfg))
78-
elif klass.__name__ == "PgStatProgressVacuum" \
79-
and Plugin.VersionPG['number'] >= LooseVersion('9.6'):
76+
if klass.__name__ == "PgWaitSampling": # check if plugin is for EE
77+
if Plugin.VersionPG['type'] == 'PGEE':
8078
plugins.append(klass(cfg))
81-
else:
79+
80+
else:
81+
if klass.__name__ != "Cfs":
8282
plugins.append(klass(cfg))
8383
types = args.plugin_type.split(',')
8484
# check if any plugin types is equal
@@ -89,9 +89,29 @@ def quit_handler(_signo=None, _stack_frame=None):
8989
if len(types) > 1:
9090
args.plugin_type = 'all'
9191
if args.plugin_type == 'pg' or args.plugin_type == 'sys' or args.plugin_type == 'all':
92+
# check if conf file has a path
93+
len_path = commands[2].rfind("/")
94+
#print(len_path)
95+
#print(len(commands[2]))
96+
# get path for conf file and scripts
97+
if len_path != -1:
98+
path = commands[2][:len_path] + "/scripts"
99+
Plugin.PATH = path
100+
else:
101+
path = "./scripts"
102+
Plugin.PATH = path
103+
# create directory for scripts along the path of conf file if needed
104+
if not os.path.exists(path):
105+
os.makedirs(path)
106+
print("directory for scripts has created")
92107
template = GetKeys()
108+
# write conf file
93109
with codecs.open(commands[2], 'w', 'utf-8') as f:
94110
f.write(template.txt(args.plugin_type, plugins)) # pass command type
111+
# write bash scripts for zabbix - agent to a file
112+
for key in Scripts.Bash:
113+
with codecs.open(path + "/" + key + ".sh", 'w', 'utf-8') as f:
114+
f.write(Scripts.Bash[key]) # pass script itself
95115
else:
96116
print_total_help()
97117
sys.exit(0)
@@ -102,16 +122,11 @@ def quit_handler(_signo=None, _stack_frame=None):
102122
elif commands[1] == 'template':
103123
plugins = []
104124
for klass in Plugin.only_child_subclasses():
105-
# temporary generate template for agent for classes that have been refactored
106-
if klass.__name__ in refactored_classes:
107-
if klass.__name__ == "PgWaitSampling": # check if plugin is for EE
108-
if Plugin.VersionPG['type'] == 'PGEE':
109-
plugins.append(klass(cfg))
110-
elif klass.__name__ == "PgStatProgressVacuum" \
111-
and Plugin.VersionPG['number'] >= LooseVersion('9.6'):
112-
plugins.append(klass(cfg))
113-
else:
125+
if klass.__name__ == "PgWaitSampling": # check if plugin is for EE
126+
if Plugin.VersionPG['type'] == 'PGEE':
114127
plugins.append(klass(cfg))
128+
else:
129+
plugins.append(klass(cfg))
115130
template = ZbxTemplate(args.template, args.application)
116131
with codecs.open(commands[2], 'w', 'utf-8') as f:
117132
f.write(template.xml(plugins))
@@ -124,18 +139,17 @@ def quit_handler(_signo=None, _stack_frame=None):
124139
if klass.__name__ == "PgWaitSampling": # check if plugin is for EE
125140
if Plugin.VersionPG['type'] == 'PGEE':
126141
plugins.append(klass(cfg))
127-
elif klass.__name__ == "PgStatProgressVacuum" \
128-
and Plugin.VersionPG['number'] >= LooseVersion('9.6'):
129-
plugins.append(klass(cfg))
130142
else:
131-
plugins.append(klass(cfg))
132-
template = ZbxTemplate(args.template,
133-
args.application)
143+
if klass.__name__ != "Cfs":
144+
plugins.append(klass(cfg))
145+
template = ZbxTemplate(args.template, args.application)
134146
with codecs.open(commands[2], 'w', 'utf-8') as f:
135147
f.write(template.xml(plugins))
136148
sys.exit(0)
137149
else:
138150
print_total_help()
151+
else:
152+
print_total_help()
139153
args, commands = parse_args()
140154
if len(commands) > 0:
141155
print_total_help()

0 commit comments

Comments
 (0)