-
Notifications
You must be signed in to change notification settings - Fork 595
Expand file tree
/
Copy pathpostgres.py
More file actions
723 lines (635 loc) · 23.5 KB
/
postgres.py
File metadata and controls
723 lines (635 loc) · 23.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
# coding=utf-8
"""
Collect metrics from postgresql
#### Dependencies
* psycopg2
#### Example Configuration
#Section with defaults
enabled=True
password=default_password
port = 5432
#Instance specific configs
[instances]
[[postgres_a]]
host = db1.loc
password = instance_password_a
[[postgres_b]]
host = db2.loc
port = 5433
password = instance_password_b
"""
import diamond.collector
from diamond.collector import str_to_bool
try:
import psycopg2
import psycopg2.extras
except ImportError:
psycopg2 = None
class PostgresqlCollector(diamond.collector.Collector):
"""
PostgreSQL collector class
"""
def get_default_config_help(self):
"""
Return help text for collector
"""
config_help = super(PostgresqlCollector,
self).get_default_config_help()
config_help.update({
'dbname': 'DB to connect to in order to get list of DBs in PgSQL',
'user': 'Username',
'password': 'Password',
'port': 'Port number',
'password_provider': "Whether to auth with supplied password or"
" .pgpass file <password|pgpass>",
'sslmode': 'Whether to use SSL - <disable|allow|require|...>',
'underscore': 'Convert _ to .',
'extended': 'Enable collection of extended database stats.',
'metrics': 'List of enabled metrics to collect',
'pg_version': "The version of postgres that you'll be monitoring"
"eg. in format 9.2",
'has_admin': 'Admin privileges are required to execute some'
' queries.',
'instances': 'A subcategory of postgres instances with a host '
'and port. Optionally all variables can be '
'overridden per instance (see example).',
})
return config_help
def get_default_config(self):
"""
Return default config.
"""
config = super(PostgresqlCollector, self).get_default_config()
config.update({
'path': 'postgres',
'host': 'localhost',
'dbname': 'postgres',
'user': 'postgres',
'password': 'postgres',
'port': 5432,
'password_provider': 'password',
'sslmode': 'disable',
'underscore': False,
'extended': False,
'metrics': [],
'pg_version': 9.2,
'has_admin': True,
'instances': {},
})
return config
def collect(self):
"""
Do pre-flight checks, get list of db names, collect metrics, publish
"""
if psycopg2 is None:
self.log.error('Unable to import module psycopg2')
return {}
instances = self.config.get('instances')
# HACK: setting default with subcategory messes up merging of configs,
# so we only set the default if one wasn't provided.
if not instances:
instances = {
'default': {
'host': self.config['host'],
}
}
for instance in instances:
# Get list of databases
dbs = self._get_db_names(instance)
if len(dbs) == 0:
self.log.error("I have 0 databases!")
return {}
if self._get_config(instance, 'metrics'):
metrics = self._get_config(instance, 'metrics')
elif str_to_bool(self._get_config(instance, 'extended')):
metrics = registry['extended']
if str_to_bool(self._get_config(instance, 'has_admin')) \
and 'WalSegmentStats' not in metrics:
metrics.append('WalSegmentStats')
else:
metrics = registry['basic']
# Iterate every QueryStats class
for metric_name in set(metrics):
if metric_name not in metrics_registry:
self.log.error(
'metric_name %s not found in metric registry'
% metric_name)
continue
for dbase in dbs:
conn = self._connect(instance, database=dbase)
try:
klass = metrics_registry[metric_name]
stat = klass(dbase, conn,
underscore=self._get_config(instance,
'underscore'))
stat.fetch(self._get_config(instance, 'pg_version'))
for metric, value in stat:
if value is not None:
self.publish("%s.%s" % (instance, metric),
value)
# Setting multi_db to True will run this query on all
# known databases. This is bad for queries that hit
# views like pg_database, which are shared
# across databases.
#
# If multi_db is False, bail early after the first query
# iteration. Otherwise, continue to remaining databases.
if stat.multi_db is False:
break
finally:
conn.close()
def _get_config(self, instance, name):
"""
Return instance config value or value from default section
if it is overriden or None
"""
instance_config = self.config['instances'].get(instance)
if instance_config:
return instance_config.get(name, self.config.get(name, None)
if name != 'host' else None)
else:
return None
def _get_db_names(self, instance):
"""
Try to get a list of db names
"""
query = """
SELECT datname FROM pg_database
WHERE datallowconn AND NOT datistemplate
AND NOT datname='postgres' AND NOT datname='rdsadmin' ORDER BY 1
"""
conn = self._connect(instance,
self._get_config(instance, 'dbname'))
cursor = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cursor.execute(query)
datnames = [d['datname'] for d in cursor.fetchall()]
conn.close()
# Exclude `postgres` database list, unless it is the
# only database available (required for querying pg_stat_database)
if not datnames:
datnames = ['postgres']
return datnames
def _connect(self, instance, database=None):
"""
Connect to given database
"""
conn_args = {
'host': self._get_config(instance, 'host'),
'user': self._get_config(instance, 'user'),
'password': self._get_config(instance, 'password'),
'port': self._get_config(instance, 'port'),
'sslmode': self._get_config(instance, 'sslmode'),
}
if database:
conn_args['database'] = database
else:
conn_args['database'] = 'postgres'
# libpq will use ~/.pgpass only if no password supplied
if self._get_config(instance, 'password_provider') == 'pgpass':
del conn_args['password']
try:
conn = psycopg2.connect(**conn_args)
except Exception, e:
self.log.error(e)
raise e
# Avoid using transactions, set isolation level to autocommit
conn.set_isolation_level(0)
return conn
class QueryStats(object):
query = None
path = None
def __init__(self, dbname, conn, parameters=None, underscore=False):
self.conn = conn
self.dbname = dbname
self.underscore = underscore
self.parameters = parameters
self.data = list()
def _translate_datname(self, datname):
"""
Replace '_' with '.'
"""
if self.underscore:
datname = datname.replace("_", ".")
return datname
def fetch(self, pg_version):
if float(pg_version) >= 9.6 and hasattr(self, 'post_96_query'):
q = self.post_96_query
elif float(pg_version) >= 9.2 and hasattr(self, 'post_92_query'):
q = self.post_92_query
else:
q = self.query
cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
try:
cursor.execute(q, self.parameters)
rows = cursor.fetchall()
for row in rows:
# If row is length 2, assume col1, col2 forms key: value
if len(row) == 2:
self.data.append({
'datname': self._translate_datname(self.dbname),
'metric': row[0],
'value': row[1],
})
# If row > length 2, assume each column name maps to
# key => value
else:
for key, value in row.iteritems():
if key in ('datname', 'schemaname', 'relname',
'indexrelname', 'funcname',):
continue
self.data.append({
'datname': self._translate_datname(row.get(
'datname', self.dbname)),
'schemaname': row.get('schemaname', None),
'relname': row.get('relname', None),
'indexrelname': row.get('indexrelname', None),
'funcname': row.get('funcname', None),
'metric': key,
'value': value,
})
# Clean up
finally:
cursor.close()
def __iter__(self):
for data_point in self.data:
yield (self.path % data_point, data_point['value'])
class DatabaseStats(QueryStats):
"""
Database-level summary stats
"""
path = "database.%(datname)s.%(metric)s"
multi_db = False
post_92_query = """
SELECT pg_stat_database.datname as datname,
pg_stat_database.numbackends as numbackends,
pg_stat_database.xact_commit as xact_commit,
pg_stat_database.xact_rollback as xact_rollback,
pg_stat_database.blks_read as blks_read,
pg_stat_database.blks_hit as blks_hit,
pg_stat_database.tup_returned as tup_returned,
pg_stat_database.tup_fetched as tup_fetched,
pg_stat_database.tup_inserted as tup_inserted,
pg_stat_database.tup_updated as tup_updated,
pg_stat_database.tup_deleted as tup_deleted,
pg_database_size(pg_database.datname) AS size
FROM pg_database
JOIN pg_stat_database
ON pg_database.datname = pg_stat_database.datname
WHERE pg_stat_database.datname
NOT IN ('template0','template1','postgres', 'rdsadmin')
"""
query = post_92_query.replace(
'pg_stat_database.temp_files as temp_files,',
'').replace(
'pg_stat_database.temp_bytes as temp_bytes,',
'')
class UserFunctionStats(QueryStats):
# http://www.pateldenish.com/2010/11/postgresql-track-functions-to-tune.html
path = "%(datname)s.functions.%(funcname)s.%(metric)s"
multi_db = True
query = """
SELECT funcname,
calls,
total_time/calls as time_per_call
FROM pg_stat_user_functions
WHERE calls <> 0
"""
class UserTableStats(QueryStats):
path = "%(datname)s.tables.%(schemaname)s.%(relname)s.%(metric)s"
multi_db = True
query = """
SELECT relname,
schemaname,
seq_scan,
seq_tup_read,
idx_scan,
idx_tup_fetch,
n_tup_ins,
n_tup_upd,
n_tup_del,
n_tup_hot_upd,
n_live_tup,
n_dead_tup
FROM pg_stat_user_tables
"""
class UserIndexStats(QueryStats):
path = "%(datname)s.indexes.%(schemaname)s.%(relname)s." \
"%(indexrelname)s.%(metric)s"
multi_db = True
query = """
SELECT relname,
schemaname,
indexrelname,
idx_scan,
idx_tup_read,
idx_tup_fetch
FROM pg_stat_user_indexes
"""
class UserTableIOStats(QueryStats):
path = "%(datname)s.tables.%(schemaname)s.%(relname)s.%(metric)s"
multi_db = True
query = """
SELECT relname,
schemaname,
heap_blks_read,
heap_blks_hit,
idx_blks_read,
idx_blks_hit
FROM pg_statio_user_tables
"""
class UserIndexIOStats(QueryStats):
path = "%(datname)s.indexes.%(schemaname)s.%(relname)s." \
"%(indexrelname)s.%(metric)s"
multi_db = True
query = """
SELECT relname,
schemaname,
indexrelname,
idx_blks_read,
idx_blks_hit
FROM pg_statio_user_indexes
"""
class ConnectionStateStats(QueryStats):
path = "%(datname)s.connections.%(metric)s"
multi_db = True
query = """
SELECT tmp.state AS key,COALESCE(count,0) FROM
(VALUES ('active'),
('waiting'),
('idle'),
('idletransaction'),
('unknown')
) AS tmp(state)
LEFT JOIN
(SELECT CASE WHEN waiting THEN 'waiting'
WHEN current_query = '<IDLE>' THEN 'idle'
WHEN current_query = '<IDLE> in transaction'
THEN 'idletransaction'
WHEN current_query = '<insufficient privilege>'
THEN 'unknown'
ELSE 'active' END AS state,
count(*) AS count
FROM pg_stat_activity
WHERE procpid != pg_backend_pid()
GROUP BY CASE WHEN waiting THEN 'waiting'
WHEN current_query = '<IDLE>' THEN 'idle'
WHEN current_query = '<IDLE> in transaction'
THEN 'idletransaction'
WHEN current_query = '<insufficient privilege>'
THEN 'unknown' ELSE 'active' END
) AS tmp2
ON tmp.state=tmp2.state ORDER BY 1
"""
post_92_query = """
SELECT tmp.mstate AS state,COALESCE(count,0) FROM
(VALUES ('active'),
('waiting'),
('idle'),
('idletransaction'),
('unknown')
) AS tmp(mstate)
LEFT JOIN
(SELECT CASE WHEN waiting THEN 'waiting'
WHEN state = 'idle' THEN 'idle'
WHEN state LIKE 'idle in transaction%'
THEN 'idletransaction'
WHEN state = 'disabled'
THEN 'unknown'
WHEN query = '<insufficient privilege>'
THEN 'unknown'
ELSE 'active' END AS mstate,
count(*) AS count
FROM pg_stat_activity
WHERE pid != pg_backend_pid()
GROUP BY CASE WHEN waiting THEN 'waiting'
WHEN state = 'idle' THEN 'idle'
WHEN state LIKE 'idle in transaction%'
THEN 'idletransaction'
WHEN state = 'disabled'
THEN 'unknown'
WHEN query = '<insufficient privilege>'
THEN 'unknown'
ELSE 'active'
END
) AS tmp2
ON tmp.mstate=tmp2.mstate ORDER BY 1
"""
post_96_query = """
SELECT tmp.state AS key,COALESCE(count,0) FROM
(VALUES ('active'),
('waiting'),
('idle'),
('idletransaction'),
('unknown')
) AS tmp(state)
LEFT JOIN
(SELECT CASE WHEN wait_event IS NOT NULL THEN 'waiting'
WHEN state= 'idle' THEN 'idle'
WHEN state= 'idle in transaction'
THEN 'idletransaction'
WHEN state = 'active' THEN 'active'
ELSE 'unknown' END AS state,
count(*) AS count
FROM pg_stat_activity
WHERE pid != pg_backend_pid()
GROUP BY CASE WHEN wait_event IS NOT NULL THEN 'waiting'
WHEN state= 'idle' THEN 'idle'
WHEN state= 'idle in transaction'
THEN 'idletransaction'
WHEN state = 'active' THEN 'active'
ELSE 'unknown' END
) AS tmp2
ON tmp.state=tmp2.state ORDER BY 1
"""
class LockStats(QueryStats):
path = "%(datname)s.locks.%(metric)s"
multi_db = False
query = """
SELECT lower(mode) AS key,
count(*) AS value
FROM pg_locks
WHERE database IS NOT NULL
GROUP BY mode ORDER BY 1
"""
class RelationSizeStats(QueryStats):
path = "%(datname)s.sizes.%(schemaname)s.%(relname)s.%(metric)s"
multi_db = True
query = """
SELECT pg_class.relname,
pg_namespace.nspname as schemaname,
pg_relation_size(pg_class.oid) as relsize
FROM pg_class
INNER JOIN
pg_namespace
ON pg_namespace.oid = pg_class.relnamespace
WHERE reltype != 0
AND relkind != 'S'
AND nspname NOT IN ('pg_catalog', 'information_schema')
"""
class BackgroundWriterStats(QueryStats):
path = "bgwriter.%(metric)s"
multi_db = False
query = """
SELECT checkpoints_timed,
checkpoints_req,
buffers_checkpoint,
buffers_clean,
maxwritten_clean,
buffers_backend,
buffers_alloc
FROM pg_stat_bgwriter
"""
class WalSegmentStats(QueryStats):
path = "wals.%(metric)s"
multi_db = False
query = """
SELECT count(*) AS segments
FROM pg_ls_dir('pg_xlog') t(fn)
WHERE fn ~ '^[0-9A-Z]{24}$'
"""
class TransactionCount(QueryStats):
path = "transactions.%(metric)s"
multi_db = False
query = """
SELECT 'commit' AS type,
sum(pg_stat_get_db_xact_commit(oid))
FROM pg_database
UNION ALL
SELECT 'rollback',
sum(pg_stat_get_db_xact_rollback(oid))
FROM pg_database
"""
class IdleInTransactions(QueryStats):
path = "%(datname)s.idle_in_tranactions.%(metric)s"
multi_db = True
base_query = """
SELECT 'idle_in_transactions',
max(COALESCE(ROUND(EXTRACT(epoch FROM now()-query_start)),0))
AS idle_in_transaction
FROM pg_stat_activity
WHERE %s
GROUP BY 1
"""
query = base_query % ("current_query = '<IDLE> in transaction'", )
post_92_query = base_query % ("state LIKE 'idle in transaction%'", )
class LongestRunningQueries(QueryStats):
path = "%(datname)s.longest_running.%(metric)s"
multi_db = True
base_query = """
SELECT 'query',
COALESCE(max(extract(epoch FROM CURRENT_TIMESTAMP-query_start)),0)
FROM pg_stat_activity
WHERE %s
UNION ALL
SELECT 'transaction',
COALESCE(max(extract(epoch FROM CURRENT_TIMESTAMP-xact_start)),0)
FROM pg_stat_activity
WHERE 1=1
"""
query = base_query % ("current_query NOT LIKE '<IDLE%'", )
post_92_query = base_query % ("state NOT LIKE 'idle%'", )
class UserConnectionCount(QueryStats):
path = "%(datname)s.user_connections.%(metric)s"
multi_db = True
query = """
SELECT usename,
count(*) as count
FROM pg_stat_activity
WHERE procpid != pg_backend_pid()
GROUP BY usename
ORDER BY 1
"""
post_92_query = query.replace('procpid', 'pid')
class DatabaseConnectionCount(QueryStats):
path = "database.%(metric)s.connections"
multi_db = False
query = """
SELECT datname,
count(datname) as connections
FROM pg_stat_activity
GROUP BY pg_stat_activity.datname
"""
class TableScanStats(QueryStats):
path = "%(datname)s.scans.%(metric)s"
multi_db = True
query = """
SELECT 'relname' AS relname,
COALESCE(sum(seq_scan),0) AS sequential,
COALESCE(sum(idx_scan),0) AS index
FROM pg_stat_user_tables
"""
class TupleAccessStats(QueryStats):
path = "%(datname)s.tuples.%(metric)s"
multi_db = True
query = """
SELECT COALESCE(sum(seq_tup_read),0) AS seqread,
COALESCE(sum(idx_tup_fetch),0) AS idxfetch,
COALESCE(sum(n_tup_ins),0) AS inserted,
COALESCE(sum(n_tup_upd),0) AS updated,
COALESCE(sum(n_tup_del),0) AS deleted,
COALESCE(sum(n_tup_hot_upd),0) AS hotupdated
FROM pg_stat_user_tables
"""
class DatabaseReplicationStats(QueryStats):
path = "database.replication.%(metric)s"
multi_db = False
query = """
SELECT EXTRACT(epoch FROM
current_timestamp - pg_last_xact_replay_timestamp()) as replay_lag
"""
class DatabaseXidAge(QueryStats):
path = "%(datname)s.datfrozenxid.%(metric)s"
multi_db = False
query = """
SELECT datname, age(datfrozenxid) AS age
FROM pg_database WHERE datallowconn = TRUE
"""
metrics_registry = {
'DatabaseStats': DatabaseStats,
'DatabaseConnectionCount': DatabaseConnectionCount,
'UserFunctionStats': UserFunctionStats,
'UserTableStats': UserTableStats,
'UserIndexStats': UserIndexStats,
'UserTableIOStats': UserTableIOStats,
'UserIndexIOStats': UserIndexIOStats,
'ConnectionStateStats': ConnectionStateStats,
'LockStats': LockStats,
'RelationSizeStats': RelationSizeStats,
'BackgroundWriterStats': BackgroundWriterStats,
'WalSegmentStats': WalSegmentStats,
'TransactionCount': TransactionCount,
'IdleInTransactions': IdleInTransactions,
'LongestRunningQueries': LongestRunningQueries,
'UserConnectionCount': UserConnectionCount,
'TableScanStats': TableScanStats,
'TupleAccessStats': TupleAccessStats,
'DatabaseReplicationStats': DatabaseReplicationStats,
'DatabaseXidAge': DatabaseXidAge,
}
registry = {
'basic': [
'DatabaseStats',
'DatabaseConnectionCount',
],
'extended': [
'DatabaseStats',
'DatabaseConnectionCount',
'DatabaseReplicationStats',
'DatabaseXidAge',
'UserFunctionStats',
'UserTableStats',
'UserIndexStats',
'UserTableIOStats',
'UserIndexIOStats',
'ConnectionStateStats',
'LockStats',
'RelationSizeStats',
'BackgroundWriterStats',
'TransactionCount',
'IdleInTransactions',
'LongestRunningQueries',
'UserConnectionCount',
'TableScanStats',
'TupleAccessStats',
],
}