check_forgejo_upgrade/check_forgejo_upgrade
Benjamin Renard 4dce16df85
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
Initial release
2023-01-19 12:54:08 +01:00

110 lines
3.1 KiB
Python
Executable file

#!/usr/bin/python3
"""
Icinga/Nagios plugin to check Forgejo instance upgrade status.
Copyright (c) 2023 Benjamin Renard <brenard@zionetrix.net>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License version 3
as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
"""
import argparse
import logging
import re
import subprocess
import sys
import requests
import xmltodict
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--debug', action='store_true')
parser.add_argument(
'-p', '--path', type=str,
help='Forgejo bin path', default='forgejo')
parser.add_argument(
'-U', '--url', type=str,
help='Forgejo releases RSS URL',
default='https://forgejo.org/releases/rss.xml')
parser.add_argument(
'--rc', action='store_true', dest='include_rc',
help=(
'Allow release candidate (default: only stable release are '
'considered)')
)
options = parser.parse_args()
logging.basicConfig(level=logging.DEBUG if options.debug else logging.WARNING)
current = None
cmd = [options.path, '--version']
logging.debug(
'Command use to retreive current version of Forgejo: %s',
' '.join(cmd))
out = None
exc = None
try:
out = subprocess.check_output(cmd)
logging.debug('Output:\n%s', out)
m = re.search('version ([^ ]+) built', out.decode('utf8', errors='ignore'))
if m:
current = m.group(1)
except Exception as err:
exc = err
logging.debug('Current version: %s', current)
if not current:
print('UNKNOWN - Fail to retreive current Forgejo')
print(f'Command: {" ".join(cmd)}')
print('Output:')
print(out if out else "")
print('Exception:')
print(exc if exc else "")
sys.exit(3)
latest = None
try:
logging.debug('Get releases RSS feed from %s...', options.url)
r = requests.get(options.url)
logging.debug('Data retreive:\n%s', r.text)
data = xmltodict.parse(r.text)
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)
continue
latest = version
break
except Exception:
pass
logging.debug('Latest version: %s', current)
if not latest:
print(
'UNKNOWN - Fail to retreive 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})')
sys.exit(0)
print(
'WARNING - The version of Forgejo currently used is not the latest '
f'({current} vs {latest})')
sys.exit(1)