From 667c342cebdc71278af758eb6188df6f91233e8b Mon Sep 17 00:00:00 2001 From: tristan Date: Wed, 16 Jul 2014 17:01:36 -0400 Subject: [PATCH 1/4] Fix posture check when several versions of postgres - don't update self.DB_METRICS, use a new dict instead --- checks.d/postgres.py | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/checks.d/postgres.py b/checks.d/postgres.py index 629f44547b..f59a5b19a4 100644 --- a/checks.d/postgres.py +++ b/checks.d/postgres.py @@ -19,18 +19,7 @@ class PostgreSql(AgentCheck): 'descriptors': [ ('datname', 'db') ], - 'metrics': { - 'numbackends' : ('postgresql.connections', GAUGE), - 'xact_commit' : ('postgresql.commits', RATE), - 'xact_rollback' : ('postgresql.rollbacks', RATE), - 'blks_read' : ('postgresql.disk_read', RATE), - 'blks_hit' : ('postgresql.buffer_hit', RATE), - 'tup_returned' : ('postgresql.rows_returned', RATE), - 'tup_fetched' : ('postgresql.rows_fetched', RATE), - 'tup_inserted' : ('postgresql.rows_inserted', RATE), - 'tup_updated' : ('postgresql.rows_updated', RATE), - 'tup_deleted' : ('postgresql.rows_deleted', RATE), - }, + 'metrics': {}, 'query': """ SELECT datname, %s @@ -41,6 +30,19 @@ class PostgreSql(AgentCheck): 'relation': False, } + COMMON_METRICS = { + 'numbackends' : ('postgresql.connections', GAUGE), + 'xact_commit' : ('postgresql.commits', RATE), + 'xact_rollback' : ('postgresql.rollbacks', RATE), + 'blks_read' : ('postgresql.disk_read', RATE), + 'blks_hit' : ('postgresql.buffer_hit', RATE), + 'tup_returned' : ('postgresql.rows_returned', RATE), + 'tup_fetched' : ('postgresql.rows_fetched', RATE), + 'tup_inserted' : ('postgresql.rows_inserted', RATE), + 'tup_updated' : ('postgresql.rows_updated', RATE), + 'tup_deleted' : ('postgresql.rows_deleted', RATE), + } + NEWER_92_METRICS = { 'deadlocks' : ('postgresql.deadlocks', GAUGE), 'temp_bytes' : ('postgresql.temp_bytes', RATE), @@ -100,6 +102,7 @@ def __init__(self, name, init_config, agentConfig): AgentCheck.__init__(self, name, init_config, agentConfig) self.dbs = {} self.versions = {} + self.instance_metrics = {} def _get_version(self, key, db): if key not in self.versions: @@ -121,15 +124,24 @@ def _is_9_2_or_above(self, key, db): return False + def _get_instance_metrics(self, key, db): + # Extended 9.2+ metrics if needed + metrics = self.instance_metrics.get(key) + if metrics is None: + if self._is_9_2_or_above(key, db): + self.instance_metrics[key] = self.COMMON_METRICS.items() + self.NEWER_92_METRICS.items() + else: + self.instance_metrics[key] = self.COMMON_METRICS.items() + metrics = self.instance_metrics.get(key) + return metrics + def _collect_stats(self, key, db, instance_tags, relations): """Query pg_stat_* for various metrics If relations is not an empty list, gather per-relation metrics on top of that. """ - # Extended 9.2+ metrics - if self._is_9_2_or_above(key, db): - self.DB_METRICS['metrics'].update(self.NEWER_92_METRICS) + self.DB_METRICS['metrics'] = self._get_instance_metrics(key, db) # Do we need relation-specific metrics? if not relations: From 9b5dbbd8d2a87274754363de6d51626d59cbfcdd Mon Sep 17 00:00:00 2001 From: tristan Date: Wed, 16 Jul 2014 17:12:06 -0400 Subject: [PATCH 2/4] Add come comments --- checks.d/postgres.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/checks.d/postgres.py b/checks.d/postgres.py index f59a5b19a4..6d22efe010 100644 --- a/checks.d/postgres.py +++ b/checks.d/postgres.py @@ -125,6 +125,10 @@ def _is_9_2_or_above(self, key, db): return False def _get_instance_metrics(self, key, db): + """Use either COMMON_METRICS or COMMON_METRICS + NEWER_92_METRICS + depending on the postgres version. + Uses a dictionnary to save the result for each instance + """ # Extended 9.2+ metrics if needed metrics = self.instance_metrics.get(key) if metrics is None: From 2fa5aad5a2f23c2654ee5e712723b3c7abfd8491 Mon Sep 17 00:00:00 2001 From: tristan Date: Wed, 16 Jul 2014 17:14:54 -0400 Subject: [PATCH 3/4] Update the self.instance_metrics to store dicts instead of lists --- checks.d/postgres.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/checks.d/postgres.py b/checks.d/postgres.py index 6d22efe010..0d0ad68d7b 100644 --- a/checks.d/postgres.py +++ b/checks.d/postgres.py @@ -133,9 +133,9 @@ def _get_instance_metrics(self, key, db): metrics = self.instance_metrics.get(key) if metrics is None: if self._is_9_2_or_above(key, db): - self.instance_metrics[key] = self.COMMON_METRICS.items() + self.NEWER_92_METRICS.items() + self.instance_metrics[key] = dict(self.COMMON_METRICS.items() + self.NEWER_92_METRICS.items()) else: - self.instance_metrics[key] = self.COMMON_METRICS.items() + self.instance_metrics[key] = dict(self.COMMON_METRICS.items()) metrics = self.instance_metrics.get(key) return metrics From 331b240fc0eed0f5cbf533cdf39ae8f70bbad20a Mon Sep 17 00:00:00 2001 From: tristan Date: Wed, 16 Jul 2014 17:56:21 -0400 Subject: [PATCH 4/4] small Update of the code format --- checks.d/postgres.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/checks.d/postgres.py b/checks.d/postgres.py index 0d0ad68d7b..63ec50e7ca 100644 --- a/checks.d/postgres.py +++ b/checks.d/postgres.py @@ -133,9 +133,9 @@ def _get_instance_metrics(self, key, db): metrics = self.instance_metrics.get(key) if metrics is None: if self._is_9_2_or_above(key, db): - self.instance_metrics[key] = dict(self.COMMON_METRICS.items() + self.NEWER_92_METRICS.items()) + self.instance_metrics[key] = dict(self.COMMON_METRICS, **self.NEWER_92_METRICS) else: - self.instance_metrics[key] = dict(self.COMMON_METRICS.items()) + self.instance_metrics[key] = dict(self.COMMON_METRICS) metrics = self.instance_metrics.get(key) return metrics