Matteo Scandolo | 35113f7 | 2017-08-08 13:05:25 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
Srikanth Vavilapalli | b9bcda7 | 2017-01-19 18:25:39 +0000 | [diff] [blame] | 17 | #!/usr/bin/python |
| 18 | from flask import request, Request, jsonify |
| 19 | from flask import Flask |
| 20 | from flask import make_response |
| 21 | from kombu.connection import BrokerConnection |
| 22 | from kombu.messaging import Exchange, Queue, Consumer, Producer |
| 23 | import logging |
| 24 | import logging.handlers |
| 25 | import logging.config |
| 26 | import exampleservice_stats as stats |
| 27 | import threading |
| 28 | import subprocess |
| 29 | import six |
| 30 | import uuid |
| 31 | import datetime |
| 32 | from urlparse import urlparse |
| 33 | app = Flask(__name__) |
| 34 | |
| 35 | start_publish = False |
| 36 | keystone_tenant_id='3a397e70f64e4e40b69b6266c634d9d0' |
| 37 | keystone_user_id='1e3ce043029547f1a61c1996d1a531a2' |
| 38 | rabbit_user='openstack' |
| 39 | rabbit_password='80608318c273f348a7c3' |
| 40 | rabbit_host='10.11.10.1' |
| 41 | rabbit_exchange='cord' |
| 42 | publisher_id='exampleservice_publisher' |
| 43 | |
| 44 | @app.route('/monitoring/agent/exampleservice/start',methods=['POST']) |
| 45 | def exampleservice_start_monitoring_agent(): |
| 46 | global start_publish, rabbit_user, rabbit_password, rabbit_host, rabbit_exchange |
Srikanth Vavilapalli | 974c9ce | 2017-01-25 01:50:27 +0000 | [diff] [blame] | 47 | global keystone_tenant_id, keystone_user_id, publisher_id |
Srikanth Vavilapalli | b9bcda7 | 2017-01-19 18:25:39 +0000 | [diff] [blame] | 48 | try: |
| 49 | # To do validation of user inputs for all the functions |
| 50 | target = request.json['target'] |
| 51 | logging.debug("target:%s",target) |
| 52 | keystone_user_id = request.json['keystone_user_id'] |
| 53 | keystone_tenant_id = request.json['keystone_tenant_id'] |
| 54 | url = urlparse(target) |
| 55 | rabbit_user = url.username |
| 56 | rabbit_password = url.password |
| 57 | rabbit_host = url.hostname |
| 58 | |
| 59 | setup_rabbit_mq_channel() |
| 60 | |
| 61 | start_publish = True |
| 62 | periodic_publish() |
| 63 | |
| 64 | logging.info("Exampleservice monitoring is enabled") |
| 65 | return "Exampleservice monitoring is enabled" |
| 66 | except Exception as e: |
| 67 | return e.__str__() |
| 68 | |
| 69 | @app.route('/monitoring/agent/exampleservice/stop',methods=['POST']) |
| 70 | def openstack_stop(): |
| 71 | global start_publish |
| 72 | start_publish = False |
| 73 | logging.info ("Exampleservice monitoring is stopped") |
| 74 | return "Exampleservice monitoring is stopped" |
| 75 | |
| 76 | |
| 77 | producer = None |
| 78 | def setup_rabbit_mq_channel(): |
| 79 | global producer |
| 80 | global rabbit_user, rabbit_password, rabbit_host, rabbit_exchange,publisher_id |
| 81 | service_exchange = Exchange(rabbit_exchange, "topic", durable=False) |
| 82 | # connections/channels |
| 83 | connection = BrokerConnection(rabbit_host, rabbit_user, rabbit_password) |
| 84 | logging.info('Connection to RabbitMQ server successful') |
| 85 | channel = connection.channel() |
| 86 | # produce |
| 87 | producer = Producer(channel, exchange=service_exchange, routing_key='notifications.info') |
| 88 | p = subprocess.Popen('hostname', shell=True, stdout=subprocess.PIPE) |
| 89 | (hostname, error) = p.communicate() |
| 90 | publisher_id = publisher_id + '_on_' + hostname |
| 91 | logging.info('publisher_id=%s',publisher_id) |
| 92 | |
| 93 | def publish_exampleservice_stats(example_stats): |
| 94 | global producer |
| 95 | global keystone_tenant_id, keystone_user_id, publisher_id |
| 96 | |
| 97 | for k,v in example_stats.iteritems(): |
| 98 | msg = {'event_type': 'cord.'+k, |
| 99 | 'message_id':six.text_type(uuid.uuid4()), |
| 100 | 'publisher_id': publisher_id, |
| 101 | 'timestamp':datetime.datetime.now().isoformat(), |
| 102 | 'priority':'INFO', |
Srikanth Vavilapalli | 974c9ce | 2017-01-25 01:50:27 +0000 | [diff] [blame] | 103 | 'payload': {'counter_name':k, |
| 104 | 'counter_unit':v['unit'], |
| 105 | 'counter_volume':v['val'], |
| 106 | 'counter_type':v['metric_type'], |
Srikanth Vavilapalli | b9bcda7 | 2017-01-19 18:25:39 +0000 | [diff] [blame] | 107 | 'resource_id':'exampleservice', |
| 108 | 'user_id':keystone_user_id, |
| 109 | 'tenant_id':keystone_tenant_id |
| 110 | } |
| 111 | } |
| 112 | producer.publish(msg) |
| 113 | logging.debug('Publishing exampleservice event: %s', msg) |
| 114 | |
| 115 | def periodic_publish(): |
| 116 | global start_publish |
| 117 | if not start_publish: |
| 118 | return |
| 119 | stats.retrieve_status_page() |
| 120 | resParse = stats.parse_status_page() |
| 121 | logging.debug ("publish:%(data)s" % {'data':resParse}) |
| 122 | publish_exampleservice_stats(resParse) |
Srikanth Vavilapalli | 974c9ce | 2017-01-25 01:50:27 +0000 | [diff] [blame] | 123 | threading.Timer(60, periodic_publish).start() |
Srikanth Vavilapalli | b9bcda7 | 2017-01-19 18:25:39 +0000 | [diff] [blame] | 124 | |
| 125 | if __name__ == "__main__": |
| 126 | logging.config.fileConfig('monitoring_agent.conf', disable_existing_loggers=False) |
| 127 | logging.info ("Exampleservice monitoring is listening on port 5004") |
| 128 | app.run(host="0.0.0.0",port=5004,debug=False) |