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',
23 'options': ["tuples", "PostgreSQL tuple access", "Tuples / sec", "tuples", "postgres.tuples", "line"],
25 ["tup_inserted", "tup_inserted", "incremental", 1, 1],
26 ["tup_inserted", "tup_inserted", "incremental", 1, 1],
27 ["tup_fetched", "tup_fetched", "incremental", 1, 1],
28 ["tup_updated", "tup_updated", "incremental", 1, 1],
29 ["tup_deleted", "tup_deleted", "incremental", 1, 1],
32 'options': ["transactions", "Transactions", "transactions / sec", "transactions", "postgres.transactions", "line"],
34 ["xact_commit", "xact_commit", "incremental", 1, 1],
35 ["xact_rollback", "xact_rollback", "incremental", 1, 1],
38 'options': ["block_access", "block_access", "Block / sec ", "block_access", "postgres.block_access", "line"],
40 ["blks_read", "blks_read", "incremental", 1, 1],
41 ["blks_hit", "blks_hit", "incremental", 1, 1],
44 'options': ["checkpoints", "Checkpoints", "Checkpoints", "checkpoints", "postgres.checkpoints", "line"],
46 ["bg_checkpoint_time", "bg_checkpoint_time", "absolute", 1, 1],
47 ["bg_checkpoint_requested", "bg_checkpoint_requested", "absolute", 1, 1],
50 'options': ["buffers", "buffers", "Buffer/ sec", "buffers", "postgres.buffers", "line"],
52 ["buffers_written", "buffers_written", "incremental", 1, 1],
53 ["buffers_allocated", "buffers_allocated", "incremental", 1, 1],
56 ORDER = ["Tuples", "Transactions", "BlockAccess", "Checkpoints", "Buffers"]
59 class Service(SimpleService):
60 def __init__(self, configuration=None, name=None):
61 super(self.__class__, self).__init__(configuration=configuration, name=name)
63 self.definitions = CHARTS
64 self.configuration = configuration
65 self.connection = None
68 params = dict(user='postgres',
73 params.update(self.configuration)
74 if self.connection is None:
75 self.connection = psycopg2.connect(**params)
76 self.connection.set_session(readonly=True)
82 except Exception as e:
87 cursor = self.connection.cursor(cursor_factory=DictCursor)
91 pg_stat_get_bgwriter_timed_checkpoints() AS bg_checkpoint_time,
92 pg_stat_get_bgwriter_requested_checkpoints() AS bg_checkpoint_requested,
93 pg_stat_get_buf_written_backend() AS buffers_written,
94 pg_stat_get_buf_alloc() AS buffers_allocated
96 WHERE datname = %(database)s
97 """, self.configuration)
98 graph_data = dict(cursor.fetchone())
99 self.connection.commit()