Srikanth Vavilapalli | b9bcda7 | 2017-01-19 18:25:39 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | from flask import request, Request, jsonify |
| 3 | from flask import Flask |
| 4 | from flask import make_response |
| 5 | from kombu.connection import BrokerConnection |
| 6 | from kombu.messaging import Exchange, Queue, Consumer, Producer |
| 7 | import logging |
| 8 | import logging.handlers |
| 9 | import logging.config |
| 10 | import exampleservice_stats as stats |
| 11 | import threading |
| 12 | import subprocess |
| 13 | import six |
| 14 | import uuid |
| 15 | import datetime |
| 16 | from urlparse import urlparse |
| 17 | app = Flask(__name__) |
| 18 | |
| 19 | start_publish = False |
| 20 | keystone_tenant_id='3a397e70f64e4e40b69b6266c634d9d0' |
| 21 | keystone_user_id='1e3ce043029547f1a61c1996d1a531a2' |
| 22 | rabbit_user='openstack' |
| 23 | rabbit_password='80608318c273f348a7c3' |
| 24 | rabbit_host='10.11.10.1' |
| 25 | rabbit_exchange='cord' |
| 26 | publisher_id='exampleservice_publisher' |
| 27 | |
| 28 | @app.route('/monitoring/agent/exampleservice/start',methods=['POST']) |
| 29 | def exampleservice_start_monitoring_agent(): |
| 30 | global start_publish, rabbit_user, rabbit_password, rabbit_host, rabbit_exchange |
Srikanth Vavilapalli | 974c9ce | 2017-01-25 01:50:27 +0000 | [diff] [blame^] | 31 | global keystone_tenant_id, keystone_user_id, publisher_id |
Srikanth Vavilapalli | b9bcda7 | 2017-01-19 18:25:39 +0000 | [diff] [blame] | 32 | try: |
| 33 | # To do validation of user inputs for all the functions |
| 34 | target = request.json['target'] |
| 35 | logging.debug("target:%s",target) |
| 36 | keystone_user_id = request.json['keystone_user_id'] |
| 37 | keystone_tenant_id = request.json['keystone_tenant_id'] |
| 38 | url = urlparse(target) |
| 39 | rabbit_user = url.username |
| 40 | rabbit_password = url.password |
| 41 | rabbit_host = url.hostname |
| 42 | |
| 43 | setup_rabbit_mq_channel() |
| 44 | |
| 45 | start_publish = True |
| 46 | periodic_publish() |
| 47 | |
| 48 | logging.info("Exampleservice monitoring is enabled") |
| 49 | return "Exampleservice monitoring is enabled" |
| 50 | except Exception as e: |
| 51 | return e.__str__() |
| 52 | |
| 53 | @app.route('/monitoring/agent/exampleservice/stop',methods=['POST']) |
| 54 | def openstack_stop(): |
| 55 | global start_publish |
| 56 | start_publish = False |
| 57 | logging.info ("Exampleservice monitoring is stopped") |
| 58 | return "Exampleservice monitoring is stopped" |
| 59 | |
| 60 | |
| 61 | producer = None |
| 62 | def setup_rabbit_mq_channel(): |
| 63 | global producer |
| 64 | global rabbit_user, rabbit_password, rabbit_host, rabbit_exchange,publisher_id |
| 65 | service_exchange = Exchange(rabbit_exchange, "topic", durable=False) |
| 66 | # connections/channels |
| 67 | connection = BrokerConnection(rabbit_host, rabbit_user, rabbit_password) |
| 68 | logging.info('Connection to RabbitMQ server successful') |
| 69 | channel = connection.channel() |
| 70 | # produce |
| 71 | producer = Producer(channel, exchange=service_exchange, routing_key='notifications.info') |
| 72 | p = subprocess.Popen('hostname', shell=True, stdout=subprocess.PIPE) |
| 73 | (hostname, error) = p.communicate() |
| 74 | publisher_id = publisher_id + '_on_' + hostname |
| 75 | logging.info('publisher_id=%s',publisher_id) |
| 76 | |
| 77 | def publish_exampleservice_stats(example_stats): |
| 78 | global producer |
| 79 | global keystone_tenant_id, keystone_user_id, publisher_id |
| 80 | |
| 81 | for k,v in example_stats.iteritems(): |
| 82 | msg = {'event_type': 'cord.'+k, |
| 83 | 'message_id':six.text_type(uuid.uuid4()), |
| 84 | 'publisher_id': publisher_id, |
| 85 | 'timestamp':datetime.datetime.now().isoformat(), |
| 86 | 'priority':'INFO', |
Srikanth Vavilapalli | 974c9ce | 2017-01-25 01:50:27 +0000 | [diff] [blame^] | 87 | 'payload': {'counter_name':k, |
| 88 | 'counter_unit':v['unit'], |
| 89 | 'counter_volume':v['val'], |
| 90 | 'counter_type':v['metric_type'], |
Srikanth Vavilapalli | b9bcda7 | 2017-01-19 18:25:39 +0000 | [diff] [blame] | 91 | 'resource_id':'exampleservice', |
| 92 | 'user_id':keystone_user_id, |
| 93 | 'tenant_id':keystone_tenant_id |
| 94 | } |
| 95 | } |
| 96 | producer.publish(msg) |
| 97 | logging.debug('Publishing exampleservice event: %s', msg) |
| 98 | |
| 99 | def periodic_publish(): |
| 100 | global start_publish |
| 101 | if not start_publish: |
| 102 | return |
| 103 | stats.retrieve_status_page() |
| 104 | resParse = stats.parse_status_page() |
| 105 | logging.debug ("publish:%(data)s" % {'data':resParse}) |
| 106 | publish_exampleservice_stats(resParse) |
Srikanth Vavilapalli | 974c9ce | 2017-01-25 01:50:27 +0000 | [diff] [blame^] | 107 | threading.Timer(60, periodic_publish).start() |
Srikanth Vavilapalli | b9bcda7 | 2017-01-19 18:25:39 +0000 | [diff] [blame] | 108 | |
| 109 | if __name__ == "__main__": |
| 110 | logging.config.fileConfig('monitoring_agent.conf', disable_existing_loggers=False) |
| 111 | logging.info ("Exampleservice monitoring is listening on port 5004") |
| 112 | app.run(host="0.0.0.0",port=5004,debug=False) |