NFVBENCH-25 Send run results to fluentd
[nfvbench.git] / nfvbench / nfvbenchd.py
index 4bbd69d..3ab30de 100644 (file)
@@ -21,6 +21,8 @@ from flask import request
 
 from flask_socketio import emit
 from flask_socketio import SocketIO
+from fluentd import FluentLogHandler
+from summarizer import NFVBenchSummarizer
 
 import json
 from log import LOG
@@ -135,6 +137,7 @@ def setup_flask(root_path):
     app.root_path = root_path
     socketio = SocketIO(app, async_mode='threading')
     busy_json = result_json(STATUS_ERROR, 'there is already an NFVbench request running')
+    config_is_null_json = result_json(STATUS_ERROR, 'configuration is missing')
     not_busy_json = result_json(STATUS_ERROR, 'no pending NFVbench run')
     not_found_msg = 'results not found'
     pending_msg = 'NFVbench run still pending'
@@ -166,12 +169,15 @@ def setup_flask(root_path):
     @app.route('/start_run', methods=['POST'])
     def start_run():
         config = load_json(request.json)
-        if Ctx.is_busy():
-            return jsonify(busy_json)
+        if config:
+            if Ctx.is_busy():
+                return jsonify(busy_json)
+            else:
+                request_id = get_uuid()
+                Ctx.enqueue(config, request_id)
+                return jsonify(result_json(STATUS_PENDING, pending_msg, request_id))
         else:
-            request_id = get_uuid()
-            Ctx.enqueue(config, request_id)
-            return jsonify(result_json(STATUS_PENDING, pending_msg, request_id))
+            return jsonify(config_is_null_json)
 
     @app.route('/status', defaults={'request_id': None}, methods=['GET'])
     @app.route('/status/<request_id>', methods=['GET'])
@@ -207,10 +213,15 @@ class WebSocketIoServer(object):
     notifications using websocket events (send_ methods). Caller should simply create an instance
     of this class and pass a runner object then invoke the run method
     """
+
     def __init__(self, http_root, runner, logger):
         self.nfvbench_runner = runner
         setup_flask(http_root)
         self.fluent_logger = logger
+        self.result_fluent_logger = FluentLogHandler("resultnfvbench",
+                                                     fluentd_ip=self.fluent_logger.sender.host,
+                                                     fluentd_port=self.fluent_logger.sender.port) \
+            if self.fluent_logger else None
 
     def run(self, host='127.0.0.1', port=7556):
 
@@ -242,8 +253,11 @@ class WebSocketIoServer(object):
             else:
                 # this might overwrite a previously unfetched result
                 Ctx.set_result(results)
+            summary = NFVBenchSummarizer(results['result'], self.result_fluent_logger)
+            LOG.info(str(summary))
             Ctx.release()
-            self.fluent_logger.send_run_summary(True)
+            if self.fluent_logger:
+                self.fluent_logger.send_run_summary(True)
 
     def send_interval_stats(self, time_ms, tx_pps, rx_pps, drop_pct):
         stats = {'time_ms': time_ms, 'tx_pps': tx_pps, 'rx_pps': rx_pps, 'drop_pct': drop_pct}