2021-02-05 08:48:47 +00:00
|
|
|
#! /usr/bin/env python3
|
2020-06-18 16:55:50 +00:00
|
|
|
# SPDX-License-Identifier: BSD-3-Clause
|
2019-06-25 11:12:58 +00:00
|
|
|
# Copyright(c) 2018 Intel Corporation
|
|
|
|
|
|
|
|
import socket
|
|
|
|
import os
|
|
|
|
import time
|
2025-01-10 11:50:43 +00:00
|
|
|
import argparse
|
2019-06-25 11:12:58 +00:00
|
|
|
|
|
|
|
BUFFER_SIZE = 200000
|
|
|
|
|
|
|
|
METRICS_REQ = "{\"action\":0,\"command\":\"ports_all_stat_values\",\"data\":null}"
|
|
|
|
API_REG = "{\"action\":1,\"command\":\"clients\",\"data\":{\"client_path\":\""
|
|
|
|
API_UNREG = "{\"action\":2,\"command\":\"clients\",\"data\":{\"client_path\":\""
|
2020-06-18 16:55:50 +00:00
|
|
|
GLOBAL_METRICS_REQ = "{\"action\":0,\"command\":\"global_stat_values\",\"data\":null}"
|
2019-06-25 11:12:58 +00:00
|
|
|
DEFAULT_FP = "/var/run/dpdk/default_client"
|
2025-01-10 11:50:43 +00:00
|
|
|
DEFAULT_PREFIX = 'rte'
|
|
|
|
RUNTIME_SOCKET_NAME = 'telemetry'
|
|
|
|
|
2019-06-25 11:12:58 +00:00
|
|
|
|
|
|
|
class Socket:
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.send_fd = socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET)
|
|
|
|
self.recv_fd = socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET)
|
|
|
|
self.client_fd = None
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
try:
|
|
|
|
self.send_fd.close()
|
|
|
|
self.recv_fd.close()
|
|
|
|
self.client_fd.close()
|
|
|
|
except:
|
|
|
|
print("Error - Sockets could not be closed")
|
|
|
|
|
2025-01-10 11:50:43 +00:00
|
|
|
|
2019-06-25 11:12:58 +00:00
|
|
|
class Client:
|
|
|
|
|
2025-01-10 11:50:43 +00:00
|
|
|
def __init__(self):
|
|
|
|
# Creates a client instance
|
2019-06-25 11:12:58 +00:00
|
|
|
self.socket = Socket()
|
|
|
|
self.file_path = None
|
2025-01-10 11:50:43 +00:00
|
|
|
self.run_path = None
|
2019-06-25 11:12:58 +00:00
|
|
|
self.choice = None
|
|
|
|
self.unregistered = 0
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
try:
|
|
|
|
if self.unregistered == 0:
|
2025-01-10 11:50:43 +00:00
|
|
|
self.unregister()
|
2019-06-25 11:12:58 +00:00
|
|
|
except:
|
|
|
|
print("Error - Client could not be destroyed")
|
|
|
|
|
2025-01-10 11:50:43 +00:00
|
|
|
def getFilepath(self, file_path):
|
|
|
|
# Gets arguments from Command-Line and assigns to instance of client
|
2019-06-25 11:12:58 +00:00
|
|
|
self.file_path = file_path
|
|
|
|
|
2025-01-10 11:50:43 +00:00
|
|
|
def setRunpath(self, file_prefix):
|
|
|
|
self.run_path = os.path.join(get_dpdk_runtime_dir(file_prefix),
|
|
|
|
RUNTIME_SOCKET_NAME)
|
|
|
|
|
|
|
|
def register(self):
|
|
|
|
# Connects a client to DPDK-instance
|
2019-06-25 11:12:58 +00:00
|
|
|
if os.path.exists(self.file_path):
|
|
|
|
os.unlink(self.file_path)
|
|
|
|
try:
|
|
|
|
self.socket.recv_fd.bind(self.file_path)
|
|
|
|
except socket.error as msg:
|
2025-01-10 11:50:43 +00:00
|
|
|
print("Error - Socket binding error: " + str(msg) + "\n")
|
2019-06-25 11:12:58 +00:00
|
|
|
self.socket.recv_fd.settimeout(2)
|
2025-01-10 11:50:43 +00:00
|
|
|
self.socket.send_fd.connect(self.run_path)
|
2019-06-25 11:12:58 +00:00
|
|
|
JSON = (API_REG + self.file_path + "\"}}")
|
2020-06-18 16:55:50 +00:00
|
|
|
self.socket.send_fd.sendall(JSON.encode())
|
|
|
|
|
2019-06-25 11:12:58 +00:00
|
|
|
self.socket.recv_fd.listen(1)
|
|
|
|
self.socket.client_fd = self.socket.recv_fd.accept()[0]
|
|
|
|
|
2025-01-10 11:50:43 +00:00
|
|
|
def unregister(self):
|
|
|
|
# Unregister a given client
|
2020-06-18 16:55:50 +00:00
|
|
|
self.socket.client_fd.send((API_UNREG + self.file_path + "\"}}").encode())
|
2019-06-25 11:12:58 +00:00
|
|
|
self.socket.client_fd.close()
|
|
|
|
|
2025-01-10 11:50:43 +00:00
|
|
|
def requestMetrics(self):
|
|
|
|
# Requests metrics for given client
|
2020-06-18 16:55:50 +00:00
|
|
|
self.socket.client_fd.send(METRICS_REQ.encode())
|
|
|
|
data = self.socket.client_fd.recv(BUFFER_SIZE).decode()
|
|
|
|
print("\nResponse: \n", data)
|
2019-06-25 11:12:58 +00:00
|
|
|
|
2025-01-10 11:50:43 +00:00
|
|
|
def repeatedlyRequestMetrics(self, sleep_time):
|
|
|
|
# Recursively requests metrics for given client
|
2019-06-25 11:12:58 +00:00
|
|
|
print("\nPlease enter the number of times you'd like to continuously request Metrics:")
|
2021-02-05 08:48:47 +00:00
|
|
|
n_requests = int(input("\n:"))
|
2025-01-10 11:50:43 +00:00
|
|
|
# Removes the user input from screen, cleans it up
|
|
|
|
print("\033[F")
|
2019-06-25 11:12:58 +00:00
|
|
|
print("\033[K")
|
|
|
|
for i in range(n_requests):
|
|
|
|
self.requestMetrics()
|
|
|
|
time.sleep(sleep_time)
|
|
|
|
|
2025-01-10 11:50:43 +00:00
|
|
|
def requestGlobalMetrics(self):
|
|
|
|
# Requests global metrics for given client
|
2020-06-18 16:55:50 +00:00
|
|
|
self.socket.client_fd.send(GLOBAL_METRICS_REQ.encode())
|
|
|
|
data = self.socket.client_fd.recv(BUFFER_SIZE).decode()
|
|
|
|
print("\nResponse: \n", data)
|
|
|
|
|
2025-01-10 11:50:43 +00:00
|
|
|
def interactiveMenu(self, sleep_time):
|
|
|
|
# Creates Interactive menu within the script
|
2020-06-18 16:55:50 +00:00
|
|
|
while self.choice != 4:
|
2019-06-25 11:12:58 +00:00
|
|
|
print("\nOptions Menu")
|
|
|
|
print("[1] Send for Metrics for all ports")
|
|
|
|
print("[2] Send for Metrics for all ports recursively")
|
2020-06-18 16:55:50 +00:00
|
|
|
print("[3] Send for global Metrics")
|
|
|
|
print("[4] Unregister client")
|
2019-06-25 11:12:58 +00:00
|
|
|
|
|
|
|
try:
|
2021-02-05 08:48:47 +00:00
|
|
|
self.choice = int(input("\n:"))
|
2025-01-10 11:50:43 +00:00
|
|
|
# Removes the user input for screen, cleans it up
|
|
|
|
print("\033[F")
|
2019-06-25 11:12:58 +00:00
|
|
|
print("\033[K")
|
|
|
|
if self.choice == 1:
|
|
|
|
self.requestMetrics()
|
|
|
|
elif self.choice == 2:
|
|
|
|
self.repeatedlyRequestMetrics(sleep_time)
|
|
|
|
elif self.choice == 3:
|
2020-06-18 16:55:50 +00:00
|
|
|
self.requestGlobalMetrics()
|
|
|
|
elif self.choice == 4:
|
2019-06-25 11:12:58 +00:00
|
|
|
self.unregister()
|
|
|
|
self.unregistered = 1
|
|
|
|
else:
|
|
|
|
print("Error - Invalid request choice")
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2025-01-10 11:50:43 +00:00
|
|
|
|
|
|
|
def get_dpdk_runtime_dir(fp):
|
|
|
|
""" Using the same logic as in DPDK's EAL, get the DPDK runtime directory
|
|
|
|
based on the file-prefix and user """
|
|
|
|
run_dir = os.environ.get('RUNTIME_DIRECTORY')
|
|
|
|
if not run_dir:
|
|
|
|
if (os.getuid() == 0):
|
|
|
|
run_dir = '/var/run'
|
|
|
|
else:
|
|
|
|
run_dir = os.environ.get('XDG_RUNTIME_DIR', '/tmp')
|
|
|
|
return os.path.join(run_dir, 'dpdk', fp)
|
|
|
|
|
|
|
|
|
2019-06-25 11:12:58 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
|
|
sleep_time = 1
|
2025-01-10 11:50:43 +00:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('-f', '--file-prefix', default=DEFAULT_PREFIX,
|
|
|
|
help='Provide file-prefix for DPDK runtime directory')
|
|
|
|
parser.add_argument('sock_path', nargs='?', default=DEFAULT_FP,
|
|
|
|
help='Provide socket file path connected by legacy client')
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2019-06-25 11:12:58 +00:00
|
|
|
client = Client()
|
2025-01-10 11:50:43 +00:00
|
|
|
client.getFilepath(args.sock_path)
|
|
|
|
client.setRunpath(args.file_prefix)
|
2019-06-25 11:12:58 +00:00
|
|
|
client.register()
|
|
|
|
client.interactiveMenu(sleep_time)
|