Revert "fix(IPsecUtil): Delete keywords no longer used"
[csit.git] / resources / libraries / python / TelemetryUtil.py
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:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
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.
13
14 """Telemetry utility."""
15
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
20
21 __all__ = ["TelemetryUtil"]
22
23
24 class TelemetryUtil:
25     """Class contains methods for telemetry utility."""
26
27     @staticmethod
28     def _run_telemetry(
29             node, profile, sid=None, spath=None, rate="", export=False):
30         """Get telemetry read on node.
31
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).
38         :type node: dict
39         :type profile: str
40         :type sid: str
41         :type spath: str
42         :type rate: str
43         :type export: bool
44         """
45         config = ""
46         config += f"{Constants.REMOTE_FW_DIR}/"
47         config += f"{Constants.RESOURCES_TPL_TELEMETRY}/"
48         config += f"{profile}"
49
50         cd_cmd = ""
51         cd_cmd += f"sh -c \"cd {Constants.REMOTE_FW_DIR}/"
52         cd_cmd += f"{Constants.RESOURCES_TOOLS}"
53
54         if spath:
55             bin_cmd = f"python3 -m telemetry --config {config} --hook {spath}\""
56         else:
57             bin_cmd = f"python3 -m telemetry --config {config}\""
58         exec_cmd_no_error(node, f"{cd_cmd} && {bin_cmd}", sudo=True)
59
60         if not export:
61             return
62
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
66         )
67         prefix = "{"
68         prefix += f"hostname=\"{hostname}\","
69         if sid:
70             prefix += f"hook=\"{sid}\","
71         prefix += f"rate=\"{rate}\","
72         for line in stdout.splitlines():
73             if line and not line.startswith("#"):
74                 append_telemetry(
75                     prefix.join(line.rsplit("{", 1)).replace("\"", "'")
76                 )
77
78     def run_telemetry_on_all_duts(self, nodes, profile, rate="", export=False):
79         """Get telemetry read on all DUTs.
80
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).
85         :type nodes: dict
86         :type profile: str
87         :type rate: str
88         :type export: bool
89         """
90         for node in nodes.values():
91             if node["type"] == NodeType.DUT:
92                 try:
93                     for sid, spath in node["sockets"]["CLI"].items():
94                         self._run_telemetry(
95                             node, profile=profile, sid=sid, spath=spath,
96                             rate=rate, export=export
97                         )
98                 except IndexError:
99                     pass