Skip to content

Commit 81b4e21

Browse files
author
alexander popov
committed
fix: add database name in plugin relationssize
1 parent 7a0573e commit 81b4e21

File tree

2 files changed

+57
-56
lines changed

2 files changed

+57
-56
lines changed

mamonsu/plugins/pgsql/relations_size.py

Lines changed: 54 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -8,77 +8,78 @@
88
class RelationsSize(Plugin):
99
def __init__(self, config):
1010
super(Plugin, self).__init__(config)
11-
self.query = None
11+
self.relations = []
12+
self.create_relations()
1213
self.key_rel_size_discovery = "pgsql.relation.size{0}"
14+
self.query_template = """SELECT relation.schema
15+
, relation.name
16+
, CASE WHEN l.mode = 'AccessExclusiveLock' THEN '-1'
17+
ELSE (pg_total_relation_size(cl.oid))
18+
END AS pg_total_relation_size
19+
, CASE WHEN l.mode = 'AccessExclusiveLock' THEN '-1'
20+
ELSE (sum(pg_total_relation_size(inh.inhrelid)))
21+
END AS pg_total_relation_size_part
22+
FROM (VALUES {values}) as relation (schema,name)
23+
LEFT JOIN pg_catalog.pg_class cl ON cl.relname = relation.name
24+
LEFT JOIN pg_catalog.pg_namespace ns ON ns.oid = cl.relnamespace AND ns.nspname=relation.schema
25+
LEFT JOIN pg_catalog.pg_inherits inh ON inh.inhparent = cl.oid
26+
LEFT JOIN pg_catalog.pg_locks l ON l.relation = cl.oid AND l.mode= 'AccessExclusiveLock' AND l.locktype = 'relation'
27+
LEFT JOIN pg_catalog.pg_locks l_part ON l_part.relation = inh.inhrelid AND l.mode= 'AccessExclusiveLock' AND l.locktype = 'relation'
28+
GROUP BY relation.schema
29+
, relation.name
30+
, l.mode
31+
, cl.oid"""
1332

14-
15-
def create_query(self):
16-
query_template = """SELECT relation.schema
17-
, relation.name
18-
, CASE WHEN l.mode = 'AccessExclusiveLock' THEN '-1'
19-
ELSE (pg_total_relation_size(cl.oid))
20-
END AS pg_total_relation_size
21-
, CASE WHEN l.mode = 'AccessExclusiveLock' THEN '-1'
22-
ELSE (sum(pg_total_relation_size(inh.inhrelid)))
23-
END AS pg_total_relation_size_part
24-
FROM (VALUES {values}) as relation (schema,name)
25-
LEFT JOIN pg_catalog.pg_class cl ON cl.relname = relation.name
26-
LEFT JOIN pg_catalog.pg_namespace ns ON ns.oid = cl.relnamespace AND ns.nspname=relation.schema
27-
LEFT JOIN pg_catalog.pg_inherits inh ON inh.inhparent = cl.oid
28-
LEFT JOIN pg_catalog.pg_locks l ON l.relation = cl.oid AND l.mode= 'AccessExclusiveLock' AND l.locktype = 'relation'
29-
LEFT JOIN pg_catalog.pg_locks l_part ON l_part.relation = inh.inhrelid AND l.mode= 'AccessExclusiveLock' AND l.locktype = 'relation'
30-
GROUP BY relation.schema
31-
, relation.name
32-
, l.mode
33-
, cl.oid"""
33+
def create_relations(self):
3434

3535
config_relations = self._plugin_config.get('relations', None)
3636
if config_relations is None or config_relations == '':
3737
self.disable()
38-
raise PluginDisableException ("""Disable plugin and exit, because the parameter 'relations' in section [relationssize] is not set. Set this parameter like relations=pg_catalog.pg_class,pg_catalog.pg_user to count size if needed and restart.""")
38+
raise PluginDisableException(
39+
"""Disable plugin and exit, because the parameter 'relations' in section [relationssize] is not set. Set this parameter like relations=pg_catalog.pg_class,pg_catalog.pg_user to count size if needed and restart.""")
3940

40-
values = []
41+
self.relations = []
4142
for relation in config_relations.split(','):
4243
tmp_rel = relation.split('.')
43-
if len(tmp_rel) == 0:
44-
pass
45-
elif len(tmp_rel) == 1:
46-
values.append("(NULL, '{relation}')".format(relation=tmp_rel[0].strip()))
47-
elif len(tmp_rel) == 2:
48-
values.append(
49-
"('{schema}', '{relation}')".format(schema=tmp_rel[0].strip(), relation=tmp_rel[1].strip()))
44+
if len(tmp_rel) == 3:
45+
self.relations.append([tmp_rel[0].strip(), tmp_rel[1].strip(), tmp_rel[2].strip()])
5046
else:
5147
self.log.error(
52-
'The relation "{relation}" is not correct. You need to specify "schema.table" in the configuration file for mamonsu. Section: [relationssize], parameter: relations '.format(
48+
'The relation "{relation}" is not correct. You need to specify "database_name.schema.table" in the configuration file for mamonsu. Section: [relationssize], parameter: relations '.format(
5349
relation=relation))
5450

55-
self.query = query_template.format(values=',\n '.join(values))
56-
5751
def run(self, zbx):
58-
if not self.query:
59-
self.create_query()
60-
result = Pooler.query(self.query)
6152
rels = []
62-
for schema, name, pg_total_relation_size, pg_total_relation_size_part in result:
63-
if not schema:
64-
full_name_relation = name
65-
else:
66-
full_name_relation = schema + '.' + name
67-
rels.append({'{#RELATIONNAME}': full_name_relation })
68-
69-
if pg_total_relation_size is None and pg_total_relation_size_part is None:
70-
self.log.error('The relation: "{full_name_relation}" in not correct'.format(full_name_relation = full_name_relation))
71-
size = -1
72-
elif pg_total_relation_size ==-1 or pg_total_relation_size_part ==-1:
53+
all_databases = Pooler.databases()
54+
for database_name, schema, relation in self.relations:
55+
rels.append({'{#RELATIONNAME}': database_name + '.' + schema + '.' + relation})
56+
if not (database_name in all_databases):
7357
self.log.error(
74-
"The relation: {full_name_relation} is locked. "
75-
"You can find this lock in query: "
76-
"SELECT relation::regclass AS lock_relation, mode FROM pg_locks WHERE relation::regclass = 'pg_locks'::regclass;".format(full_name_relation=full_name_relation))
77-
size = -1
58+
'''The database: "{database_name}" do not exists. '''
59+
'''Check the the parameter "relations" in section [relatonssize].'''.format(
60+
database_name=database_name))
7861
else:
79-
size = (pg_total_relation_size or 0) + (pg_total_relation_size_part or 0)
62+
result = Pooler.query(query=self.query_template.format(
63+
values="('{schema}','{relation}')".format(schema=schema, relation=relation)), db=database_name)
64+
for schema, name, pg_total_relation_size, pg_total_relation_size_part in result:
65+
full_name_relation = database_name + '.' + schema + '.' + relation
66+
67+
if pg_total_relation_size is None and pg_total_relation_size_part is None:
68+
self.log.error('The relation: "{full_name_relation}" in not correct'.format(
69+
full_name_relation=full_name_relation))
70+
size = -1
71+
elif pg_total_relation_size == -1 or pg_total_relation_size_part == -1:
72+
self.log.error(
73+
"The relation: {full_name_relation} is locked. "
74+
"You can find this lock in query: "
75+
"SELECT relation::regclass AS lock_relation, mode FROM pg_locks "
76+
"WHERE relation::regclass = 'pg_locks'::regclass;".format(
77+
full_name_relation=full_name_relation))
78+
size = -1
79+
else:
80+
size = (pg_total_relation_size or 0) + (pg_total_relation_size_part or 0)
8081

81-
zbx.send('pgsql.relation.size[{0}]'.format(full_name_relation), int(size))
82+
zbx.send('pgsql.relation.size[{0}]'.format(full_name_relation), int(size))
8283

8384
zbx.send('pgsql.relation.size[]', zbx.json({'data': rels}))
8485

packaging/conf/example.conf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,14 @@ interval = 2
168168
interval = 60
169169

170170
# Get size of relations defined in this section
171-
# Relations - comma separated list of objects - tables and endexes (schema.relation) used to calculate relations size.
171+
# Relations - comma separated list of objects - tables and endexes (database_name.schema.relation) used to calculate relations size.
172172
# Example:
173-
# relations=pg_catalog.pg_class,pg_catalog.pg_user
173+
# relations=postgres.pg_catalog.pg_class,postgres.pg_catalog.pg_user
174174
# If the relation is blocked by some process such as vacuum full or create index, the result will be -1
175175
# by default this plugin disabled. To enable this plugin - set bellow "enabled = False" and define a list of relations.
176176
[relationssize]
177177
enabled = False
178-
relations=pg_catalog.pg_class,pg_catalog.pg_user
178+
relations=postgres.pg_catalog.pg_class,postgres.pg_catalog.pg_user
179179
interval = 500
180180

181181
# Get age (in seconds) of the oldest running prepared transaction and number of all prepared transactions for two-phase commit.

0 commit comments

Comments
 (0)