1 # -*- coding: utf-8 -*-
2 # Description: example netdata python.d module
3 # Author: Pawel Krupa (paulfantom)
6 from base import SimpleService
7 from psycopg2.extras import DictCursor
11 # default module values
16 # Default Config options.
21 # 'host': 'localhost',
25 ORDER = ["Tuples", "Scans"]
28 'options': ["tuples", "PostgreSQL tuple access", "Tuples / sec", "tuples", "postgres.tuples", "line"],
30 ["inserted", "inserted", "incremental", 1, 1],
31 ["seqread", "seqread", "incremental", 1, 1],
32 ["hotupdated", "hotupdated", "incremental", 1, 1],
33 ["deleted", "deleted", "incremental", -1, 1],
34 ["updated", "updated", "incremental", 1, 1],
35 ["idxfetch", "idxfetch", "incremental", 1, 1],
38 'options': ["scans", "PostgreSQL scan types", "Scans / sec", "scans", "postgres.scans", "line"],
40 ["sequential", "sequential", "incremental", 1, 1],
41 ["index", "index", "incremental", 1, 1],
46 class Service(SimpleService):
47 def __init__(self, configuration=None, name=None):
48 super(self.__class__, self).__init__(configuration=configuration, name=name)
50 self.definitions = CHARTS
51 self.error(str(configuration))
52 self.configuration = configuration
53 self.connection = None
56 params = dict(user='postgres',
61 params.update(self.configuration)
62 if self.connection is None:
63 self.connection = psycopg2.connect(**params)
69 except Exception as e:
74 cursor = self.connection.cursor(cursor_factory=DictCursor)
76 SELECT COALESCE(sum(seq_tup_read),0) AS seqread,
77 COALESCE(sum(idx_tup_fetch),0) AS idxfetch,
78 COALESCE(sum(n_tup_ins),0) AS inserted,
79 COALESCE(sum(n_tup_upd),0) AS updated,
80 COALESCE(sum(n_tup_del),0) AS deleted,
81 COALESCE(sum(n_tup_hot_upd),0) AS hotupdated,
84 COALESCE(sum(seq_scan),0) AS sequential,
85 COALESCE(sum(idx_scan),0) AS index
86 FROM pg_stat_user_tables
88 data = {k: float(v) for k, v in cursor.fetchone().items()}
89 self.connection.commit()