2020-09-30 00:44:06 +02:00
|
|
|
#!/usr/bin/python3
|
2020-07-10 00:58:55 +02:00
|
|
|
|
2020-09-30 00:44:06 +02:00
|
|
|
import sys
|
2020-07-10 00:58:55 +02:00
|
|
|
import subprocess
|
|
|
|
import json
|
2020-09-30 00:44:06 +02:00
|
|
|
import salt.modules.smtp
|
2020-07-10 00:58:55 +02:00
|
|
|
|
2021-01-17 17:44:58 +01:00
|
|
|
|
2020-07-10 00:58:55 +02:00
|
|
|
'''
|
2021-01-17 17:44:58 +01:00
|
|
|
For use with salt reactor
|
2020-07-10 00:58:55 +02:00
|
|
|
'''
|
|
|
|
|
2021-01-17 17:44:58 +01:00
|
|
|
|
2020-07-10 00:58:55 +02:00
|
|
|
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:
|
2020-09-30 00:44:06 +02:00
|
|
|
for _, result in data['return'].iteritems():
|
2020-07-10 00:58:55 +02:00
|
|
|
if not result['result']:
|
|
|
|
error = True
|
|
|
|
break
|
|
|
|
if result['changes']:
|
|
|
|
changes = True
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
if not data['success']:
|
|
|
|
error = True
|
2020-09-30 00:44:06 +02:00
|
|
|
except KeyError:
|
|
|
|
sys.exit()
|
2020-07-10 00:58:55 +02:00
|
|
|
|
2020-09-30 00:44:06 +02:00
|
|
|
if error or changes:
|
2021-01-17 17:44:58 +01:00
|
|
|
js = subprocess.check_output(["salt-run", "--out=json",
|
|
|
|
"jobs.lookup_jid", data['jid']])
|
2021-09-25 16:08:25 +02:00
|
|
|
body = "JobId is data['jid']\n"
|
2021-01-17 17:44:58 +01:00
|
|
|
outdata = json.loads(js)
|
2020-07-10 00:58:55 +02:00
|
|
|
nodename = outdata.keys()[0]
|
|
|
|
for i in outdata[nodename]:
|
|
|
|
if not outdata[nodename][i]["result"]:
|
2020-09-30 00:44:06 +02:00
|
|
|
name = outdata[nodename][i]["name"]
|
2020-07-10 00:58:55 +02:00
|
|
|
comment = outdata[nodename][i]["comment"].rstrip('\n')
|
|
|
|
data = "%s- %s / %s\n" % (body, name, comment)
|
2020-09-30 00:44:06 +02:00
|
|
|
salt.modules.smtp.send_msg(recipient=toaddrs, message=data,
|
|
|
|
subject=subject, sender=fromaddr,
|
|
|
|
server=smtp_server, use_ssl=False)
|
2021-01-17 17:44:58 +01:00
|
|
|
|
2020-07-10 00:58:55 +02:00
|
|
|
return True
|
|
|
|
|
2021-01-17 17:44:58 +01:00
|
|
|
|
2020-07-10 00:58:55 +02:00
|
|
|
def email_auth(fromaddr, toaddrs, subject, data_str, smtp_server):
|
|
|
|
data = eval(data_str)
|
2020-09-30 00:44:06 +02:00
|
|
|
salt.modules.smtp.send_msg(recipient=toaddrs, message=data,
|
|
|
|
subject=subject, sender=fromaddr,
|
|
|
|
server=smtp_server, use_ssl=False)
|
2020-07-10 00:58:55 +02:00
|
|
|
|
2020-09-30 00:44:06 +02:00
|
|
|
return True
|