Skip to content

Instantly share code, notes, and snippets.

@lucindo
Last active February 16, 2016 01:09
Show Gist options
  • Save lucindo/15ccd0e87070c5d94cec to your computer and use it in GitHub Desktop.
Save lucindo/15ccd0e87070c5d94cec to your computer and use it in GitHub Desktop.
sends a report of running process on servers
import sys
import subprocess
import smtplib
from email.mime.text import MIMEText
from threading import Thread
FROM = ""
TO = ""
SMTP_SERVER = "localhost"
def check_process_on_server(server, process, results, idx):
command = "ps -ef | grep %s | grep -v grep" % process
results[idx] = "server %s\r\t%s\n" % (server, subprocess.check_output('ssh %s "%s"' % (server, command), shell=True))
def check_process(process, servers):
threads = [None] * len(servers)
results = [None] * len(servers)
t = 0
for server in servers:
threads[t] = Thread(target=check_process_on_server, args=(server, process, results, t))
threads[t].start()
t = t + 1
for i in range(len(threads)):
threads[i].join()
return "\n".join(results)
def send_report(report):
msg = MIMEText(report)
msg['Subject'] = 'The contents of %s' % textfile
msg['From'] = FROM
msg['To'] = TO
s = smtplib.SMTP(SMTP_SERVER)
s.sendmail(FROM, [TO], msg.as_string())
s.quit()
if __name__ == "__main__":
if len(sys.argv) < 3:
sys.exit("usage: %s <process> <host_list>" % sys.argv[0])
send_report(check_process(sys.argv[1], sys.argv[2:]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment