blob: 19204b16a59d877d9a4bf9eeb0ac98c75633700d [file] [log] [blame]
Srikanth Vavilapallib9bcda72017-01-19 18:25:39 +00001#!/usr/bin/python
2from flask import request, Request, jsonify
3from flask import Flask
4from flask import make_response
5from kombu.connection import BrokerConnection
6from kombu.messaging import Exchange, Queue, Consumer, Producer
7import logging
8import logging.handlers
9import logging.config
10import exampleservice_stats as stats
11import threading
12import subprocess
13import six
14import uuid
15import datetime
16from urlparse import urlparse
17app = Flask(__name__)
18
19start_publish = False
20keystone_tenant_id='3a397e70f64e4e40b69b6266c634d9d0'
21keystone_user_id='1e3ce043029547f1a61c1996d1a531a2'
22rabbit_user='openstack'
23rabbit_password='80608318c273f348a7c3'
24rabbit_host='10.11.10.1'
25rabbit_exchange='cord'
26publisher_id='exampleservice_publisher'
27
28@app.route('/monitoring/agent/exampleservice/start',methods=['POST'])
29def exampleservice_start_monitoring_agent():
30 global start_publish, rabbit_user, rabbit_password, rabbit_host, rabbit_exchange
Srikanth Vavilapalli974c9ce2017-01-25 01:50:27 +000031 global keystone_tenant_id, keystone_user_id, publisher_id
Srikanth Vavilapallib9bcda72017-01-19 18:25:39 +000032 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'])
54def 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
61producer = None
62def 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
77def 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 Vavilapalli974c9ce2017-01-25 01:50:27 +000087 'payload': {'counter_name':k,
88 'counter_unit':v['unit'],
89 'counter_volume':v['val'],
90 'counter_type':v['metric_type'],
Srikanth Vavilapallib9bcda72017-01-19 18:25:39 +000091 '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
99def 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 Vavilapalli974c9ce2017-01-25 01:50:27 +0000107 threading.Timer(60, periodic_publish).start()
Srikanth Vavilapallib9bcda72017-01-19 18:25:39 +0000108
109if __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)