From 9b7a1a0ad6dda662fa82dedbeeaf71f7ac625e31 Mon Sep 17 00:00:00 2001 From: pmikus Date: Tue, 6 Jul 2021 12:31:26 +0000 Subject: [PATCH] Telemetry: Add node info Signed-off-by: pmikus Change-Id: I428bf2628f5d6b87d335de415a82d6e94867d8c0 --- resources/libraries/python/TelemetryUtil.py | 15 +++++++++++++-- resources/libraries/python/ssh.py | 23 ++++++++++++++++++----- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/resources/libraries/python/TelemetryUtil.py b/resources/libraries/python/TelemetryUtil.py index 3031647915..be2f4796b2 100644 --- a/resources/libraries/python/TelemetryUtil.py +++ b/resources/libraries/python/TelemetryUtil.py @@ -11,7 +11,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""Linux perf utility.""" +"""Telemetry utility.""" + +from robot.api import logger from resources.libraries.python.Constants import Constants from resources.libraries.python.OptionString import OptionString @@ -104,9 +106,18 @@ class TelemetryUtil: cd_cmd += f"{Constants.RESOURCES_TOOLS}" bin_cmd = f"python3 -m telemetry --config {config} --hook {hook}\"" + hostname = node[u"host"] exec_cmd_no_error(node, f"{cd_cmd} && {bin_cmd}", sudo=True) - exec_cmd_no_error(node, f"cat /tmp/metric.prom", sudo=True) + stdout, _ = exec_cmd_no_error( + node, u"cat /tmp/metric.prom", sudo=True, log_stdout_err=False + ) + logger.info( + u"# TYPE target info\n" + u"# HELP target Target metadata\n" + f"target_info{{hostname=\"{hostname}\",hook=\"{hook}\"}} 1\n" + f"{stdout}" + ) @staticmethod def run_telemetry_on_all_duts(nodes, profile): diff --git a/resources/libraries/python/ssh.py b/resources/libraries/python/ssh.py index 476cb62ad9..6f6fc557f4 100644 --- a/resources/libraries/python/ssh.py +++ b/resources/libraries/python/ssh.py @@ -410,7 +410,9 @@ class SSH: logger.trace(f"SCP took {end-start} seconds") -def exec_cmd(node, cmd, timeout=600, sudo=False, disconnect=False): +def exec_cmd( + node, cmd, timeout=600, sudo=False, disconnect=False, + log_stdout_err=True): """Convenience function to ssh/exec/return rc, out & err. Returns (rc, stdout, stderr). @@ -420,11 +422,15 @@ def exec_cmd(node, cmd, timeout=600, sudo=False, disconnect=False): :param timeout: Timeout value in seconds. Default: 600. :param sudo: Sudo privilege execution flag. Default: False. :param disconnect: Close the opened SSH connection if True. + :param log_stdout_err: If True, stdout and stderr are logged. stdout + and stderr are logged also if the return code is not zero + independently of the value of log_stdout_err. :type node: dict :type cmd: str or OptionString :type timeout: int :type sudo: bool :type disconnect: bool + :type log_stdout_err: bool :returns: RC, Stdout, Stderr. :rtype: tuple(int, str, str) """ @@ -445,10 +451,12 @@ def exec_cmd(node, cmd, timeout=600, sudo=False, disconnect=False): try: if not sudo: - ret_code, stdout, stderr = ssh.exec_command(cmd, timeout=timeout) + ret_code, stdout, stderr = ssh.exec_command( + cmd, timeout=timeout, log_stdout_err=log_stdout_err + ) else: ret_code, stdout, stderr = ssh.exec_command_sudo( - cmd, timeout=timeout + cmd, timeout=timeout, log_stdout_err=log_stdout_err ) except SSHException as err: logger.error(repr(err)) @@ -462,7 +470,7 @@ def exec_cmd(node, cmd, timeout=600, sudo=False, disconnect=False): def exec_cmd_no_error( node, cmd, timeout=600, sudo=False, message=None, disconnect=False, - retries=0, include_reason=False): + retries=0, include_reason=False, log_stdout_err=True): """Convenience function to ssh/exec/return out & err. Verifies that return code is zero. @@ -478,6 +486,9 @@ def exec_cmd_no_error( :param disconnect: Close the opened SSH connection if True. :param retries: How many times to retry on failure. :param include_reason: Whether default info should be appended to message. + :param log_stdout_err: If True, stdout and stderr are logged. stdout + and stderr are logged also if the return code is not zero + independently of the value of log_stdout_err. :type node: dict :type cmd: str or OptionString :type timeout: int @@ -486,13 +497,15 @@ def exec_cmd_no_error( :type disconnect: bool :type retries: int :type include_reason: bool + :type log_stdout_err: bool :returns: Stdout, Stderr. :rtype: tuple(str, str) :raises RuntimeError: If bash return code is not 0. """ for _ in range(retries + 1): ret_code, stdout, stderr = exec_cmd( - node, cmd, timeout=timeout, sudo=sudo, disconnect=disconnect + node, cmd, timeout=timeout, sudo=sudo, disconnect=disconnect, + log_stdout_err=log_stdout_err ) if ret_code == 0: break -- 2.16.6