MKP 1.2.0, added version check
This commit is contained in:
@@ -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__":
|
||||
|
||||
Reference in New Issue
Block a user