Redone the patch changes from 2413/5 as repo upload had messed up the commit

Change-Id: I711f57d6ef11b863108432235a64fbc68718976f
diff --git a/xos/synchronizer/manifest b/xos/synchronizer/manifest
index 8f43610..b4f7827 100644
--- a/xos/synchronizer/manifest
+++ b/xos/synchronizer/manifest
@@ -1,9 +1,14 @@
 manifest
+monitoring_agent/__init__.py
+monitoring_agent/monitoring_agent.py
+monitoring_agent/exampleservice_stats.py
+monitoring_agent/monitoring_agent.conf
 steps/sync_exampletenant.py
 steps/roles/install_apache/tasks/main.yml
 steps/roles/create_index/templates/index.html.j2
 steps/roles/create_index/tasks/main.yml
 steps/exampletenant_playbook.yaml
+steps/monitoring_agent.yaml
 exampleservice-synchronizer.py
 model-deps
 run.sh
diff --git a/xos/synchronizer/monitoring_agent/exampleservice_stats.py b/xos/synchronizer/monitoring_agent/exampleservice_stats.py
index 64843fd..896356b 100644
--- a/xos/synchronizer/monitoring_agent/exampleservice_stats.py
+++ b/xos/synchronizer/monitoring_agent/exampleservice_stats.py
@@ -39,30 +39,30 @@
     while line:
         line = line.strip()
         if "Total Accesses:" in line:
-            key = "total.accesses"
+            key = "exampleservice.apache.total.accesses"
             val  = {'val':int(line.strip("Total Accesses:")), 'unit':'accesses', 'metric_type':'gauge'}
         elif "Total kBytes:" in line:
-            key = "total.kBytes"
+            key = "exampleservice.apache.total.kBytes"
             val  = {'val':float(line.strip("Total kBytes:")), 'unit':'kBytes', 'metric_type':'gauge'}
         elif "Uptime:" in line:
-            key = "uptime"
+            key = "exampleservice.apache.uptime"
             val  = {'val':int(line.strip("Uptime:")), 'unit':'seconds', 'metric_type':'gauge'}
         elif "ReqPerSec:" in line:
-            key = "reqpersec"
+            key = "exampleservice.apache.reqpersec"
             val  = {'val':float(line.strip("ReqPerSec:")), 'unit':'rate', 'metric_type':'gauge'}
         elif "BytesPerSec:" in line:
-            key = "bytespersec"
+            key = "exampleservice.apache.bytespersec"
             val  = {'val':float(line.strip("BytesPerSec:")), 'unit':'rate', 'metric_type':'gauge'}
         elif "BytesPerReq:" in line:
-            key = "bytesperreq"
+            key = "exampleservice.apache.bytesperreq"
             val  = {'val':float(line.strip("BytesPerReq:")), 'unit':'rate', 'metric_type':'gauge'}
         elif "BusyWorkers:" in line:
-            key = "busyworkers"
+            key = "exampleservice.apache.busyworkers"
             val  = {'val':int(line.strip("BusyWorkers:")), 'unit':'workers', 'metric_type':'gauge'}
         elif "IdleWorkers:" in line:
-            key = "idleworkers"
+            key = "exampleservice.apache.idleworkers"
             val  = {'val':int(line.strip("IdleWorkers:")), 'unit':'workers', 'metric_type':'gauge'}
-   
+
         dictStatus[key] = val
         counter = counter + 1
         line = file.readline()
diff --git a/xos/synchronizer/monitoring_agent/monitoring_agent.py b/xos/synchronizer/monitoring_agent/monitoring_agent.py
index 1839ed5..19204b1 100644
--- a/xos/synchronizer/monitoring_agent/monitoring_agent.py
+++ b/xos/synchronizer/monitoring_agent/monitoring_agent.py
@@ -28,6 +28,7 @@
 @app.route('/monitoring/agent/exampleservice/start',methods=['POST'])
 def exampleservice_start_monitoring_agent():
     global start_publish, rabbit_user, rabbit_password, rabbit_host, rabbit_exchange
+    global keystone_tenant_id, keystone_user_id, publisher_id
     try:
         # To do validation of user inputs for all the functions
         target = request.json['target']
@@ -83,10 +84,10 @@
                  'publisher_id': publisher_id,
                  'timestamp':datetime.datetime.now().isoformat(),
                  'priority':'INFO',
-                 'payload': {'name':k,
-                             'unit':v['unit'],
-                             'result':v['val'],
-                             'type':v['metric_type'],
+                 'payload': {'counter_name':k,
+                             'counter_unit':v['unit'],
+                             'counter_volume':v['val'],
+                             'counter_type':v['metric_type'],
                              'resource_id':'exampleservice',
                              'user_id':keystone_user_id,
                              'tenant_id':keystone_tenant_id
@@ -103,7 +104,7 @@
     resParse = stats.parse_status_page()
     logging.debug ("publish:%(data)s" % {'data':resParse})
     publish_exampleservice_stats(resParse)
-    threading.Timer(5, periodic_publish).start()
+    threading.Timer(60, periodic_publish).start()
 
 if __name__ == "__main__":
     logging.config.fileConfig('monitoring_agent.conf', disable_existing_loggers=False)
diff --git a/xos/synchronizer/steps/monitoring_agent.yaml b/xos/synchronizer/steps/monitoring_agent.yaml
new file mode 100644
index 0000000..87cffde
--- /dev/null
+++ b/xos/synchronizer/steps/monitoring_agent.yaml
@@ -0,0 +1,61 @@
+---
+- hosts: {{ instance_name }}
+  gather_facts: False
+  connection: ssh
+  user: ubuntu
+  vars:
+      keystone_tenant_id: {{ keystone_tenant_id }}
+      keystone_user_id: {{ keystone_user_id }}
+      target_uri: {{ target_uri }}
+
+  tasks:
+  - name: Verify if monitoring_agent ([] is to avoid capturing the shell process) cron job is already running
+    shell: pgrep -f [m]onitoring_agent | wc -l
+    register: cron_job_pids_count
+
+  - name: DEBUG
+    debug: var=cron_job_pids_count.stdout
+
+  - name: make sure /usr/local/share/monitoring_agent exists
+    file: path=/usr/local/share/monitoring_agent state=directory owner=root group=root
+    become: yes
+    when: cron_job_pids_count.stdout == "0"
+
+  - name: Copy cron job to destination
+    copy: src=/opt/xos/synchronizers/exampleservice/monitoring_agent/
+      dest=/usr/local/share/monitoring_agent/
+    become: yes
+    when: cron_job_pids_count.stdout == "0"
+
+  - name: Installing python-pip
+    apt: name=python-pip state=present update_cache=yes
+    become: yes
+    when: cron_job_pids_count.stdout == "0"
+
+  - name: Installing Flask
+    pip: name=Flask
+    become: yes
+    when: cron_job_pids_count.stdout == "0"
+
+  - name: install python-kombu
+    apt: name=python-kombu state=present
+    become: yes
+    when: cron_job_pids_count.stdout == "0"
+
+  - name: Initiate monitoring agent cron job
+    command: python monitoring_agent.py &
+    async: 9999999999999999
+    poll: 0
+    become: yes
+    when: cron_job_pids_count.stdout == "0"
+    args:
+       chdir: /usr/local/share/monitoring_agent/
+
+  - name : starting monitoring agent
+    uri:
+      url: http://localhost:5004/monitoring/agent/exampleservice/start
+      method: POST
+      body: '{ "target":"{{target_uri}}", "keystone_user_id": "{{keystone_user_id}}", "keystone_tenant_id": "{{keystone_tenant_id}}" }'
+      force_basic_auth: yes
+      status_code: 200
+      body_format: json    
diff --git a/xos/synchronizer/steps/sync_exampletenant.py b/xos/synchronizer/steps/sync_exampletenant.py
index 80685e9..1b2f619 100644
--- a/xos/synchronizer/steps/sync_exampletenant.py
+++ b/xos/synchronizer/steps/sync_exampletenant.py
@@ -1,13 +1,16 @@
 import os
 import sys
 from django.db.models import Q, F
-from core.models import ModelLink, CoarseTenant
+from core.models import ModelLink, CoarseTenant, ServiceMonitoringAgentInfo
 from services.exampleservice.models import ExampleService, ExampleTenant
 from synchronizers.base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
+from xos.logger import Logger, logging
 
 parentdir = os.path.join(os.path.dirname(__file__), "..")
 sys.path.insert(0, parentdir)
 
+logger = Logger(level=logging.INFO)
+
 class SyncExampleTenant(SyncInstanceUsingAnsible):
 
     provides = [ExampleTenant]
@@ -20,7 +23,7 @@
 
     service_key_name = "/opt/xos/synchronizers/exampleservice/exampleservice_private_key"
 
-    watches = [ModelLink(CoarseTenant,via='coarsetenant')]
+    watches = [ModelLink(CoarseTenant,via='coarsetenant'), ModelLink(ServiceMonitoringAgentInfo,via='monitoringagentinfo')]
 
     def __init__(self, *args, **kwargs):
         super(SyncExampleTenant, self).__init__(*args, **kwargs)
@@ -61,3 +64,33 @@
         # when the instance holding the exampleservice is deleted.
         pass
 
+    def handle_service_monitoringagentinfo_watch_notification(self, monitoring_agent_info):
+        if not monitoring_agent_info.service:
+            logger.info("handle watch notifications for service monitoring agent info...ignoring because service attribute in monitoring agent info:%s is null" % (monitoring_agent_info))
+            return
+
+        if not monitoring_agent_info.target_uri:
+            logger.info("handle watch notifications for service monitoring agent info...ignoring because target_uri attribute in monitoring agent info:%s is null" % (monitoring_agent_info))
+            return
+
+        objs = ExampleTenant.get_tenant_objects().all()
+        for obj in objs:
+            if obj.provider_service.id != monitoring_agent_info.service.id:
+                logger.info("handle watch notifications for service monitoring agent info...ignoring because service attribute in monitoring agent info:%s is not matching" % (monitoring_agent_info))
+                return
+
+            instance = self.get_instance(obj)
+            if not instance:
+               logger.warn("handle watch notifications for service monitoring agent info...: No valid instance found for object %s" % (str(obj)))
+               return
+
+            logger.info("handling watch notification for monitoring agent info:%s for ExampleTenant object:%s" % (monitoring_agent_info, obj))
+
+            #Run ansible playbook to update the routing table entries in the instance
+            fields = self.get_ansible_fields(instance)
+            fields["ansible_tag"] =  obj.__class__.__name__ + "_" + str(obj.id) + "_monitoring"
+            fields["target_uri"] = monitoring_agent_info.target_uri
+
+            template_name = "monitoring_agent.yaml"
+            super(SyncExampleTenant, self).run_playbook(obj, fields, template_name)
+        pass