1# SPDK-License-Identifier: BSD-3-Clause 2# Copyright(c) 2018 Intel Corporation 3 4import socket 5import os 6import sys 7import time 8 9BUFFER_SIZE = 200000 10 11METRICS_REQ = "{\"action\":0,\"command\":\"ports_all_stat_values\",\"data\":null}" 12API_REG = "{\"action\":1,\"command\":\"clients\",\"data\":{\"client_path\":\"" 13API_UNREG = "{\"action\":2,\"command\":\"clients\",\"data\":{\"client_path\":\"" 14DEFAULT_FP = "/var/run/dpdk/default_client" 15 16try: 17 raw_input # Python 2 18except NameError: 19 raw_input = input # Python 3 20 21class Socket: 22 23 def __init__(self): 24 self.send_fd = socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET) 25 self.recv_fd = socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET) 26 self.client_fd = None 27 28 def __del__(self): 29 try: 30 self.send_fd.close() 31 self.recv_fd.close() 32 self.client_fd.close() 33 except: 34 print("Error - Sockets could not be closed") 35 36class Client: 37 38 def __init__(self): # Creates a client instance 39 self.socket = Socket() 40 self.file_path = None 41 self.choice = None 42 self.unregistered = 0 43 44 def __del__(self): 45 try: 46 if self.unregistered == 0: 47 self.unregister(); 48 except: 49 print("Error - Client could not be destroyed") 50 51 def getFilepath(self, file_path): # Gets arguments from Command-Line and assigns to instance of client 52 self.file_path = file_path 53 54 def register(self): # Connects a client to DPDK-instance 55 if os.path.exists(self.file_path): 56 os.unlink(self.file_path) 57 try: 58 self.socket.recv_fd.bind(self.file_path) 59 except socket.error as msg: 60 print ("Error - Socket binding error: " + str(msg) + "\n") 61 self.socket.recv_fd.settimeout(2) 62 self.socket.send_fd.connect("/var/run/dpdk/rte/telemetry") 63 JSON = (API_REG + self.file_path + "\"}}") 64 self.socket.send_fd.sendall(JSON) 65 self.socket.recv_fd.listen(1) 66 self.socket.client_fd = self.socket.recv_fd.accept()[0] 67 68 def unregister(self): # Unregister a given client 69 self.socket.client_fd.send(API_UNREG + self.file_path + "\"}}") 70 self.socket.client_fd.close() 71 72 def requestMetrics(self): # Requests metrics for given client 73 self.socket.client_fd.send(METRICS_REQ) 74 data = self.socket.client_fd.recv(BUFFER_SIZE) 75 print "\nResponse: \n", str(data) 76 77 def repeatedlyRequestMetrics(self, sleep_time): # Recursively requests metrics for given client 78 print("\nPlease enter the number of times you'd like to continuously request Metrics:") 79 n_requests = int(raw_input("\n:")) 80 print("\033[F") #Removes the user input from screen, cleans it up 81 print("\033[K") 82 for i in range(n_requests): 83 self.requestMetrics() 84 time.sleep(sleep_time) 85 86 def interactiveMenu(self, sleep_time): # Creates Interactive menu within the script 87 while self.choice != 3: 88 print("\nOptions Menu") 89 print("[1] Send for Metrics for all ports") 90 print("[2] Send for Metrics for all ports recursively") 91 print("[3] Unregister client") 92 93 try: 94 self.choice = int(raw_input("\n:")) 95 print("\033[F") #Removes the user input for screen, cleans it up 96 print("\033[K") 97 if self.choice == 1: 98 self.requestMetrics() 99 elif self.choice == 2: 100 self.repeatedlyRequestMetrics(sleep_time) 101 elif self.choice == 3: 102 self.unregister() 103 self.unregistered = 1 104 else: 105 print("Error - Invalid request choice") 106 except: 107 pass 108 109if __name__ == "__main__": 110 111 sleep_time = 1 112 file_path = "" 113 if (len(sys.argv) == 2): 114 file_path = sys.argv[1] 115 else: 116 print("Warning - No filepath passed, using default (" + DEFAULT_FP + ").") 117 file_path = DEFAULT_FP 118 client = Client() 119 client.getFilepath(file_path) 120 client.register() 121 client.interactiveMenu(sleep_time) 122