#!/usr/pkg/bin/python2 #-*- coding: utf-8 -*- import os import subprocess import salt.modules.smtp import json ''' 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 state, 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 as e: exit() #if error or changes: if error: js = subprocess.check_output(["salt-run", "--out=json", "jobs.lookup_jid", data['jid']]) body = "JobId is %s\n" % (data['jid']) 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