2 # SPDX-License-Identifier: BSD-3-Clause
3 # Copyright(c) 2018 Intel Corporation
5 from __future__ import print_function
6 from __future__ import unicode_literals
15 METRICS_REQ = "{\"action\":0,\"command\":\"ports_all_stat_values\",\"data\":null}"
16 API_REG = "{\"action\":1,\"command\":\"clients\",\"data\":{\"client_path\":\""
17 API_UNREG = "{\"action\":2,\"command\":\"clients\",\"data\":{\"client_path\":\""
18 GLOBAL_METRICS_REQ = "{\"action\":0,\"command\":\"global_stat_values\",\"data\":null}"
19 DEFAULT_FP = "/var/run/dpdk/default_client"
24 raw_input = input # Python 3
26 if sys.version_info.major < 3:
27 print("WARNING: Python 2 is deprecated for use in DPDK, and will not work in future releases.", file=sys.stderr)
28 print("Please use Python 3 instead", file=sys.stderr)
33 self.send_fd = socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET)
34 self.recv_fd = socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET)
41 self.client_fd.close()
43 print("Error - Sockets could not be closed")
47 def __init__(self): # Creates a client instance
48 self.socket = Socket()
55 if self.unregistered == 0:
58 print("Error - Client could not be destroyed")
60 def getFilepath(self, file_path): # Gets arguments from Command-Line and assigns to instance of client
61 self.file_path = file_path
63 def register(self): # Connects a client to DPDK-instance
64 if os.path.exists(self.file_path):
65 os.unlink(self.file_path)
67 self.socket.recv_fd.bind(self.file_path)
68 except socket.error as msg:
69 print ("Error - Socket binding error: " + str(msg) + "\n")
70 self.socket.recv_fd.settimeout(2)
71 self.socket.send_fd.connect("/var/run/dpdk/rte/telemetry")
72 JSON = (API_REG + self.file_path + "\"}}")
73 self.socket.send_fd.sendall(JSON.encode())
75 self.socket.recv_fd.listen(1)
76 self.socket.client_fd = self.socket.recv_fd.accept()[0]
78 def unregister(self): # Unregister a given client
79 self.socket.client_fd.send((API_UNREG + self.file_path + "\"}}").encode())
80 self.socket.client_fd.close()
82 def requestMetrics(self): # Requests metrics for given client
83 self.socket.client_fd.send(METRICS_REQ.encode())
84 data = self.socket.client_fd.recv(BUFFER_SIZE).decode()
85 print("\nResponse: \n", data)
87 def repeatedlyRequestMetrics(self, sleep_time): # Recursively requests metrics for given client
88 print("\nPlease enter the number of times you'd like to continuously request Metrics:")
89 n_requests = int(raw_input("\n:"))
90 print("\033[F") #Removes the user input from screen, cleans it up
92 for i in range(n_requests):
94 time.sleep(sleep_time)
96 def requestGlobalMetrics(self): #Requests global metrics for given client
97 self.socket.client_fd.send(GLOBAL_METRICS_REQ.encode())
98 data = self.socket.client_fd.recv(BUFFER_SIZE).decode()
99 print("\nResponse: \n", data)
101 def interactiveMenu(self, sleep_time): # Creates Interactive menu within the script
102 while self.choice != 4:
103 print("\nOptions Menu")
104 print("[1] Send for Metrics for all ports")
105 print("[2] Send for Metrics for all ports recursively")
106 print("[3] Send for global Metrics")
107 print("[4] Unregister client")
110 self.choice = int(raw_input("\n:"))
111 print("\033[F") #Removes the user input for screen, cleans it up
114 self.requestMetrics()
115 elif self.choice == 2:
116 self.repeatedlyRequestMetrics(sleep_time)
117 elif self.choice == 3:
118 self.requestGlobalMetrics()
119 elif self.choice == 4:
121 self.unregistered = 1
123 print("Error - Invalid request choice")
127 if __name__ == "__main__":
131 if (len(sys.argv) == 2):
132 file_path = sys.argv[1]
134 print("Warning - No filepath passed, using default (" + DEFAULT_FP + ").")
135 file_path = DEFAULT_FP
137 client.getFilepath(file_path)
139 client.interactiveMenu(sleep_time)