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