Enable WAL chart when connected as superuser.
Facetoe committed
Oct 25, 2016 at 19:14 UTC
6ea992dc26812fcd55429ab216e0d44c5e61724a
1 file changed
+16
-7
python.d/postgres.chart.py
+16
-7
@@ -203,6 +203,7 @@ class Service(SimpleService):
203
self.index_stats = configuration.pop('index_stats', True)
204
self.configuration = configuration
205
self.connection = None
206
+ self.is_superuser = False
207
self.data = {}
208
self.old_data = {}
209
self.databases = set()
@@ -223,22 +224,31 @@ class Service(SimpleService):
224
def check(self):
225
try:
226
self._connect()
226
- self._discover_databases()
227
+ cursor = self.connection.cursor()
228
+ self._discover_databases(cursor)
229
+ self._check_if_superuser(cursor)
230
+ cursor.close()
231
+
232
self._create_definitions()
233
return True
234
except Exception as e:
235
self.error(e)
236
return False
237
233
- def _discover_databases(self):
234
- cursor = self.connection.cursor()
238
+ def _discover_databases(self, cursor):
239
cursor.execute("""
240
SELECT datname
241
FROM pg_stat_database
242
WHERE NOT datname ~* '^template\d+'
243
""")
244
self.databases = set(r[0] for r in cursor)
241
- cursor.close()
245
+
246
+ def _check_if_superuser(self, cursor):
247
+ cursor.execute("""
248
+ SELECT current_setting('is_superuser') = 'on' AS is_superuser;
249
+ """)
250
+ self.is_superuser = cursor.fetchone()[0]
251
+
252
253
def _create_definitions(self):
254
for database_name in self.databases:
@@ -313,9 +323,8 @@ class Service(SimpleService):
323
324
# self.add_replication_stats(cursor)
325
316
- # add_wal_metrics needs superuser to get directory listings
317
- # if self.config.get('superuser', True):
318
- # self.add_wal_stats(cursor)
326
+ if self.is_superuser:
327
+ self.add_wal_stats(cursor)
328
329
def add_database_stats(self, cursor):
330
cursor.execute(DATABASE)