updated python scripts

This commit is contained in:
Paul 2021-11-14 13:35:25 +01:00
parent 351dab96dc
commit cd4e14a8dd
2 changed files with 24 additions and 19 deletions

View File

@ -48,6 +48,7 @@ def set_config(url, verify, apikey, config):
req = Request(method="POST",
url=fullurl,
data=json.dumps(config).encode())
req.add_header("Content-Type", "application/json")
req.add_header("X-API-Key", apikey)
try:
res = urlopen(req, context=get_context(verify))

View File

@ -3,7 +3,7 @@
import sys
import subprocess
import json
import salt.modules.smtp
from salt.modules.smtp import send_msg
'''
@ -16,20 +16,18 @@ def email_errors(fromaddr, toaddrs, subject, data_str, smtp_server):
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 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
except KeyError:
sys.exit()
if error or changes:
js = subprocess.check_output(["salt-run", "--out=json",
@ -42,17 +40,23 @@ def email_errors(fromaddr, toaddrs, subject, data_str, smtp_server):
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)
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)
send_msg(recipient=toaddrs,
message=data,
subject=subject,
sender=fromaddr,
server=smtp_server,
use_ssl=False)
return True