This repository has been archived by the owner on Oct 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ceilomarius.py
executable file
·110 lines (90 loc) · 4.33 KB
/
ceilomarius.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import os
import requests
class Ceilomarius:
def __init__(self, api_version, endpoint, token, verbose):
self.api_version = api_version
self.endpoint = endpoint
self.token = token
self.verbose = verbose
def prepare_headers(self):
return {'User-Agent': 'ceilomarius',
'X-Auth-Token': str(self.token),
'Content-Type': 'application/json'}
def get_metric(self, meter_name, q = [], limit = 10):
headers = self.prepare_headers()
query = {"limit": limit}
if q:
query["q"] = q
url = self.endpoint + '/v' + str(self.api_version) + '/meters/' + meter_name
if self.verbose:
print( "INFO: Parameters of the request:" )
print( "INFO: > url: %s" % url )
print( "INFO: > headers: %s" % headers )
print( "INFO: > query: %s" % query )
req = requests.get(url = url, json = query, headers = headers)
if self.verbose:
print( "INFO: Got the response: {} [{}] on {}".format(req.status_code, req.reason, req.headers.get('date')) )
print( "INFO: Your request ID: %s" % req.headers.get('x-openstack-request-id') )
print( "INFO: The whole operation took: %s [h:m:s.us]" % req.elapsed )
return req
def get_meters_for_resource(self, resource, limit = 10):
# skip those weird resources with empty IDs
if not resource.id:
if self.verbose:
print( "WARNING: Wrong resource ID {}. Skipping.".format(resource.id) )
return {}
headers = self.prepare_headers()
url = self.endpoint + '/v' + str(self.api_version) + '/resources/' + str(resource.id)
resp = requests.get(url = url, headers = headers)
# get a list of dicts with urls to meters:
links = resp.json().get('links')
if self.verbose:
print( "INFO: Links associated with url %s:" % url )
print( links )
# check whether we have get meters
if links:
query = [{"field": "resource_id", "op": "eq", "value": str(resource.id)}]
# this dict will store responses associated with the specific meter name
meters = dict()
for link in links:
# the name of the meter can be retrieved from the 'rel' key
rel = link.get('rel')
# ignore the reference to the original url (where rel == 'self'):
if rel != 'self':
meters[rel] = self.get_metric(meter_name=rel, q=query, limit=limit).json()
return meters
else:
if self.verbose:
print( "WARNING: Empty list of links! Skipping the resource." )
return {}
def get_statistics(self, meter_name, q = [], count_only = False):
"""Returns statistics for a given query q
Arguments:
count_only -- set to True to only return the number of records.
"""
query = {}
headers = self.prepare_headers()
if q:
query["q"] = q
url = self.endpoint + '/v' + str(self.api_version) + '/meters/' + meter_name + ('/statistics?aggregate.func=count' if count_only else '/statistics')
if self.verbose:
print( "INFO: Parameters of the request:" )
print( "INFO: > url: %s" % url )
print( "INFO: > headers: %s" % headers )
print( "INFO: > query: %s" % query )
req = requests.get(url = url, json = query, headers = headers)
if self.verbose:
print( "INFO: Got the response: {} [{}] at {}".format(req.status_code, req.reason, req.headers.get('date')) )
print( "INFO: Your request ID: %s" % req.headers.get('x-openstack-request-id') )
print( "INFO: The whole operation took: %s [h:m:s.us]" % req.elapsed )
return req
def main():
ceilo = Ceilomarius(api_version = 2,
endpoint = os.environ['OS_CEILOMETER_URL'],
token = os.environ['OS_TOKEN'],
verbose = True)
query = [{"field": "metadata.event_type", "op": "eq", "value": "compute.instance.exists"}]
print( ceilo.get_metric(meter_name="instance", q=query, limit=1).json() )
print( ceilo.get_statistics(meter_name="instance", q=query).json() )
if __name__ == "__main__":
main()