blob: 457244998e30c45c0111f5efb6bac168b82b9a9d [file] [log] [blame]
stevenvanrossem2fdfbf42016-05-13 15:08:47 +02001"""
peusterm79ef6ae2016-07-08 13:53:57 +02002Copyright (c) 2015 SONATA-NFV
3ALL RIGHTS RESERVED.
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16
17Neither the name of the SONATA-NFV [, ANY ADDITIONAL AFFILIATION]
18nor the names of its contributors may be used to endorse or promote
19products derived from this software without specific prior written
20permission.
21
22This work has been performed in the framework of the SONATA project,
23funded by the European Commission under Grant number 671517 through
24the Horizon 2020 and 5G-PPP programmes. The authors would like to
25acknowledge the contributions of their colleagues of the SONATA
26partner consortium (www.sonata-nfv.eu).
stevenvanrossem2fdfbf42016-05-13 15:08:47 +020027"""
28
stevenvanrossem1a6843a2016-05-19 12:19:36 +020029#import urllib2
30import requests
31#import ast
stevenvanrossem2fdfbf42016-05-13 15:08:47 +020032
stevenvanrossem48db0512016-05-18 15:43:24 +020033# set this to localhost for now
34# this is correct for son-emu started outside of a container or as a container with net=host
stevenvanrossem73efd192016-06-29 01:44:07 +020035#TODO prometheus sdk DB is started outside of emulator, place these globals in an external SDK config file?
stevenvanrossem48db0512016-05-18 15:43:24 +020036prometheus_ip = '127.0.0.1'
stevenvanrossem2fdfbf42016-05-13 15:08:47 +020037prometheus_port = '9090'
38prometheus_REST_api = 'http://{0}:{1}'.format(prometheus_ip, prometheus_port)
39
40
41def query_Prometheus(query):
42 url = prometheus_REST_api + '/' + 'api/v1/query?query=' + query
43 # logging.info('query:{0}'.format(url))
stevenvanrossem1a6843a2016-05-19 12:19:36 +020044 #req = urllib2.Request(url)
45 req = requests.get(url)
46 #ret = urllib2.urlopen(req).read()
47 #ret = ast.literal_eval(ret)
48 ret = req.json()
stevenvanrossem2fdfbf42016-05-13 15:08:47 +020049 if ret['status'] == 'success':
50 # logging.info('return:{0}'.format(ret))
51 try:
52 ret = ret['data']['result'][0]['value']
53 except:
54 ret = None
55 else:
56 ret = None
57 return ret