1 # Copyright (c) 2022 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
14 """Telemetry utility."""
16 from resources.libraries.python.model.ExportResult import append_telemetry
17 from resources.libraries.python.Constants import Constants
18 from resources.libraries.python.ssh import exec_cmd_no_error
19 from resources.libraries.python.topology import NodeType
21 __all__ = ["TelemetryUtil"]
25 """Class contains methods for telemetry utility."""
29 node, profile, sid=None, spath=None, rate="", export=False):
30 """Get telemetry read on node.
32 :param node: Node in the topology.
33 :param profile: Telemetry configuration profile.
34 :param sid: Socket ID used to describe recipient side of socket.
35 :param spath: Socket path.
36 :param rate: Telemetry load, unique within the test (optional).
37 :param export: If false, do not attempt JSON export (default false).
46 config += f"{Constants.REMOTE_FW_DIR}/"
47 config += f"{Constants.RESOURCES_TPL_TELEMETRY}/"
48 config += f"{profile}"
51 cd_cmd += f"sh -c \"cd {Constants.REMOTE_FW_DIR}/"
52 cd_cmd += f"{Constants.RESOURCES_TOOLS}"
55 bin_cmd = f"python3 -m telemetry --config {config} --hook {spath}\""
57 bin_cmd = f"python3 -m telemetry --config {config}\""
58 exec_cmd_no_error(node, f"{cd_cmd} && {bin_cmd}", sudo=True)
63 hostname = exec_cmd_no_error(node, "hostname")[0].strip()
64 stdout, _ = exec_cmd_no_error(
65 node, "cat /tmp/metric.prom", sudo=True, log_stdout_err=False
68 prefix += f"hostname=\"{hostname}\","
70 prefix += f"hook=\"{sid}\","
71 prefix += f"rate=\"{rate}\","
72 for line in stdout.splitlines():
73 if line and not line.startswith("#"):
75 prefix.join(line.rsplit("{", 1)).replace("\"", "'")
78 def run_telemetry_on_all_duts(self, nodes, profile, rate="", export=False):
79 """Get telemetry read on all DUTs.
81 :param nodes: Nodes in the topology.
82 :param profile: Telemetry configuration profile.
83 :param rate: Telemetry load, unique within the test (optional).
84 :param export: If false, do not attempt JSON export (default false).
90 for node in nodes.values():
91 if node["type"] == NodeType.DUT:
93 for sid, spath in node["sockets"]["CLI"].items():
95 node, profile=profile, sid=sid, spath=spath,
96 rate=rate, export=export