diff --git a/checks.d/postgres.py b/checks.d/postgres.py index 68ff1b947a..0bd11be0a6 100644 --- a/checks.d/postgres.py +++ b/checks.d/postgres.py @@ -172,10 +172,26 @@ class PostgreSql(AgentCheck): relname = ANY(%s)""" } + COUNT_METRICS = { + 'descriptors': [ + ('schemaname', 'schema') + ], + 'metrics': { + 'pg_stat_user_tables': ('postgresql.total_tables', GAUGE), + }, + 'relation': False, + 'query': """ +SELECT schemaname, count(*) +FROM %s +GROUP BY schemaname + """ + } + REPLICATION_METRICS = { 'descriptors': [], 'metrics': { - 'GREATEST(0, EXTRACT(EPOCH FROM now() - pg_last_xact_replay_timestamp())) AS replication_delay': ('postgresql.replication_delay', GAUGE), + 'CASE WHEN pg_last_xlog_receive_location() = pg_last_xlog_replay_location() THEN 0 ELSE GREATEST (0, EXTRACT (EPOCH FROM now() - pg_last_xact_replay_timestamp())) END': ('postgresql.replication_delay', GAUGE), + 'abs(pg_xlog_location_diff(pg_last_xlog_receive_location(), pg_last_xlog_replay_location())) AS replication_delay_bytes': ('postgres.replication_delay_bytes', GAUGE) }, 'relation': False, 'query': """ @@ -274,7 +290,8 @@ def _collect_stats(self, key, db, instance_tags, relations, custom_metrics): self.DB_METRICS, self.CONNECTION_METRICS, self.BGW_METRICS, - self.LOCK_METRICS + self.LOCK_METRICS, + self.COUNT_METRICS ] # Do we need relation-specific metrics? diff --git a/tests/test_postgresql.py b/tests/test_postgresql.py index 9427459ea0..4feee79015 100644 --- a/tests/test_postgresql.py +++ b/tests/test_postgresql.py @@ -63,6 +63,7 @@ def test_checks(self): self.assertTrue(len([m for m in metrics if m[0] == u'postgresql.total_size']) >= 1, pprint(metrics)) self.assertTrue(len([m for m in metrics if m[0] == u'postgresql.max_connections']) >= 1, pprint(metrics)) self.assertTrue(len([m for m in metrics if m[0] == u'postgresql.percent_usage_connections']) >= 1, pprint(metrics)) + self.assertTrue(len([m for m in metrics if m[0] == u'postgresql.total_tables']) >= 1, pprint(metrics)) self.assertTrue(len([m for m in metrics if m[0] == u'postgresql.db.count']) == 1, pprint(metrics)) # Don't test for locks # self.assertTrue(len([m for m in metrics if m[0] == u'postgresql.locks']) >= 1, pprint(metrics))