Use Codeberg API to retreive latest release info
All checks were successful
Run tests / test-precommit (push) Successful in 1m11s
All checks were successful
Run tests / test-precommit (push) Successful in 1m11s
This commit is contained in:
parent
be72d13cc0
commit
7bf258de8f
4 changed files with 42 additions and 40 deletions
|
@ -16,6 +16,6 @@ jobs:
|
|||
DEBIAN_FRONTEND: noninteractive
|
||||
run: |
|
||||
apt-get update
|
||||
apt-get -y install --no-install-recommends python3-requests python3-xmltodict
|
||||
apt-get -y install --no-install-recommends python3-requests
|
||||
- name: Run pre-commit
|
||||
run: pre-commit run --all-files
|
||||
|
|
16
README.md
16
README.md
|
@ -5,7 +5,7 @@ This Icinga/Nagios check plugin permit to check Forgejo instance upgrade status
|
|||
## Installation
|
||||
|
||||
```
|
||||
apt install git python3-requests python3-xmltodict
|
||||
apt install git python3-requests
|
||||
git clone https://gitea.zionetrix.net/bn8/check_forgejo_upgrade.git /usr/local/src/check_forgejo_upgrade
|
||||
mkdir -p /usr/local/lib/nagios/plugins
|
||||
ln -s /usr/local/src/check_forgejo_upgrade/check_forgejo_upgrade /usr/local/lib/nagios/plugins/
|
||||
|
@ -18,20 +18,22 @@ service nagios-nrpe-server reload
|
|||
## Usage
|
||||
|
||||
```
|
||||
usage: check_forgejo_upgrade [-h] [-d] [-p PATH] [-U URL] [--rc]
|
||||
usage: check_forgejo_upgrade [-h] [-d] [-p PATH] [-U URL] [--pre-release] [--draft] [-t TIMEOUT]
|
||||
|
||||
optional arguments:
|
||||
options:
|
||||
-h, --help show this help message and exit
|
||||
-d, --debug
|
||||
-p PATH, --path PATH Forgejo bin path
|
||||
-U URL, --url URL Forgejo releases RSS URL
|
||||
--rc Allow release candidate (default: only stable release are
|
||||
considered)
|
||||
-U URL, --url URL Forgejo releases URL
|
||||
--pre-release Allow pre-release (default: only stable release are considered)
|
||||
--draft Allow draft release (default: only stable release are considered)
|
||||
-t TIMEOUT, --timeout TIMEOUT
|
||||
Specify timeout for HTTP requests (default: 20)
|
||||
```
|
||||
|
||||
## Copyright
|
||||
|
||||
Copyright (c) 2023 Benjamin Renard <brenard@zionetrix.net>
|
||||
Copyright (c) 2023-2024 Benjamin Renard <brenard@zionetrix.net>
|
||||
|
||||
## License
|
||||
|
||||
|
|
|
@ -25,7 +25,6 @@ import subprocess # nosec
|
|||
import sys
|
||||
|
||||
import requests
|
||||
import xmltodict
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
|
||||
|
@ -35,14 +34,18 @@ parser.add_argument(
|
|||
"-U",
|
||||
"--url",
|
||||
type=str,
|
||||
help="Forgejo releases RSS URL",
|
||||
default="https://forgejo.org/releases/rss.xml",
|
||||
help="Forgejo releases URL",
|
||||
default="https://codeberg.org/api/v1/repos/forgejo/forgejo/releases",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--rc",
|
||||
"--pre-release",
|
||||
action="store_true",
|
||||
dest="include_rc",
|
||||
help="Allow release candidate (default: only stable release are considered)",
|
||||
help="Allow pre-release (default: only stable release are considered)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--draft",
|
||||
action="store_true",
|
||||
help="Allow draft release (default: only stable release are considered)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-t", "--timeout", type=int, help="Specify timeout for HTTP requests (default: 20)", default=20
|
||||
|
@ -83,43 +86,40 @@ logging.debug("Cleaned current version: %s", CURRENT)
|
|||
LATEST = None
|
||||
LATEST_INT = None
|
||||
try:
|
||||
logging.debug("Get releases RSS feed from %s...", options.url)
|
||||
logging.debug("Get releases from %s...", options.url)
|
||||
r = requests.get(options.url, timeout=options.timeout)
|
||||
logging.debug("Data retrieve:\n%s", r.text)
|
||||
data = xmltodict.parse(r.text)
|
||||
versions = {}
|
||||
for item in data["rss"]["channel"]["item"]:
|
||||
version = re.sub("^v", "", item["title"])
|
||||
if not options.include_rc and "-rc" in version:
|
||||
logging.debug("Ignore release candidate %s", version)
|
||||
data = r.json()
|
||||
logging.debug("Data retrieve:\n%s", data)
|
||||
for item in data:
|
||||
if not options.pre_release and item["prerelease"]:
|
||||
logging.debug("Ignore pre-release %s", item["name"])
|
||||
continue
|
||||
version_int = int(re.sub(r"[\.-]", "000", version))
|
||||
logging.debug("Found version %s (%s)", version, version_int)
|
||||
if not LATEST_INT or LATEST_INT < version_int:
|
||||
if LATEST:
|
||||
logging.debug(
|
||||
"Version %s considered as newer than %s, override latest version",
|
||||
version,
|
||||
LATEST,
|
||||
)
|
||||
LATEST = version
|
||||
LATEST_INT = version_int
|
||||
else:
|
||||
logging.debug("Version %s considered as oldest than %s", version, LATEST)
|
||||
if not options.draft and item["draft"]:
|
||||
logging.debug("Ignore draft release %s", item["name"])
|
||||
continue
|
||||
LATEST = item
|
||||
break
|
||||
except Exception: # pylint: disable=broad-except # nosec
|
||||
pass
|
||||
logging.debug("Latest version: %s", LATEST)
|
||||
|
||||
if not LATEST:
|
||||
print("UNKNOWN - Fail to retrieve latest Forgejo release from the project RSS feed")
|
||||
print(f"Current version: {CURRENT}")
|
||||
sys.exit(3)
|
||||
|
||||
if LATEST == CURRENT:
|
||||
print(f"OK - The latest release of Forgejo is currently used ({LATEST})")
|
||||
logging.debug("Latest version is %s", LATEST["name"])
|
||||
|
||||
if LATEST["name"] == CURRENT:
|
||||
print(
|
||||
f"OK - The latest release of Forgejo is currently used "
|
||||
f"({LATEST['name']}, published on {LATEST['published_at']})"
|
||||
)
|
||||
sys.exit(0)
|
||||
|
||||
print(
|
||||
"WARNING - The version of Forgejo currently used is not the latest " f"({CURRENT} vs {LATEST})"
|
||||
"WARNING - The version of Forgejo currently used is not the latest "
|
||||
f"({CURRENT} vs {LATEST['name']}), published on {LATEST['published_at']})"
|
||||
)
|
||||
print(LATEST["body"])
|
||||
print(f"URL: {LATEST['html_url']}")
|
||||
sys.exit(1)
|
||||
|
|
2
debian/control
vendored
2
debian/control
vendored
|
@ -7,7 +7,7 @@ Standards-Version: 3.9.6
|
|||
|
||||
Package: check-forgejo-upgrade
|
||||
Architecture: all
|
||||
Depends: ${misc:Depends}, python3, python3-requests, python3-xmltodict
|
||||
Depends: ${misc:Depends}, python3, python3-requests
|
||||
Description: Monitoring plugin to check Forgejo instance upgrade status
|
||||
This Icinga/Nagios check plugin permit to check Forgejo instance upgrade
|
||||
status by comparing the local forgejo binary version against the latest
|
||||
|
|
Loading…
Reference in a new issue