#!/usr/bin/python3 import sys import subprocess import json import salt.modules.smtp ''' For use with salt reactor ''' def email_errors(fromaddr, toaddrs, subject, data_str, smtp_server): data = eval(data_str) error = False changes = False try: if type(data['return']) is dict: for _, result in data['return'].iteritems(): if not result['result']: error = True break if result['changes']: changes = True break else: if not data['success']: error = True except KeyError: sys.exit() if error or changes: js = subprocess.check_output(["salt-run", "--out=json", "jobs.lookup_jid", data['jid']]) body = f"JobId is data['jid']\n" outdata = json.loads(js) nodename = outdata.keys()[0] for i in outdata[nodename]: if not outdata[nodename][i]["result"]: name = outdata[nodename][i]["name"] comment = outdata[nodename][i]["comment"].rstrip('\n') data = "%s- %s / %s\n" % (body, name, comment) salt.modules.smtp.send_msg(recipient=toaddrs, message=data, subject=subject, sender=fromaddr, server=smtp_server, use_ssl=False) return True def email_auth(fromaddr, toaddrs, subject, data_str, smtp_server): data = eval(data_str) salt.modules.smtp.send_msg(recipient=toaddrs, message=data, subject=subject, sender=fromaddr, server=smtp_server, use_ssl=False) return True