Skip to content

Commit e726ef2

Browse files
authored
Merge pull request #61 from cybertec-postgresql/monitoring_fix_pgbackrest
add function for pgbackrest-data
2 parents acef3d2 + 644066f commit e726ef2

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

pkg/cluster/database.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,40 @@ const (
9898
CREATE EXTENSION IF NOT EXISTS pgnodemx with SCHEMA exporter;
9999
alter extension pgnodemx UPDATE;
100100
CREATE TABLE IF NOT EXISTS exporter.pgbackrestbackupinfo (
101-
name text NOT NULL,
102101
data jsonb NOT NULL,
103102
data_time timestamp with time zone DEFAULT now() NOT NULL
104103
)
105104
WITH (autovacuum_analyze_scale_factor='0', autovacuum_vacuum_scale_factor='0', autovacuum_vacuum_threshold='2', autovacuum_analyze_threshold='2');
106105
ALTER TABLE exporter.pgbackrestbackupinfo OWNER TO cpo_exporter;
106+
107+
CREATE OR REPLACE FUNCTION exporter.update_pgbackrest_info()
108+
RETURNS VOID AS $$
109+
DECLARE
110+
last_entry_timestamp TIMESTAMP;
111+
record_count INT;
112+
BEGIN
113+
SELECT COUNT(*) INTO record_count
114+
FROM exporter.pgbackrestbackupinfo;
115+
116+
IF record_count > 0 THEN
117+
SELECT data_time INTO last_entry_timestamp
118+
FROM exporter.pgbackrestbackupinfo
119+
ORDER BY data_time DESC
120+
LIMIT 1;
121+
122+
IF last_entry_timestamp < NOW() - INTERVAL '5 minutes' THEN
123+
DELETE FROM exporter.pgbackrestbackupinfo;
124+
ELSE
125+
RETURN;
126+
END IF;
127+
END IF;
128+
129+
EXECUTE format(
130+
'COPY exporter.pgbackrestbackupinfo (data) FROM program ''pgbackrest info --output=json'' WITH (FORMAT text, DELIMITER ''|'')'
131+
);
132+
END;
133+
$$ LANGUAGE plpgsql;
134+
107135
`
108136
)
109137

0 commit comments

Comments
 (0)