Created
June 17, 2013 16:52
-
-
Save iMilnb/5798369 to your computer and use it in GitHub Desktop.
Human-readable smtp returner for Salt
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
''' | |
Return yaml-formatted salt data via email | |
The following fields can be set in the minion conf file: | |
smtp.from (required) | |
smtp.to (required) | |
smtp.host (required) | |
smtp.username (optional) | |
smtp.password (optional) | |
smtp.tls (optional, defaults to False) | |
smtp.subject (optional, but helpful) | |
smtp.fields (optional) | |
There are a few things to keep in mind: | |
* If a username is used, a password is also required. | |
* You should at least declare a subject, but you don't have to. | |
* smtp.fields lets you include the value(s) of various fields in the subject | |
line of the email. These are comma-delimited. For instance: | |
smtp.fields: id,fun | |
...will display the id of the minion and the name of the function in the | |
subject line. You may also use 'jid' (the job id), but it is generally | |
recommended not to use 'return', which contains the entire return data | |
structure (which can be very large). | |
''' | |
# Import python libs | |
import logging | |
import smtplib | |
import yaml | |
import re | |
log = logging.getLogger(__name__) | |
def __virtual__(): | |
return 'yaml_smtp' | |
def returner(ret): | |
''' | |
Send an email with the data | |
''' | |
from_addr = __salt__['config.option']('smtp.from') | |
to_addrs = __salt__['config.option']('smtp.to') | |
host = __salt__['config.option']('smtp.host') | |
user = __salt__['config.option']('smtp.username') | |
passwd = __salt__['config.option']('smtp.password') | |
subject = __salt__['config.option']('smtp.subject') | |
out = {} | |
status = 'success' | |
if isinstance(ret['return'], dict): | |
for state in ret['return'].keys(): | |
act = state.split('_|-') | |
out[act[1]] = {} | |
out[act[1]]['type'] = act[0] | |
for item in ['name', 'comment', 'result']: | |
out[act[1]][item] = ret['return'][state][item] | |
if ret['return'][state]['result'] is False: | |
status = 'failure' | |
else: | |
if isinstance(ret['return'], str) and re.match(r'^error', | |
ret['return'], flags=re.IGNORECASE): | |
status = 'failure' | |
out = ret['return'] | |
fields = __salt__['config.option']('smtp.fields').split(',') | |
for field in fields: | |
if field in ret.keys(): | |
subject += ' {0}'.format(ret[field]) | |
subject = '[{0}] {1}'.format(status, subject) | |
log.debug('subject') | |
message = ('From: {0}\r\n' | |
'To: {1}\r\n' | |
'Subject: {2}\r\n' | |
'\r\n' | |
'id: {3}\r\n' | |
'function: {4}\r\n' | |
'jid: {5}\r\n' | |
'{6}').format(from_addr, | |
to_addrs, | |
subject, | |
ret['id'], | |
ret['fun'], | |
ret['jid'], | |
yaml.dump(out, default_flow_style=False)) | |
server = smtplib.SMTP(host) | |
if __salt__['config.option']('smtp.tls') is True: | |
server.starttls() | |
if user and passwd: | |
server.login(user, passwd) | |
server.sendmail(from_addr, to_addrs, message) | |
server.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment