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