|
8 | 8 | class RelationsSize(Plugin):
|
9 | 9 | def __init__(self, config):
|
10 | 10 | super(Plugin, self).__init__(config)
|
11 |
| - self.query = None |
| 11 | + self.relations = [] |
| 12 | + self.create_relations() |
12 | 13 | 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""" |
13 | 32 |
|
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): |
34 | 34 |
|
35 | 35 | config_relations = self._plugin_config.get('relations', None)
|
36 | 36 | if config_relations is None or config_relations == '':
|
37 | 37 | 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.""") |
39 | 40 |
|
40 |
| - values = [] |
| 41 | + self.relations = [] |
41 | 42 | for relation in config_relations.split(','):
|
42 | 43 | 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()]) |
50 | 46 | else:
|
51 | 47 | 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( |
53 | 49 | relation=relation))
|
54 | 50 |
|
55 |
| - self.query = query_template.format(values=',\n '.join(values)) |
56 |
| - |
57 | 51 | def run(self, zbx):
|
58 |
| - if not self.query: |
59 |
| - self.create_query() |
60 |
| - result = Pooler.query(self.query) |
61 | 52 | 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): |
73 | 57 | 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)) |
78 | 61 | 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) |
80 | 81 |
|
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)) |
82 | 83 |
|
83 | 84 | zbx.send('pgsql.relation.size[]', zbx.json({'data': rels}))
|
84 | 85 |
|
|
0 commit comments