Created
March 4, 2019 15:48
-
-
Save kellyebler/c6cb54fbfdbff38485541fdc6bfb9690 to your computer and use it in GitHub Desktop.
Finds Code42 users expiring in the same month. Runs as a cron job
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python3 | |
# Import Modules | |
import os | |
import json | |
from datetime import datetime | |
import requests | |
from jinja2 import Environment, FileSystemLoader | |
import smtplib | |
# Credentials as variables | |
username = os.environ.get('C42_USER') | |
password = os.environ.get('C42_PW') | |
C42url = os.environ.get('C42url') # Full URL, including https:// + :port + /api/ColdStorage? and whatever org you want to query | |
# Create request | |
r = requests.get((C42url), auth=(username, password)) | |
data = r.json() # Convert API response to JSON | |
userlist = data.get("data").get("coldStorageRows") # Strip JSON element | |
''' | |
Loop through users and see if archiveHoldExpireDate is this month and archiveType is Backup. | |
If so, print sourceUserUserName, sourceUserComputerName, archiveHoldExpireDate, | |
''' | |
format_str = '%Y-%m-%dT%H:%M:%S.%fZ' # Format archiveHoldExpireDate as datetime | |
expiring_users = [ | |
{ | |
"username":user.get("sourceUserUserName"), | |
"computername":user.get('sourceComputerName'), | |
"expiredate": datetime.strftime( | |
datetime.strptime(user.get('archiveHoldExpireDate'), | |
format_str), "%b %d %Y %H:%M:%S") | |
} | |
for user in userlist if datetime.strptime(user.get('archiveHoldExpireDate'), format_str).month == datetime.now().month | |
] | |
# Make email | |
jinja = Environment( | |
loader=FileSystemLoader("/etc/cron.monthly/cold-storage-notifications"), trim_blocks=True, lstrip_blocks=True, | |
) | |
template = jinja.get_template('template.html') | |
# Send email to aus-it@rmn.com | |
email_content = template.render(userlist=expiring_users) | |
def sendMail(): | |
sendmail_location = "/usr/sbin/sendmail" # sendmail location | |
p = os.popen("%s -t" % sendmail_location, "w") | |
p.write("From: %s\n" % "rmn-account-automator@rmn.com") | |
p.write("To: %s\n" % "aus-it@rmn.com") | |
p.write("Subject: Code42 Users Expiring This Month\n") | |
p.write("Content-Type: text/html\n") | |
p.write("\n") # blank line separating headers from body | |
p.write(email_content) | |
status = p.close() | |
# if status != 0: | |
# print("Sendmail exit status", status) | |
sendMail() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment