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
29 self.send_fd = socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET)
30 self.recv_fd = socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET)
37 self.client_fd.close()
39 print("Error - Sockets could not be closed")
43 def __init__(self): # Creates a client instance
44 self.socket = Socket()
51 if self.unregistered == 0:
54 print("Error - Client could not be destroyed")
56 def getFilepath(self, file_path): # Gets arguments from Command-Line and assigns to instance of client
57 self.file_path = file_path
59 def register(self): # Connects a client to DPDK-instance
60 if os.path.exists(self.file_path):
61 os.unlink(self.file_path)
63 self.socket.recv_fd.bind(self.file_path)
64 except socket.error as msg:
65 print ("Error - Socket binding error: " + str(msg) + "\n")
66 self.socket.recv_fd.settimeout(2)
67 self.socket.send_fd.connect("/var/run/dpdk/rte/telemetry")
68 JSON = (API_REG + self.file_path + "\"}}")
69 self.socket.send_fd.sendall(JSON.encode())
71 self.socket.recv_fd.listen(1)
72 self.socket.client_fd = self.socket.recv_fd.accept()[0]
74 def unregister(self): # Unregister a given client
75 self.socket.client_fd.send((API_UNREG + self.file_path + "\"}}").encode())
76 self.socket.client_fd.close()
78 def requestMetrics(self): # Requests metrics for given client
79 self.socket.client_fd.send(METRICS_REQ.encode())
80 data = self.socket.client_fd.recv(BUFFER_SIZE).decode()
81 print("\nResponse: \n", data)
83 def repeatedlyRequestMetrics(self, sleep_time): # Recursively requests metrics for given client
84 print("\nPlease enter the number of times you'd like to continuously request Metrics:")
85 n_requests = int(raw_input("\n:"))
86 print("\033[F") #Removes the user input from screen, cleans it up
88 for i in range(n_requests):
90 time.sleep(sleep_time)
92 def requestGlobalMetrics(self): #Requests global metrics for given client
93 self.socket.client_fd.send(GLOBAL_METRICS_REQ.encode())
94 data = self.socket.client_fd.recv(BUFFER_SIZE).decode()
95 print("\nResponse: \n", data)
97 def interactiveMenu(self, sleep_time): # Creates Interactive menu within the script
98 while self.choice != 4:
99 print("\nOptions Menu")
100 print("[1] Send for Metrics for all ports")
101 print("[2] Send for Metrics for all ports recursively")
102 print("[3] Send for global Metrics")
103 print("[4] Unregister client")
106 self.choice = int(raw_input("\n:"))
107 print("\033[F") #Removes the user input for screen, cleans it up
110 self.requestMetrics()
111 elif self.choice == 2:
112 self.repeatedlyRequestMetrics(sleep_time)
113 elif self.choice == 3:
114 self.requestGlobalMetrics()
115 elif self.choice == 4:
117 self.unregistered = 1
119 print("Error - Invalid request choice")
123 if __name__ == "__main__":
127 if (len(sys.argv) == 2):
128 file_path = sys.argv[1]
130 print("Warning - No filepath passed, using default (" + DEFAULT_FP + ").")
131 file_path = DEFAULT_FP
133 client.getFilepath(file_path)
135 client.interactiveMenu(sleep_time)