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