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