base agent working

This commit is contained in:
Ralf Mellis 2023-08-19 16:32:29 +02:00
parent 01463479fd
commit 70222535d1
1 changed files with 94 additions and 5 deletions

View File

@ -25,11 +25,11 @@ OPTIONS:
-h, --help Show this help message and exit
""")
# set this to true to produce debug output (this clutters the agent output)
# set this to True to produce debug output (this clutters the agent output)
# be aware: activating this logs very sensitive information to debug files in ~/tmp
# !!DO NOT FORGET to delete these files after debugging is done!!
DEBUG = True
DEBUG = False
mc_api_base = "api/v1/get"
@ -91,17 +91,86 @@ def showOptions():
with open(help_file, "a") as file:
file.write(f"Hostname: {opt_hostname}, Port: {opt_port}, No HTTPS: {opt_no_https}, No Cert Check: {opt_no_cert_check}\n")
def getDomainInfo(headers, verify, base_url):
url = f"{base_url}/domain/all"
response = requests.get(url, headers=headers, verify=verify)
if (response.status_code == 200):
jsdata = response.text
data = json.loads(jsdata) # returns a list of dictionaries
i = 0
while i < len(data):
#pprint(data[i])
# get domain name
domain_name = data[i].get("domain_name")
#print(domain_name)
# get maximum number of mailboxes in this domain
max_num_mboxes_for_domain = data[i].get("max_num_mboxes_for_domain")
# get current number of mailboxes in this domain
mboxes_in_domain = data[i].get("mboxes_in_domain")
# get maximum number of aliases in this domain
max_num_aliases_for_domain = data[i].get("max_num_aliases_for_domain")
# get current number of aliases in this domain
aliases_in_domain = (data[i].get("aliases_in_domain"))
i += 1
# return number of email domains
return i
else:
sys.stderr.write(f"Request response code is {response.status_code} with URL {url}\n")
sys.exit(1)
def getMailboxInfo(headers, verify, base_url):
url = f"{base_url}/mailbox/all"
response = requests.get(url, headers=headers, verify=verify)
if (response.status_code == 200):
jsdata = response.text
data = json.loads(jsdata) # returns a list of dictionaries
i = 0
global_num_messages = 0
while i < len(data):
#pprint(data[i])
# get username of mailbox
username = data[i].get("username")
# get number of messages in mailbox
num_messages = data[i].get("messages")
#print(username)
i += 1
global_num_messages += num_messages
# return number of mailboxes and global number of messages
return i, global_num_messages
else:
sys.stderr.write(f"Request response code is {response.status_code} with URL {url}\n")
sys.exit(1)
def getMailcowInfo(headers, verify, base_url):
url = f"{base_url}/status/version"
response = requests.get(url, headers=headers, verify=verify)
if (response.status_code == 200):
jsdata = response.text
data = json.loads(jsdata) # returns a dictionary
#pprint(data)
# get Mailcow version
mc_version = data["version"]
return mc_version
else:
sys.stderr.write(f"Request response code is {response.status_code} with URL {url}\n")
sys.exit(1)
def main():
getOptions()
# do some parameter checks
if (opt_hostname == ""):
sys.stderr.write(f"No hostname given.\n")
showUsage()
sys.exit(1)
else:
hostname = opt_hostname
if (opt_apikey == ""):
sys.stderr.write(f"No API key given.\n")
showUsage()
sys.exit(1)
if (opt_no_cert_check):
# disable certificate warnings
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
verify = False
else:
@ -119,14 +188,34 @@ def main():
else:
protocol = "https"
port = opt_port
if DEBUG:
showOptions()
if (protocol == "http" and port == "443"):
sys.stderr.write(f"Combining HTTP with port 443 is not supported.\n")
sys.exit(1)
if (protocol == "https" and port == "80"):
sys.stderr.write(f"Combining HTTPS with port 80 is not supported.\n")
sys.exit(1)
sys.exit(1)
headers = CaseInsensitiveDict()
headers["Accept"] = "application/json"
headers["X-API-Key"] = opt_apikey
# now "hostname" contains the FQDN of the host running Mailcow
# now "protocol" is http or https
# now "port" contains the port number to use
# now "verify" signals whether certificate checking will be done (True) or not (False)
# now "headers" contains the accepted format (JSON) and the API key to use
if DEBUG:
showOptions()
print(f"hostname: {hostname}, protocol: {protocol}, port: {port}, verify: {verify}")
base_url = f"{protocol}://{hostname}:{port}/{mc_api_base}"
print(base_url)
# get domain data
num_domains = getDomainInfo(headers, verify, base_url)
print(num_domains)
# get mailbox data
num_mailboxes, num_global_messages = getMailboxInfo(headers, verify, base_url)
print(num_mailboxes, num_global_messages)
# get global Mailcow info
mailcow_version = getMailcowInfo(headers, verify, base_url)
print(mailcow_version)
if __name__ == "__main__":
main()