]> arthur.barton.de Git - netdata.git/blob - python.d/postgres.chart.py
Add connections per database.
[netdata.git] / python.d / postgres.chart.py
1 # -*- coding: utf-8 -*-
2 # Description: example netdata python.d module
3 # Authors: facetoe, dangtranhoang
4
5 import re
6 from copy import deepcopy
7
8 import psycopg2
9 from psycopg2 import extensions
10 from psycopg2.extras import DictCursor
11
12 from base import SimpleService
13
14 # default module values
15 update_every = 1
16 priority = 90000
17 retries = 60
18
19 # Default Config options.
20 # {
21 #    'database': None,
22 #    'user': 'postgres',
23 #    'password': None,
24 #    'host': 'localhost',
25 #    'port': 5432
26 # }
27
28 ARCHIVE = """
29 SELECT
30     CAST(COUNT(*) AS INT) AS file_count,
31     CAST(COALESCE(SUM(CAST(archive_file ~ $r$\.ready$$r$ as INT)), 0) AS INT) AS ready_count,
32     CAST(COALESCE(SUM(CAST(archive_file ~ $r$\.done$$r$ AS INT)), 0) AS INT) AS done_count
33 FROM
34     pg_catalog.pg_ls_dir('pg_xlog/archive_status') AS archive_files (archive_file);
35 """
36
37 BACKENDS = """
38 SELECT
39     count(*) - (SELECT count(*) FROM pg_stat_activity WHERE state = 'idle') AS backends_active,
40     (SELECT count(*) FROM pg_stat_activity WHERE state = 'idle' ) AS backends_idle
41 FROM
42     pg_stat_activity;
43 """
44
45 TABLE_STATS = """
46 SELECT
47   ((sum(relpages) * 8) * 1024) AS size_relations,
48   count(1)                     AS relations
49 FROM pg_class
50 WHERE relkind IN ('r', 't');
51 """
52
53 INDEX_STATS = """
54 SELECT
55   ((sum(relpages) * 8) * 1024) AS size_indexes,
56   count(1)                     AS indexes
57 FROM pg_class
58 WHERE relkind = 'i';"""
59
60 DATABASE = """
61 SELECT
62   datname AS database_name,
63   sum(numbackends) AS connections,
64   sum(xact_commit) AS xact_commit,
65   sum(xact_rollback) AS xact_rollback,
66   sum(blks_read) AS blks_read,
67   sum(blks_hit) AS blks_hit,
68   sum(tup_returned) AS tup_returned,
69   sum(tup_fetched) AS tup_fetched,
70   sum(tup_inserted) AS tup_inserted,
71   sum(tup_updated) AS tup_updated,
72   sum(tup_deleted) AS tup_deleted,
73   sum(conflicts) AS conflicts
74 FROM pg_stat_database
75 WHERE NOT datname ~* '^template\d+'
76 GROUP BY database_name;
77 """
78
79 STATIO = """
80 SELECT
81     sum(heap_blks_read) AS heap_blocks_read,
82     sum(heap_blks_hit) AS heap_blocks_hit,
83     sum(idx_blks_read) AS index_blocks_read,
84     sum(idx_blks_hit) AS index_blocks_hit,
85     sum(toast_blks_read) AS toast_blocks_read,
86     sum(toast_blks_hit) AS toast_blocks_hit,
87     sum(tidx_blks_read) AS toastindex_blocks_read,
88     sum(tidx_blks_hit) AS toastindex_blocks_hit
89 FROM
90     pg_statio_all_tables
91 WHERE
92     schemaname <> 'pg_catalog';
93 """
94 BGWRITER = 'SELECT * FROM pg_stat_bgwriter;'
95 DATABASE_LOCKS = """
96 SELECT
97   pg_database.datname as database_name,
98   mode,
99   count(mode) AS count
100 FROM pg_locks
101   INNER JOIN pg_database ON pg_database.oid = pg_locks.database
102 GROUP BY datname, mode
103 ORDER BY datname, mode;
104 """
105 REPLICATION = """
106 SELECT
107     client_hostname,
108     client_addr,
109     state,
110     sent_offset - (
111         replay_offset - (sent_xlog - replay_xlog) * 255 * 16 ^ 6 ) AS byte_lag
112 FROM (
113     SELECT
114         client_addr, client_hostname, state,
115         ('x' || lpad(split_part(sent_location,   '/', 1), 8, '0'))::bit(32)::bigint AS sent_xlog,
116         ('x' || lpad(split_part(replay_location, '/', 1), 8, '0'))::bit(32)::bigint AS replay_xlog,
117         ('x' || lpad(split_part(sent_location,   '/', 2), 8, '0'))::bit(32)::bigint AS sent_offset,
118         ('x' || lpad(split_part(replay_location, '/', 2), 8, '0'))::bit(32)::bigint AS replay_offset
119     FROM pg_stat_replication
120 ) AS s;
121 """
122
123 LOCK_TYPES = [
124     'ExclusiveLock',
125     'RowShareLock',
126     'SIReadLock',
127     'ShareUpdateExclusiveLock',
128     'AccessExclusiveLock',
129     'AccessShareLock',
130     'ShareRowExclusiveLock',
131     'ShareLock',
132     'RowExclusiveLock'
133 ]
134
135 ORDER = ['db_stat_transactions', 'db_stat_tuple_read', 'db_stat_tuple_returned', 'db_stat_tuple_write',
136          'backend_process', 'index_count', 'index_size', 'table_count', 'table_size', 'wal', 'operations_heap',
137          'operations_index', 'operations_toast', 'operations_toast_index', 'background_writer']
138
139 CHARTS = {
140     'db_stat_transactions': {
141         'options': [None, ' Transactions', 'Count', ' database statistics', '.db_stat_transactions', 'line'],
142         'lines': [
143             ['db_stat_xact_commit', 'Committed', 'absolute'],
144             ['db_stat_xact_rollback', 'Rolled Back', 'absolute']
145         ]},
146     'db_stat_connections': {
147         'options': [None, ' Connections', 'Count', ' database statistics', '.db_stat_connections', 'line'],
148         'lines': [
149             ['db_stat_connections', 'Connections', 'absolute']
150         ]},
151     'db_stat_tuple_read': {
152         'options': [None, ' Tuple read', 'Count', ' database statistics', '.db_stat_tuple_read', 'line'],
153         'lines': [
154             ['db_stat_blks_read', 'Disk', 'absolute'],
155             ['db_stat_blks_hit', 'Cache', 'absolute']
156         ]},
157     'db_stat_tuple_returned': {
158         'options': [None, ' Tuple returned', 'Count', ' database statistics', '.db_stat_tuple_returned', 'line'],
159         'lines': [
160             ['db_stat_tup_returned', 'Sequential', 'absolute'],
161             ['db_stat_tup_fetched', 'Bitmap', 'absolute']
162         ]},
163     'db_stat_tuple_write': {
164         'options': [None, ' Tuple write', 'Count', ' database statistics', '.db_stat_tuple_write', 'line'],
165         'lines': [
166             ['db_stat_tup_inserted', 'Inserted', 'absolute'],
167             ['db_stat_tup_updated', 'Updated', 'absolute'],
168             ['db_stat_tup_deleted', 'Deleted', 'absolute'],
169             ['db_stat_conflicts', 'Conflicts', 'absolute']
170         ]},
171     'backend_process': {
172         'options': [None, 'Backend processes', 'Count', 'Backend processes', 'postgres.backend_process', 'line'],
173         'lines': [
174             ['backend_process_active', 'Active', 'absolute'],
175             ['backend_process_idle', 'Idle', 'absolute']
176         ]},
177     'index_count': {
178         'options': [None, 'Total index', 'Count', 'Index', 'postgres.index_count', 'line'],
179         'lines': [
180             ['index_count', 'Total index', 'absolute']
181         ]},
182     'index_size': {
183         'options': [None, 'Index size', 'MB', 'Index', 'postgres.index_size', 'line'],
184         'lines': [
185             ['index_size', 'Size', 'absolute', 1, 1024 * 1024]
186         ]},
187     'table_count': {
188         'options': [None, 'Total table', 'Count', 'Table', 'postgres.table_count', 'line'],
189         'lines': [
190             ['table_count', 'Total table', 'absolute']
191         ]},
192     'table_size': {
193         'options': [None, 'Table size', 'MB', 'Table', 'postgres.table_size', 'line'],
194         'lines': [
195             ['table_size', 'Size', 'absolute', 1, 1024 * 1024]
196         ]},
197     'wal': {
198         'options': [None, 'WAL stats', 'Files', 'WAL', 'postgres.wal', 'line'],
199         'lines': [
200             ['wal_total', 'Total', 'absolute'],
201             ['wal_ready', 'Ready', 'absolute'],
202             ['wal_done', 'Done', 'absolute']
203         ]},
204     'operations_heap': {
205         'options': [None, 'Heap', 'iops', 'IO Operations', 'postgres.operations_heap', 'line'],
206         'lines': [
207             ['operations_heap_blocks_read', 'Read', 'absolute'],
208             ['operations_heap_blocks_hit', 'Hit', 'absolute']
209         ]},
210     'operations_index': {
211         'options': [None, 'Index', 'iops', 'IO Operations', 'postgres.operations_index', 'line'],
212         'lines': [
213             ['operations_index_blocks_read', 'Read', 'absolute'],
214             ['operations_index_blocks_hit', 'Hit', 'absolute']
215         ]},
216     'operations_toast': {
217         'options': [None, 'Toast', 'iops', 'IO Operations', 'postgres.operations_toast', 'line'],
218         'lines': [
219             ['operations_toast_blocks_read', 'Read', 'absolute'],
220             ['operations_toast_blocks_hit', 'Hit', 'absolute']
221         ]},
222     'operations_toast_index': {
223         'options': [None, 'Toast index', 'iops', 'IO Operations', 'postgres.operations_toast_index', 'line'],
224         'lines': [
225             ['operations_toastindex_blocks_read', 'Read', 'absolute'],
226             ['operations_toastindex_blocks_hit', 'Hit', 'absolute']
227         ]},
228     'background_writer': {
229         'options': [None, 'Checkpoints', 'Count', 'Background Writer', 'postgres.background_writer', 'line'],
230         'lines': [
231             ['background_writer_scheduled', 'Scheduled', 'absolute'],
232             ['background_writer_requested', 'Requested', 'absolute']
233         ]}
234 }
235
236
237 class Service(SimpleService):
238     def __init__(self, configuration=None, name=None):
239         super(self.__class__, self).__init__(configuration=configuration, name=name)
240         self.order = ORDER
241         self.definitions = CHARTS
242         self.configuration = configuration
243         self.connection = None
244         self.data = {}
245         self.old_data = {}
246         self.databases = set()
247
248     def connect(self):
249         params = dict(user='postgres',
250                       database=None,
251                       password=None,
252                       host='localhost',
253                       port=5432)
254         params.update(self.configuration)
255         if not self.connection:
256             self.connection = psycopg2.connect(**params)
257             self.connection.set_isolation_level(extensions.ISOLATION_LEVEL_AUTOCOMMIT)
258             self.connection.set_session(readonly=True)
259
260     def check(self):
261         try:
262             self.connect()
263             self.discover_databases()
264             self._create_definitions()
265             return True
266         except Exception as e:
267             self.error(e)
268             return False
269
270     def _create_definitions(self):
271         for database_name in self.databases:
272             self.databases.add(database_name)
273             for chart_template_name in list(CHARTS):
274                 if chart_template_name.startswith('db_stat'):
275                     self._add_database_stat_chart(chart_template_name, database_name)
276             self._add_database_lock_chart(database_name)
277
278     def discover_databases(self):
279         cursor = self.connection.cursor()
280         cursor.execute("""
281             SELECT datname
282             FROM pg_stat_database
283             WHERE NOT datname ~* '^template\d+'
284         """)
285         self.databases = set(r[0] for r in cursor)
286         cursor.close()
287
288     def _add_database_stat_chart(self, chart_template_name, database_name):
289         chart_template = CHARTS[chart_template_name]
290         chart_name = "{}_{}".format(database_name, chart_template_name)
291         if chart_name not in self.order:
292             self.order.insert(0, chart_name)
293             name, title, units, family, context, chart_type = chart_template['options']
294             self.definitions[chart_name] = {
295                 'options': [
296                     name,
297                     database_name + title,
298                     units,
299                     database_name + family,
300                     database_name + context,
301                     chart_type
302                 ]
303             }
304
305             self.definitions[chart_name]['lines'] = []
306             for line in deepcopy(chart_template['lines']):
307                 line[0] = "{}_{}".format(database_name, line[0])
308                 self.definitions[chart_name]['lines'].append(line)
309
310     def _add_database_lock_chart(self, database_name):
311         chart_name = "{}_locks".format(database_name)
312         if chart_name not in self.order:
313             self.order.insert(0, chart_name)
314             self.definitions[chart_name] = dict(
315                 options=
316                 [
317                     None,
318                     database_name + ' locks',
319                     'Count',
320                     database_name + ' database statistics',
321                     database_name + '.locks',
322                     'line'
323                 ],
324                 lines=[]
325             )
326
327             for lock_type in LOCK_TYPES:
328                 lock_id = "{}_{}".format(database_name, lock_type.lower())
329                 label = re.sub("([a-z])([A-Z])", "\g<1> \g<2>", lock_type)
330                 self.definitions[chart_name]['lines'].append([lock_id, label, 'absolute'])
331
332     def _get_data(self):
333         self.connect()
334
335         cursor = self.connection.cursor(cursor_factory=DictCursor)
336         self.add_stats(cursor)
337
338         cursor.close()
339         return self.data
340
341     def add_stats(self, cursor):
342         self.add_database_stats(cursor)
343         self.add_backend_stats(cursor)
344         self.add_index_stats(cursor)
345         self.add_table_stats(cursor)
346         self.add_lock_stats(cursor)
347         self.add_statio_stats(cursor)
348         self.add_bgwriter_stats(cursor)
349
350         # self.add_replication_stats(cursor)
351
352         # add_wal_metrics needs superuser to get directory listings
353         # if self.config.get('superuser', True):
354         # self.add_wal_stats(cursor)
355
356     def add_database_stats(self, cursor):
357         cursor.execute(DATABASE)
358         for row in cursor:
359             database_name = row.get('database_name')
360             self.add_derive_value('db_stat_xact_commit', prefix=database_name, value=int(row.get('xact_commit', 0)))
361             self.add_derive_value('db_stat_xact_rollback', prefix=database_name, value=int(row.get('xact_rollback', 0)))
362             self.add_derive_value('db_stat_blks_read', prefix=database_name, value=int(row.get('blks_read', 0)))
363             self.add_derive_value('db_stat_blks_hit', prefix=database_name, value=int(row.get('blks_hit', 0)))
364             self.add_derive_value('db_stat_tup_returned', prefix=database_name, value=int(row.get('tup_returned', 0)))
365             self.add_derive_value('db_stat_tup_fetched', prefix=database_name, value=int(row.get('tup_fetched', 0)))
366             self.add_derive_value('db_stat_tup_inserted', prefix=database_name, value=int(row.get('tup_inserted', 0)))
367             self.add_derive_value('db_stat_tup_updated', prefix=database_name, value=int(row.get('tup_updated', 0)))
368             self.add_derive_value('db_stat_tup_deleted', prefix=database_name, value=int(row.get('tup_deleted', 0)))
369             self.add_derive_value('db_stat_conflicts', prefix=database_name, value=int(row.get('conflicts', 0)))
370             conn_key = "{}_{}".format(database_name, 'db_stat_connections')
371             self.data[conn_key] = int(row.get('connections', 0))
372
373     def add_backend_stats(self, cursor):
374         cursor.execute(BACKENDS)
375         temp = cursor.fetchone()
376
377         self.data['backend_process_active'] = int(temp.get('backends_active', 0))
378         self.data['backend_process_idle'] = int(temp.get('backends_idle', 0))
379
380     def add_index_stats(self, cursor):
381         cursor.execute(INDEX_STATS)
382         temp = cursor.fetchone()
383         self.data['index_count'] = int(temp.get('indexes', 0))
384         self.data['index_size'] = int(temp.get('size_indexes', 0))
385
386     def add_table_stats(self, cursor):
387         cursor.execute(TABLE_STATS)
388         temp = cursor.fetchone()
389         self.data['table_count'] = int(temp.get('relations', 0))
390         self.data['table_size'] = int(temp.get('size_relations', 0))
391
392     def add_lock_stats(self, cursor):
393         cursor.execute(DATABASE_LOCKS)
394         # First zero out all current lock values.
395         for database_name in self.databases:
396             for lock_type in LOCK_TYPES:
397                 lock_id = "{}_{}".format(database_name, lock_type.lower())
398                 self.data[lock_id] = 0
399
400         # Now populate those that have current locks
401         for row in cursor:
402             database_name, lock_type, lock_count = row
403             lock_id = "{}_{}".format(database_name, lock_type.lower())
404             self.data[lock_id] = lock_count
405
406     def add_wal_stats(self, cursor):
407         cursor.execute(ARCHIVE)
408         temp = cursor.fetchone()
409         self.add_derive_value('wal_total', int(temp.get('file_count', 0)))
410         self.add_derive_value('wal_ready', int(temp.get('ready_count', 0)))
411         self.add_derive_value('wal_done', int(temp.get('done_count', 0)))
412
413     def add_statio_stats(self, cursor):
414         cursor.execute(STATIO)
415         temp = cursor.fetchone()
416         self.add_derive_value('operations_heap_blocks_read', int(temp.get('heap_blocks_read', 0)))
417         self.add_derive_value('operations_heap_blocks_hit', int(temp.get('heap_blocks_hit', 0)))
418         self.add_derive_value('operations_index_blocks_read', int(temp.get('index_blocks_read', 0)))
419         self.add_derive_value('operations_index_blocks_hit', int(temp.get('index_blocks_hit', 0)))
420         self.add_derive_value('operations_toast_blocks_read', int(temp.get('toast_blocks_read', 0)))
421         self.add_derive_value('operations_toast_blocks_hit', int(temp.get('toast_blocks_hit', 0)))
422         self.add_derive_value('operations_toastindex_blocks_read', int(temp.get('toastindex_blocks_read', 0)))
423         self.add_derive_value('operations_toastindex_blocks_hit', int(temp.get('toastindex_blocks_hit', 0)))
424
425     def add_bgwriter_stats(self, cursor):
426         cursor.execute(BGWRITER)
427         temp = cursor.fetchone()
428
429         self.add_derive_value('background_writer_scheduled', temp.get('checkpoints_timed', 0))
430         self.add_derive_value('background_writer_requested', temp.get('checkpoints_requests', 0))
431
432     def add_derive_value(self, key, value, prefix=None):
433         if prefix:
434             key = "{}_{}".format(prefix, key)
435         if key not in self.old_data.keys():
436             self.data[key] = 0
437         else:
438             self.data[key] = value - self.old_data[key]
439
440         self.old_data[key] = value
441
442
443 '''
444     def add_replication_stats(self, cursor):
445         cursor.execute(REPLICATION)
446         temp = cursor.fetchall()
447         for row in temp:
448             self.add_gauge_value('Replication/%s' % row.get('client_addr', 'Unknown'),
449                                  'byte_lag',
450                                  int(row.get('byte_lag', 0)))
451 '''