Skip to content

Instantly share code, notes, and snippets.

@crpb
Last active October 2, 2022 21:37
Show Gist options
  • Save crpb/afd8db7431342689545b26ea4d18fd0b to your computer and use it in GitHub Desktop.
Save crpb/afd8db7431342689545b26ea4d18fd0b to your computer and use it in GitHub Desktop.
CheckMK Local-Check Mailstore Licenses Version Profiles
#!/usr/bin/python3
import os
import configparser
import sys
from mailstore.mgmt import ServerClient as ms_api
import requests
from datetime import datetime, timedelta
# Config-File
config_file = 'mailstore.cfg'
# Version-Check-URL is static
url = 'https://my.mailstore.com/Downloads/Server/'
conf = configparser.RawConfigParser()
# Defaults
conf.read_dict({'server': {'host': 'localhost',
'port': 8463,
'username': 'admin',
'password': 'admin',
'ignoressl': False},
'checks': {'version': True,
'licenses': True,
'profiles': True}})
conf_file = os.path.join(os.path.dirname(sys.argv[0]), config_file)
if not os.path.isfile(conf_file):
print("INFO: " + conf_file + " doesn't exist - using defaults")
else:
try:
conf.read(conf_file)
except EnvironmentError:
e = sys.exc_info()[1]
sys.exit(1)
api = ms_api(username=conf['server']['username'],
password=conf['server']['password'],
host=conf['server']['host'],
port=conf['server']['port'],
ignoreInvalidSSLCerts=conf['server'].getboolean('ignoressl'))
serverinfo = api.GetServerInfo()['result']
licenses = api.GetLicenseInformation()['result']
profiles = api.GetProfiles()
compliance = api.GetComplianceConfiguration()
def check_version():
r = requests.get(url, allow_redirects=False).headers
download_url = r['Location']
filename = download_url.split('/')[-1]
version = filename.lstrip('MailstoreServerSetup-').rstrip('.exe')
current = serverinfo['version']
if current < version:
print('1 "MS:VERSION" - outdated: %s < %s' % (current, version))
else:
print('0 "MS:VERSION" - up to date: %s' % (current))
def check_licenses():
licenses = api.GetLicenseInformation()['result']
users = licenses['namedUsers']
maxusers = licenses['maxNamedUsers']
if users >= maxusers:
stat = 1
else:
stat = 0
print('%s "MS:LICENSE" licensecount=%s;%s use-max' % (
stat,
users,
# maxusers,
# maxusers,
# 0,
maxusers))
def profile_status():
status = ""
profs = api.GetProfiles()
for prof in profs['result']:
res = worker_results(prof['id'])
if res:
status += res
count = len(profiles['result'])
if not status:
stat = 0
else:
stat = 2
text = '%s "MS:PROFILES" profilecount=%s %s' % (stat, count, status)
print(text)
def worker_results(profileID):
timeZoneID = "W. Europe Standard Time"
yest = datetime.now()-timedelta(days=1)
fromIncluding = yest.strftime("%Y-%m-%dT%H:%M:%S")
toExcluding = datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
worker = api.GetWorkerResults(fromIncluding, toExcluding, timeZoneID, profileID)
if worker["statusCode"] == "succeeded":
for x in reversed(worker["result"]):
if x["result"] == "succeeded":
break
else:
string = f'FAIL: "{x["profileName"]}@{x["completeTime"]}" '
return string
#print("<<<mailstore>>>")
if conf['checks'].getboolean('version'):
check_version()
if conf['checks'].getboolean('licenses'):
check_licenses()
if conf['checks'].getboolean('profiles'):
profile_status()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment