blob: 54699df7e80ab354a5cb36082c26c05ffb90355f [file] [log] [blame]
Matteo Scandolo35113f72017-08-08 13:05:25 -07001
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 Vavilapallib9bcda72017-01-19 18:25:39 +000017#!/usr/bin/python
18from flask import request, Request, jsonify
19from flask import Flask
20from flask import make_response
21from kombu.connection import BrokerConnection
22from kombu.messaging import Exchange, Queue, Consumer, Producer
23import logging
24import logging.handlers
25import logging.config
26import exampleservice_stats as stats
27import threading
28import subprocess
29import six
30import uuid
31import datetime
32from urlparse import urlparse
33app = Flask(__name__)
34
35start_publish = False
36keystone_tenant_id='3a397e70f64e4e40b69b6266c634d9d0'
37keystone_user_id='1e3ce043029547f1a61c1996d1a531a2'
38rabbit_user='openstack'
39rabbit_password='80608318c273f348a7c3'
40rabbit_host='10.11.10.1'
41rabbit_exchange='cord'
42publisher_id='exampleservice_publisher'
43
44@app.route('/monitoring/agent/exampleservice/start',methods=['POST'])
45def exampleservice_start_monitoring_agent():
46 global start_publish, rabbit_user, rabbit_password, rabbit_host, rabbit_exchange
Srikanth Vavilapalli974c9ce2017-01-25 01:50:27 +000047 global keystone_tenant_id, keystone_user_id, publisher_id
Srikanth Vavilapallib9bcda72017-01-19 18:25:39 +000048 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'])
70def 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
77producer = None
78def 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
93def 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 Vavilapalli974c9ce2017-01-25 01:50:27 +0000103 'payload': {'counter_name':k,
104 'counter_unit':v['unit'],
105 'counter_volume':v['val'],
106 'counter_type':v['metric_type'],
Srikanth Vavilapallib9bcda72017-01-19 18:25:39 +0000107 '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
115def 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 Vavilapalli974c9ce2017-01-25 01:50:27 +0000123 threading.Timer(60, periodic_publish).start()
Srikanth Vavilapallib9bcda72017-01-19 18:25:39 +0000124
125if __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)