This repository has been archived by the owner on Feb 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathCinderMetrics.py
119 lines (101 loc) · 3.89 KB
/
CinderMetrics.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
111
112
113
114
115
116
117
118
119
from keystoneauth1 import identity
from keystoneauth1 import session
from cinderclient import client
METRIC_NAME_PREFIX = "openstack."
CINDER_LIMIT_PREFIX = "cinder.limit."
CINDER_VOLUME_PREFIX = "cinder.volume."
CINDER_SNAPSHOT_PREFIX = "cinder.snapshot."
DEFAULT_CINDER_CLIENT_VERSION = "2.0"
class CinderMetrics:
def __init__(
self,
auth_url,
username,
password,
project_name,
project_domain_id,
user_domain_id,
region_name,
ssl_verify,
http_timeout
):
self._auth_url = auth_url
self._username = username
self._password = password
self._project_name = project_name
self._project_domain_id = project_domain_id
self._user_domain_id = user_domain_id
self._region_name = region_name
self._ssl_verify = ssl_verify
self.auth = identity.Password(
auth_url=self._auth_url,
username=self._username,
password=self._password,
project_name=self._project_name,
project_domain_id=self._project_domain_id,
user_domain_id=self._user_domain_id
)
self.sess = session.Session(auth=self.auth, verify=self._ssl_verify, timeout=http_timeout)
self.cinder = client.Client(
DEFAULT_CINDER_CLIENT_VERSION,
session=self.sess,
region_name=self._region_name
)
def collect_cinder_metrics(self):
metrics = []
dims = {}
props = {}
self.collect_volume_metrics(metrics)
self.collect_snapshot_metrics(metrics)
self.collect_limit_metrics(metrics)
dims["project_id"] = self.cinder.client.get_project_id()
props["project_name"] = self._project_name
props["project_domain_name"] = self._project_domain_id
props["user_domain_name"] = self._user_domain_id
if self._region_name:
props["region_name"] = self._region_name
return {'0': (metrics, dims, props)}
def collect_volume_metrics(self, metrics):
volumes = self.cinder.volumes.list(search_opts={'all_tenants': 1})
data_tenant = dict()
data_tenant['volumes'] = {'count': 0, 'bytes': 0}
for volume in volumes:
data_tenant['volumes']['count'] += 1
data_tenant['volumes']['bytes'] += (volume.size * 1024 * 1024 * 1024)
if data_tenant is not None:
metrics.append(("{0}{1}{2}".format(
METRIC_NAME_PREFIX,
CINDER_VOLUME_PREFIX,
'count'
), data_tenant['volumes']['count']))
metrics.append(("{0}{1}{2}".format(
METRIC_NAME_PREFIX,
CINDER_VOLUME_PREFIX,
'size'
), data_tenant['volumes']['bytes']))
def collect_snapshot_metrics(self, metrics):
snapshots = self.cinder.volume_snapshots.list(search_opts={'all_tenants': 1})
data_tenant = dict()
data_tenant['snapshot'] = {'count': 0, 'bytes': 0}
for snapshot in snapshots:
data_tenant['snapshot']['count'] += 1
data_tenant['snapshot']['bytes'] += (snapshot.size * 1024 * 1024 * 1024)
if data_tenant is not None:
metrics.append(("{0}{1}{2}".format(
METRIC_NAME_PREFIX,
CINDER_SNAPSHOT_PREFIX,
'count'
), data_tenant['snapshot']['count']))
metrics.append(("{0}{1}{2}".format(
METRIC_NAME_PREFIX,
CINDER_SNAPSHOT_PREFIX,
'size'
), data_tenant['snapshot']['bytes']))
def collect_limit_metrics(self, metrics):
limits = self.cinder.limits.get().to_dict()['absolute']
for limit in limits:
metrics.append(("{0}{1}{2}".format(
METRIC_NAME_PREFIX,
CINDER_LIMIT_PREFIX,
limit
), limits[limit]))