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 pathNovaMetrics.py
353 lines (298 loc) · 10.1 KB
/
NovaMetrics.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
from concurrent.futures import ThreadPoolExecutor
import re
from keystoneauth1 import identity
from keystoneauth1 import session
from novaclient import client
METRIC_NAME_PREFIX = "openstack."
NOVA_HYPERVISOR_PREFIX = "nova.hypervisor."
NOVA_SERVER_PREFIX = "nova.server."
NOVA_LIMIT_PREFIX = "nova.limit."
DEFAULT_NOVA_CLIENT_VERSION = "2.1"
NOVA_HYPERVISOR_METRICS = [
"current_workload",
"disk_available_least",
"free_disk_gb",
"free_ram_mb",
"local_gb",
"local_gb_used",
"memory_mb",
"memory_mb_used",
"running_vms",
"vcpus",
"vcpus_used"
]
NOVA_HYPERVISOR_DIMS = [
"id"
]
NOVA_HYPERVISOR_PROPS = [
"cpu_info_model",
"cpu_info_vendor",
"host_ip",
"hypervisor_type",
"hypervisor_version",
"service_host",
"service_id",
"state",
"status",
"hypervisor_hostname",
]
NOVA_HYPERVISOR_UPTIME_METRICS = [
"uptime"
]
CPU_TIMES = tuple("cpu{0}_time".format(n) for n in range(32))
NOVA_SERVER_DIAG_METRICS = CPU_TIMES + (
"memory",
"memory-actual",
"memory-rss",
"vda_errors",
"vda_read",
"vda_read_req",
"vda_write",
"vda_write_req",
"hdd_read",
"hdd_write",
"hdd_errors",
"hdd_read_req",
"hdd_write_req",
"rx",
"rx_drop",
"rx_packets",
"tx",
"tx_drop",
"tx_packets"
)
NOVA_SERVER_DIMS = [
"id",
]
NOVA_SERVER_PROPS = [
"status",
"user_id",
"hostId",
"name",
"flavor",
"OS-EXT-AZ:availability_zone",
"OS-EXT-SRV-ATTR:hypervisor_hostname",
"OS-EXT-SRV-ATTR:hostname",
"OS-EXT-SRV-ATTR:instance_name"
]
NOVA_LIMIT_METRICS = [
"maxImageMeta",
"maxSecurityGroups",
"maxTotalCores",
"maxTotalFloatingIps",
"maxTotalInstances",
"maxTotalKeypairs",
"maxTotalRAMSize",
"totalCoresUsed",
"totalFloatingIpsUsed",
"totalInstancesUsed",
"totalRAMUsed",
"totalSecurityGroupsUsed"
]
class NovaMetrics:
def __init__(
self,
auth_url,
username,
password,
project_name,
project_domain_id,
user_domain_id,
region_name,
ssl_verify,
http_timeout,
request_batch_size,
list_servers_search_opts={},
):
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._tpe = ThreadPoolExecutor(request_batch_size)
self._list_servers_search_opts = list_servers_search_opts
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.nova = client.Client(
DEFAULT_NOVA_CLIENT_VERSION,
session=self.sess,
region_name=self._region_name,
)
def _build_hypervisor_metrics(self, hypervisorId):
metrics = []
dims = {}
props = {}
try:
hypervisor = self.nova.hypervisors.get(hypervisorId)
except Exception:
# In case of error, do nothing
hypervisor = None
try:
uptime = self.nova.hypervisors.uptime(hypervisorId)
except Exception:
# In case of error, do nothing
uptime = None
# Create metrics
for metric in NOVA_HYPERVISOR_METRICS:
metricValue = getattr(hypervisor, metric, None)
if metricValue is not None:
metrics.append(("{0}{1}{2}".format(
METRIC_NAME_PREFIX,
NOVA_HYPERVISOR_PREFIX,
metric
), metricValue))
for uptimeMetric in NOVA_HYPERVISOR_UPTIME_METRICS:
uptimeValue = getattr(uptime, uptimeMetric, None)
if uptimeValue is not None:
if uptimeMetric == "uptime":
reLoadAvg = re.search(
"load average: ([0-9.]+), ([0-9.]+), ([0-9.]+)",
uptimeValue
)
metrics.append(("{0}{1}load_average_1m".format(
METRIC_NAME_PREFIX,
NOVA_HYPERVISOR_PREFIX
), reLoadAvg.group(1)))
metrics.append(("{0}{1}load_average_5m".format(
METRIC_NAME_PREFIX,
NOVA_HYPERVISOR_PREFIX
), reLoadAvg.group(2)))
metrics.append(("{0}{1}load_average_15m".format(
METRIC_NAME_PREFIX,
NOVA_HYPERVISOR_PREFIX
), reLoadAvg.group(3)))
# Pull dimensions
for dim in NOVA_HYPERVISOR_DIMS:
dimValue = getattr(hypervisor, dim, None)
if dimValue is not None:
dims[dim] = dimValue
# Pull properties
for prop in NOVA_HYPERVISOR_PROPS:
propValue = getattr(hypervisor, prop, None)
if propValue is not None:
props[prop] = propValue
dims["project_id"] = self.nova.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 (metrics, dims, props)
def _build_server_metrics(self, serverId):
metrics = []
dims = {}
props = {}
try:
server = self.nova.servers.get(serverId)
serverDiagnostics = server.diagnostics()[1]
except Exception:
# In case of error, do nothing
server = None
serverDiagnostics = {}
# Create metrics
diagNetworkMetrics = (
"rx",
"rx_drop",
"rx_packets",
"tx",
"tx_drop",
"tx_packets"
)
for diagMetric in NOVA_SERVER_DIAG_METRICS:
diagValue = None
# Sum up network stats since they are reported per network device
if diagMetric in diagNetworkMetrics:
attrs = list(serverDiagnostics.keys())
for a in attrs:
if a.endswith(diagMetric):
tmpValue = serverDiagnostics.get(a, 0)
if diagValue is not None:
diagValue += tmpValue
else:
diagValue = tmpValue
else:
diagValue = serverDiagnostics.get(diagMetric, None)
if diagValue is not None:
metrics.append(("{0}{1}{2}".format(
METRIC_NAME_PREFIX,
NOVA_SERVER_PREFIX,
diagMetric
), diagValue))
# Pull dimensions
for dim in NOVA_SERVER_DIMS:
dimValue = getattr(server, dim, None)
if dimValue is not None:
dims[dim] = dimValue
# Pull properties
for prop in NOVA_SERVER_PROPS:
propValue = None
if prop == "flavor":
tmpValue = getattr(server, "flavor", None)
if tmpValue is not None:
flavor = self.nova.flavors.get(tmpValue["id"])
flavorName = getattr(flavor, "name", None)
if flavorName is not None:
propValue = flavorName
else:
propValue = getattr(server, prop, None)
if propValue is not None:
propName = prop
if prop.startswith("OS-EXT"):
propName = prop.split(":")[1]
props[propName] = propValue
project_id = self.nova.client.get_project_id()
tenant_id = getattr(server, 'tenant_id')
if tenant_id is not None and tenant_id != project_id:
project_id = tenant_id
dims["project_id"] = 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 (metrics, dims, props)
def collect_hypervisor_metrics(self):
hypervisorList = self.nova.hypervisors.list()
hypervisorMetrics = {}
for hypervisor in hypervisorList:
hypervisorMetrics[hypervisor.id] = self._build_hypervisor_metrics(
hypervisor.id
)
return hypervisorMetrics
def collect_server_metrics(self):
serverList = self.nova.servers.list(search_opts=self._list_servers_search_opts)
serverMetrics = {}
futures = [(server.id, self._tpe.submit(self._build_server_metrics, server.id)) for server in serverList]
for server_id, future in futures:
serverMetrics[server_id] = future.result()
return serverMetrics
def collect_limit_metrics(self):
limits = self.nova.limits.get().to_dict()['absolute']
metrics = []
dims = {}
props = {}
for metric in NOVA_LIMIT_METRICS:
metricValue = limits.get(metric, None)
if metricValue is not None:
metrics.append(("{0}{1}{2}".format(
METRIC_NAME_PREFIX,
NOVA_LIMIT_PREFIX,
metric
), metricValue))
dims["project_id"] = self.nova.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)}