From cb8c8a249454b7150013c7fdf61eaaee2ed61a4e Mon Sep 17 00:00:00 2001
From: MJB <mjb@it-innovation.soton.ac.uk>
Date: Wed, 30 May 2018 16:49:39 +0100
Subject: [PATCH] deployment of clmcservice and aggregator

---
 Vagrantfile                                  |  2 +-
 clmctest/monitoring/E2ESim.py                |  6 ++--
 clmctest/monitoring/rspec.yml                |  3 ++
 scripts/clmc-service/install.sh              | 33 ++++++++++++++++++--
 src/clmcwebservice/clmcservice/aggregator.py | 18 ++++++++---
 5 files changed, 51 insertions(+), 11 deletions(-)

diff --git a/Vagrantfile b/Vagrantfile
index c195390..8fbb539 100644
--- a/Vagrantfile
+++ b/Vagrantfile
@@ -91,7 +91,7 @@ Vagrant.configure("2") do |config|
         when 'test-runner'
           instance_config.vm.provision :shell, :path => "clmctest/services/pytest/install.sh"
         when 'clmc-service'
-          instance_config.vm.provision :shell, :path => "scripts/clmc-service/install.sh"       
+          instance_config.vm.provision :shell, :path => "scripts/clmc-service/install.sh", :args => "#{host["influxdb_url"]} #{host["database_name"]} #{host["report_period"]}"       
         else
           # specific service install
           instance_config.vm.provision :shell, :path => "clmctest/services/#{host["service_name"]}/install.sh", env: {"REPO_ROOT" => "/vagrant"}
diff --git a/clmctest/monitoring/E2ESim.py b/clmctest/monitoring/E2ESim.py
index a6520d2..4a3fa2d 100644
--- a/clmctest/monitoring/E2ESim.py
+++ b/clmctest/monitoring/E2ESim.py
@@ -1,6 +1,6 @@
 #!/usr/bin/python3
 """
-## © University of Southampton IT Innovation Centre, 2018
+## Copyright University of Southampton IT Innovation Centre, 2018
 ##
 ## Copyright in this software belongs to University of Southampton
 ## IT Innovation Centre of Gamma House, Enterprise Road,
@@ -37,8 +37,8 @@ class Simulator(object):
     Simulator used to generate E2E measurements.
     """
 
-    DATABASE = 'E2EMetrics'  # default database name
-    DATABASE_URL = 'http://203.0.113.100:8086'  # default database url
+    DATABASE = 'CLMCMetrics'  # default database name
+    DATABASE_URL = 'http://172.40.231.51:8086'  # default database url
 
     TICK = 1  # a simulation tick represents 1s
     SIMULATION_LENGTH = 120  # simulation time in seconds
diff --git a/clmctest/monitoring/rspec.yml b/clmctest/monitoring/rspec.yml
index 7d7c38d..fa5297a 100644
--- a/clmctest/monitoring/rspec.yml
+++ b/clmctest/monitoring/rspec.yml
@@ -34,6 +34,9 @@ hosts:
       - guest: 9080
         host: 9080
     ip_address: "172.40.231.51"
+    influxdb_url: "http://172.40.231.51:8086"
+    database_name: "CLMCMetrics"
+    report_period: 25    
   - name: ipendpoint1
     cpus: 1
     memory: 2048
diff --git a/scripts/clmc-service/install.sh b/scripts/clmc-service/install.sh
index 7cf997f..acd817f 100755
--- a/scripts/clmc-service/install.sh
+++ b/scripts/clmc-service/install.sh
@@ -27,6 +27,20 @@
 # Force fail on command fail (off for now as virtualenvwrapper install fails)
 # set -euo pipefail
 
+
+echo "Configuring CLMC service"
+
+# Get command line parameters
+if [ "$#" -ne 3 ]; then
+    echo "Error: illegal number of arguments: "$#
+    echo "Usage: install.sh INFLUX_URL DATABASE_NAME REPORT_PERIOD"
+    exit 1 
+fi
+
+INFLUX_URL=$1
+DATABASE_NAME=$2
+REPORT_PERIOD=$3
+
 # Define tickstack software versions
 INFLUX_VERSION=1.5.2
 INFLUX_CHECKSUM=42fede7b497bdf30d4eb5138db218d1add986fca4fce4a8bcd9c7d6dabaf572a
@@ -121,7 +135,7 @@ fi
 
 # navigate to the clmc-webservice - and check
 echo "----> Moving to CLMC webservice"
-cd /vagrant/src/clmc-webservice
+cd /vagrant/src/clmcwebservice
 if [ $? -ne 0 ] ; then
         echo "Failed: could not find clmc-webservice"
 		exit 1
@@ -129,7 +143,7 @@ fi
 
 # install the service
 echo "----> Installing CLMC web service"
-pip3 install .
+pip3 install  .
 if [ $? -ne 0 ] ; then
         echo "Failed: installing clmc-webservice"
 		exit 1
@@ -143,4 +157,17 @@ if [ $? -ne 0 ] ; then
 		exit 1
 else
 	echo "CLMC service started."
-fi
\ No newline at end of file
+fi
+
+while ! nc -z localhost 9080
+do
+  echo "Waiting for clmc service port 9080 to be ready on localhost..."
+  sleep 5
+done
+
+JSON="{\"aggregator_report_period\": ${REPORT_PERIOD}, \"aggregator_database_name\": \"${DATABASE_NAME}\", \"aggregator_database_url\": \"${INFLUX_URL}\"}"
+curl -H 'Content-Type: application/json' -X PUT -d "${JSON}" http://localhost:9080/aggregator/config
+
+JSON="{\"action\": \"start\"}"
+curl -H 'Content-Type: application/json' -X PUT -d "${JSON}" http://localhost:9080/aggregator/control
+
diff --git a/src/clmcwebservice/clmcservice/aggregator.py b/src/clmcwebservice/clmcservice/aggregator.py
index 2c0c7d9..18a3317 100644
--- a/src/clmcwebservice/clmcservice/aggregator.py
+++ b/src/clmcwebservice/clmcservice/aggregator.py
@@ -51,6 +51,7 @@ class Aggregator(object):
         """
 
         # initialise a database client using the database url and the database name
+        print("Creating InfluxDB Connection")
         url_object = urlparse(database_url)
         while True:
             try:
@@ -82,6 +83,8 @@ class Aggregator(object):
         Performs the functionality of the aggregator - query data from both measurements merge that data and post it back in influx every 5 seconds.
         """
 
+        print("Running aggregator")
+
         current_time = int(time())
         while not self._stop_flag.is_set():
 
@@ -94,12 +97,16 @@ class Aggregator(object):
             network_delays = {}
 
             while True:
+
                 try:
+                    print("Query for network delays")
                     result = self.db_client.query(
-                        'SELECT mean(latency) as "net_latency", mean(bandwidth) as "net_bandwidth" FROM "E2EMetrics"."autogen"."network_delays" WHERE time >= {0} and time < {1} GROUP BY path, source, target'.format(
+                        'SELECT mean(latency) as "net_latency", mean(bandwidth) as "net_bandwidth" FROM "{0}"."autogen"."network_delays" WHERE time >= {1} and time < {2} GROUP BY path, source, target'.format(self.db_name,
                             boundary_time_nano, current_time_nano))
                     break
-                except:
+                except Exception as e:
+                    print("Exception getting network delay")
+                    print(e)
                     sleep(self.RETRY_PERIOD)
 
             for item in result.items():
@@ -116,11 +123,14 @@ class Aggregator(object):
 
             while True:
                 try:
+                    print("Query for service delays")                    
                     result = self.db_client.query(
-                        'SELECT mean(response_time) as "response_time", mean(request_size) as "request_size", mean(response_size) as "response_size" FROM "E2EMetrics"."autogen"."service_delays" WHERE time >= {0} and time < {1} GROUP BY endpoint, sf_instance, sfr'.format(
+                        'SELECT mean(response_time) as "response_time", mean(request_size) as "request_size", mean(response_size) as "response_size" FROM "{0}"."autogen"."service_delays" WHERE time >= {1} and time < {2} GROUP BY endpoint, sf_instance, sfr'.format(self.db_name,
                             boundary_time_nano, current_time_nano))
                     break
-                except:
+                except Exception as e:
+                    print("Exception getting service delay")
+                    print(e)
                     sleep(self.RETRY_PERIOD)
 
             for item in result.items():
-- 
GitLab