Add postgres plugin.
Facetoe committed
Oct 2, 2016 at 22:05 UTC
ff6874dafec285c060be8ac0575bd4f67a2d31eb
1 file changed
+91
python.d/postgres.chart.py
new
+91
@@ -0,0 +1,91 @@
1
+# -*- coding: utf-8 -*-
2
+# Description: example netdata python.d module
3
+# Author: Pawel Krupa (paulfantom)
4
+
5
+import psycopg2
6
+from base import SimpleService
7
+from psycopg2.extras import DictCursor
8
+
9
+NAME = "test"
10
+
11
+# default module values
12
+update_every = 1
13
+priority = 90000
14
+retries = 60
15
+
16
+# Default Config options.
17
+# {
18
+# 'database': None,
19
+# 'user': 'postgres',
20
+# 'password': None,
21
+# 'host': 'localhost',
22
+# 'port': 5432
23
+# }
24
+
25
+ORDER = ["Tuples", "Scans"]
26
+CHARTS = {
27
+ "Tuples": {
28
+ 'options': ["tuples", "PostgreSQL tuple access", "Tuples / sec", "tuples", "postgres.tuples", "line"],
29
+ 'lines': [
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],
36
+ ]},
37
+ "Scans": {
38
+ 'options': ["scans", "PostgreSQL scan types", "Scans / sec", "scans", "postgres.scans", "line"],
39
+ 'lines': [
40
+ ["sequential", "sequential", "incremental", 1, 1],
41
+ ["index", "index", "incremental", 1, 1],
42
+ ]}
43
+}
44
+
45
+
46
+class Service(SimpleService):
47
+ def __init__(self, configuration=None, name=None):
48
+ super(self.__class__, self).__init__(configuration=configuration, name=name)
49
+ self.order = ORDER
50
+ self.definitions = CHARTS
51
+ self.error(str(configuration))
52
+ self.configuration = configuration
53
+ self.connection = None
54
+
55
+ def connect(self):
56
+ params = dict(user='postgres',
57
+ database=None,
58
+ password=None,
59
+ host='localhost',
60
+ port=5432)
61
+ params.update(self.configuration)
62
+ if self.connection is None:
63
+ self.connection = psycopg2.connect(**params)
64
+
65
+ def check(self):
66
+ try:
67
+ self.connect()
68
+ return True
69
+ except Exception as e:
70
+ self.error(e)
71
+ return False
72
+
73
+ def _get_data(self):
74
+ cursor = self.connection.cursor(cursor_factory=DictCursor)
75
+ cursor.execute("""
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,
82
+
83
+ -- Scans
84
+ COALESCE(sum(seq_scan),0) AS sequential,
85
+ COALESCE(sum(idx_scan),0) AS index
86
+ FROM pg_stat_user_tables
87
+ """)
88
+ data = {k: float(v) for k, v in cursor.fetchone().items()}
89
+ self.connection.commit()
90
+ cursor.close()
91
+ return data