]> arthur.barton.de Git - netdata.git/blobdiff - python.d/postgres.chart.py
ab-debian 0.20170327.01-0ab1, upstream v1.6.0-42-gaa6b96fc
[netdata.git] / python.d / postgres.chart.py
index 832092e16565f3a0ad4b32be6255783a1a061c78..1976e2a61c49a0bb025677ac4c347f47ec3829a1 100644 (file)
@@ -242,6 +242,7 @@ class Service(SimpleService):
         self.definitions = deepcopy(CHARTS)
         self.table_stats = configuration.pop('table_stats', False)
         self.index_stats = configuration.pop('index_stats', False)
+        self.database_poll = configuration.pop('database_poll', None)
         self.configuration = configuration
         self.connection = False
         self.is_superuser = False
@@ -262,16 +263,18 @@ class Service(SimpleService):
                 self.connection = psycopg2.connect(**params)
                 self.connection.set_isolation_level(extensions.ISOLATION_LEVEL_AUTOCOMMIT)
                 self.connection.set_session(readonly=True)
-            except OperationalError:
-                return False
-        return True
+            except OperationalError as error:
+                return False, str(error)
+        return True, True
 
     def check(self):
         if not PSYCOPG2:
             self.error('\'python-psycopg2\' module is needed to use postgres.chart.py')
             return False
-        if not self._connect():
-            self.error('Can\'t connect to %s' % str(self.configuration))
+        result, error = self._connect()
+        if not result:
+            conf = dict([(k, (lambda k, v: v if k != 'password' else '*****')(k, v)) for k, v in self.configuration.items()])
+            self.error('Failed to connect to %s. Error: %s' % (str(conf), error))
             return False
         try:
             cursor = self.connection.cursor()
@@ -279,6 +282,9 @@ class Service(SimpleService):
             is_superuser = check_if_superuser_(cursor, QUERIES['IF_SUPERUSER'])
             cursor.close()
 
+            if (self.database_poll and isinstance(self.database_poll, str)):
+                self.databases = [dbase for dbase in self.databases if dbase in self.database_poll.split()] or self.databases
+
             self.locks_zeroed = populate_lock_types(self.databases)
             self.add_additional_queries_(is_superuser)
             self.create_dynamic_charts_()
@@ -307,7 +313,8 @@ class Service(SimpleService):
             add_database_lock_chart_(order=self.order, definitions=self.definitions, database_name=database_name)
 
     def _get_data(self):
-        if self._connect():
+        result, error = self._connect()
+        if result:
             cursor = self.connection.cursor(cursor_factory=DictCursor)
             try:
                 self.data.update(self.locks_zeroed)