MKP 1.2.0, added version check

This commit is contained in:
mellis
2024-01-26 15:05:03 +01:00
parent 276e67d473
commit 3ae427459a
9 changed files with 165 additions and 16 deletions

View File

@@ -24,6 +24,7 @@ OPTIONS:
-H, --hostname Hostname (FQDN or IP) of Nextcloud server
-k, --apikey API Key
-P, --port Port
--check-version True|False If "True": Running version will be checked against official Github repo
--no-https True|False If "True": Disable HTTPS, use HTTP (not recommended!)
--no-cert-check True|False If "True": Disable TLS certificate check (not recommended!)
-h, --help Show this help message and exit
@@ -40,12 +41,13 @@ mc_api_base = "api/v1/get"
opt_hostname = ""
opt_apikey = ""
opt_port = ""
opt_check_version = False
opt_no_https = False
opt_no_cert_check = False
short_options = 'hH:k:P:'
long_options = [
'hostname=', 'apikey=', 'port=', 'no-https=', 'no-cert-check=', 'help'
'hostname=', 'apikey=', 'port=', 'check-version=', 'no-https=', 'no-cert-check=', 'help'
]
domain_data = {}
@@ -121,6 +123,7 @@ def getOptions() -> None:
global opt_hostname
global opt_apikey
global opt_port
global opt_check_version
global opt_no_https
global opt_no_cert_check
@@ -132,6 +135,11 @@ def getOptions() -> None:
opt_apikey = arg
elif opt in ['-P', '--port']:
opt_port = arg
elif opt in ['--check-version']:
if arg == 'True':
opt_check_version = True
else:
opt_check_version = False
elif opt in ['--no-https']:
if arg == 'True':
opt_no_https = True
@@ -158,6 +166,7 @@ def showOptions() -> None:
print(f"Hostname: {opt_hostname}")
print(f"Username: {opt_apikey}")
print(f"Port: {opt_port}")
print(f"Check Version: {opt_check_version}")
print(f"No HTTPS: {opt_no_https}")
print(f"No TLS Check: {opt_no_cert_check}")
home_path = os.getenv("HOME")
@@ -272,8 +281,60 @@ def getMailboxInfo(headers: str, verify: bool, base_url: str) -> tuple:
sys.stderr.write(f"Request response code is {response.status_code} with URL {url}\n")
sys.exit(1)
def getGitVersion() -> str:
url = "https://api.github.com/repos/mailcow/mailcow-dockerized/releases/latest"
git_version = ""
response = requests.get(url)
if (response.status_code == 200):
jsdata = response.text
data = json.loads(jsdata) # returns a dictionary
git_version = data["tag_name"]
#print(git_version)
else:
sys.stderr.write(f"Request response code is {response.status_code} with URL {url}\n")
sys.exit(1)
return git_version
def compareVersions(mc_ver: str, git_ver: str) -> bool:
update_available = False
try:
mc_year, mc_month = mc_ver.split("-")
git_year, git_month = git_ver.split("-")
#print(mc_year, mc_month, git_year, git_month)
mc_year_int = int(mc_year)
git_year_int = int(git_year)
if git_year_int > mc_year_int:
update_available = True
else:
len_mc_month = len(mc_month)
len_git_month = len(git_month)
if len_mc_month == 2:
mc_month_int = int(mc_month)
elif len_mc_month == 3:
mc_month_int = int(mc_month[0:2])
mc_month_ver = mc_month[-1]
else:
pass
if len_git_month == 2:
git_month_int = int(git_month)
elif len_git_month == 3:
git_month_int = int(git_month[0:2])
git_month_ver = git_month[-1]
else:
pass
if git_month_int > mc_month_int:
update_available = True
elif len_mc_month == 2 and len_git_month == 3:
update_available = True
elif git_month_ver > mc_month_ver:
update_available = True
except:
pass
return update_available
#@debugLog
def getMailcowInfo(headers: str, verify: bool, base_url: str) -> str:
def getMailcowInfo(headers: str, verify: bool, base_url: str) -> dict:
info_data = {}
url = f"{base_url}/status/version"
response = requests.get(url, headers=headers, verify=verify)
if (response.status_code == 200):
@@ -282,7 +343,20 @@ def getMailcowInfo(headers: str, verify: bool, base_url: str) -> str:
#pprint(data)
# get Mailcow version
mc_version = data["version"]
return mc_version
# if enabled, check this version against the official Mailcow repo on Github
if opt_check_version:
git_version = getGitVersion()
update_available = compareVersions(mc_version, git_version)
#update_available = compareVersions("2023-01a", "2023-02")
#print(update_available)
info_data["git_version"] = git_version
info_data["update_available"] = update_available
else:
info_data["git_version"] = "Version check disabled"
info_data["update_available"] = False
info_data["mc_version"] = mc_version
info_data["check_version_enabled"] = opt_check_version
return info_data
else:
sys.stderr.write(f"Request response code is {response.status_code} with URL {url}\n")
sys.exit(1)
@@ -346,11 +420,15 @@ def doCmkOutputMailboxes() -> None:
print(f"{mb};{active};{created};{modified};{name};{num_messages};{percent_in_use};{quota};{quota_used};{last_imap_login};{last_pop3_login};{last_smtp_login}")
#@debugLog
def doCmkOutputMailcow(version: str, num_domains: int, num_mailboxes: int, num_global_messages: int, solr_enabled: bool, solr_size: float, solr_documents: int) -> None:
def doCmkOutputMailcow(version: str,
num_domains: int, num_mailboxes: int, num_global_messages: int,
solr_enabled: bool, solr_size: float, solr_documents: int,
git_version: str, update_available: bool, check_version_enabled: bool
) -> None:
print("<<<mailcow_info:sep(59)>>>")
# strip semicolons, if present, since we use it as delimiter
version = version.replace(";", "")
print(f"{version};{num_domains};{num_mailboxes};{num_global_messages};{solr_enabled};{solr_size};{solr_documents}")
print(f"{version};{num_domains};{num_mailboxes};{num_global_messages};{solr_enabled};{solr_size};{solr_documents};{git_version};{update_available};{check_version_enabled}")
'''
@@ -449,11 +527,19 @@ def main():
num_mailboxes, num_global_messages = getMailboxInfo(headers, verify, base_url)
# get global Mailcow info
solr_enabled, solr_size, solr_documents = getSolrInfo(headers, verify, base_url)
mailcow_version = getMailcowInfo(headers, verify, base_url)
mailcow_info = getMailcowInfo(headers, verify, base_url)
mailcow_version = mailcow_info["mc_version"]
github_version = mailcow_info["git_version"]
update_available = mailcow_info["update_available"]
check_version_enabled = mailcow_info["check_version_enabled"]
# create agent output
doCmkOutputDomains()
doCmkOutputMailboxes()
doCmkOutputMailcow(mailcow_version, num_domains, num_mailboxes, num_global_messages, solr_enabled, solr_size, solr_documents)
doCmkOutputMailcow(mailcow_version,
num_domains, num_mailboxes, num_global_messages,
solr_enabled, solr_size, solr_documents,
github_version, update_available, check_version_enabled
)
if __name__ == "__main__":

View File

@@ -10,6 +10,7 @@ description:
Got to configuration settings and create a new API key (read-only access is sufficient).
Allow API access to at least your CheckMK server IP address.
Shows several information about a Mailcow instance, e.g. number of domains/mailboxes/messages.
The check will raise WARN if an update is available (and if this check is enabled)
The check will raise WARN/CRIT if the number of domains is above the configurable levels.
The check will raise WARN/CRIT if the number of mailboxes is above the configurable levels.
The check will raise WARN/CRIT if the number of messages is above the configurable levels.

View File

@@ -3,6 +3,7 @@ def agent_mailcow_arguments(params, hostname, ipaddress):
"--hostname", params["hostname"],
"--apikey", passwordstore_get_cmdline("%s", params["apikey"]),
"--port", params["port"],
"--check-version", params["check_version"],
"--no-https", params["no_https"],
"--no-cert-check", params["no_cert_check"],
ipaddress,

View File

@@ -22,10 +22,12 @@ def _parameter_spec_mailcow_info():
elements=[
Integer(
title=_("Warning at"),
size=32,
default_value=100,
),
Integer(
title=_("Critical at"),
size=32,
default_value=200,
)
],
@@ -35,10 +37,12 @@ def _parameter_spec_mailcow_info():
elements=[
Integer(
title=_("Warning at"),
size=32,
default_value=500,
),
Integer(
title=_("Critical at"),
size=32,
default_value=1000,
)
],
@@ -48,10 +52,12 @@ def _parameter_spec_mailcow_info():
elements=[
Integer(
title=_("Warning at"),
size=32,
default_value=100000,
),
Integer(
title=_("Critical at"),
size=32,
default_value=250000,
)
],
@@ -61,11 +67,13 @@ def _parameter_spec_mailcow_info():
elements=[
Float(
title=_("Warning at"),
size=32,
default_value=4096.0,
unit="MBytes",
),
Float(
title=_("Critical at"),
size=32,
default_value=8192.0,
unit="MBytes",
)
@@ -76,10 +84,12 @@ def _parameter_spec_mailcow_info():
elements=[
Integer(
title=_("Warning at"),
size=32,
default_value=20000,
),
Integer(
title=_("Critical at"),
size=32,
default_value=40000,
)
],

View File

@@ -40,10 +40,12 @@ def _parameter_spec_mailcow_mailboxes():
elements=[
Integer(
title=_("Warning at"),
size=32,
default_value=1000,
),
Integer(
title=_("Critical at"),
size=32,
default_value=2500,
)
],

View File

@@ -43,6 +43,8 @@ def _valuespec_special_agent_mailcow():
("port", TextAscii(title=_("Port"),
allow_empty=True,
help=_("Specify port if not listening to HTTPS/HTTP, optional"))),
("check_version", Checkbox(title=_("Check version"),
help=_("Checks the running version against the public Github repo"))),
("no_https", Checkbox(title=_("Disable HTTPS"),
help=_("Activate to disable TLS encryption (not recommended), optional"))),
("no_cert_check", Checkbox(title=_("Disable certificate validation"),