all necessary info from agent present

This commit is contained in:
Ralf Mellis 2023-08-19 17:40:08 +02:00
parent 70222535d1
commit ab0d6b1caf

View File

@ -44,6 +44,10 @@ long_options = [
'hostname=', 'apikey=', 'port=', 'no-https=', 'no-cert-check=', 'help' 'hostname=', 'apikey=', 'port=', 'no-https=', 'no-cert-check=', 'help'
] ]
domain_data = {}
mailbox_data = {}
def getOptions(): def getOptions():
global opt_hostname global opt_hostname
global opt_apikey global opt_apikey
@ -79,6 +83,7 @@ def getOptions():
with open(help_file, "a") as file: with open(help_file, "a") as file:
file.write(f"Number of Arguments: {len(sys.argv)}, Argument List: {str(sys.argv)}\n") file.write(f"Number of Arguments: {len(sys.argv)}, Argument List: {str(sys.argv)}\n")
def showOptions(): def showOptions():
print(f"Hostname: {opt_hostname}") print(f"Hostname: {opt_hostname}")
print(f"Username: {opt_apikey}") print(f"Username: {opt_apikey}")
@ -91,6 +96,7 @@ def showOptions():
with open(help_file, "a") as file: 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") 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): def getDomainInfo(headers, verify, base_url):
url = f"{base_url}/domain/all" url = f"{base_url}/domain/all"
response = requests.get(url, headers=headers, verify=verify) response = requests.get(url, headers=headers, verify=verify)
@ -100,17 +106,24 @@ def getDomainInfo(headers, verify, base_url):
i = 0 i = 0
while i < len(data): while i < len(data):
#pprint(data[i]) #pprint(data[i])
# get domain name # get domain name and status (active (1) or not (0))
domain_name = data[i].get("domain_name") domain_name = data[i].get("domain_name")
#print(domain_name) active = data[i].get("active")
# get maximum number of mailboxes in this domain # get creation and last modification date
created = data[i].get("created")
modified = data[i].get("modified") # returns "None" or date
# get maximum and current number of mailboxes in this domain
max_num_mboxes_for_domain = data[i].get("max_num_mboxes_for_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") mboxes_in_domain = data[i].get("mboxes_in_domain")
# get maximum number of aliases in this domain # get maximum and current number of aliases in this domain
max_num_aliases_for_domain = data[i].get("max_num_aliases_for_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")
aliases_in_domain = (data[i].get("aliases_in_domain")) # get total messages in this domain
msgs_total = data[i].get("msgs_total")
# get total bytes used in this domain
bytes_total = data[i].get("bytes_total")
# get maximum quota for this domain
max_quota_for_domain = data[i].get("max_quota_for_domain")
i += 1 i += 1
# return number of email domains # return number of email domains
return i return i
@ -118,6 +131,7 @@ def getDomainInfo(headers, verify, base_url):
sys.stderr.write(f"Request response code is {response.status_code} with URL {url}\n") sys.stderr.write(f"Request response code is {response.status_code} with URL {url}\n")
sys.exit(1) sys.exit(1)
def getMailboxInfo(headers, verify, base_url): def getMailboxInfo(headers, verify, base_url):
url = f"{base_url}/mailbox/all" url = f"{base_url}/mailbox/all"
response = requests.get(url, headers=headers, verify=verify) response = requests.get(url, headers=headers, verify=verify)
@ -128,11 +142,25 @@ def getMailboxInfo(headers, verify, base_url):
global_num_messages = 0 global_num_messages = 0
while i < len(data): while i < len(data):
#pprint(data[i]) #pprint(data[i])
# get status of mailbox (0-->inactive or 1-->active)
active = data[i].get("active")
# get username of mailbox # get username of mailbox
username = data[i].get("username") username = data[i].get("username")
# get friendly name of user
name = data[i].get("name")
# get number of messages in mailbox # get number of messages in mailbox
num_messages = data[i].get("messages") num_messages = data[i].get("messages")
#print(username) # get quota used in percent
percent_in_use = data[i].get("percent_in_use")
# get creation and last modification date
created = data[i].get("created")
modified = data[i].get("modified")
# get number of messages
messages = data[i].get("messages")
# get last login dates
last_imap_login = data[i].get("last_imap_login")
last_pop3_login = data[i].get("last_pop3_login")
last_smtp_login = data[i].get("last_smtp_login")
i += 1 i += 1
global_num_messages += num_messages global_num_messages += num_messages
# return number of mailboxes and global number of messages # return number of mailboxes and global number of messages
@ -141,6 +169,7 @@ def getMailboxInfo(headers, verify, base_url):
sys.stderr.write(f"Request response code is {response.status_code} with URL {url}\n") sys.stderr.write(f"Request response code is {response.status_code} with URL {url}\n")
sys.exit(1) sys.exit(1)
def getMailcowInfo(headers, verify, base_url): def getMailcowInfo(headers, verify, base_url):
url = f"{base_url}/status/version" url = f"{base_url}/status/version"
response = requests.get(url, headers=headers, verify=verify) response = requests.get(url, headers=headers, verify=verify)
@ -206,16 +235,12 @@ def main():
showOptions() showOptions()
print(f"hostname: {hostname}, protocol: {protocol}, port: {port}, verify: {verify}") print(f"hostname: {hostname}, protocol: {protocol}, port: {port}, verify: {verify}")
base_url = f"{protocol}://{hostname}:{port}/{mc_api_base}" base_url = f"{protocol}://{hostname}:{port}/{mc_api_base}"
print(base_url)
# get domain data # get domain data
num_domains = getDomainInfo(headers, verify, base_url) num_domains = getDomainInfo(headers, verify, base_url)
print(num_domains)
# get mailbox data # get mailbox data
num_mailboxes, num_global_messages = getMailboxInfo(headers, verify, base_url) num_mailboxes, num_global_messages = getMailboxInfo(headers, verify, base_url)
print(num_mailboxes, num_global_messages)
# get global Mailcow info # get global Mailcow info
mailcow_version = getMailcowInfo(headers, verify, base_url) mailcow_version = getMailcowInfo(headers, verify, base_url)
print(mailcow_version)
if __name__ == "__main__": if __name__ == "__main__":
main() main()