Last active
September 29, 2020 22:36
-
-
Save pcuzner/fb2c3c0a9afe70890a55c5244760b511 to your computer and use it in GitHub Desktop.
test script to understand the elapsed time of a remoto (ssh) call for a basic cephadm -h command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import os | |
import json | |
# 1. ensure your host has python3-remoto installed | |
import remoto | |
# 2. Pick the location of the cephadm 'binary' to ship to the target | |
# cephadm_path = '/home/paul/git/ceph/src/cephadm/cephadm' | |
# cephadm_path = '/usr/sbin/cephadm' | |
cephadm_path = '/root/bin/cephadm' | |
# 3. define the user account to use that has passwordless ssh | |
ssh_user = 'root' | |
# 4. define the target host | |
target_host = 'rhs-srv-02' | |
def profile_it(f): | |
def timer(*args, **kwargs): | |
stime = datetime.datetime.now() | |
print(f.__name__ + " starting @ " + str(stime)) | |
result = f(*args, **kwargs) | |
etime = datetime.datetime.now() | |
elapsed = etime - stime | |
print(f.__name__ + " ended @ " + str(etime)) | |
print(f.__name__ + " elapsed time: " + str(elapsed)) | |
return result | |
return timer | |
class RemoteExec: | |
def __init__(self, user, host, command='', args=''): | |
self.user = user | |
self.target = host | |
self.cmd = command | |
self.cmd_args = args | |
self.script = self._load_script() | |
@profile_it | |
def _load_script(self): | |
with open(cephadm_path, 'r') as f: | |
data = f.read() | |
return data | |
@profile_it | |
def _get_connection(self): | |
con = remoto.Connection( | |
f'{self.user}@{self.target}', | |
sudo=False | |
) | |
return con | |
@profile_it | |
def exec(self, con): | |
script = "injected_argv = " + json.dumps(['-h']) + "\n" | |
script += self.script | |
out, err, code = remoto.process.check( | |
con, | |
['/usr/bin/python3', '-u'], | |
stdin=script.encode('utf-8') | |
) | |
return out, err, code | |
def main(): | |
r = RemoteExec(ssh_user, target_host, args='-h') | |
stime = datetime.datetime.now() | |
con = r._get_connection() | |
out, err, code = r.exec(con) | |
etime = datetime.datetime.now() | |
elapsed = etime - stime | |
if not code: | |
lines = '' | |
for line in out: | |
lines += line + '\n' | |
print(lines) | |
print("\n") | |
print(f"elapsed time to connect and execute a cephadm -h : {str(elapsed)}") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment