Skip to content

Commit

Permalink
New admin stats
Browse files Browse the repository at this point in the history
Added error line to error messages
  • Loading branch information
pdjohntony committed Apr 29, 2021
1 parent 72c690f commit 0e9bdd6
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 42 deletions.
6 changes: 3 additions & 3 deletions _version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__version__ = '1.0.3'
__build__ = '0004'
__build_date__ = '04-28-2021'
__version__ = '1.0.4'
__build__ = '0005'
__build_date__ = '04-29-2021'
47 changes: 41 additions & 6 deletions email_assets/admin_report_template.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,47 @@
<html>
<head>
<style>
/* double {{}} required due to python .format() */
table, th, td {{border: 1px solid black;border-collapse: collapse;padding: 4px;}}
</style>
</head>
<body>
<p>
Unity Connection PIN Reminder & Report Tool<br><br>

Total Mailboxes = {total_mailboxes}<br>
Total Emails Sent = {total_emails_sent}<br>
Total Errors Occured = {total_mailbox_errors}<br>
Tool Runtime = {time_total}<br><br>
<table>
<tr>
<td>Total Mailboxes</td>
<td>{total_mailboxes}</td>
</tr>
<tr>
<td>Mailboxes configured with PIN expiration</td>
<td>{mailboxes_with_exp_days}</td>
</tr>
<tr>
<td>Mailboxes configured without PIN expiration</td>
<td>{mailboxes_without_exp_days}</td>
</tr>
<tr>
<td>Mailboxes with expired PIN</td>
<td>{total_expired_pins}</td>
</tr>
<tr>
<td>PINs changed within 24 hours</td>
<td>{total_24hr_pin_changes}</td>
</tr>
<tr>
<td>User Reminder Emails Sent</td>
<td>{total_emails_sent}</td>
</tr>
<tr>
<td>Errors Occured</td>
<td>{total_mailbox_errors}</td>
</tr>
<tr>
<td>Tool Runtime</td>
<td>{time_total}</td>
</tr>
</table>
<br>

See attached report for more details<br><br>

Expand Down
14 changes: 8 additions & 6 deletions email_assets/admin_report_template.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
Unity Connection PIN Reminder & Report Tool

Total Mailboxes = {total_mailboxes}
Total Emails Sent = {total_emails_sent}
Total Errors Occured = {total_mailbox_errors}
Tool Runtime = {time_total}
Total Mailboxes {total_mailboxes}
Mailboxes configured with PIN expiration {mailboxes_with_exp_days}
Mailboxes configured without PIN expiration {mailboxes_without_exp_days}
Mailboxes with expired PIN {total_expired_pins}
PINs changed within 24 hours {total_24hr_pin_changes}
User Reminder Emails Sent {total_emails_sent}
Errors Occured {total_mailbox_errors}
Tool Runtime {time_total}

See attached report for more details

Expand Down
73 changes: 46 additions & 27 deletions pin-reminder.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def read_ini(cfg_file_name):

return cfg
except Exception as e:
logger.error(f"Error in {cfg_file_name} file: {e}")
logger.error(f"Error in {cfg_file_name} file: {e} on line {sys.exc_info()[2].tb_lineno}")
sys.exit(1)

def validate_ini(cfg_file_name):
Expand Down Expand Up @@ -145,7 +145,7 @@ def validate_ini(cfg_file_name):
logger.error(f"Error in config file: retention_days must be a number not a string")
sys.exit(1)
except Exception as e:
logger.error(f"Error in {cfg_file_name} file: {e}")
logger.error(f"Error in {cfg_file_name} file: {e} on line {sys.exc_info()[2].tb_lineno}")
sys.exit(1)

def init_logger(console_debug_lvl = '1'):
Expand Down Expand Up @@ -243,7 +243,7 @@ def get_auth_rules():
logger.debug(f"authrules = {authrules}")
return authrules
except Exception as e:
logger.error(f"Error: {e}")
logger.error(f"Error: {e} on line {sys.exc_info()[2].tb_lineno}")
send_admin_email_error()
sys.exit(1)

Expand Down Expand Up @@ -294,7 +294,7 @@ def get_mailboxes():
})
return mailboxes
except Exception as e:
logger.error(f"Error: {e}")
logger.error(f"Error: {e} on line {sys.exc_info()[2].tb_lineno}")
send_admin_email_error()
sys.exit(1)

Expand All @@ -310,6 +310,10 @@ def get_pin_data():
Returns:
mailboxes (dict)
"""
global mailboxes_with_exp_days
global mailboxes_without_exp_days
global total_expired_pins
global total_24hr_pin_changes
for m in tqdm(mailboxes):
try:
logger.debug(f"Mailbox Alias = {m['Alias']}")
Expand Down Expand Up @@ -339,17 +343,20 @@ def get_pin_data():
m["PIN Must Change"] = resp_json["CredMustChange"]
m["Date Last Changed"] = datetime.datetime.strptime(resp_json["TimeChanged"], "%Y-%m-%d %H:%M:%S.%f")
m["Expiration Date"] = m["Date Last Changed"] + datetime.timedelta(days=int(m["Expiration Days"]))
if m["Expiration Days"] == "0":
if m["Expiration Days"] == "0" or m["PIN Doesnt Expire"] == "true":
mailboxes_without_exp_days += 1
m["Days Until Expired"] = 0
else:
m["Days Until Expired"] = m["Expiration Date"] - today
m["Days Until Expired"] = m["Days Until Expired"].days
mailboxes_with_exp_days += 1
m["Days Until Expired"] = m["Expiration Date"] - today
m["Days Until Expired"] = m["Days Until Expired"].days
if m["Days Until Expired"] <= 0: total_expired_pins += 1
m["Date Last Changed"] = m["Date Last Changed"].date() # Convert datetime to date
m["Expiration Date"] = m["Expiration Date"].date() # Convert datetime to date
m["Expiration Email Sent"] = "false"

if (today.date() - m["Date Last Changed"]).days < 1: total_24hr_pin_changes += 1
except Exception as e:
logger.error(f"Error: {e}")
logger.error(f"Error: {e} on line {sys.exc_info()[2].tb_lineno}")
m["Auth Rule"] = "ERROR"
global total_mailbox_errors
total_mailbox_errors += 1
Expand Down Expand Up @@ -427,7 +434,7 @@ def send_user_email():
global total_user_emails_sent
total_user_emails_sent += 1
except Exception as e:
logger.error(f"Error: User email was not sent: {e}")
logger.error(f"Error: User email was not sent: {e} on line {sys.exc_info()[2].tb_lineno}")

return mailboxes

Expand All @@ -453,20 +460,28 @@ def send_admin_email():
text = open(cfg["admin_report_email_file_fqdn_txt"], "r")
text = text.read()
text = text.format(
total_mailboxes = total_mailboxes,
total_mailbox_errors = total_mailbox_errors,
total_emails_sent = total_user_emails_sent,
time_total = f"{time_total[0]} minutes {time_total[1]} seconds",
client_info = f"{hostname} / {ip_address}"
total_mailboxes = total_mailboxes,
mailboxes_with_exp_days = mailboxes_with_exp_days,
mailboxes_without_exp_days = mailboxes_without_exp_days,
total_expired_pins = total_expired_pins,
total_24hr_pin_changes = total_24hr_pin_changes,
total_mailbox_errors = total_mailbox_errors,
total_emails_sent = total_user_emails_sent,
time_total = f"{time_total[0]} minutes {time_total[1]} seconds",
client_info = f"{hostname} / {ip_address}"
)
html = open(cfg["admin_report_email_file_fqdn_html"], "r")
html = html.read()
html = html.format(
total_mailboxes = total_mailboxes,
total_mailbox_errors = total_mailbox_errors,
total_emails_sent = total_user_emails_sent,
time_total = f"{time_total[0]} minutes {time_total[1]} seconds",
client_info = f"{hostname} / {ip_address}"
total_mailboxes = total_mailboxes,
mailboxes_with_exp_days = mailboxes_with_exp_days,
mailboxes_without_exp_days = mailboxes_without_exp_days,
total_expired_pins = total_expired_pins,
total_24hr_pin_changes = total_24hr_pin_changes,
total_mailbox_errors = total_mailbox_errors,
total_emails_sent = total_user_emails_sent,
time_total = f"{time_total[0]} minutes {time_total[1]} seconds",
client_info = f"{hostname} / {ip_address}"
)

attachment_filename = report_filename # In same directory as script
Expand All @@ -493,7 +508,7 @@ def send_admin_email():
smtpObj.sendmail(sender, receivers, message.as_string())
logger.info(f"Admin email successfully sent to: {receivers}")
except Exception as e:
logger.error(f"Error: Admin email was not sent: {e}")
logger.error(f"Error: Admin email was not sent: {e} on line {sys.exc_info()[2].tb_lineno}")

def send_admin_email_error():
"""
Expand Down Expand Up @@ -540,7 +555,7 @@ def send_admin_email_error():
smtpObj.sendmail(sender, receivers, message.as_string())
logger.info(f"Admin error email successfully sent to: {receivers}")
except Exception as e:
logger.error(f"Error: Admin error email was not sent: {e}")
logger.error(f"Error: Admin error email was not sent: {e} on line {sys.exc_info()[2].tb_lineno}")

def generate_report():
"""
Expand Down Expand Up @@ -634,11 +649,15 @@ def purge_files(retention_days, file_dir, file_ext):
except IndexError as e:
rmode = None

today = datetime.datetime.today()
time_start = datetime.datetime.now()
total_mailboxes = 0
total_user_emails_sent = 0
total_mailbox_errors = 0
today = datetime.datetime.today()
time_start = datetime.datetime.now()
total_mailboxes = 0
mailboxes_with_exp_days = 0
mailboxes_without_exp_days = 0
total_expired_pins = 0
total_24hr_pin_changes = 0
total_user_emails_sent = 0
total_mailbox_errors = 0

# Initiate logger
logger = logging.getLogger('global-log')
Expand Down

0 comments on commit 0e9bdd6

Please sign in to comment.