summaryrefslogtreecommitdiffstats
path: root/scripts/automation/trex_control_plane
diff options
context:
space:
mode:
authorYaroslav Brustinov <ybrustin@cisco.com>2015-10-26 08:17:54 +0200
committerYaroslav Brustinov <ybrustin@cisco.com>2015-10-26 09:50:14 +0200
commitffab8fdc68245ff525780554774e3a7615500e28 (patch)
tree289212d60dfad029c51bb484819f703f55202967 /scripts/automation/trex_control_plane
parented13067343186b5dad0566962cd308445136f3b7 (diff)
API add: get_trex_log(), get_trex_daemon_log(), get_trex_version()
Diffstat (limited to 'scripts/automation/trex_control_plane')
-rwxr-xr-xscripts/automation/trex_control_plane/client/trex_client.py78
-rwxr-xr-xscripts/automation/trex_control_plane/server/trex_server.py43
2 files changed, 118 insertions, 3 deletions
diff --git a/scripts/automation/trex_control_plane/client/trex_client.py b/scripts/automation/trex_control_plane/client/trex_client.py
index c3677132..160abdec 100755
--- a/scripts/automation/trex_control_plane/client/trex_client.py
+++ b/scripts/automation/trex_control_plane/client/trex_client.py
@@ -22,7 +22,7 @@ import time
import re
import copy
import binascii
-from collections import deque
+from collections import deque, OrderedDict
from json import JSONDecoder
from distutils.util import strtobool
@@ -497,6 +497,78 @@ class CTRexClient(object):
finally:
self.prompt_verbose_data()
+ def get_trex_daemon_log (self):
+ """
+ Get Trex daemon log.
+
+ :return:
+ String representation of TRex daemon log
+
+ :raises:
+ + :exc:`trex_exceptions.TRexRequestDenied`, in case file could not be read.
+ + ProtocolError, in case of error in JSON-RPC protocol.
+
+ """
+ try:
+ return binascii.a2b_base64(self.server.get_trex_daemon_log())
+ except AppError as err:
+ self._handle_AppError_exception(err.args[0])
+ except ProtocolError:
+ raise
+ finally:
+ self.prompt_verbose_data()
+
+ def get_trex_log (self):
+ """
+ Get TRex CLI output log
+
+ :return:
+ String representation of TRex log
+
+ :raises:
+ + :exc:`trex_exceptions.TRexRequestDenied`, in case file could not be fetched at server side.
+ + ProtocolError, in case of error in JSON-RPC protocol.
+
+ """
+ try:
+ return binascii.a2b_base64(self.server.get_trex_log())
+ except AppError as err:
+ self._handle_AppError_exception(err.args[0])
+ except ProtocolError:
+ raise
+ finally:
+ self.prompt_verbose_data()
+
+ def get_trex_version (self):
+ """
+ Get TRex version details.
+
+ :return:
+ Trex details (Version, User, Date, Uuid) as ordered dictionary
+
+ :raises:
+ + :exc:`trex_exceptions.TRexRequestDenied`, in case TRex version could not be determined.
+ + ProtocolError, in case of error in JSON-RPC protocol.
+ + General Exception is case one of the keys is missing in response
+ """
+
+ try:
+ version_dict = OrderedDict()
+ result_lines = binascii.a2b_base64(self.server.get_trex_version()).split('\n')
+ for line in result_lines:
+ key, value = line.strip().split(':', 1)
+ version_dict[key.strip()] = value.strip()
+ for key in ('Version', 'User', 'Date', 'Uuid'):
+ if key not in version_dict:
+ raise Exception('get_trex_version: got server response without key: {0}'.format(key))
+ return version_dict
+ except AppError as err:
+ self._handle_AppError_exception(err.args[0])
+ except ProtocolError:
+ raise
+ finally:
+ self.prompt_verbose_data()
+
def reserve_trex (self, user = None):
"""
Reserves the usage of TRex to a certain user.
@@ -650,8 +722,8 @@ class CTRexClient(object):
"""
if self.verbose:
print ('\n')
- print ("(*) JSON-RPC request: "+ self.history.request)
- print ("(*) JSON-RPC response: "+ self.history.response)
+ print ("(*) JSON-RPC request:", self.history.request)
+ print ("(*) JSON-RPC response:", self.history.response)
def __verbose_print(self, print_str):
"""
diff --git a/scripts/automation/trex_control_plane/server/trex_server.py b/scripts/automation/trex_control_plane/server/trex_server.py
index e48f8963..ca53de81 100755
--- a/scripts/automation/trex_control_plane/server/trex_server.py
+++ b/scripts/automation/trex_control_plane/server/trex_server.py
@@ -59,6 +59,7 @@ class CTRexServer(object):
self.__check_trex_path_validity()
self.__check_files_path_validity()
self.trex = CTRex()
+ self.trex_version = None
self.trex_host = trex_host
self.trex_daemon_port = trex_daemon_port
self.trex_zmq_port = trex_zmq_port
@@ -66,6 +67,7 @@ class CTRexServer(object):
self.start_lock = threading.Lock()
self.__reservation = None
self.zmq_monitor = ZmqMonitorSession(self.trex, self.trex_zmq_port) # intiate single ZMQ monitor thread for server usage
+ logger.info(self.get_trex_version(base64 = False))
def add(self, x, y):
print "server function add ",x,y
@@ -116,6 +118,9 @@ class CTRexServer(object):
# set further functionality and peripherals to server instance
try:
self.server.register_function(self.add)
+ self.server.register_function(self.get_trex_log)
+ self.server.register_function(self.get_trex_daemon_log)
+ self.server.register_function(self.get_trex_version)
self.server.register_function(self.connectivity_check)
self.server.register_function(self.start_trex)
self.server.register_function(self.stop_trex)
@@ -140,6 +145,44 @@ class CTRexServer(object):
self.server.shutdown()
pass
+ # get files from Trex server and return their content (mainly for logs)
+ @staticmethod
+ def _pull_file(filepath):
+ try:
+ with open(filepath, 'rb') as f:
+ file_content = f.read()
+ return binascii.b2a_base64(file_content)
+ except Exception as e:
+ err_str = "Can't get requested file: {0}, possibly due to TRex that did not run".format(filepath)
+ logger.error('{0}, error: {1}'.format(err_str, e))
+ return Fault(-33, err_str)
+
+ # get Trex log /tmp/trex.txt
+ def get_trex_log(self):
+ logger.info("Processing get_trex_log() command.")
+ return self._pull_file('/tmp/trex.txt')
+
+ # get daemon log /var/log/trex/trex_daemon_server.log
+ def get_trex_daemon_log (self):
+ logger.info("Processing get_trex_daemon_log() command.")
+ return self._pull_file('/var/log/trex/trex_daemon_server.log')
+
+ # get Trex version from ./t-rex-64 --help (last 4 lines)
+ def get_trex_version (self, base64 = True):
+ try:
+ logger.info("Processing get_trex_version() command.")
+ if not self.trex_version:
+ help_print = os.popen('cd {path}; ./t-rex-64 --help'.format(path=self.TREX_PATH), 'r').read()
+ self.trex_version = binascii.b2a_base64('\n'.join(help_print.split('\n')[-5:-1]))
+ if base64:
+ return self.trex_version
+ else:
+ return binascii.a2b_base64(self.trex_version)
+ except Exception as e:
+ err_str = "Can't get trex version, error: {0}".format(e)
+ logger.error(err_str)
+ return Fault(-33, err_str)
+
def stop_handler (self, signum, frame):
logger.info("Daemon STOP request detected.")
if self.is_running():