diff --git a/checks.d/pgbouncer.py b/checks.d/pgbouncer.py index d84d22bf03..5caabc5e35 100644 --- a/checks.d/pgbouncer.py +++ b/checks.d/pgbouncer.py @@ -6,14 +6,16 @@ import psycopg2 as pg + class ShouldRestartException(Exception): pass + class PgBouncer(AgentCheck): """Collects metrics from pgbouncer """ - SOURCE_TYPE_NAME = 'pgbouncer' RATE = AgentCheck.rate GAUGE = AgentCheck.gauge + DB_NAME = 'pgbouncer' STATS_METRICS = { 'descriptors': [ @@ -77,13 +79,13 @@ def _collect_stats(self, key, db, instance_tags): continue for row in results: - if row[0] == 'pgbouncer': + if row[0] == self.DB_NAME: continue desc = scope['descriptors'] assert len(row) == len(cols) + len(desc) - tags = [t for t in instance_tags] + tags = instance_tags[:] tags += ["%s:%s" % (d[0][1], d[1]) for d in zip(desc, row[:len(desc)])] values = zip([scope['metrics'][c] for c in cols], row[len(desc):]) @@ -98,7 +100,7 @@ def _collect_stats(self, key, db, instance_tags): self.log.error("Connection error: %s" % str(e)) raise ShouldRestartException - def _get_connection(self, key, host, port, user, password, dbname, use_cached=True): + def _get_connection(self, key, host, port, user, password, use_cached=True): "Get and memoize connections to instances" if key in self.dbs and use_cached: return self.dbs[key] @@ -109,18 +111,17 @@ def _get_connection(self, key, host, port, user, password, dbname, use_cached=Tr "host:%s" % host, "port:%s" % port ] - if dbname: - service_check_tags.append("db:%s" % dbname) + service_check_tags.append("db:%s" % self.DB_NAME) if host == 'localhost' and password == '': # Use ident method - connection = pg.connect("user=%s dbname=%s" % (user, dbname)) + connection = pg.connect("user=%s dbname=%s" % (user, self.DB_NAME)) elif port != '': connection = pg.connect(host=host, port=port, user=user, - password=password, database=dbname) + password=password, database=self.DB_NAME) else: connection = pg.connect(host=host, user=user, password=password, - database=dbname) + database=self.DB_NAME) status = AgentCheck.OK self.service_check('pgbouncer.can_connect', status, tags=service_check_tags) self.log.debug('pgbouncer status: %s' % status) @@ -137,7 +138,6 @@ def _get_connection(self, key, host, port, user, password, dbname, use_cached=Tr raise CheckException("Please specify a user to connect to PgBouncer as.") connection.set_isolation_level(pg.extensions.ISOLATION_LEVEL_AUTOCOMMIT) - # connection.autocommit = True self.dbs[key] = connection return connection @@ -148,9 +148,8 @@ def check(self, instance): user = instance.get('username', '') password = instance.get('password', '') tags = instance.get('tags', []) - dbname = 'pgbouncer' - key = '%s:%s:%s' % (host, port, dbname) + key = '%s:%s:%s' % (host, port, self.DB_NAME) if tags is None: tags = [] @@ -158,9 +157,9 @@ def check(self, instance): tags = list(set(tags)) try: - db = self._get_connection(key, host, port, user, password, dbname) + db = self._get_connection(key, host, port, user, password) self._collect_stats(key, db, tags) except ShouldRestartException: self.log.info("Resetting the connection") - db = self._get_connection(key, host, port, user, password, dbname, use_cached=False) + db = self._get_connection(key, host, port, user, password, use_cached=False) self._collect_stats(key, db, tags)