From ac144410154a847be8bcebfacfe412a5973b687d Mon Sep 17 00:00:00 2001
From: Rowan Powell <rp17@it-innovation.soton.ac.uk>
Date: Fri, 5 Jan 2018 16:21:23 +0000
Subject: [PATCH] Added more metrics to simulation

---
 scripts/influx/telegraf.conf      |  10 +++
 src/mediaServiceSim/serviceSim.py | 124 +++++++++++++++++++++++++++---
 2 files changed, 125 insertions(+), 9 deletions(-)

diff --git a/scripts/influx/telegraf.conf b/scripts/influx/telegraf.conf
index abeb11e..d727cf5 100644
--- a/scripts/influx/telegraf.conf
+++ b/scripts/influx/telegraf.conf
@@ -13,6 +13,7 @@
 [global_tags]
   # dc = "us-east-1" # will tag all metrics with dc=us-east-1
   # rack = "1a"
+  auth = "IT-Inn"
 
 # Configuration for telegraf agent
 [agent]
@@ -80,6 +81,15 @@
   # user_agent = "telegraf"
   # Set UDP payload size, defaults to InfluxDB UDP Client default (512 bytes)
   # udp_payload = 512
+[[outputs.file]]
+  ## Files to write to, "stdout" is a specially handled file.
+  files = ["stdout", "/tmp/metrics.out"]
+
+  ## Data format to output.
+  ## Each data format has its own unique set of configuration options, read
+  ## more about them here:
+  ## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_OUTPUT.md
+  data_format = "influx"
 
 
 ###############################################################################
diff --git a/src/mediaServiceSim/serviceSim.py b/src/mediaServiceSim/serviceSim.py
index 2c02546..bf35c43 100644
--- a/src/mediaServiceSim/serviceSim.py
+++ b/src/mediaServiceSim/serviceSim.py
@@ -128,6 +128,33 @@ class DemoServer(object):
     def reportStatus( self ):
         print ('Number of clients: ' + str(len(self.clients)) )
 
+    def configure_servers(self):
+        print("Configuring Servers")
+        server_conf_block = []
+        ids = ['A', 'B', 'C']
+        locations = ['locA', 'locB', 'locC']
+        for i, id in enumerate(ids):
+            server_conf_block.append(self._generateServerConfig(id,locations[i],8,'100G','1T',self._selectDelay(len(ids))))
+        self._sendInfluxDataBlock(server_conf_block)
+    def configure_VMs(self):
+        print("Configuring VM nodes")
+        VM_conf_block = []
+        self._generateVMS('starting',10,VM_conf_block)
+        self._generateVMS('running',10,VM_conf_block)
+
+        self._sendInfluxDataBlock(VM_conf_block)
+
+    def shutdown_VMs(self):
+        print("Shutting down VM nodes")
+        VM_conf_block = []
+        self._generateVMS('stopping',10,VM_conf_block)
+
+        self._sendInfluxDataBlock(VM_conf_block)
+
+    def _generateVMS(self,state, amount, datablock):
+        for i in range(0, amount):
+            datablock.append(self._generateVMConfig(state, 1, '100G', '1T', self._selectDelay(amount)))
+
     def iterateService( self ):
         # The simulation will run through 'X' iterations of the simulation
         # each time this method is called. This allows request/response messages to be
@@ -139,6 +166,8 @@ class DemoServer(object):
 
         requestBlock = []
         responseBlock = []
+        networkBlock = []
+        SFBlock = []
         totalDifference = sumOfclientQuality = percentageDifference = 0
 
         # Keep going until this stride (count) completes
@@ -183,8 +212,10 @@ class DemoServer(object):
                    # print('percentageOfQualityDifference = ' + str(percentageDifference) + '%')
  
                     responseBlock.append( self._generateServerResponse( client.getLastRequestID(), qualitySelect, delaySelect, cpuUsagePercentage, percentageDifference ) )
+                    SFBlock.append( self._generateMpegDashReport('https://netflix.com/scream',qualitySelect,delaySelect))
+
 
-                
+                    networkBlock.append(self._generateNetworkReport(sumOfclientQuality,delaySelect))
                 # Iterate the service simulation
                 self.simIterations -= 1
                 self.currentTime += 1000 # advance 1 second
@@ -193,6 +224,9 @@ class DemoServer(object):
         if ( len(requestBlock) > 0 and len(responseBlock) > 0 ):
             self._sendInfluxDataBlock( requestBlock )
             self._sendInfluxDataBlock( responseBlock )
+            self._sendInfluxDataBlock( networkBlock )
+            self._sendInfluxDataBlock( SFBlock )
+            #print("Sending influx data blocks")
 
         return self.simIterations
 
@@ -285,10 +319,10 @@ class DemoServer(object):
     def _generateServerResponse( self, reqID, quality, delay, cpuUsage, qualityDifference ):
 
         # Tags first
-        # None here yet
+        result = ' '
 
         # Fields
-        result =  'quality=' + str( quality ) + ','
+        result += 'quality=' + str( quality ) + ','
         result += 'cpuUsage=' + str(cpuUsage) + ','
         result += 'qualityDifference=' + str(qualityDifference) +','
         result += 'requestID="' + str( reqID ) + '",'
@@ -298,9 +332,78 @@ class DemoServer(object):
         result += ' ' + str( self._getNSTime(delay) )
 
         # Measurement
-        return 'response ' + result
+        #print('response'+result)
+        return 'response' + result
+
+    def _generateNetworkReport(self, sum_of_client_quality, delay):
+        # Tags
+        result = ',port_id=enps03 '
+        # Fields
+        result += 'RX_BYTES_PORT_M='+str(sum_of_client_quality*32)+","
+        result += 'TX_BYTES_PORT_M='+str(sum_of_client_quality*1024)
+        # Timestamp
+        result += ' ' + str( self._getNSTime(delay) )
+
+        # Measurement
+        #print('network'+result)
+        return 'net_port_io' + result
+
+    def _generateMpegDashReport(self, resource, quality, delay):
+        # Tags
+        result = ' '
+        # Fields
+        requests = randint(10,30)
+        avg_response_time = 50+randint(0,100)+randint(0,10*quality)
+        peak_response_time = avg_response_time + randint(30,60) + randint(5,10)*quality
+        result += 'cont_nav=\"'+str(resource)+"\","
+        result += 'cont_rep='+str(quality)+','
+        result += 'requests='+str(requests)+','
+        result += 'avg_response_time='+str(avg_response_time)+','
+        result += 'peak_response_time='+str(peak_response_time)
+        # Timestamp
+        result += ' ' + str( self._getNSTime(delay) )
+
+        # Measurement
+        #print('mpegdash_service'+result)
+        return 'mpegdash_service' + result
+
+    def _generateServerConfig(self, ID, location, cpu, mem, storage, delay):
+        # metric
+        result = 'host_resource'
+        # Tags
+        result += ',slide_id='+self.quote_wrap(ID)
+        result += ',location='+self.quote_wrap(location)
+        result += ' '
+        # Fields
+        result += 'cpu='+str(cpu)
+        result += ',memory='+self.quote_wrap(mem)
+        result += ',storage=' + self.quote_wrap(storage)
+
+        #Time
+        result += ' ' + str( self._getNSTime(delay) )
 
+        print(result)
+        return result
+
+    def _generateVMConfig(self, state, cpu, mem, storage, delay):
+        # metric
+        result = 'vm_res_alloc'
+        # Tags
+        result += ',vm_state='+self.quote_wrap(state)
+        result += ' '
+        # Fields
+        result += 'cpu='+str(cpu)
+        result += ',memory='+self.quote_wrap(mem)
+        result += ',storage=' + self.quote_wrap(storage)
 
+        #Time
+        result += ' ' + str( self._getNSTime(delay) )
+
+        print(result)
+        return result
+
+    def quote_wrap(self,str):
+        return "\""+str+"\""
     # InfluxDB likes to have time-stamps in nanoseconds
     def _getNSTime( self, offset = 0 ):
         # Convert to nano-seconds
@@ -346,7 +449,9 @@ class DemoServer(object):
 print( "Preparing simulation" )
 clients    = 10
 iterations = 3000
-demoServer = DemoServer( clients, iterations, 'http://localhost:8186', 'testDB' )
+# port 8086: Direct to DB specified
+# port 8186: To telegraf, telegraf specifies DB
+demoServer = DemoServer(clients, iterations, 'http://localhost:8186', 'testDB')
 
 # Set up InfluxDB (need to wait a little while)
 demoServer.destroyDatabase()
@@ -354,7 +459,8 @@ time.sleep(2)
 demoServer.prepareDatabase()
 time.sleep(2)
 demoServer.reportStatus()
-
+demoServer.configure_servers()
+demoServer.configure_VMs()
 # Start simulation
 print( "Starting simulation" )
 while ( True ):
@@ -364,6 +470,6 @@ while ( True ):
     print( "Simulation remaining (%): " + str( pcDone ) +" \r", end='' )
 
     if ( itCount == 0 ):
-        break;
-
-print( "\nFinished" )
\ No newline at end of file
+        break
+demoServer.shutdown_VMs()
+print("\nFinished")
\ No newline at end of file
-- 
GitLab