Skip to content

Instantly share code, notes, and snippets.

@deeso
Last active August 29, 2015 14:19
Show Gist options
  • Save deeso/6b032285e2820bff44fa to your computer and use it in GitHub Desktop.
Save deeso/6b032285e2820bff44fa to your computer and use it in GitHub Desktop.
This script will ssh to a client and execute a command, and then proceed to memory snapshots using the "virsh" command.
import sys, libvirt, paramiko, subprocess, time, os, threading, select
DATE_FORMAT = "%Y-%m-%d_%H.%M.%S"
# ah the beauty of NFS and circular redirects
# your FS is my FS is the NFS!
BASE_DIR = "/srv/nfs/cortana/logs/"
CMD_DIR = "cmd/"
OUTPUT_DIR = "output"
DATA_DIR = "data"
DUMPS_DIR = "ssldumps"
DEF_HOSTLIST = "hostlist_all_medium.txt"
JAR = "tls-client-0.0.1-SNAPSHOT-jar-with-dependencies.jar"
SLEEP = 1000 # in miliseconds
ITERATIONS = 50
PREPEND = "java_base"
PAUSE_BETWEEN_DUMPS = 25 # in seconds
from datetime import datetime
def time_str():
return str(datetime.now().strftime("%H:%M:%S.%f %m-%d-%Y"))
DUMP = """virsh dump {client} {dumploc} --bypass-cache""" +\
""" --live --verbose --memory-only"""
TLS_TEST = """java -jar {jar} {hostlist} {outputdir} {sleep} {iter} {prepend}"""
DUMP_RAM = True
def ssh_to_target (hostname, username, password):
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname, username=username, password=password)
return client
def dumping_ram(client, dumploc_base, sleep=60):
global DUMP_RAM
num = 0
# let the command execute quickly?
time.sleep(1)
fmt = os.path.join(dumploc_base, "{iter:04d}_{client}_{timedate:08x}.dump")
while DUMP_RAM:
print ("[==] Dumping memory")
keyed = {"timedate":int(time.time()),
"iter":num, "client":client}
dumploc = fmt.format(**keyed)
dump_memory_function (client, dumploc)
print ("[==] Memory dump completed, sleeping for %ds"%(sleep))
if not DUMP_RAM:
break
time.sleep(sleep)
num += 1
time.sleep(60)
print ("[==] Dumping final memory")
keyed = {"timedate":int(time.time()),
"iter":num, "client":client}
dumploc = fmt.format(**keyed)
dump_memory_function (client, dumploc)
print ("[==] Dumping ram completed function")
def dump_memory_function (client, dumploc):
keyed = {"client":client, "dumploc":dumploc}
cmd = DUMP.format(**keyed)
#print ("Executing: %s"%(str(cmd.split())))
p = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
content = p.communicate()[0].decode('ascii', 'replace')
content = content.replace('\x1b[0K\n', '').replace('\x1b[0K\r', '')
return content
def get_target_cmd(jar, hostlist, outputdir, sleep, iterations, prepend):
keyed = {"jar":jar, "hostlist":hostlist, "outputdir":outputdir,
"sleep":sleep, "iter":iterations, "prepend":prepend }
return TLS_TEST.format(**keyed)
jar = os.path.join(BASE_DIR, CMD_DIR, JAR)
outputdir = os.path.join(BASE_DIR, OUTPUT_DIR)
dumpsdir = os.path.join(BASE_DIR, DUMPS_DIR)
hostlist = os.path.join(BASE_DIR, DATA_DIR, DEF_HOSTLIST)
vmname = "java_base"
prepend = PREPEND
sleep = SLEEP
iterations = ITERATIONS
start_time = time_str()
print ("%s Starting experiments"%(start_time))
# todo the host list. {outputdir} {sleep} {iter} {prepend}
cmd = get_target_cmd(jar, hostlist, outputdir, sleep, iterations, prepend)
client = ssh_to_target("10.16.121.122", username="java", password="java")
chan = client.get_transport().open_session()
t = threading.Thread(target=dumping_ram, args=(vmname, dumpsdir, PAUSE_BETWEEN_DUMPS))
t.start()
chan.exec_command(cmd)
data = ''
while True:
rl, wl, xl = select.select([chan],[],[],0.0)
if len(rl) > 0:
# Must be stdout
tmp = chan.recv(1024)
if tmp == -1 or len(tmp) == 0:
break
data = data + tmp
if data.find("=====DONE=====") > -1:
break
else:
time.sleep(2)
DUMP_RAM = False
t.join()
end_time = time_str()
print ("Experiment completed, perform your analysis")
print ("%s Started experiments"%(start_time))
print ("%s Completed experiments"%(end_time))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment