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"]
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],
50 class Service(SimpleService):
51 def __init__(self, configuration=None, name=None):
52 super(self.__class__, self).__init__(configuration=configuration, name=name)
54 self.definitions = CHARTS
55 self.error(str(configuration))
56 self.configuration = configuration
57 self.connection = None
60 params = dict(user='postgres',
65 params.update(self.configuration)
66 if self.connection is None:
67 self.connection = psycopg2.connect(**params)
73 except Exception as e:
78 cursor = self.connection.cursor(cursor_factory=DictCursor)
82 COALESCE(sum(seq_tup_read),0) AS seqread,
83 COALESCE(sum(idx_tup_fetch),0) AS idxfetch,
84 COALESCE(sum(n_tup_ins),0) AS inserted,
85 COALESCE(sum(n_tup_upd),0) AS updated,
86 COALESCE(sum(n_tup_del),0) AS deleted,
87 COALESCE(sum(n_tup_hot_upd),0) AS hotupdated,
90 COALESCE(sum(seq_scan),0) AS sequential,
91 COALESCE(sum(idx_scan),0) AS index
92 FROM pg_stat_user_tables
94 graph_data = {k: float(v) for k, v in cursor.fetchone().items()}
105 graph_data.update(dict(cursor.fetchone()))
107 self.connection.commit()