From 7c395a80fe656720ce75ec26847227d349b8b7eb Mon Sep 17 00:00:00 2001 From: James Campbell Date: Fri, 13 Jun 2025 01:16:24 -0400 Subject: [PATCH] Add metrics for tracking cache hit ratios --- sample-config/pgmon-metrics.yml | 9 ++- src/pgmon.py | 7 +- zabbix_templates/pgmon_templates.yaml | 92 +++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 2 deletions(-) diff --git a/sample-config/pgmon-metrics.yml b/sample-config/pgmon-metrics.yml index 3d02d41..5647241 100644 --- a/sample-config/pgmon-metrics.yml +++ b/sample-config/pgmon-metrics.yml @@ -19,10 +19,12 @@ metrics: type: value query: 0: SHOW server_version_num + max_frozen_age: type: row query: - 0: SELECT max(age(datfrozenxid)) AS xid_age, max(mxid_age(datminmxid)) AS mxid_age FROM pg_database + 0: SELECT max(age(datfrozenxid)) AS xid_age, 0 AS mxid_age FROM pg_database + 90600: SELECT max(age(datfrozenxid)) AS xid_age, max(mxid_age(datminmxid)) AS mxid_age FROM pg_database # Per-database metrics db_stats: @@ -33,6 +35,11 @@ metrics: test_args: dbname: postgres + hit_ratios: + type: row + query: + 0: SELECT sum(heap_blks_read)::float / sum(heap_blks_read + heap_blks_hit) AS avg_heap_hit_ratio, sum(idx_blks_hit)::float / sum(idx_blks_read + idx_blks_hit) AS avg_idx_hit_ratio, sum(toast_blks_hit)::float / sum(toast_blks_read + toast_blks_hit) AS avg_toast_hit_ratio, sum(tidx_blks_hit)::float / sum(tidx_blks_read + tidx_blks_hit) AS avg_tidx_hit_ratio FROM pg_statio_all_tables + # Debugging ntables: type: value diff --git a/src/pgmon.py b/src/pgmon.py index 310a024..2ca313c 100755 --- a/src/pgmon.py +++ b/src/pgmon.py @@ -656,7 +656,11 @@ def test_queries(): # If the metric has arguments to use while testing, grab those args = metric.get("test_args", {}) # Run the query without the ability to retry. - res = sample_metric(dbname, name, args, retry=False) + try: + res = sample_metric(dbname, name, args, retry=False) + except MetricVersionError: + print("{} -> Unsupported for this version".format(name)) + continue # Compare the result to the provided sample results # TODO print("{} -> {}".format(name, res)) @@ -714,6 +718,7 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler): except LatestVersionCheckError as e: log.error("Failed to retrieve latest version information: {}".format(e)) self._reply(503, "Failed to retrieve latest version info") + return # Note: parse_qs returns the values as a list. Since we always expect # single values, just grab the first from each. diff --git a/zabbix_templates/pgmon_templates.yaml b/zabbix_templates/pgmon_templates.yaml index 1c8eb78..8d5094e 100644 --- a/zabbix_templates/pgmon_templates.yaml +++ b/zabbix_templates/pgmon_templates.yaml @@ -673,6 +673,98 @@ zabbix_export: value: '{#DBNAME}' - tag: Type value: Calculated + - uuid: bb0690e123e94e35a9bbd0f05ee322b4 + name: 'Database Hit Ratios for {#DBNAME}' + type: HTTP_AGENT + key: 'pgmon_db_hit_ratios[{#DBNAME}]' + history: '0' + value_type: TEXT + trends: '0' + url: 'http://localhost:{$AGENT_PORT}/hit_ratios' + query_fields: + - name: dbname + value: '{#DBNAME}' + tags: + - tag: Application + value: PostgreSQL + - tag: Database + value: '{#DBNAME}' + - uuid: e0edbaf6f98249a6b357ade2ab41a76a + name: 'Average heap hit ratio on {#DBNAME}' + type: DEPENDENT + key: 'pgmon_db_hit_ratio[heap,{#DBNAME}]' + delay: '0' + history: 90d + value_type: FLOAT + description: 'The cache (shared_buffers) hit ratio for heap entries for the given database' + preprocessing: + - type: JSONPATH + parameters: + - $.avg_heap_hit_ratio + master_item: + key: 'pgmon_db_hit_ratios[{#DBNAME}]' + tags: + - tag: Application + value: PostgreSQL + - tag: Database + value: '{#DBNAME}' + - uuid: e4956d433a47455ca0c00d67a0f59355 + name: 'Average index hit ratio on {#DBNAME}' + type: DEPENDENT + key: 'pgmon_db_hit_ratio[index,{#DBNAME}]' + delay: '0' + history: 90d + value_type: FLOAT + description: 'The cache (shared_buffers) hit ratio for index entries for the given database' + preprocessing: + - type: JSONPATH + parameters: + - $.avg_idx_hit_ratio + master_item: + key: 'pgmon_db_hit_ratios[{#DBNAME}]' + tags: + - tag: Application + value: PostgreSQL + - tag: Database + value: '{#DBNAME}' + - uuid: 0335267c14a8410a80a16b38167efcb3 + name: 'Average toast hit ratio on {#DBNAME}' + type: DEPENDENT + key: 'pgmon_db_hit_ratio[toast,{#DBNAME}]' + delay: '0' + history: 90d + value_type: FLOAT + description: 'The cache (shared_buffers) hit ratio for heap entries for the given database' + preprocessing: + - type: JSONPATH + parameters: + - $.avg_toast_hit_ratio + master_item: + key: 'pgmon_db_hit_ratios[{#DBNAME}]' + tags: + - tag: Application + value: PostgreSQL + - tag: Database + value: '{#DBNAME}' + - uuid: 3f7da45e351443528c6c36145d38f9b8 + name: 'Average toast index hit ratio on {#DBNAME}' + type: DEPENDENT + key: 'pgmon_db_hit_ratio[toast_index,{#DBNAME}]' + delay: '0' + history: 90d + value_type: FLOAT + description: 'The cache (shared_buffers) hit ratio for toast index entries for the given database' + preprocessing: + - type: JSONPATH + parameters: + - $.avg_tidx_hit_ratio + master_item: + key: 'pgmon_db_hit_ratios[{#DBNAME}]' + tags: + - tag: Application + value: PostgreSQL + - tag: Database + value: '{#DBNAME}' - uuid: 492b3cac15f348c2b85f97b69c114d1b name: 'Database Stats for {#DBNAME}' type: HTTP_AGENT