paulbsd-salt/states/_runners/process_minion_data.py
2021-09-25 16:08:30 +02:00

59 lines
1.7 KiB
Python
Executable File

#!/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 = "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