in kif.py [0:0]
def run_actions(config, actions, debug=False):
goods = 0
bads = 0
triggered_total = 0
email_triggers = ""
email_actions = ""
for action in actions:
triggered_total += 1
print("Following triggers were detected:")
print(f"- {action['trigger']}")
if action.get('notify', 'email') in [None, 'email']:
email_triggers += f"- {action['trigger']}\n"
print("Running triggered commands:")
rloutput = ""
for item in action['runlist']:
print(f"- {item}")
rloutput += f"- {item}"
if action.get('notify', 'email') in [None, 'email']:
email_actions += f"- {item}"
try:
if not debug:
subprocess.check_output(item, shell=True, stderr=subprocess.STDOUT)
rloutput += " (success)"
if action.get('notify', 'email') in [None, 'email']:
email_actions += " (success)"
else:
print("(disabled due to --debug flag)")
rloutput += " (disabled due to --debug)"
if action.get('notify', 'email') in [None, 'email']:
email_actions += " (disabled due to --debug)"
goods += 1
except subprocess.CalledProcessError as e:
print(f"command failed: {e.output}")
rloutput += f" (failed!: {e.output})"
if action.get('notify', 'email') in [None, 'email']:
email_actions += f" (failed!: {e.output})"
bads += 1
rloutput += "\n"
if action.get('notify', 'email') in [None, 'email']:
email_actions += "\n"
for pid, sig in action['kills'].items():
print(f"- KILL PID {pid} with sig {sig}")
rloutput += f"- KILL PID {pid} with sig {sig}"
if action.get('notify', 'email') in [None, 'email']:
email_actions += f"- KILL PID {pid} with sig {sig}"
if not debug:
try:
os.kill(pid, sig)
except OSError:
email_actions += "(failed, no such process!)"
else:
print(" (disabled due to --debug flag)")
rloutput += " (disabled due to --debug flag)"
if action.get('notify', 'email') in [None, 'email']:
email_actions += " (disabled due to --debug flag)"
rloutput += "\n"
if action.get('notify', 'email') in [None, 'email']:
email_actions += "\n"
goods += 1
print(f"{goods} calls succeeded, {bads} failed.")
if email_actions and 'notifications' in config and 'email' in config['notifications']:
ecfg = config['notifications']['email']
if 'rcpt' in ecfg and 'from' in ecfg and not debug:
subject = f"[KIF] events triggered on {ME}"
msg = TEMPLATE_EMAIL.format(ME=who_am_i(), triggers=email_triggers, actions=email_actions)
asfpy.messaging.mail(sender=ecfg['from'], recipient=ecfg['rcpt'], subject=subject, message=msg)