Skip to content

Commit f5ba486

Browse files
committed
fix: auto export template for version 4.0+ zabbix server and export template for unsupported versions with flag --old-zabbix
1 parent f1a47a7 commit f5ba486

File tree

11 files changed

+167
-29
lines changed

11 files changed

+167
-29
lines changed

mamonsu/lib/parser.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,13 @@
4646
--pg-version <pg_version>
4747
--add-plugins <directory>
4848
--config <file>
49+
--old-zabbix
4950
Default plugin_type = all, template name = PostgresPro-<platform name>,
5051
application = App-PostgresPro-<platform name>, pg-version = 10,
5152
Note: default pg-version is vanilla, but with PGPRO or PGEE before version number it can be changed. Supported version
5253
numbers are 10, 11, 9.6, 9.5
54+
default template export for currently supported zabbix -server versions, if template for unsupported versions
55+
is needed use --old zabbix flag
5356
Example: PGPRO_10 or PGEE_11 or PGPRO_9.6
5457
5558
@@ -62,7 +65,10 @@
6265
--template-name <template name>
6366
--application <application name in template>
6467
--config <file>
68+
--old-zabbix
6569
Default template name = PostgresPro-<platform name>, application = App-PostgresPro-<platform name>
70+
Note: default template export for currently supported zabbix -server versions, if template for unsupported versions
71+
is needed use --old zabbix flag
6672
6773
6874
Bootstrap DDL for monitoring:
@@ -261,4 +267,8 @@ def parse_args():
261267
parser.add_option(
262268
'--application', dest='application',
263269
default='App-PostgresPro-{0}'.format(sys.platform.title()))
270+
parser.add_option(
271+
'--old-zabbix',
272+
dest='old_zabbix', action='store_true',
273+
help='Create special template for currently unsupported zabbix versions')
264274
return parser.parse_args()

mamonsu/lib/plugin.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ class Plugin(object):
4242
# for all childs
4343
is_child = True
4444

45+
# old_zabbix_server_version
46+
old_zabbix = False
47+
4548
# const
4649
PATH = "/etc/zabbix/zabbix_agent.d/scripts"
4750
DELTA = Template.DELTA

mamonsu/lib/runner.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ 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 args.old_zabbix:
61+
Plugin.old_zabbix = True
6062
if not len(commands) == 2 and not len(commands) == 3:
6163
print_total_help()
6264
if commands[1] == 'zabbix-parameters':
@@ -122,7 +124,7 @@ def quit_handler(_signo=None, _stack_frame=None):
122124
commands.append('postgrespro.xml')
123125
print('Template for mamonsu have been saved in postgrespro.conf file')
124126
for klass in Plugin.only_child_subclasses():
125-
if klass.__name__ == "PgWaitSampling": # check if plugin is for EE
127+
if klass.__name__ == "PgWaitSampling" or klass.__name__ == "Cfs" : # check if plugin is for EE
126128
if Plugin.VersionPG['type'] == 'PGEE':
127129
plugins.append(klass(cfg))
128130
else:

mamonsu/lib/zbx_template.py

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class ZbxTemplate(object):
5555
('snmpv3_contextname', None), ('snmpv3_securityname', None),
5656
('snmpv3_securitylevel', 0), ('snmpv3_authprotocol', 0),
5757
('snmpv3_authpassphrase', None), ('snmpv3_privprotocol', 0),
58-
('snmpv3_privpassphrase', None), ('formula', 1),
58+
('snmpv3_privpassphrase', None), ('logtimefmt', None), ('formula', 1),
5959
('delay_flex', None), ('params', None),
6060
('ipmi_sensor', None), ('data_type', 0), ('authtype', 0),
6161
('username', None), ('password', None), ('publickey', None),
@@ -89,14 +89,20 @@ class ZbxTemplate(object):
8989
('calc_fnc', 2), ('type', Template.GRAPH_TYPE.normal)
9090
]
9191

92+
condition_defaults = [
93+
('macro', None), ('value', 0),
94+
('operator', None),
95+
('formulaid', None)
96+
]
97+
9298
discovery_defaults = [
9399
('name', None), ('type', 2), ('snmp_community', None),
94100
('snmp_oid', None), ('delay', 60), ('status', 0),
95101
('allowed_hosts', None), ('snmpv3_contextname', None),
96102
('snmpv3_securityname', None), ('snmpv3_securitylevel', 0),
97103
('snmpv3_authprotocol', 0), ('snmpv3_authpassphrase', None),
98104
('snmpv3_privprotocol', 0), ('snmpv3_privpassphrase', None),
99-
('delay_flex', None), ('params', None), ('filter', None),
105+
('delay_flex', None), ('params', None),
100106
('ipmi_sensor', None), ('authtype', 0),
101107
('username', None), ('password', None), ('publickey', None),
102108
('privatekey', None), ('port', None), ('lifetime', 7),
@@ -194,7 +200,23 @@ def graph(self, args={}, xml_key='graph'):
194200
self._format_args(self.graph_values_defaults, args),
195201
graph_items, xml_key)
196202

197-
def discovery_rule(self, rule={}, items=[], triggers=[], graphs=[]):
203+
# condition for template creation for zabbix version 4.4
204+
def condition(self, args={}, xml_key='condition'):
205+
try:
206+
conditions = args['condition']
207+
except KeyError:
208+
raise LookupError(
209+
'Miss item in conditions: {0}.'.format(args))
210+
res = ''
211+
for idx, item in enumerate(conditions):
212+
res += '<{1}>{0}</{1}>'.format(
213+
self._format_args(self.condition_defaults, item),
214+
xml_key)
215+
res = '<conditions>' + res + '</conditions>' + '<formula/><evaltype>0</evaltype>'
216+
217+
return res
218+
219+
def discovery_rule(self, rule={},conditions=[], items=[], triggers=[], graphs=[]):
198220

199221
result_items = '<item_prototypes>'
200222
for item in items:
@@ -214,10 +236,24 @@ def discovery_rule(self, rule={}, items=[], triggers=[], graphs=[]):
214236
graph, xml_key='graph_prototype')
215237
result_graphs += '</graph_prototypes>'
216238

217-
result = '<discovery_rule>{0}{1}{2}{3}</discovery_rule>'
239+
if len(conditions) > 0:
240+
241+
result_conditions = '<filter>'
242+
for condition in conditions:
243+
result_conditions += self.condition(
244+
condition, xml_key='condition')
245+
result_conditions += '</filter>'
246+
else:
247+
result = '<discovery_rule>{0}{1}{2}{3}</discovery_rule>'
248+
self.discovery_defaults.append(('filter', None))
249+
return result.format(
250+
self._format_args(self.discovery_defaults, rule),
251+
result_items, result_triggers, result_graphs)
252+
253+
result = '<discovery_rule>{0}{1}{2}{3}{4}</discovery_rule>'
218254
return result.format(
219255
self._format_args(self.discovery_defaults, rule),
220-
result_items, result_triggers, result_graphs)
256+
result_conditions, result_items, result_triggers, result_graphs)
221257

222258
def _application(self):
223259
result = '<applications><application><name>{0}'

mamonsu/plugins/pgsql/cfs.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,21 +159,36 @@ def graphs(self, template):
159159
def discovery_rules(self, template):
160160
rule = {
161161
'name': 'Compressed relations discovery',
162-
'key': 'pgsql.cfs.discovery_compressed_relations[]',
163-
'filter': '{#COMPRESSED_RELATION}:.*'
162+
'key': 'pgsql.cfs.discovery_compressed_relations[]'
163+
# 'filter': '{#COMPRESSED_RELATION}:.*'
164164
}
165+
if Plugin.old_zabbix:
166+
rule['filter'] = '{#DATABASE}:.*'
167+
conditions = []
168+
else:
169+
conditions = [
170+
{
171+
'condition': [
172+
{'macro': '{#COMPRESSED_RELATION}',
173+
'value': '.*',
174+
'operator': '',
175+
'formulaid': 'A'}
176+
]
177+
}
178+
179+
]
165180
items = [
166181
{'key': 'pgsql.cfs.compress_ratio[{#COMPRESSED_RELATION}]',
167-
'name': 'Relation {#COMPRESSED_RELATION}: compress ratio',
168-
'delay': self.timeRatioInterval}
182+
'name': 'Relation {#COMPRESSED_RELATION}: compress ratio',
183+
'delay': self.timeRatioInterval}
169184
]
170185
graphs = [
171186
{
172187
'name': 'Relation {#COMPRESSED_RELATION}: compress ratio',
173188
'delay': self.timeRatioInterval,
174189
'items': [
175190
{'color': '00CC00',
176-
'key': 'pgsql.cfs.compress_ratio[{#COMPRESSED_RELATION}]'}]
191+
'key': 'pgsql.cfs.compress_ratio[{#COMPRESSED_RELATION}]'}]
177192
},
178193
]
179-
return template.discovery_rule(rule=rule, items=items, graphs=graphs)
194+
return template.discovery_rule(rule=rule, conditions=conditions, items=items, graphs=graphs)

mamonsu/plugins/pgsql/databases.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,23 @@ def discovery_rules(self, template):
6464
rule = {
6565
'name': 'Database discovery',
6666
'key': self.key_db_discovery.format('[{0}]'.format(self.Macros[self.Type])),
67-
'filter': '{#DATABASE}:.*'
67+
6868
}
69+
if Plugin.old_zabbix:
70+
conditions = []
71+
rule['filter'] = '{#DATABASE}:.*'
72+
else:
73+
conditions = [
74+
{
75+
'condition': [
76+
{'macro': '{#DATABASE}',
77+
'value': '.*',
78+
'operator': '',
79+
'formulaid': 'A'}
80+
]
81+
}
82+
83+
]
6984
items = [
7085
{'key': self.right_type(self.key_db_size, var_discovery="{#DATABASE},"),
7186
'name': 'Database {#DATABASE}: size',
@@ -106,7 +121,7 @@ def discovery_rules(self, template):
106121
'yaxisside': 1}]
107122
}
108123
]
109-
return template.discovery_rule(rule=rule, items=items, graphs=graphs)
124+
return template.discovery_rule(rule=rule, conditions=conditions, items=items, graphs=graphs)
110125

111126
def keys_and_queries(self, template_zabbix):
112127
result = []

mamonsu/plugins/pgsql/pg_wait_sampling.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,24 @@ def graphs(self, template):
206206
def discovery_rules(self, template):
207207
rule = ({
208208
'name': 'AllLockItems',
209-
'key': self.key_all_lock_discovery.format('[{0}]'.format(self.Macros[self.Type])),
210-
'filter': '{#ALL_LOCK}:.*'
209+
'key': self.key_all_lock_discovery.format('[{0}]'.format(self.Macros[self.Type]))
211210
})
212211

212+
if Plugin.old_zabbix:
213+
rule['filter'] = '{#ALL_LOCK}:.*'
214+
conditions = []
215+
else:
216+
conditions = [
217+
{
218+
'condition': [
219+
{'macro': '{#ALL_LOCK}',
220+
'value': '.*',
221+
'operator': '',
222+
'formulaid': 'A'}
223+
]
224+
}
225+
226+
]
213227
if self.Type == "mamonsu":
214228
delta = Plugin.DELTA.as_is
215229
else:
@@ -235,7 +249,7 @@ def discovery_rules(self, template):
235249
'color': item[3]})
236250
graphs.append({'name': graph_name, 'type': 1, 'items': items})
237251

238-
return template.discovery_rule(rule=rule, items=items, graphs=graphs)
252+
return template.discovery_rule(rule=rule, conditions=conditions, items=items, graphs=graphs)
239253

240254
def keys_and_queries(self, template_zabbix):
241255
result = []

mamonsu/plugins/system/linux/disk_sizes.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class DiskSizes(Plugin):
2828
'efivarfs', 'fusectl', 'fuse.gvfsd-fuse',
2929
'hugetlbfs', 'mqueue', 'binfmt_misc',
3030
'nfsd', 'proc', 'pstore', 'selinuxfs'
31-
'rpc_pipefs', 'securityfs', 'sysfs',
31+
'rpc_pipefs', 'securityfs', 'sysfs',
3232
'nsfs', 'tmpfs', 'tracefs']
3333

3434
def run(self, zbx):
@@ -72,9 +72,23 @@ def discovery_rules(self, template):
7272
key_discovery = 'system.vfs.discovery'
7373
rule = {
7474
'name': 'VFS discovery',
75-
'key': key_discovery,
76-
'filter': '{#MOUNTPOINT}:.*'
75+
'key': key_discovery
7776
}
77+
if Plugin.old_zabbix:
78+
rule['filter'] = '{#MOUNTPOINT}:.*'
79+
conditions = []
80+
else:
81+
conditions = [
82+
{
83+
'condition': [
84+
{'macro': '{#MOUNTPOINT}',
85+
'value': '.*',
86+
'operator': '',
87+
'formulaid': 'A'}
88+
]
89+
}
90+
91+
]
7892
items = [
7993
{
8094
'key': 'system.vfs.used[{#MOUNTPOINT}]',
@@ -129,7 +143,7 @@ def discovery_rules(self, template):
129143
})
130144

131145
return template.discovery_rule(
132-
rule=rule, items=items, graphs=graphs, triggers=triggers)
146+
rule=rule, conditions=conditions, items=items, graphs=graphs, triggers=triggers)
133147

134148
def keys_and_queries(self, template_zabbix):
135149
result = []

mamonsu/plugins/system/linux/disk_stats.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,23 @@ def discovery_rules(self, template):
124124
delta = Plugin.DELTA_SPEED
125125
rule = {
126126
'name': 'Block device discovery',
127-
'key': key_discovery,
128-
'filter': '{#BLOCKDEVICE}:.*'
127+
'key': key_discovery
129128
}
129+
if Plugin.old_zabbix:
130+
rule['filter'] = '{#BLOCKDEVICE}:.*'
131+
conditions = []
132+
else:
133+
conditions = [
134+
{
135+
'condition': [
136+
{'macro': '{#BLOCKDEVICE}',
137+
'value': '.*',
138+
'operator': '',
139+
'formulaid': 'A'}
140+
]
141+
}
142+
143+
]
130144
items = [
131145
{
132146
'key': 'system.disk.utilization[{#BLOCKDEVICE}]',
@@ -183,7 +197,7 @@ def discovery_rules(self, template):
183197
'key': 'system.disk.utilization[{#BLOCKDEVICE}]'}]
184198
}]
185199

186-
return template.discovery_rule(rule=rule, items=items, graphs=graphs)
200+
return template.discovery_rule(rule=rule, conditions=conditions, items=items, graphs=graphs)
187201

188202
def keys_and_queries(self, template_zabbix):
189203
result = []

mamonsu/plugins/system/linux/net.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,23 @@ def discovery_rules(self, template):
5252

5353
rule = {
5454
'name': 'Net iface discovery',
55-
'key': key_discovery,
56-
'filter': '{#NETDEVICE}:.*'
55+
'key': key_discovery
5756
}
57+
if Plugin.old_zabbix:
58+
rule['filter'] = '{#NETDEVICE}:.*'
59+
conditions = []
60+
else:
61+
conditions = [
62+
{
63+
'condition': [
64+
{'macro': '{#NETDEVICE}',
65+
'value': '.*',
66+
'operator': '',
67+
'formulaid': 'A'}
68+
]
69+
}
70+
71+
]
5872
graphs = [{
5973
'name': 'Network device: {#NETDEVICE}',
6074
'items': [{
@@ -64,7 +78,7 @@ def discovery_rules(self, template):
6478
'color': '0000CC',
6579
'key': 'system.net.tx_bytes[{#NETDEVICE}]'}]
6680
}]
67-
return template.discovery_rule(rule=rule, items=items, graphs=graphs)
81+
return template.discovery_rule(rule=rule,conditions=conditions, items=items, graphs=graphs)
6882

6983
def keys_and_queries(self, template_zabbix):
7084
result = []

0 commit comments

Comments
 (0)