-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[*] сleanup pgbackrest helper function by cybertec-postgresql/pgwatch…
- Loading branch information
1 parent
741d6e5
commit 91caa2f
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
src/metrics/00_helpers/get_backup_age_pgbackrest/11/metric.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
CREATE EXTENSION IF NOT EXISTS plpython3u; | ||
/* | ||
Gets age of last successful pgBackRest backup via "pgbackrest --output=json info" unix timestamp. Returns 0 retcode on success. | ||
Expects pgBackRest is correctly configured on monitored DB and "jq" tool is installed on the DB server. | ||
*/ | ||
CREATE OR REPLACE FUNCTION get_backup_age_pgbackrest(OUT retcode int, OUT backup_age_seconds int, OUT message text) AS | ||
$$ | ||
import time | ||
import json | ||
import subprocess | ||
|
||
PGBACKREST_TIMEOUT = 30 | ||
|
||
def error(message, returncode=1): | ||
return returncode, 1000000, 'Not OK. '+message | ||
|
||
pgbackrest_cmd=["pgbackrest", "--output=json", "info"] | ||
|
||
try: | ||
p = subprocess.Popen(pgbackrest_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='utf-8') | ||
stdout, stderr = p.communicate(timeout=PGBACKREST_TIMEOUT) | ||
except OSError as e: | ||
return error('Failed to execute pgbackrest: {}'.format(e)) | ||
except subprocess.TimeoutExpired: | ||
p.terminate() | ||
try: | ||
p.wait(0.5) | ||
except subprocess.TimeoutExpired: | ||
p.kill() | ||
return error('pgbackrest failed to respond in {} seconds'.format(PGBACKREST_TIMEOUT)) | ||
|
||
if p.returncode != 0: | ||
return error('Failed on "pgbackrest info" call', returncode=p.returncode) | ||
|
||
try: | ||
data = json.loads(stdout) | ||
backup_age_seconds = int(time.time()) - data[0]['backup'][-1]['timestamp']['stop'] | ||
return 0, backup_age_seconds, 'OK. Last backup age in seconds: {}'.format(backup_age_seconds) | ||
except (json.JSONDecodeError, KeyError) : | ||
return error('Failed to parse pgbackrest output') | ||
$$ LANGUAGE plpython3u VOLATILE; | ||
|
||
/* contacting S3 could be laggy depending on location */ | ||
ALTER FUNCTION get_backup_age_pgbackrest() SET statement_timeout TO '30s'; | ||
|
||
GRANT EXECUTE ON FUNCTION get_backup_age_pgbackrest() TO pgwatch2; | ||
|
||
COMMENT ON FUNCTION get_backup_age_pgbackrest() is 'created for pgwatch2'; |