diff --git a/sample-config/pgmon-metrics.yml b/sample-config/pgmon-metrics.yml index 92b83a2..8c1045d 100644 --- a/sample-config/pgmon-metrics.yml +++ b/sample-config/pgmon-metrics.yml @@ -196,12 +196,28 @@ metrics: type: set query: 0: > - SELECT state, - count(*) AS backend_count, - COALESCE(EXTRACT(EPOCH FROM max(now() - state_change)), 0) AS max_state_time - FROM pg_stat_activity - WHERE datname = %(dbname)s - GROUP BY state + SELECT + states.state, + COALESCE(a.backend_count, 0) AS backend_count, + COALESCE(a.max_state_time, 0) AS max_state_time + FROM ( + SELECT state, + count(*) AS backend_count, + COALESCE(EXTRACT(EPOCH FROM now() - min(state_change)), 0) AS max_state_time + FROM pg_stat_activity + WHERE datname = %(dbname)s + GROUP BY state + ) AS a + RIGHT JOIN + unnest(ARRAY[ + 'active', + 'idle', + 'idle in transaction', + 'idle in transaction (aborted)', + 'fastpath function call', + 'disabled' + ]) AS states(state) + USING (state); test_args: dbname: postgres @@ -235,7 +251,7 @@ metrics: locks: type: row query: - 0: + 0: > SELECT COUNT(*) AS total, SUM(CASE WHEN granted THEN 1 ELSE 0 END) AS granted FROM pg_locks diff --git a/zabbix_templates/pgmon_templates.yaml b/zabbix_templates/pgmon_templates.yaml index 260dec3..17dd276 100644 --- a/zabbix_templates/pgmon_templates.yaml +++ b/zabbix_templates/pgmon_templates.yaml @@ -411,7 +411,7 @@ zabbix_export: preprocessing: - type: JSONPATH parameters: - - '$[?(@.state == "active")].max_state_time' + - '$[?(@.state == "active")].max_state_time.first()' master_item: key: 'pgmon_connection_states[{#DBNAME}]' tags: @@ -429,7 +429,7 @@ zabbix_export: preprocessing: - type: JSONPATH parameters: - - '$[?(@.state == "active")].backend_count' + - '$[?(@.state == "active")].backend_count.first()' master_item: key: 'pgmon_connection_states[{#DBNAME}]' tags: @@ -448,7 +448,7 @@ zabbix_export: preprocessing: - type: JSONPATH parameters: - - '$[?(@.state == "disabled")].max_state_time' + - '$[?(@.state == "disabled")].max_state_time.first()' master_item: key: 'pgmon_connection_states[{#DBNAME}]' tags: @@ -466,7 +466,7 @@ zabbix_export: preprocessing: - type: JSONPATH parameters: - - '$[?(@.state == "disabled")].backend_count' + - '$[?(@.state == "disabled")].backend_count.first()' master_item: key: 'pgmon_connection_states[{#DBNAME}]' tags: @@ -485,7 +485,7 @@ zabbix_export: preprocessing: - type: JSONPATH parameters: - - '$[?(@.state == "fastpath function call")].max_state_time' + - '$[?(@.state == "fastpath function call")].max_state_time.first()' master_item: key: 'pgmon_connection_states[{#DBNAME}]' tags: @@ -503,7 +503,7 @@ zabbix_export: preprocessing: - type: JSONPATH parameters: - - '$[?(@.state == "fastpath function call")].backend_count' + - '$[?(@.state == "fastpath function call")].backend_count.first()' master_item: key: 'pgmon_connection_states[{#DBNAME}]' tags: @@ -522,7 +522,7 @@ zabbix_export: preprocessing: - type: JSONPATH parameters: - - '$[?(@.state == "idle")].max_state_time' + - '$[?(@.state == "idle")].max_state_time.first()' master_item: key: 'pgmon_connection_states[{#DBNAME}]' tags: @@ -540,7 +540,7 @@ zabbix_export: preprocessing: - type: JSONPATH parameters: - - '$[?(@.state == "idle")].backend_count' + - '$[?(@.state == "idle")].backend_count.first()' master_item: key: 'pgmon_connection_states[{#DBNAME}]' tags: @@ -559,7 +559,7 @@ zabbix_export: preprocessing: - type: JSONPATH parameters: - - '$[?(@.state == "idle in transaction (aborted)")].max_state_time' + - '$[?(@.state == "idle in transaction (aborted)")].max_state_time.first()' master_item: key: 'pgmon_connection_states[{#DBNAME}]' tags: @@ -577,7 +577,7 @@ zabbix_export: preprocessing: - type: JSONPATH parameters: - - '$[?(@.state == "idle in transaction (aborted)")].backend_count' + - '$[?(@.state == "idle in transaction (aborted)")].backend_count.first()' master_item: key: 'pgmon_connection_states[{#DBNAME}]' tags: @@ -596,7 +596,7 @@ zabbix_export: preprocessing: - type: JSONPATH parameters: - - '$[?(@.state == "idle in transaction")].max_state_time' + - '$[?(@.state == "idle in transaction")].max_state_time.first()' master_item: key: 'pgmon_connection_states[{#DBNAME}]' tags: @@ -614,7 +614,7 @@ zabbix_export: preprocessing: - type: JSONPATH parameters: - - '$[?(@.state == "idle in transaction")].backend_count' + - '$[?(@.state == "idle in transaction")].backend_count.first()' master_item: key: 'pgmon_connection_states[{#DBNAME}]' tags: @@ -633,7 +633,7 @@ zabbix_export: preprocessing: - type: JSONPATH parameters: - - '$[?(@.state == "starting")].max_state_time' + - '$[?(@.state == "starting")].max_state_time.first()' master_item: key: 'pgmon_connection_states[{#DBNAME}]' tags: @@ -651,7 +651,7 @@ zabbix_export: preprocessing: - type: JSONPATH parameters: - - '$[?(@.state == "starting")].backend_count' + - '$[?(@.state == "starting")].backend_count.first()' master_item: key: 'pgmon_connection_states[{#DBNAME}]' tags: