Support optionally disabling the table and index charts.
Facetoe committed
Oct 25, 2016 at 19:00 UTC
a33df9ff1a4296830a657e31f98842f877513500
2 files changed
+24
-15
conf.d/python.d/postgres.conf
+4
@@ -63,6 +63,10 @@
63
# host: localhost
64
# port: 5432
65
#
66
+# Additionally, the following options allow selective disabling of charts
67
+#
68
+# table_stats: false
69
+# index_stats: false
70
# ----------------------------------------------------------------------
71
72
# Attempt to connect via the Unix socket as the postgres user.
python.d/postgres.chart.py
+20
-15
@@ -199,19 +199,22 @@ class Service(SimpleService):
199
super(self.__class__, self).__init__(configuration=configuration, name=name)
200
self.order = ORDER
201
self.definitions = CHARTS
202
+ self.table_stats = configuration.pop('table_stats', True)
203
+ self.index_stats = configuration.pop('index_stats', True)
204
self.configuration = configuration
205
self.connection = None
206
self.data = {}
207
self.old_data = {}
208
self.databases = set()
209
208
- def connect(self):
210
+ def _connect(self):
211
params = dict(user='postgres',
212
database=None,
213
password=None,
214
host='localhost',
215
port=5432)
216
params.update(self.configuration)
217
+
218
if not self.connection:
219
self.connection = psycopg2.connect(**params)
220
self.connection.set_isolation_level(extensions.ISOLATION_LEVEL_AUTOCOMMIT)
@@ -219,23 +222,15 @@ class Service(SimpleService):
222
223
def check(self):
224
try:
222
- self.connect()
223
- self.discover_databases()
225
+ self._connect()
226
+ self._discover_databases()
227
self._create_definitions()
228
return True
229
except Exception as e:
230
self.error(e)
231
return False
232
230
- def _create_definitions(self):
231
- for database_name in self.databases:
232
- self.databases.add(database_name)
233
- for chart_template_name in list(CHARTS):
234
- if chart_template_name.startswith('db_stat'):
235
- self._add_database_stat_chart(chart_template_name, database_name)
236
- self._add_database_lock_chart(database_name)
237
-
238
- def discover_databases(self):
233
+ def _discover_databases(self):
234
cursor = self.connection.cursor()
235
cursor.execute("""
236
SELECT datname
@@ -245,6 +240,14 @@ class Service(SimpleService):
240
self.databases = set(r[0] for r in cursor)
241
cursor.close()
242
243
+ def _create_definitions(self):
244
+ for database_name in self.databases:
245
+ self.databases.add(database_name)
246
+ for chart_template_name in list(CHARTS):
247
+ if chart_template_name.startswith('db_stat'):
248
+ self._add_database_stat_chart(chart_template_name, database_name)
249
+ self._add_database_lock_chart(database_name)
250
+
251
def _add_database_stat_chart(self, chart_template_name, database_name):
252
chart_template = CHARTS[chart_template_name]
253
chart_name = "{}_{}".format(database_name, chart_template_name)
@@ -290,7 +293,7 @@ class Service(SimpleService):
293
self.definitions[chart_name]['lines'].append([lock_id, label, 'absolute'])
294
295
def _get_data(self):
293
- self.connect()
296
+ self._connect()
297
298
cursor = self.connection.cursor(cursor_factory=DictCursor)
299
self.add_stats(cursor)
@@ -301,8 +304,10 @@ class Service(SimpleService):
304
def add_stats(self, cursor):
305
self.add_database_stats(cursor)
306
self.add_backend_stats(cursor)
304
- self.add_index_stats(cursor)
305
- self.add_table_stats(cursor)
307
+ if self.index_stats:
308
+ self.add_index_stats(cursor)
309
+ if self.table_stats:
310
+ self.add_table_stats(cursor)
311
self.add_lock_stats(cursor)
312
self.add_bgwriter_stats(cursor)
313