1 # -*- coding: utf-8 -*-
4 from base import SimpleService
5 from psycopg2.extras import DictCursor
7 # default module values
12 # Default Config options.
17 # 'host': 'localhost',
21 ORDER = ["Tuples", "Scans", "BGWriter", "BufferCache"]
24 'options': ["tuples", "PostgreSQL tuple access", "Tuples / sec", "tuples", "postgres.tuples", "line"],
26 ["inserted", "inserted", "incremental", 1, 1],
27 ["seqread", "seqread", "incremental", 1, 1],
28 ["hotupdated", "hotupdated", "incremental", 1, 1],
29 ["deleted", "deleted", "incremental", 1, 1],
30 ["updated", "updated", "incremental", 1, 1],
31 ["idxfetch", "idxfetch", "incremental", 1, 1],
34 'options': ["scans", "PostgreSQL scan types", "Scans / sec", "scans", "postgres.scans", "line"],
36 ["sequential", "sequential", "incremental", 1, 1],
37 ["index", "index", "incremental", 1, 1],
40 'options': ["bgwriter", "BG Writer Activity", "Buffers / sec", "bgwriter", "postgres.bgwriter", "line"],
42 ["buffers_alloc", "buffers_alloc", "incremental", 1, 1],
43 ["buffers_clean", "buffers_clean", "incremental", 1, 1],
44 ["buffers_checkpoint", "buffers_checkpoint", "incremental", 1, 1],
45 ["buffers_backend", "buffers_backend", "incremental", 1, 1],
48 'options': ["buffer_cache", "Buffer Cache", "Buffers / sec", "buffer_cache", "postgres.buffer_cache", "line"],
50 ["blks_read", "blks_read", "incremental", 1, 1],
51 ["blks_hit", "blks_hit", "incremental", 1, 1],
56 class Service(SimpleService):
57 def __init__(self, configuration=None, name=None):
58 super(self.__class__, self).__init__(configuration=configuration, name=name)
60 self.definitions = CHARTS
61 self.configuration = configuration
62 self.connection = None
65 params = dict(user='postgres',
70 params.update(self.configuration)
71 if self.connection is None:
72 self.connection = psycopg2.connect(**params)
78 except Exception as e:
83 cursor = self.connection.cursor(cursor_factory=DictCursor)
87 COALESCE(sum(seq_tup_read),0) AS seqread,
88 COALESCE(sum(idx_tup_fetch),0) AS idxfetch,
89 COALESCE(sum(n_tup_ins),0) AS inserted,
90 COALESCE(sum(n_tup_upd),0) AS updated,
91 COALESCE(sum(n_tup_del),0) AS deleted,
92 COALESCE(sum(n_tup_hot_upd),0) AS hotupdated,
95 COALESCE(sum(seq_scan),0) AS sequential,
96 COALESCE(sum(idx_scan),0) AS index
97 FROM pg_stat_user_tables
99 graph_data = {k: float(v) for k, v in cursor.fetchone().items()}
101 # Pull in BGWriter info
111 graph_data.update(dict(cursor.fetchone()))
115 sum(blks_read) AS blks_read,
116 sum(blks_hit) AS blks_hit
120 datname = %(database)s
121 """, self.configuration)
122 graph_data.update({k: float(v) for k, v in cursor.fetchone().items()})
124 self.connection.commit()