[python]Email alert when Tufin has a failed automated step

This is the enhanced code for sending customized mail when automation step fails in Tufin. When the Tufin triggers, it sends the ticket_info to the input stream. the code has to read from the input stream and parse the xml.

#!/opt/tufin/securitysuite/ps/python/bin/python3
import smtplib
from sys import stdin
from email.mime.text import MIMEText
from lxml import etree

lines = ""

try:
    lines = etree.tostring(etree.parse(stdin), pretty_print=True, encoding="utf-8").decode("utf-8")
except EOFError:
    pass
except KeyboardInterrupt:
    pass


SERVER = "obfuscated.obfuscated.com.sg"
FROM = "tufin-noreply@obfuscated.com.sg"
TO = "myself@obfuscated.com.sg"
CC = ""

SUBJECT = "Test - Tufin securechange auto step stuck"
TEXT = "Dear admin,\r\
This message is generated for your attention that a step in tufin secure change is stucked.\r \
Manual intervention by you is required.\r\
On next version this message will add the tufin ticket number.\r\
This is a test message please IGNORE.\r\
{} ".format(str(lines))

message = MIMEText(TEXT)
message["From"] = FROM
message["To"] = TO
message["Subject"] = SUBJECT
message["Cc"] = CC
server = smtplib.SMTP(SERVER)
server.sendmail(FROM, TO, message.as_string())
server.quit()
Advertisements
This entry was posted in General stuffs. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s