update monitoring test scripts
[osm/vim-emu.git] / src / emuvim / cli / prometheus.py
1 """
2 Prometheus API helper functions
3 (c) 2016 by Steven Van Rossem <steven.vanrossem@intec.ugent.be>
4 """
5
6 import urllib2
7 import ast
8
9 # set this to localhost for now
10 # this is correct for son-emu started outside of a container or as a container with net=host
11 prometheus_ip = '127.0.0.1'
12 prometheus_port = '9090'
13 prometheus_REST_api = 'http://{0}:{1}'.format(prometheus_ip, prometheus_port)
14
15
16 def query_Prometheus(query):
17 url = prometheus_REST_api + '/' + 'api/v1/query?query=' + query
18 # logging.info('query:{0}'.format(url))
19 req = urllib2.Request(url)
20 ret = urllib2.urlopen(req).read()
21 ret = ast.literal_eval(ret)
22 if ret['status'] == 'success':
23 # logging.info('return:{0}'.format(ret))
24 try:
25 ret = ret['data']['result'][0]['value']
26 except:
27 ret = None
28 else:
29 ret = None
30 return ret