Initial work on latest version check code

* Add code to pull the latest supported versions of PostgreSQL from the
  official RSS feed.

* TODO: Split rss parsing code into separate function for unit testing.

* TODO: Test/debug

* TODO: Add metrics to return how far behind the latest version the
  current cluster is.
This commit is contained in:
James Campbell 2025-06-01 02:44:01 -04:00
parent 15097dcba4
commit 487386a7cc
Signed by: james
GPG Key ID: 2287C33A40DC906A

View File

@ -24,6 +24,9 @@ from http.server import BaseHTTPRequestHandler, HTTPServer
from http.server import ThreadingHTTPServer
from urllib.parse import urlparse, parse_qs
import requests
import re
VERSION = "1.0.2"
# Configuration
@ -43,6 +46,12 @@ cluster_version = None
cluster_version_next_check = None
cluster_version_lock = Lock()
# PostgreSQL latest version information
latest_version = None
latest_version_next_check = None
latest_version_lock = Lock()
release_supported = None
# Running state (used to gracefully shut down)
running = True
@ -83,6 +92,10 @@ class MetricVersionError(Exception):
pass
class LatestVersionCheckError(Exception):
pass
# Default config settings
default_config = {
# The address the agent binds to
@ -116,6 +129,8 @@ default_config = {
"reconnect_cooldown": 30,
# How often to check the version of PostgreSQL (seconds)
"version_check_period": 300,
# How often to check the latest supported version of PostgreSQL (seconds)
"latest_version_check_period": 86400,
# Metrics
"metrics": {},
}
@ -482,6 +497,102 @@ def get_cluster_version():
return cluster_version
def get_latest_version():
"""
Get the latest supported version of the major PostgreSQL release running on the server being monitored.
"""
global latest_version
global latest_version_next_check
global release_supported
# If we don't know the latest version or it's past the recheck time, get the
# version from the PostgreSQL RSS feed. Only one thread needs to do this, so
# they all try to grab the lock, and then make sure nobody else beat them to it.
if (
latest_version is None
or latest_version_next_check is None
or latest_version_next_check < datetime.now()
):
with latest_version_lock:
# Only check if nobody already got the version before us
if (
latest_version is None
or latest_version_next_check is None
or latest_version_next_check < datetime.now()
):
log.info("Checking latest PostgreSQL version")
cluster_version = get_cluster_version()
latest_version_next_check = datetime.now() + timedelta(
seconds=int(config["latest_version_check_period"])
)
# Extract the release
# 90603 => 9.6
# 130010 => 13
if cluster_version // 10000 < 10:
release = cluster_version // 10000 + (
cluster_version % 10000 // 100 / 10
)
else:
release = cluster_version // 10000
# Grab the RSS feed
raw_rss = requests.get("https://www.postgresql.org/versions.rss")
if raw_rss.status != 200:
raise LatestVersionCheckError("code={}".format(r.status))
# Regular expressions for parsing the RSS document
version_line = re.compile(
r"([0-9][0-9.]+) is the latest release in the ([0-9][0-9.]+) series"
)
unsupported_line = re.compile(r"^This version is unsupported")
# Loop through the RSS until we find the current release
release_found = False
for line in raw_rss.text.lines():
m = version_line.match(line)
if m:
if release == int(m.group(2)):
release_found = True
version = float(m.group(1))
if version < 10:
parts = list(map(int, version.split(".")))
latest_version = int(
"{}{:02}{:02}".format(parts[0], parts[1], parts[2])
)
else:
parts = list(map(int, version.split(".")))
latest_version = int(
"{}00{:02}".format(parts[0], parts[1])
)
release_found = True
elif release_found:
if unsupported.match(line):
release_supported = False
else:
release_supported = True
break
# Make sure we actually found it
if not release_found:
raise LatestVersionCheckError(
"Current release ({}) not found".format(release)
)
log.info(
"Got latest PostgreSQL version: {} supported={}".format(
latest_version, release_supported
)
)
log.debug(
"Next latest PostgreSQL version check will be after: {}".format(
latest_version_next_check
)
)
return latest_version
def sample_metric(dbname, metric_name, args, retry=True):
"""
Run the appropriate query for the named metric against the specified database