-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_mediawiki_version.py
executable file
·81 lines (61 loc) · 2.19 KB
/
check_mediawiki_version.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#! /usr/bin/python3
#
# Icinga plugin for monitoring MediaWiki versions.
#
# Copy this file into /usr/local/lib/nagios/plugins/
#
# Usage: check_mediawiki_version.py <url_of_wiki>
#
import re
import sys
from lxml import etree
import requests
USER_AGENT = "check_mediawiki_version.py/0.0.1 Icinga Plugin"
URL_MW_DOWNLOAD = "https://www.mediawiki.org/wiki/Download"
parser = etree.HTMLParser()
headers = {"user-agent": USER_AGENT}
try:
# Scrape the monitored mediawiki site. Look for:
# <meta name="generator" content="MediaWiki 1.26.2" />
current_version = None
r = requests.get(sys.argv[1], headers=headers)
r.raise_for_status()
root = etree.fromstring(r.content, parser)
for e in root.xpath('//meta[@name="generator"]'):
m = re.match(r"^MediaWiki (\d+\.\d+\.\d+)$", e.get("content"))
if m:
current_version = m.group(1).split(".")
break
if current_version is None:
raise Exception('<meta name="generator"> not found')
# print (current_version)
# Next scrape the mediawiki foundation download site. Look for:
# <a href="//releases.wikimedia.org/mediawiki/1.27/mediawiki-1.27.3.tar.gz">download</a>
supported_versions = []
r = requests.get(URL_MW_DOWNLOAD, headers=headers)
r.raise_for_status()
root = etree.fromstring(r.content, parser)
for e in root.xpath(
'//table//a[starts-with (@href, "//releases.wikimedia.org/mediawiki/")]'
):
m = re.search("\d+\.\d+\.\d+", e.get("href"))
if m:
supported_versions.append(m.group(0).split("."))
# print (supported_versions)
# check if the monitored site is still supported
# and exit accordingly
if current_version in supported_versions:
print("MEDIAWIKI OK - Version: %s" % ".".join(current_version))
sys.exit(0)
upgrade_paths = [
".".join(v) for v in sorted(supported_versions) if v > current_version
]
# print (upgrade_paths)
print(
"MEDIAWIKI CRITICAL - Version: %s, Supported versions: %s"
% (".".join(current_version), ", ".join(upgrade_paths))
)
sys.exit(2)
except Exception as exc:
print("MEDIAWIKI UNKNOWN: %s" % str(exc))
sys.exit(3)