-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpg_multigraph
More file actions
executable file
·1695 lines (1463 loc) · 53.6 KB
/
pg_multigraph
File metadata and controls
executable file
·1695 lines (1463 loc) · 53.6 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
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
#%# family=auto
#%# capabilities=autoconf
# pylint: disable=line-too-long, too-many-lines
#
# * The documentation sometimes requires fairly long lines which are more
# readable as long lines.
# * This module is intended to be a monolithic script to make execution as easy
# as possible. No dependencies should be required and it should be directly
# executable which means it will have many lines.
# pylint: disable=broad-except
#
# This code contains a bunch of broad except statements to ensure non-zero exit
# codes when run for munin. Mainly when looping over databases where the
# connection is refused so that only that one DB fails instead of all of them.
# This is repeated fairly often so we disable the message globally.
# pylint: disable=anomalous-backslash-in-string
#
# We use multiline strings fairly ofthen in docstrings. To make the source-code
# line up properl we escape the first newline, which causes this pylint
# message.
"""
=head1 Combined Postgres Plugin for Munin
This plugin monitors several aspects of a PostgreSQL DB Cluster.
The plugin creates several multi-level graphs showing aggregated graphs on the
main munin dashboard. A more details breakdown is accessible by clicking on the
graphs.
Currently the plugin supports the following graphs:
=over
=item * connections -- connections by user
=item * locks -- Acquired Locks
=item * query_ages -- Query and transaction length/ages
=item * row_access -- Types of row accesses.
=item * scantypes -- used scan types.
=item * sizes -- Database sizes.
=item * tableio -- Disk/Buffer I/O for tables.
=item * sequenceio -- Disk/Buffer I/O for sequences.
=item * indexio -- Disk/Buffer I/O for indices
=item * transactions -- Number of committed/rolled-back transactions.
=item * temp_bytes -- Bytes occupied by temporary files
=back
=head1 Configuration
As usual with munin-plugins this plugin can be configured using several
environment variables (see the munin docs for details). The variables are:
=over
=item * C<PG_DBNAME>
The database name for the initial connection. Some graphs need to connect to the
other existing DBs. This is only for the initial connection.
Default = template1
=item * C<PG_USER>
The username to use for the initial connection. Default = postgres
=item * C<PG_PASSWORD>
The password to use for the initial connection. Leaving this out will make a
passwordless connection. Default = empty.
=item * C<PG_HOST>
The hostname to use for the initial connection. Leaving this out will make a
connection using the unix domain socket. Default = empty
=item * C<PG_PORT>
The port to use for the initial connection. Leaving this out will use the
default port.
=item * C<PG_MULTIGRAPHS>
A comma-separated list of graphs to generate. Using C<__all__>, or leaving this
option out will generate all graphs. The names are referenced above.
=back
=cut
"""
from __future__ import print_function
from collections import namedtuple
from os import getenv
from textwrap import dedent
import argparse
import logging
import re
import sys
try:
from psycopg2 import connect as _connect
from psycopg2.extras import DictCursor
PSYCOPG2_AVAILABLE = True # used for autoconf
except ImportError as exc:
if exc.name != "psycopg2":
raise
PSYCOPG2_AVAILABLE = False # used for autoconf
LOG = logging.getLogger(__name__)
INVALID_CHARS = re.compile(r"[^a-zA-Z0-9_]")
ConnectionCounter = namedtuple(
"ConnectionCounter", "username idle idle_tx unknown query_running waiting"
)
Lock = namedtuple("Lock", "mode, granted")
GraphedValue = namedtuple("GraphedValue", "name, label, doc")
DbHealth = namedtuple("DbHealth", "dbname vacuum_age analyze_age")
QueryAge = namedtuple("QueryAge", "dbname query_age transact_age")
TxStats = namedtuple("TxStats", "dbname committed rolled_back")
TempBytesRow = namedtuple("TempBytesRow", "dbname bytes")
# --- Query Definitions ------------------------------------------------------
#
# The query definitions below are a 2-dimensional dictionary. The first
# dimension is the name of the query, the second is the minimum version where
# this query is needed. This allows us to adapt to changes in in postgres
# versions.
#
# If the minimum version is not known, use (0, 0, 0). This way the query will
# be used by default.
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
QUERIES = {
"list_databases": {
(0, 0, 0): "SELECT datname FROM pg_database WHERE datistemplate=false"
},
"global_sizes": {
(0, 0, 0): (
"SELECT datname, pg_database_size(datname) "
"FROM pg_database WHERE datistemplate=false"
),
},
"locks": {
(0, 0, 0): dedent(
"""\
SELECT
db.datname,
LOWER(mode),
locktype,
granted,
COUNT(mode)
FROM pg_database db
FULL OUTER JOIN pg_locks lck ON (db.oid=lck.database)
GROUP BY db.datname, mode, locktype, granted;
"""
)
},
"query_ages": {
(0, 0, 0): dedent(
"""\
SELECT
datname,
COALESCE(MAX(extract(EPOCH FROM NOW() - query_start)), 0),
COALESCE(MAX(extract(EPOCH FROM NOW() - xact_start)), 0)
FROM pg_stat_activity
WHERE current_query NOT LIKE '<IDLE%'
GROUP BY datname
"""
),
(9, 2, 0): dedent(
"""\
SELECT
datname,
COALESCE(MAX(extract(EPOCH FROM NOW() - query_start)), 0),
COALESCE(MAX(extract(EPOCH FROM NOW() - xact_start)), 0)
FROM pg_stat_activity
WHERE state NOT LIKE '%idle%'
GROUP BY datname
"""
),
},
"disk_io": {
(0, 0, 0): dedent(
"""\
SELECT
SUM(heap_blks_read) AS heap_blks_read,
SUM(heap_blks_hit) AS heap_blks_hit,
SUM(idx_blks_read) AS idx_blks_read,
SUM(idx_blks_hit) AS idx_blks_hit,
SUM(toast_blks_read) AS toast_blks_read,
SUM(toast_blks_hit) AS toast_blks_hit,
SUM(tidx_blks_read) AS tidx_blks_read
FROM pg_statio_user_tables;
"""
)
},
"index_io": {
(0, 0, 0): dedent(
"""\
SELECT
SUM(idx_blks_read) AS idx_blks_read,
SUM(idx_blks_hit) AS idx_blks_hit
FROM pg_statio_user_indexes;
"""
)
},
"sequences_io": {
(0, 0, 0): dedent(
"""\
SELECT
SUM(blks_read) AS blks_read,
SUM(blks_hit) AS blks_hit
FROM pg_statio_user_sequences;
"""
)
},
"scan_types": {
(0, 0, 0): dedent(
"""\
SELECT
SUM(idx_scan) AS idx_scan,
SUM(seq_scan) AS seq_scan
FROM pg_stat_user_tables;
"""
)
},
"row_access": {
(0, 0, 0): dedent(
"""\
SELECT
SUM(n_tup_ins) AS n_tup_ins,
SUM(n_tup_upd) AS n_tup_upd,
SUM(n_tup_del) AS n_tup_del,
SUM(n_tup_hot_upd) AS n_tup_hot_upd
FROM pg_stat_user_tables;
"""
)
},
"transactions": {
(0, 0, 0): dedent(
"""\
SELECT
datname,
pg_stat_get_db_xact_commit(oid),
pg_stat_get_db_xact_rollback(oid)
FROM pg_database"""
)
},
"size_by_db": {
(0, 0, 0): dedent(
"""\
SELECT
SUM(pg_relation_size(oid, 'main')) AS main_size,
SUM(pg_relation_size(oid, 'vm')) AS vm_size,
SUM(pg_relation_size(oid, 'fsm')) AS fsm_size,
SUM(
CASE reltoastrelid
WHEN 0 THEN 0
ELSE pg_total_relation_size(reltoastrelid)
END
) AS toast_size,
SUM(pg_indexes_size(oid)) AS indexes_size,
pg_database_size(current_database()) AS database_size
FROM pg_class
WHERE relkind not in ('t', 'i')
AND NOT relisshared"""
)
},
"temp_bytes": {
(0, 0, 0): dedent(
"""\
SELECT
datname,
temp_bytes
FROM
pg_stat_database"""
)
},
"connections": {
(0, 0, 0): dedent(
"""\
WITH users AS (SELECT usename FROM pg_user),
conntype AS (SELECT u.usename,
act.pid,
act.waiting,
current_query
FROM users u
LEFT JOIN pg_stat_activity act USING (usename))
SELECT
usename,
COUNT(CASE WHEN current_query='<IDLE>'
THEN 1 END) AS idle,
COUNT(CASE WHEN current_query='<IDLE> in transaction'
THEN 1 END) AS idle_tx,
COUNT(CASE WHEN current_query='<insufficient privilege>'
THEN 1 END) AS unknown,
COUNT(CASE WHEN current_query NOT IN (
'<IDLE>',
'<IDLE> in transaction',
'<insufficient privilege>')
THEN 1 END) AS query_running,
COUNT(CASE WHEN waiting THEN 1 END) AS waiting
FROM conntype
WHERE COALESCE(conntype.pid, 0) <> pg_backend_pid()
GROUP BY usename
ORDER BY usename;"""
),
(9, 2, 0): dedent(
"""\
WITH users AS (SELECT usename FROM pg_user),
conntype AS (SELECT u.usename,
act.pid,
act.waiting,
state,
query
FROM users u
LEFT JOIN pg_stat_activity act USING (usename))
SELECT
usename,
COUNT(CASE WHEN state = 'idle'
THEN 1 END) AS idle,
COUNT(CASE WHEN state like 'idle in transaction%'
THEN 1 END) AS idle_tx,
COUNT(CASE WHEN state NOT IN (
'idle',
'idle in transaction',
'idle in transaction (aborted)',
'active')
THEN 1 END) AS unknown,
COUNT(CASE WHEN state = 'active'
THEN 1 END) AS query_running,
COUNT(CASE WHEN waiting THEN 1 END) AS waiting
FROM conntype
WHERE COALESCE(conntype.pid, 0) <> pg_backend_pid()
GROUP BY usename
ORDER BY usename;"""
),
(10, 0, 0): dedent(
"""\
WITH users AS (SELECT usename FROM pg_user),
conntype AS (SELECT u.usename,
act.pid,
act.wait_event_type IS NOT NULL as waiting,
state,
query
FROM users u
LEFT JOIN pg_stat_activity act USING (usename))
SELECT
usename,
COUNT(CASE WHEN state = 'idle'
THEN 1 END) AS idle,
COUNT(CASE WHEN state like 'idle in transaction%'
THEN 1 END) AS idle_tx,
COUNT(CASE WHEN state NOT IN (
'idle',
'idle in transaction',
'idle in transaction (aborted)',
'active')
THEN 1 END) AS unknown,
COUNT(CASE WHEN state = 'active'
THEN 1 END) AS query_running,
COUNT(CASE WHEN waiting THEN 1 END) AS waiting
FROM conntype
WHERE COALESCE(conntype.pid, 0) <> pg_backend_pid()
GROUP BY usename
ORDER BY usename;"""
),
},
}
# ----------------------------------------------------------------------------
def connect(*args, **kwargs):
"""
Create a psycopg2 connection with autocommit ON.
This solves issues with calculations with ``NOW()``
"""
connection = _connect(*args, **kwargs)
connection.autocommit = True
return connection
def find_subclasses(cls):
"""
Recursively resolve all subclasses of *cls* and return them as a set.
"""
output = set()
for subcls in cls.__subclasses__():
if hasattr(subcls, "NAME"):
output.add(subcls)
output = output | find_subclasses(subcls)
return output
def queries_for_version(queries, target_version):
"""
Reduces the data in *queries* to the best possible version.
The expected structure of *queries* is::
{
'<query_name>': {
<min_version_1>: <query>,
<min_version_2>: <query>,
}
}
Example::
{
'locks': {
(9, 1, 0): ('SELECT datname FROM pg_database WHERE '
'datistemplate=false')
}
}
It will return the best matching query for the version given in
*target_version*.
Example::
>>> queries = {
... 'foo': {
... (1, 0, 0): 'foo-100',
... (2, 0, 0): 'foo-200',
... (3, 0, 0): 'foo-300',
... }
... }
>>> queries_for_version(queries, (2, 1, 0))
{'foo': 'foo-200'}
"""
output = {}
for query_name, variants in queries.items():
for version, query in sorted(variants.items(), key=lambda x: x[0]):
if target_version >= version:
output[query_name] = query
return output
def construct_dsn(dbname, user, password="", host="", port=0):
"""
Given explicit keyword arguments, this will construct a DSN-string usable
in the *connect* method.
"""
elements = ["dbname=%s" % dbname, "user=%s" % user]
if password:
elements.append("password=%s" % password)
if host:
elements.append("host=%s" % host)
if port:
elements.append("port=%d" % port)
return " ".join(elements)
def parse_args():
"""
Parses CLI arguments and returns an :py:class:`argparse.Namespace` object.
"""
parser = argparse.ArgumentParser()
parser.add_argument(
"action", nargs="?", help="The munin action to perform", default="fetch"
)
return parser.parse_args()
class PostgresPlugin(object):
"""
Base class for a PostgreSQL plugin for munin.
"""
TITLE = "unknown plugin"
NAME = "unknown plugin"
def __init__(self, connection, user, password, host, port):
self.connection = connection
self.user = user
self.password = password
self.host = host
self.port = port
self._dbnames = []
self.queries = None
def fetch(self):
"""
Fetch the values of the plugin and print them to stdout.
"""
if self.queries is None:
version_identifier = get_pg_version(self.connection)
self.queries = queries_for_version(QUERIES, version_identifier)
print("multigraph %s" % self.graph_name)
def config(self):
"""
Print the plugin configuration to stdout.
"""
if self.queries is None:
version_identifier = get_pg_version(self.connection)
self.queries = queries_for_version(QUERIES, version_identifier)
print("multigraph %s" % self.graph_name)
print("graph_category postgresql")
print("graph_title %s" % self.TITLE)
@property
def graph_name(self):
"""
Returns the graph-name for multigraphs
"""
return "pgmg_%s" % self.NAME
@property
def all_databases(self):
"""
Returns a list of all databases. The list is cached during the
execution of this script.
"""
if self._dbnames:
return self._dbnames
cursor = self.connection.cursor()
cursor.execute(self.queries["list_databases"])
dbnames = [row[0] for row in cursor]
cursor.close()
return dbnames
class Connections(PostgresPlugin):
"""
A plugin that shows details of database connections.
"""
NAME = "connections"
TITLE = "Connections"
def all_connections(self):
"""
Returns a list of all open connections as
:py:class:`.ConnectionCounter` instances.
"""
cursor = self.connection.cursor()
cursor.execute(self.queries["connections"])
output = []
for username, idle, idle_tx, unknown, query_running, waiting in cursor:
output.append(
ConnectionCounter(
username, idle, idle_tx, unknown, query_running, waiting
)
)
cursor.close()
return output
def fetch(self):
super(Connections, self).fetch()
conns = self.all_connections()
sums = {
"idle": sum([conn.idle for conn in conns]),
"idle_transaction": sum([conn.idle_tx for conn in conns]),
"unknown": sum([conn.unknown for conn in conns]),
"query_running": sum([conn.query_running for conn in conns]),
"waiting": sum([conn.waiting for conn in conns]),
}
print("idle.value %d" % sums["idle"])
print("idle_transaction.value %d" % sums["idle_transaction"])
print("unknown.value %d" % sums["unknown"])
print("query_running.value %d" % sums["query_running"])
print("waiting.value %d" % sums["waiting"])
# print values for the each subgraph
for subgraph in conns:
print(
"multigraph %s.%s"
% (self.graph_name, INVALID_CHARS.sub("_", subgraph.username))
)
print("idle.value %d" % subgraph.idle)
print("idle_transaction.value %d" % subgraph.idle_tx)
print("unknown.value %d" % subgraph.unknown)
print("query_running.value %d" % subgraph.query_running)
print("waiting.value %d" % subgraph.waiting)
def config(self):
super(Connections, self).config()
# We want the main and subgraph to use the same config. So we store it
# as a variable.
common_config_block = dedent(
"""\
graph_args --base 1000
graph_vlabel Active Connections
graph_order waiting query_running idle idle_transaction unknown
graph_printf %3.0lf
graph_scale no
waiting.label Waiting for lock
waiting.info Connections which are waiting for a lock to be released
waiting.draw AREA
query_running.label Active query
query_running.info Connections with running queries.
query_running.draw STACK
idle.label Idle connections
idle.info Connections which are currently not doing anything.
idle.draw STACK
idle_transaction.label Idle transaction
idle_transaction.info Connections which are not doing anything, but have an unfinished transaction open.
idle_transaction.draw STACK
unknown.label unknown
unknown.info Connections where munin did not have the access rights to get more details.
unknown.draw STACK
"""
)
conns = self.all_connections()
print(
"graph_info Shows an overview of connections and their type on "
"the PostgreSQL cluster."
)
print(common_config_block)
for subgraph in conns:
clean_username = INVALID_CHARS.sub("_", subgraph.username)
print("multigraph %s.%s" % (self.graph_name, clean_username))
print("graph_title %s for user %s" % (self.TITLE, subgraph.username))
print(common_config_block)
class Sizes(PostgresPlugin):
"""
Returns the disk-sizes for each databse.
"""
NAME = "sizes"
TITLE = "Database Sizes"
def global_stats(self):
"""
Returns a dictionary mapping the database name to its overall (no
breakdown) disk-size.
"""
cursor = self.connection.cursor()
cursor.execute(self.queries["global_sizes"])
sizes = cursor.fetchall()
cursor.close()
return {row[0]: row[1] for row in sizes}
def breakdown(self):
"""
given a specific DB, get detailed values. It will be broken down into:
- main data size
- visibility map size
- free-space-map size
- toast size
- index size
"""
# we need to get stats for each DB. We need to create new connections
# for each.
dbnames = self.all_databases
# Fetch stats for each DB
stats = {}
for dbname in dbnames:
try:
localcon = connect(
construct_dsn(
dbname, self.user, self.password, self.host, self.port
)
)
except Exception as exc:
LOG.error(exc)
continue
cursor = localcon.cursor(cursor_factory=DictCursor)
cursor.execute(self.queries["size_by_db"])
stats[dbname] = cursor.fetchone() or {}
cursor.close()
localcon.close()
return stats
def fetch(self):
super(Sizes, self).fetch()
stats = self.global_stats()
breakdown = self.breakdown()
for dbname, value in stats.items():
print("%s.value %s" % (INVALID_CHARS.sub("_", dbname), value))
for dbname, value in breakdown.items():
print("multigraph %s.%s" % (self.graph_name, dbname))
print("main.value %s" % value["main_size"])
print("vm.value %s" % value["vm_size"])
print("fsm.value %s" % value["fsm_size"])
print("toast.value %s" % value["toast_size"])
print("indexes.value %s" % value["indexes_size"])
print("size.value %s" % value["database_size"])
def config(self):
super(Sizes, self).config()
print(
dedent(
"""\
graph_args --base 1024 --lower-limit 0
graph_vlabel Size (bytes)
"""
)
)
first_graph = True
all_dbs = self.all_databases
for dbname in all_dbs:
print(
dedent(
"""\
{clean_name}.info Size in Bytes for database {clean_name}
{clean_name}.label {raw_name}
{clean_name}.info Total disk size occupied by this DB. This includes all DB objects.
{clean_name}.draw {style}
{clean_name}.min 0
""".format(
raw_name=dbname,
clean_name=INVALID_CHARS.sub("_", dbname),
style="AREA" if first_graph else "STACK",
)
)
)
first_graph = False
# Scale each graph relative to the biggest DB
template = dedent(
"""\
multigraph {graph_name}.{dbname}
graph_title {title} for {dbname}
graph_args --base 1024 --lower-limit 0
main.label main data
main.min 0
main.draw AREA
main.info The disk usage by the main data of the tables.
vm.label visibility map
vm.min 0
vm.draw STACK
vm.info The size of the "visibility map". Contains metadata for VACUUM.
fsm.label free space map
fsm.min 0
fsm.draw STACK
fsm.info The Free Space map contains metadata where unused/reclaimable pages are located on disk.
toast.label TOAST
toast.min 0
toast.draw STACK
toast.info If data does not fit into one page, the TOAST allows the DB to store the remaining data.
indexes.label indexes
indexes.min 0
indexes.draw STACK
indexes.info Size of all indexes.
size.label filesize on disk
size.min 0
size.draw LINE1
size.info Size of the DB folder on disk (may contain additional files so the size may be larger).
"""
)
for dbname in all_dbs:
print(
template.format(
title=self.TITLE, graph_name=self.graph_name, dbname=dbname
)
)
class Locks(PostgresPlugin):
"""
Retrieve statistics for existing locks on the database.
"""
NAME = "locks"
TITLE = "Locks"
ACCEPTED_LOCK_NAMES = {
GraphedValue(
"accesssharelock",
"Access share",
"The SELECT command acquires a lock of this mode on referenced tables. In general, any query that only reads a table and does not modify it will acquire this lock mode.",
),
GraphedValue(
"rowsharelock",
"Row share",
"The SELECT FOR UPDATE and SELECT FOR SHARE commands acquire a lock of this mode on the target table(s) (in addition to ACCESS SHARE locks on any other tables that are referenced but not selected FOR UPDATE/FOR SHARE).",
),
GraphedValue(
"rowexclusivelock",
"Row excl.",
"The commands UPDATE, DELETE, and INSERT acquire this lock mode on the target table (in addition to ACCESS SHARE locks on any other referenced tables). In general, this lock mode will be acquired by any command that modifies data in a table.",
),
GraphedValue(
"shareupdateexclusivelock",
"Share upd. excl.",
"Acquired by VACUUM (without FULL), ANALYZE, CREATE INDEX CONCURRENTLY, and some forms of ALTER TABLE.",
),
GraphedValue(
"sharelock", "Share", "Acquired by CREATE INDEX (without CONCURRENTLY)."
),
GraphedValue(
"sharerowexclusivelock",
"Share row excl.",
"This lock mode is not automatically acquired by any PostgreSQL command.",
),
GraphedValue(
"exclusivelock",
"Exclusive",
"This lock mode is not automatically acquired on tables by any PostgreSQL command.",
),
GraphedValue(
"accessexclusivelock",
"Access excl.",
"Acquired by the ALTER TABLE, DROP TABLE, TRUNCATE, REINDEX, CLUSTER, and VACUUM FULL commands. This is also the default lock mode for LOCK TABLE statements that do not specify a mode explicitly.",
),
}
def _graph_config(self):
first_graph = True
for lock_info in sorted(self.ACCEPTED_LOCK_NAMES):
print(
dedent(
"""\
{lock.name}_waiting.label {lock.label}
{lock.name}_waiting.info {lock.doc}
{lock.name}_waiting.draw {style}
{lock.name}_waiting.min 0
{lock.name}_waiting.graph no
{lock.name}_granted.label {lock.label}
{lock.name}_granted.info {lock.doc}
{lock.name}_granted.draw {style}
{lock.name}_granted.min 0
{lock.name}_granted.negative {lock.name}_waiting
""".format(
lock=lock_info,
style="AREA" if first_graph else "STACK",
)
)
)
first_graph = False
def get_stats(self):
"""
Fetches the statistics and returns them as dictionary mapping the
database name to another dictionary mapping :py:class:`Lock` objects to
their respective counts. Example output::
{
'mydatabase': {
Lock('AccessShareLock', True): 2,
Lock('ExclusiveLock', False): 1
}
}
"""
cursor = self.connection.cursor()
cursor.execute(self.queries["locks"])
locks = cursor.fetchall()
cursor.close()
output = {}
for dbname, lockmode, unused_locktype, granted, count in locks:
if dbname is None:
# locks without related DB, are "global" locks
dbname = "__pg__database__"
dblocks = output.setdefault(dbname, {})
dblocks[Lock(lockmode, granted)] = count
# The above query only returns rows for databases with actual
# waiting/granted locks. We always want all DBs to be monitored though.
# DBs which don't have locks should report the value `0`. This prevents
# NaN values ("holes") in munin.
for row in self.all_databases:
output.setdefault(row, {})
return output
def fetch(self):
super(Locks, self).fetch()
stats = self.get_stats()
sums = {}
for row in stats.values():
for lock_info in self.ACCEPTED_LOCK_NAMES:
granted_lock = Lock(lock_info.name, True)
waiting_lock = Lock(lock_info.name, False)
granted_value = sums.setdefault(granted_lock, 0)
granted_value += row.get(granted_lock, 0)
waiting_value = sums.setdefault(waiting_lock, 0)
waiting_value += row.get(waiting_lock, 0)
sums[granted_lock] = granted_value
sums[waiting_lock] = waiting_value
for lock, value in sums.items():
print(
"%s_%s.value %s"
% (lock.mode, "granted" if lock.granted else "waiting", value)
)
for dbname, values in sorted(stats.items()):
print("multigraph %s.%s" % (self.graph_name, dbname))
for lock_info in self.ACCEPTED_LOCK_NAMES:
granted_lock = Lock(lock_info.name, True)
waiting_lock = Lock(lock_info.name, False)
print(
"%s_granted.value %d"
% (lock_info.name, values.get(granted_lock, 0))
)
print(
"%s_waiting.value %d"
% (lock_info.name, values.get(waiting_lock, 0))
)
def config(self):
super(Locks, self).config()
stats = self.get_stats()
print(
dedent(
"""\
graph_args --base 1000
graph_vlabel locks: granted(+) / waiting(-)
graph_printf %3.0lf
graph_scale no
"""
)
)
self._graph_config()
for dbname, _ in stats.items():
print("multigraph %s.%s" % (self.graph_name, dbname))
print("graph_title %s for %s" % (self.TITLE, dbname))
self._graph_config()
class QueryAges(PostgresPlugin):
"""
This plugin will return all running queries with their respective age in
seconds.
"""
NAME = "query_ages"
TITLE = "Query Ages (seconds)"
def get_stats(self):
"""
Retrieve the statistics and return a dictionary mapping the database
name to a :py:class:`.QueryAge` instance.
"""
cursor = self.connection.cursor()
cursor.execute(self.queries["query_ages"])
oldest_queries = cursor.fetchall()
cursor.close()
output = {}
for dbname, age, transact_age in oldest_queries:
output[dbname] = QueryAge(dbname, age or 0.0, transact_age or 0.0)
# The above query only returns rows for databases with actual
# waiting/granted locks. We always want all DBs to be monitored though.
# DBs which don't have locks should report the value `0`. This prevents
# NaN values ("holes") in munin.
# TODO If we do this before the cursor loop, we can use a
# dict-comprehension
for row in self.all_databases:
output.setdefault(row, QueryAge(row, 0, 0))
return output
def fetch(self):
super(QueryAges, self).fetch()
stats = self.get_stats()
max_age = 0
max_tx_age = 0
for row in stats.values():
max_age = max(max_age, row.query_age)
max_tx_age = max(max_tx_age, row.transact_age)
print("age.value %f" % max_age)
print("tx_age.value %f" % max_tx_age)
for dbname, value in sorted(stats.items()):