Matteo Scandolo | eb0d11c | 2017-08-08 13:05:26 -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 | |
Scott Baker | 31acc65 | 2016-06-23 15:47:56 -0700 | [diff] [blame] | 17 | #!/usr/bin/env python |
| 18 | import web |
| 19 | import ConfigParser |
| 20 | import io |
| 21 | import json |
| 22 | from ceilometerclient import client |
| 23 | import logging |
| 24 | import urllib |
| 25 | import urllib2 |
| 26 | from urlparse import urlparse |
| 27 | from wsgilog import WsgiLog |
| 28 | |
| 29 | web.config.debug=False |
| 30 | |
| 31 | logfile = "ceilometer_proxy_server.log" |
| 32 | level=logging.INFO |
| 33 | logger=logging.getLogger('ceilometer_proxy_server') |
| 34 | logger.setLevel(level) |
| 35 | handler=logging.handlers.RotatingFileHandler(logfile,maxBytes=1000000, backupCount=1) |
| 36 | logger.addHandler(handler) |
| 37 | |
| 38 | class FileLog(WsgiLog): |
| 39 | def __init__(self, application): |
| 40 | WsgiLog.__init__( |
| 41 | self, |
| 42 | application, |
| 43 | logformat = '%(message)s', |
| 44 | tofile = True, |
| 45 | toprint = True, |
| 46 | prnlevel = level, |
| 47 | file = logfile, |
| 48 | backups =1 |
| 49 | ) |
| 50 | def __call__(self, environ, start_response): |
| 51 | def hstart_response(status, response_headers, *args): |
| 52 | out = start_response(status, response_headers, *args) |
| 53 | try: |
| 54 | logline=environ["SERVER_PROTOCOL"]+" "+environ["REQUEST_METHOD"]+" "+environ["REQUEST_URI"]+" - "+status |
| 55 | except err: |
| 56 | logline="Could not log <%s> due to err <%s>" % (str(environ), err) |
| 57 | logger.info(logline) |
| 58 | |
| 59 | return out |
| 60 | |
| 61 | return super(FileLog, self).__call__(environ, hstart_response) |
| 62 | |
| 63 | #TODOs: |
| 64 | #-See if we can avoid using python-ceilometerclient and instead use the REST calls directly with AuthToken |
| 65 | # |
| 66 | urls = ( |
| 67 | r'^/v2/meters$', 'meter_list', |
| 68 | r'^/v2/meters/(?P<meter_name>[A-Za-z0-9_:.\-]+)/statistics$', 'statistics_list', |
| 69 | r'^/v2/samples$', 'sample_list', |
| 70 | r'^/v2/resources$', 'resource_list', |
| 71 | r'^/v2/subscribe$', 'pubsub_handler', |
| 72 | ) |
| 73 | |
| 74 | app = web.application(urls, globals()) |
| 75 | |
| 76 | config = None |
| 77 | ceilometer_client = None |
| 78 | |
| 79 | |
| 80 | def parse_ceilometer_proxy_config(): |
| 81 | global config |
| 82 | config = ConfigParser.RawConfigParser(allow_no_value=True) |
| 83 | config.read('ceilometer_proxy_config') |
| 84 | |
| 85 | def ceilometerclient(): |
| 86 | global config, ceilometer_client |
| 87 | if ceilometer_client: |
| 88 | return ceilometer_client |
| 89 | |
| 90 | if not config: |
| 91 | parse_ceilometer_proxy_config() |
| 92 | |
| 93 | keystone = {} |
| 94 | keystone['os_username']=config.get('default','admin_user') |
| 95 | keystone['os_password']=config.get('default','admin_password') |
| 96 | keystone['os_auth_url']=config.get('default','auth_url') |
| 97 | keystone['os_tenant_name']=config.get('default','admin_tenant') |
| 98 | ceilometer_client = client.get_client(2,**keystone) |
| 99 | logger.info('ceilometer get_client is successful') |
| 100 | return ceilometer_client |
| 101 | |
| 102 | def make_query(user_id=None, tenant_id=None, resource_id=None, |
| 103 | user_ids=None, tenant_ids=None, resource_ids=None): |
| 104 | """Returns query built from given parameters. |
| 105 | |
| 106 | This query can be then used for querying resources, meters and |
| 107 | statistics. |
| 108 | |
| 109 | :Parameters: |
| 110 | - `user_id`: user_id, has a priority over list of ids |
| 111 | - `tenant_id`: tenant_id, has a priority over list of ids |
| 112 | - `resource_id`: resource_id, has a priority over list of ids |
| 113 | - `user_ids`: list of user_ids |
| 114 | - `tenant_ids`: list of tenant_ids |
| 115 | - `resource_ids`: list of resource_ids |
| 116 | """ |
| 117 | user_ids = user_ids or [] |
| 118 | tenant_ids = tenant_ids or [] |
| 119 | resource_ids = resource_ids or [] |
| 120 | |
| 121 | query = [] |
| 122 | if user_id: |
| 123 | user_ids = [user_id] |
| 124 | for u_id in user_ids: |
| 125 | query.append({"field": "user_id", "op": "eq", "value": u_id}) |
| 126 | |
| 127 | if tenant_id: |
| 128 | tenant_ids = [tenant_id] |
| 129 | for t_id in tenant_ids: |
| 130 | query.append({"field": "project_id", "op": "eq", "value": t_id}) |
| 131 | |
| 132 | if resource_id: |
| 133 | resource_ids = [resource_id] |
| 134 | for r_id in resource_ids: |
| 135 | query.append({"field": "resource_id", "op": "eq", "value": r_id}) |
| 136 | |
| 137 | return query |
| 138 | |
| 139 | def filter_query_params(query_params): |
| 140 | new_query=[] |
| 141 | i=0 |
| 142 | user_specified_tenants=[] |
| 143 | for field in query_params['q.field']: |
| 144 | if (field != 'project_id') and (field != 'project'): |
| 145 | query = {} |
| 146 | query['field']=field |
| 147 | if query_params['q.op'][i] != '': |
| 148 | query['op']=query_params['q.op'][i] |
| 149 | query['value']=query_params['q.value'][i] |
| 150 | new_query.append(query) |
| 151 | else: |
| 152 | user_specified_tenants.append(query_params['q.value'][i]) |
| 153 | i=i+1 |
| 154 | return new_query,user_specified_tenants |
| 155 | |
| 156 | class meter_list: |
| 157 | def GET(self): |
| 158 | global config |
| 159 | keyword_args = { |
| 160 | "q.field": [], |
| 161 | "q.op": [], |
| 162 | "q.type": [], |
| 163 | "q.value": [], |
| 164 | } |
| 165 | query_params = web.input(**keyword_args) |
| 166 | new_query, user_specified_tenants = filter_query_params(query_params) |
| 167 | |
| 168 | client = ceilometerclient() |
| 169 | meters=[] |
| 170 | for (k,v) in config.items('allowed_tenants'): |
| 171 | if user_specified_tenants and (k not in user_specified_tenants): |
| 172 | continue |
| 173 | final_query=[] |
| 174 | final_query.extend(new_query) |
| 175 | query = make_query(tenant_id=k) |
| 176 | final_query.extend(query) |
| 177 | logger.debug('final query=%s',final_query) |
| 178 | results = client.meters.list(q=final_query) |
| 179 | meters.extend(results) |
| 180 | return json.dumps([ob._info for ob in meters]) |
| 181 | |
| 182 | class statistics_list: |
| 183 | def GET(self, meter_name): |
| 184 | global config |
| 185 | keyword_args = { |
| 186 | "q.field": [], |
| 187 | "q.op": [], |
| 188 | "q.type": [], |
| 189 | "q.value": [], |
| 190 | "period": None |
| 191 | } |
| 192 | query_params = web.input(**keyword_args) |
| 193 | new_query, user_specified_tenants = filter_query_params(query_params) |
| 194 | |
| 195 | client = ceilometerclient() |
| 196 | period = query_params.period |
| 197 | statistics = [] |
| 198 | for (k,v) in config.items('allowed_tenants'): |
| 199 | if user_specified_tenants and (k not in user_specified_tenants): |
| 200 | continue |
| 201 | final_query=[] |
| 202 | final_query.extend(new_query) |
| 203 | query = make_query(tenant_id=k) |
| 204 | final_query.extend(query) |
| 205 | logger.debug('final query=%s',final_query) |
| 206 | results = client.statistics.list(meter_name=meter_name, q=final_query, period=period) |
| 207 | statistics.extend(results) |
| 208 | return json.dumps([ob._info for ob in statistics]) |
| 209 | |
| 210 | class sample_list: |
| 211 | def GET(self): |
| 212 | global config |
| 213 | keyword_args = { |
| 214 | "q.field": [], |
| 215 | "q.op": [], |
| 216 | "q.type": [], |
| 217 | "q.value": [], |
| 218 | "limit": None, |
| 219 | } |
| 220 | query_params = web.input(**keyword_args) |
| 221 | new_query, user_specified_tenants = filter_query_params(query_params) |
| 222 | |
| 223 | client = ceilometerclient() |
| 224 | limit=query_params.limit |
| 225 | samples=[] |
| 226 | for (k,v) in config.items('allowed_tenants'): |
| 227 | if user_specified_tenants and (k not in user_specified_tenants): |
| 228 | continue |
| 229 | final_query=[] |
| 230 | final_query.extend(new_query) |
| 231 | query = make_query(tenant_id=k) |
| 232 | final_query.extend(query) |
| 233 | logger.debug('final query=%s',final_query) |
| 234 | results = client.new_samples.list(q=final_query,limit=limit) |
| 235 | samples.extend(results) |
| 236 | return json.dumps([ob._info for ob in samples]) |
| 237 | |
| 238 | class resource_list: |
| 239 | def GET(self): |
| 240 | global config |
| 241 | keyword_args = { |
| 242 | "q.field": [], |
| 243 | "q.op": [], |
| 244 | "q.type": [], |
| 245 | "q.value": [], |
| 246 | "limit": None, |
| 247 | "links": None, |
| 248 | } |
| 249 | query_params = web.input(**keyword_args) |
| 250 | new_query, user_specified_tenants = filter_query_params(query_params) |
| 251 | |
| 252 | client = ceilometerclient() |
| 253 | limit=query_params.limit |
| 254 | links=query_params.links |
| 255 | resources=[] |
| 256 | for (k,v) in config.items('allowed_tenants'): |
| 257 | if user_specified_tenants and (k not in user_specified_tenants): |
| 258 | continue |
| 259 | final_query=[] |
| 260 | final_query.extend(new_query) |
| 261 | query = make_query(tenant_id=k) |
| 262 | final_query.extend(query) |
| 263 | logger.debug('final query=%s',final_query) |
| 264 | results = client.resources.list(q=final_query, limit=limit, links=links) |
| 265 | resources.extend(results) |
| 266 | return json.dumps([ob._info for ob in resources]) |
| 267 | |
| 268 | class pubsub_handler: |
| 269 | def POST(self): |
| 270 | global config |
| 271 | parse_ceilometer_proxy_config() |
| 272 | ceilometer_pub_sub_url = config.get('default', 'ceilometer_pub_sub_url') |
| 273 | url = urlparse(ceilometer_pub_sub_url) |
| 274 | if (not url.scheme) or (not url.netloc): |
| 275 | raise Exception("Ceilometer PUB/SUB URL not set") |
| 276 | ceilometer_pub_sub_url = url.scheme + "://" + url.netloc + "/subscribe" |
| 277 | data_str = unicode(web.data(),'iso-8859-1') |
| 278 | post_data = json.loads(data_str) |
| 279 | final_query=[] |
| 280 | for (k,v) in config.items('allowed_tenants'): |
| 281 | query = make_query(tenant_id=k) |
| 282 | final_query.extend(query) |
| 283 | if not final_query: |
| 284 | raise Exception("Not allowed to subscribe to any meters") |
| 285 | post_data["query"] = final_query |
| 286 | #TODO: The PUB/SUB url needs to be read from config |
| 287 | put_request = urllib2.Request(ceilometer_pub_sub_url, json.dumps(post_data)) |
| 288 | put_request.get_method = lambda: 'SUB' |
| 289 | put_request.add_header('Content-Type', 'application/json') |
| 290 | response = urllib2.urlopen(put_request) |
| 291 | response_text = response.read() |
| 292 | return json.dumps(response_text) |
| 293 | |
| 294 | def DELETE(self): |
| 295 | ceilometer_pub_sub_url = config.get('default', 'ceilometer_pub_sub_url') |
| 296 | url = urlparse(ceilometer_pub_sub_url) |
| 297 | if (not url.scheme) or (not url.netloc): |
| 298 | raise Exception("Ceilometer PUB/SUB URL not set") |
| 299 | ceilometer_pub_sub_url = url.scheme + "://" + url.netloc + "/unsubscribe" |
| 300 | data_str = web.data() |
| 301 | #TODO: The PUB/SUB url needs to be read from config |
| 302 | put_request = urllib2.Request(ceilometer_pub_sub_url, data_str) |
| 303 | put_request.get_method = lambda: 'UNSUB' |
| 304 | put_request.add_header('Content-Type', 'application/json') |
| 305 | response = urllib2.urlopen(put_request) |
| 306 | response_text = response.read() |
| 307 | return json.dumps(response_text) |
| 308 | |
| 309 | if __name__ == "__main__": |
| 310 | app.run(FileLog) |