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