forked from hpcloud-mon/mon-ceilometer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monclient.py
87 lines (73 loc) · 3.19 KB
/
monclient.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
from ceilometer.monclient import client
from ceilometer import publisher
import time
import calendar
from ceilometer.openstack.common import log
LOG = log.getLogger(__name__)
class monclient(publisher.PublisherBase):
"""Publisher to publish samples to monclient.
Example URL to place in pipeline.yaml:
- monclient://http://192.168.10.4:8080/v2.0?username=xxxx&password=yyyy
"""
def __init__(self, parsed_url):
super(monclient, self).__init__(parsed_url)
query_parms = parsed_url[3]
for query_parm in query_parms.split('&'):
name = query_parm.split('=')[0]
value = query_parm.split('=')[1]
if (name == 'username'):
username = value
LOG.debug(_('found username in query parameters'))
if (name == 'password'):
password = value
LOG.debug(_('found password in query parameters'))
endpoint = "http:" + parsed_url.path
LOG.debug(_('publishing samples to endpoint %s') % endpoint)
api_version = '2_0'
kwargs = {
'username': username,
'password': password,
'token': '82510970543135',
'tenant_id': '82510970543135',
'tenant_name': '',
'auth_url': '',
'service_type': '',
'endpoint_type': '',
'insecure': False
}
mon_client = client.Client(api_version, endpoint, **kwargs)
self.metrics = mon_client.metrics
def publish_samples(self, context, samples):
"""Main method called to publish samples.
"""
for sample in samples:
dimensions = {}
dimensions['project_id'] = sample.project_id
dimensions['resource_id'] = sample.resource_id
dimensions['source'] = sample.source
dimensions['type'] = sample.type
dimensions['unit'] = sample.unit
dimensions['user_id'] = sample.user_id
self._traverse_dict(dimensions, 'meta', sample.resource_metadata)
timeWithoutFractionalSeconds = sample.timestamp[0:19]
try:
seconds = \
calendar.timegm(time.strptime(timeWithoutFractionalSeconds,
"%Y-%m-%dT%H:%M:%S"))
except ValueError:
seconds = \
calendar.timegm(time.strptime(timeWithoutFractionalSeconds,
"%Y-%m-%d %H:%M:%S"))
self.metrics.create(**{'name': sample.name, 'dimensions':
dimensions, 'timestamp': seconds, 'value':
sample.volume})
def _traverse_dict(self, dimensions, name_prefix, meta_dict):
"""Method to add values of a dictionary to another dictionary.
Handles nested dictionaries.
"""
for name, value in meta_dict.iteritems():
if isinstance(value, basestring) and value:
dimensions[name_prefix + '.' + name] = value
elif isinstance(value, dict):
self._traverse_dict(dimensions, name_prefix + '.' + name,
value)