blob: 1839ed5e7b2c354be20ffd3101e2d92cbc2053fa [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
31 try:
32 # To do validation of user inputs for all the functions
33 target = request.json['target']
34 logging.debug("target:%s",target)
35 keystone_user_id = request.json['keystone_user_id']
36 keystone_tenant_id = request.json['keystone_tenant_id']
37 url = urlparse(target)
38 rabbit_user = url.username
39 rabbit_password = url.password
40 rabbit_host = url.hostname
41
42 setup_rabbit_mq_channel()
43
44 start_publish = True
45 periodic_publish()
46
47 logging.info("Exampleservice monitoring is enabled")
48 return "Exampleservice monitoring is enabled"
49 except Exception as e:
50 return e.__str__()
51
52@app.route('/monitoring/agent/exampleservice/stop',methods=['POST'])
53def openstack_stop():
54 global start_publish
55 start_publish = False
56 logging.info ("Exampleservice monitoring is stopped")
57 return "Exampleservice monitoring is stopped"
58
59
60producer = None
61def setup_rabbit_mq_channel():
62 global producer
63 global rabbit_user, rabbit_password, rabbit_host, rabbit_exchange,publisher_id
64 service_exchange = Exchange(rabbit_exchange, "topic", durable=False)
65 # connections/channels
66 connection = BrokerConnection(rabbit_host, rabbit_user, rabbit_password)
67 logging.info('Connection to RabbitMQ server successful')
68 channel = connection.channel()
69 # produce
70 producer = Producer(channel, exchange=service_exchange, routing_key='notifications.info')
71 p = subprocess.Popen('hostname', shell=True, stdout=subprocess.PIPE)
72 (hostname, error) = p.communicate()
73 publisher_id = publisher_id + '_on_' + hostname
74 logging.info('publisher_id=%s',publisher_id)
75
76def publish_exampleservice_stats(example_stats):
77 global producer
78 global keystone_tenant_id, keystone_user_id, publisher_id
79
80 for k,v in example_stats.iteritems():
81 msg = {'event_type': 'cord.'+k,
82 'message_id':six.text_type(uuid.uuid4()),
83 'publisher_id': publisher_id,
84 'timestamp':datetime.datetime.now().isoformat(),
85 'priority':'INFO',
86 'payload': {'name':k,
87 'unit':v['unit'],
88 'result':v['val'],
89 'type':v['metric_type'],
90 'resource_id':'exampleservice',
91 'user_id':keystone_user_id,
92 'tenant_id':keystone_tenant_id
93 }
94 }
95 producer.publish(msg)
96 logging.debug('Publishing exampleservice event: %s', msg)
97
98def periodic_publish():
99 global start_publish
100 if not start_publish:
101 return
102 stats.retrieve_status_page()
103 resParse = stats.parse_status_page()
104 logging.debug ("publish:%(data)s" % {'data':resParse})
105 publish_exampleservice_stats(resParse)
106 threading.Timer(5, periodic_publish).start()
107
108if __name__ == "__main__":
109 logging.config.fileConfig('monitoring_agent.conf', disable_existing_loggers=False)
110 logging.info ("Exampleservice monitoring is listening on port 5004")
111 app.run(host="0.0.0.0",port=5004,debug=False)