2 # SPDX-License-Identifier: BSD-3-Clause
3 # Copyright(c) 2018 Intel Corporation
5 from __future__ import print_function
14 METRICS_REQ = "{\"action\":0,\"command\":\"ports_all_stat_values\",\"data\":null}"
15 API_REG = "{\"action\":1,\"command\":\"clients\",\"data\":{\"client_path\":\""
16 API_UNREG = "{\"action\":2,\"command\":\"clients\",\"data\":{\"client_path\":\""
17 GLOBAL_METRICS_REQ = "{\"action\":0,\"command\":\"global_stat_values\",\"data\":null}"
18 DEFAULT_FP = "/var/run/dpdk/default_client"
23 raw_input = input # Python 3
28 self.send_fd = socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET)
29 self.recv_fd = socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET)
36 self.client_fd.close()
38 print("Error - Sockets could not be closed")
42 def __init__(self): # Creates a client instance
43 self.socket = Socket()
50 if self.unregistered == 0:
53 print("Error - Client could not be destroyed")
55 def getFilepath(self, file_path): # Gets arguments from Command-Line and assigns to instance of client
56 self.file_path = file_path
58 def register(self): # Connects a client to DPDK-instance
59 if os.path.exists(self.file_path):
60 os.unlink(self.file_path)
62 self.socket.recv_fd.bind(self.file_path)
63 except socket.error as msg:
64 print ("Error - Socket binding error: " + str(msg) + "\n")
65 self.socket.recv_fd.settimeout(2)
66 self.socket.send_fd.connect("/var/run/dpdk/rte/telemetry")
67 JSON = (API_REG + self.file_path + "\"}}")
68 self.socket.send_fd.sendall(JSON)
69 self.socket.recv_fd.listen(1)
70 self.socket.client_fd = self.socket.recv_fd.accept()[0]
72 def unregister(self): # Unregister a given client
73 self.socket.client_fd.send(API_UNREG + self.file_path + "\"}}")
74 self.socket.client_fd.close()
76 def requestMetrics(self): # Requests metrics for given client
77 self.socket.client_fd.send(METRICS_REQ)
78 data = self.socket.client_fd.recv(BUFFER_SIZE)
79 print("\nResponse: \n", str(data))
81 def repeatedlyRequestMetrics(self, sleep_time): # Recursively requests metrics for given client
82 print("\nPlease enter the number of times you'd like to continuously request Metrics:")
83 n_requests = int(raw_input("\n:"))
84 print("\033[F") #Removes the user input from screen, cleans it up
86 for i in range(n_requests):
88 time.sleep(sleep_time)
90 def requestGlobalMetrics(self): #Requests global metrics for given client
91 self.socket.client_fd.send(GLOBAL_METRICS_REQ)
92 data = self.socket.client_fd.recv(BUFFER_SIZE)
93 print("\nResponse: \n", str(data))
95 def interactiveMenu(self, sleep_time): # Creates Interactive menu within the script
96 while self.choice != 4:
97 print("\nOptions Menu")
98 print("[1] Send for Metrics for all ports")
99 print("[2] Send for Metrics for all ports recursively")
100 print("[3] Send for global Metrics")
101 print("[4] Unregister client")
104 self.choice = int(raw_input("\n:"))
105 print("\033[F") #Removes the user input for screen, cleans it up
108 self.requestMetrics()
109 elif self.choice == 2:
110 self.repeatedlyRequestMetrics(sleep_time)
111 elif self.choice == 3:
112 self.requestGlobalMetrics()
113 elif self.choice == 4:
115 self.unregistered = 1
117 print("Error - Invalid request choice")
121 if __name__ == "__main__":
125 if (len(sys.argv) == 2):
126 file_path = sys.argv[1]
128 print("Warning - No filepath passed, using default (" + DEFAULT_FP + ").")
129 file_path = DEFAULT_FP
131 client.getFilepath(file_path)
133 client.interactiveMenu(sleep_time)