1 # SPDK-License-Identifier: BSD-3-Clause
2 # Copyright(c) 2018 Intel Corporation
11 METRICS_REQ = "{\"action\":0,\"command\":\"ports_all_stat_values\",\"data\":null}"
12 API_REG = "{\"action\":1,\"command\":\"clients\",\"data\":{\"client_path\":\""
13 API_UNREG = "{\"action\":2,\"command\":\"clients\",\"data\":{\"client_path\":\""
14 DEFAULT_FP = "/var/run/dpdk/default_client"
19 self.send_fd = socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET)
20 self.recv_fd = socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET)
27 self.client_fd.close()
29 print("Error - Sockets could not be closed")
33 def __init__(self): # Creates a client instance
34 self.socket = Socket()
41 if self.unregistered == 0:
44 print("Error - Client could not be destroyed")
46 def getFilepath(self, file_path): # Gets arguments from Command-Line and assigns to instance of client
47 self.file_path = file_path
49 def register(self): # Connects a client to DPDK-instance
50 if os.path.exists(self.file_path):
51 os.unlink(self.file_path)
53 self.socket.recv_fd.bind(self.file_path)
54 except socket.error as msg:
55 print ("Error - Socket binding error: " + str(msg) + "\n")
56 self.socket.recv_fd.settimeout(2)
57 self.socket.send_fd.connect("/var/run/dpdk/rte/telemetry")
58 JSON = (API_REG + self.file_path + "\"}}")
59 self.socket.send_fd.sendall(JSON)
60 self.socket.recv_fd.listen(1)
61 self.socket.client_fd = self.socket.recv_fd.accept()[0]
63 def unregister(self): # Unregister a given client
64 self.socket.client_fd.send(API_UNREG + self.file_path + "\"}}")
65 self.socket.client_fd.close()
67 def requestMetrics(self): # Requests metrics for given client
68 self.socket.client_fd.send(METRICS_REQ)
69 data = self.socket.client_fd.recv(BUFFER_SIZE)
70 print "\nResponse: \n", str(data)
72 def repeatedlyRequestMetrics(self, sleep_time): # Recursively requests metrics for given client
73 print("\nPlease enter the number of times you'd like to continuously request Metrics:")
74 n_requests = int(input("\n:"))
75 print("\033[F") #Removes the user input from screen, cleans it up
77 for i in range(n_requests):
79 time.sleep(sleep_time)
81 def interactiveMenu(self, sleep_time): # Creates Interactive menu within the script
82 while self.choice != 3:
83 print("\nOptions Menu")
84 print("[1] Send for Metrics for all ports")
85 print("[2] Send for Metrics for all ports recursively")
86 print("[3] Unregister client")
89 self.choice = int(input("\n:"))
90 print("\033[F") #Removes the user input for screen, cleans it up
94 elif self.choice == 2:
95 self.repeatedlyRequestMetrics(sleep_time)
96 elif self.choice == 3:
100 print("Error - Invalid request choice")
104 if __name__ == "__main__":
108 if (len(sys.argv) == 2):
109 file_path = sys.argv[1]
111 print("Warning - No filepath passed, using default (" + DEFAULT_FP + ").")
112 file_path = DEFAULT_FP
114 client.getFilepath(file_path)
116 client.interactiveMenu(sleep_time)