#!/usr/bin/python3 import sys import subprocess import json from salt.modules.smtp import send_msg ''' For use with salt reactor ''' def email_errors(fromaddr, toaddrs, subject, data_str, smtp_server): data = eval(data_str) error = False changes = False if type(data['return']) is dict: for _, result in data['return'].items(): if not result['result']: error = True break if result['changes']: changes = True break else: if "success" in data.keys(): if not data['success']: error = True if error or changes: js = subprocess.check_output(["salt-run", "--out=json", "jobs.lookup_jid", data['jid']]) body = "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) 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) send_msg(recipient=toaddrs, message=data, subject=subject, sender=fromaddr, server=smtp_server, use_ssl=False) return True