forked from rchakode/kube-opex-analytics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.py
624 lines (553 loc) · 26.9 KB
/
backend.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
"""
# File: backend.py #
# Author: Rodrigue Chakode <rodrigue.chakode @ gmail dot com> #
# #
# Copyright © 2019 Rodrigue Chakode and contributors. #
# #
# This file is part of Kubernetes Opex Analytics software. #
# #
# Kubernetes Opex Analytics is licensed under the Apache License 2.0 (the "License"); #
# you may not use this file except in compliance with the License. You may obtain #
# a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software distributed #
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR #
# CONDITIONS OF ANY KIND, either express or implied. See the License for the #
# specific language governing permissions and limitations under the License. #
"""
import argparse
import flask
import requests
import threading
import time
import os
import json
import errno
import rrdtool
import logging
import calendar
import sys
import collections
import enum
import werkzeug.wsgi
import prometheus_client
# set version
KOA_VERSION='0.2.0'
# define logger
def get_logger():
logger = logging.getLogger('kube-opex-analytics')
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
return logger
# get a logger
logger = get_logger()
def get_config_hourly_billing_rate():
try:
hourly_billing_rate = float(os.getenv('KOA_BILLING_HOURLY_RATE'))
if hourly_billing_rate < 0.0:
logger.warning('negative KOA_BILLING_HOURLY_RATE reset to 0.0 (%f)' % hourly_billing_rate)
hourly_billing_rate = 0.0
except:
hourly_billing_rate = 0.0
return hourly_billing_rate
# load configuration from environment
KOA_K8S_API_ENDPOINT = os.getenv('KOA_K8S_API_ENDPOINT', 'http://127.0.0.1:8001')
KOA_K8S_API_VERIFY_SLL = (lambda v: v.lower() in ("yes", "true"))(os.getenv('KOA_K8S_API_VERIFY_SLL', 'true'))
KOA_DEFAULT_DB_LOCATION = ('%s/.kube-opex-analytics/db') % os.getenv('HOME', '/tmp')
KOA_DB_LOCATION = os.getenv('KOA_DB_LOCATION', KOA_DEFAULT_DB_LOCATION)
KOA_POLLING_INTERVAL_SEC = int(os.getenv('KOA_POLLING_INTERVAL_SEC', '300'))
KOA_BILLING_CURRENCY_SYMBOL = os.getenv('KOA_BILLING_CURRENCY_SYMBOL', '$')
KOA_BILLING_HOURLY_RATE=get_config_hourly_billing_rate()
PROMETHEUS_HOURLY_USAGE_EXPORTER = prometheus_client.Gauge('koa_namespace_last_hourly_usage',
'Last hourly resource usage per namespace',
['namespace', 'usage_type'])
PROMETHEUS_PERIODIC_USAGE_EXPORTER = prometheus_client.Gauge('koa_namespace_periodic_usage',
'Periodic resource usage per namespace',
['analytics_interval', 'namespace', 'resource', 'date'])
# fixed configuration settings
STATIC_CONTENT_LOCATION = '/static'
FRONTEND_DATA_LOCATION = '.%s/data' % (STATIC_CONTENT_LOCATION)
# create Flask application
app = flask.Flask(__name__, static_url_path=STATIC_CONTENT_LOCATION, template_folder='.')
# Add prometheus wsgi middleware to route /metrics requests
wsgi_dispatcher = werkzeug.wsgi.DispatcherMiddleware(app, {
'/metrics': prometheus_client.make_wsgi_app()
})
@app.route('/favicon.ico')
def favicon():
return flask.send_from_directory(os.path.join(app.root_path, 'static'), 'images/favicon.ico', mimetype='image/vnd.microsoft.icon')
@app.after_request
def add_header(r):
r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
r.headers["Pragma"] = "no-cache"
r.headers["Expires"] = "0"
r.headers['Cache-Control'] = 'public, max-age=0'
return r
@app.route('/js/<path:path>')
def send_js(path):
return flask.send_from_directory('js', path)
@app.route('/css/<path:path>')
def send_css(path):
return flask.send_from_directory('css', path)
@app.route('/')
def render():
return flask.render_template('index.html', koa_frontend_data_location=FRONTEND_DATA_LOCATION, koa_version=KOA_VERSION)
class Node:
def __init__(self):
self.id = ''
self.name = ''
self.state = ''
self.message = ''
self.cpuCapacity = 0.0
self.cpuAllocatable = 0.0
self.cpuUsage = 0.0
self.memCapacity = 0.0
self.memAllocatable = 0.0
self.memUsage = 0.0
self.containerRuntime = ''
self.podsRunning = []
self.podsNotRunning = []
class Pod:
def __init__(self):
self.name = ''
self.namespace = ''
self.id = ''
self.nodeName = ''
self.phase = ''
self.state = "PodNotScheduled"
class ResourceUsage:
def __init__(self, cpuUsage, memUsage):
self.cpuUsage = cpuUsage
self.memUsage = memUsage
class JSONMarshaller(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, Node):
return {
'id': obj.id,
'name': obj.name,
'state': obj.state,
'message': obj.message,
'cpuCapacity': obj.cpuCapacity,
'cpuAllocatable': obj.cpuAllocatable,
'cpuUsage': obj.cpuUsage,
'memCapacity': obj.memCapacity,
'memAllocatable': obj.memAllocatable,
'memUsage': obj.memUsage,
'containerRuntime': obj.containerRuntime,
'podsRunning': obj.podsRunning,
'podsNotRunning': obj.podsNotRunning
}
elif isinstance(obj, Pod):
return {
'id': obj.id,
'name': obj.name,
'nodeName': obj.nodeName,
'phase': obj.phase,
'state': obj.state,
'cpuUsage': obj.cpuUsage,
'memUsage': obj.memUsage
}
elif isinstance(obj, ResourceUsage):
return {
'cpuUsage': obj.cpuUsage,
'memUsage': obj.memUsage
}
return json.JSONEncoder.default(self, obj)
class K8sUsage:
def __init__(self):
self.nodes = {}
self.pods = {}
self.nsResUsage = {}
self.popupContent = ''
self.nodeHtmlList = ''
self.cpuUsedByPods = 0.0
self.memUsedByPods = 0.0
self.cpuCapacity = 0.0
self.memCapacity = 0.0
self.cpuAllocatable = 0.0
self.memAllocatable = 0.0
def decode_memory_capacity(self, cap_input):
data_length = len(cap_input)
cap_unit = ''
cap_value = ''
if cap_input.endswith("i"):
cap_unit = cap_input[data_length - 2:]
cap_value = cap_input[0:data_length - 2]
else:
cap_value = cap_input
if cap_unit == '':
return int(cap_value)
if cap_unit == 'Ki':
return 1e3 * int(cap_value)
if cap_unit == 'Mi':
return 1e6 * int(cap_value)
if cap_unit == 'Gi':
return 1e9 * int(cap_value)
if cap_unit == 'Ti':
return 1e12 * int(cap_value)
if cap_unit == 'Pi':
return 1e15 * int(cap_value)
if cap_unit == 'Ei':
return 1e18 * int(cap_value)
return 0
def decode_cpu_capacity(self, cap_input):
data_length = len(cap_input)
cap_unit = cap_input[data_length - 1:]
cap_value = cap_input[0:data_length - 1]
if cap_unit == 'n':
return 1e-9 * int(cap_value)
if cap_unit == 'm':
return 1e-3 * int(cap_value)
return int(cap_input)
def extract_namespaces_and_initialize_usage(self, data):
# exit if not valid data
if data is None:
return
# process likely valid data
data_json = json.loads(data)
for _, item in enumerate(data_json['items']):
self.nsResUsage[item['metadata']['name']] = ResourceUsage(
cpuUsage=0.0, memUsage=0.0)
def extract_nodes(self, data):
# exit if not valid data
if data is None:
return
# process likely valid data
data_json = json.loads(data)
for _, item in enumerate(data_json['items']):
node = Node()
node.podsRunning = []
node.podsNotRunning = []
node.name = item['metadata']['name']
node.id = item['metadata']['uid']
node.cpuCapacity = self.decode_cpu_capacity(item['status']['capacity']['cpu'])
node.cpuAllocatable = self.decode_cpu_capacity(item['status']['allocatable']['cpu'])
node.memCapacity = self.decode_memory_capacity(item['status']['capacity']['memory'])
node.memAllocatable = self.decode_memory_capacity(item['status']['allocatable']['memory'])
node.containerRuntime = item['status']['nodeInfo']['containerRuntimeVersion']
for _, cond in enumerate(item['status']['conditions']):
node.message = cond['message']
if cond['type'] == 'Ready' and cond['status'] == 'True':
node.state = 'Ready'
break
if cond['type'] == 'KernelDeadlock' and cond['status'] == 'True':
node.state = 'KernelDeadlock'
break
if cond['type'] == 'NetworkUnavailable' and cond['status'] == 'True':
node.state = 'NetworkUnavailable'
break
if cond['type'] == 'OutOfDisk' and cond['status'] == 'True':
node.state = 'OutOfDisk'
break
if cond['type'] == 'MemoryPressure' and cond['status'] == 'True':
node.state = 'MemoryPressure'
break
if cond['type'] == 'DiskPressure' and cond['status'] == 'True':
node.state = 'DiskPressure'
break
self.nodes[node.name] = node
def extract_node_metrics(self, data):
# exit if not valid data
if data is None:
return
# process likely valid data
data_json = json.loads(data)
for _, item in enumerate(data_json['items']):
node = self.nodes.get(item['metadata']['name'], None)
if node is not None:
node.cpuUsage = self.decode_cpu_capacity(item['usage']['cpu'])
node.memUsage = self.decode_memory_capacity(item['usage']['memory'])
self.nodes[node.name] = node
def extract_pods(self, data):
# exit if not valid data
if data is None:
return
# process likely valid data
data_json = json.loads(data)
for _, item in enumerate(data_json['items']):
pod = Pod()
pod.namespace = item['metadata']['namespace']
pod.name = '%s.%s' % (item['metadata']['name'], pod.namespace)
pod.id = item['metadata']['uid']
pod.phase = item['status']['phase']
pod.state = 'PodNotScheduled'
for _, cond in enumerate(item['status']['conditions']):
if cond['type'] == 'Ready' and cond['status'] == 'True':
pod.state = 'Ready'
break
if cond['type'] == 'ContainersReady' and cond['status'] == 'True':
pod.state = "ContainersReady"
break
if cond['type'] == 'PodScheduled' and cond['status'] == 'True':
pod.state = "PodScheduled"
break
if cond['type'] == 'Initialized' and cond['status'] == 'True':
pod.state = "Initialized"
break
if pod.state != 'PodNotScheduled':
pod.nodeName = item['spec']['nodeName']
else:
pod.nodeName = None
self.pods[pod.name] = pod
def extract_pod_metrics(self, data):
# exit if not valid data
if data is None:
return
# process likely valid data
data_json = json.loads(data)
for _, item in enumerate(data_json['items']):
podName = '%s.%s' % (item['metadata']['name'], item['metadata']['namespace'])
pod = self.pods.get(podName, None)
if pod is not None:
pod.cpuUsage = 0.0
pod.memUsage = 0.0
for _, container in enumerate(item['containers']):
pod.cpuUsage += self.decode_cpu_capacity(container['usage']['cpu'])
pod.memUsage += self.decode_memory_capacity(container['usage']['memory'])
self.pods[pod.name] = pod
def consolidate_ns_usage(self):
self.cpuUsedByPods = 0.0
self.memUsedByPods = 0.0
for pod in self.pods.values():
if hasattr(pod, 'cpuUsage') and hasattr(pod, 'memUsage'):
self.cpuUsedByPods += pod.cpuUsage
self.nsResUsage[pod.namespace].cpuUsage += pod.cpuUsage
self.nsResUsage[pod.namespace].memUsage += pod.memUsage
self.memUsedByPods += pod.memUsage
self.nodes[pod.nodeName].podsRunning.append(pod)
self.cpuCapacity += 0.0
self.memCapacity += 0.0
for node in self.nodes.values():
if hasattr(node, 'cpuCapacity') and hasattr(node, 'memCapacity'):
self.cpuCapacity += node.cpuCapacity
self.memCapacity += node.memCapacity
self.cpuAllocatable += 0.0
self.memAllocatable += 0.0
for node in self.nodes.values():
if hasattr(node, 'cpuAllocatable') and hasattr(node, 'memAllocatable'):
self.cpuAllocatable += node.cpuAllocatable
self.memAllocatable += node.memAllocatable
def dump_nodes(self):
with open(str('%s/nodes.json' % FRONTEND_DATA_LOCATION), 'w') as fd:
fd.write(json.dumps(self.nodes, cls=JSONMarshaller))
def compute_usage_percent_ratio(value, total):
return round((100.0*value) / total, 1)
def create_directory_if_not_exists(path):
try:
os.makedirs(path)
except OSError as e:
if e.errno != errno.EEXIST:
raise
class RrdPeriod(enum.IntEnum):
PERIOD_1_HOUR_SEC = 3600
PERIOD_7_DAYS_SEC = 604800
PERIOD_14_DAYS_SEC = 1209600
PERIOD_YEAR_SEC = 31968000
class ResUsageType(enum.IntEnum):
CPU = 0
MEMORY = 1
CONSOLIDATED = 2
CUMULATED_COST = 3
class Rrd:
def __init__(self, db_files_location=None, dbname=None):
create_directory_if_not_exists(db_files_location)
self.dbname = dbname
self.rrd_location = str('%s/%s.rrd' % (KOA_DB_LOCATION, dbname))
self.create_rrd_file_if_not_exists()
@staticmethod
def get_date_group(timeUTC, period):
if period == RrdPeriod.PERIOD_YEAR_SEC:
return time.strftime('%b %Y', timeUTC)
return time.strftime('%b %d', timeUTC)
def create_rrd_file_if_not_exists(self):
if not os.path.exists(self.rrd_location):
xfs = 2 * KOA_POLLING_INTERVAL_SEC
rrdtool.create(self.rrd_location,
"--step", str(KOA_POLLING_INTERVAL_SEC),
"--start", "0",
str('DS:cpu_usage:GAUGE:%d:U:U' % xfs),
str('DS:mem_usage:GAUGE:%d:U:U' % xfs),
str('DS:consolidated_usage:GAUGE:%d:U:U' % xfs),
str('DS:estimated_cost:GAUGE:%d:U:U' % xfs),
"RRA:AVERAGE:0.5:1:4032",
"RRA:AVERAGE:0.5:12:8880")
def add_value(self, probe_ts, cpu_usage, mem_usage, consolidated_usage, estimated_cost):
rrdtool.update(self.rrd_location, '%s:%s:%s:%s:%s'%(
probe_ts,
round(cpu_usage, 1),
round(mem_usage, 1),
round(consolidated_usage, 1),
round(estimated_cost, 1)))
def dump_trends_data(self, period, step_in):
end_ts_in = int(int(calendar.timegm(time.gmtime()) * step_in) / step_in)
start_ts_in = int(end_ts_in - int(period))
result = rrdtool.fetch(self.rrd_location, 'AVERAGE', '-r', str(step_in), '-s', str(start_ts_in), '-e', str(end_ts_in))
res_usage = collections.defaultdict(list)
sum_res_usage = collections.defaultdict(lambda:0.0)
cumulated_cost = 0.0
start_ts_out, _, step = result[0]
current_ts = start_ts_out
for _, cdp in enumerate( result[2] ):
current_ts += step
if len(cdp) == 4:
try:
datetime_utc = time.gmtime(current_ts)
current_cpu_usage = round(100*float(cdp[0]))/100
current_mem_usage = round(100*float(cdp[1]))/100
current_consolidated_usage = round(100*float(cdp[2]))/100
cumulated_cost += round(100*float(cdp[3]))/100
datetime_utc_json = time.strftime('%Y-%m-%dT%H:%M:%SZ', datetime_utc)
res_usage[ResUsageType.CPU].append('{"name":"%s","dateUTC":"%s","usage":%f}' % (self.dbname, datetime_utc_json, current_cpu_usage))
res_usage[ResUsageType.MEMORY].append('{"name":"%s","dateUTC":"%s","usage":%f}' % (self.dbname, datetime_utc_json, current_mem_usage))
res_usage[ResUsageType.CONSOLIDATED].append('{"name":"%s","dateUTC":"%s","usage":%s}' % (self.dbname, datetime_utc_json, current_consolidated_usage))
res_usage[ResUsageType.CUMULATED_COST].append('{"name":"%s", "dateUTC":"%s","usage":%s}' % (self.dbname, datetime_utc_json, cumulated_cost))
sum_res_usage[ResUsageType.CPU] += current_cpu_usage
sum_res_usage[ResUsageType.MEMORY] += current_mem_usage
sum_res_usage[ResUsageType.CONSOLIDATED] += current_consolidated_usage
sum_res_usage[ResUsageType.CUMULATED_COST] += cumulated_cost
except:
pass
if sum_res_usage[ResUsageType.CPU] > 0.0 and sum_res_usage[ResUsageType.MEMORY] > 0.0:
PROMETHEUS_HOURLY_USAGE_EXPORTER.labels(self.dbname, ResUsageType.CPU.name).set(current_cpu_usage)
PROMETHEUS_HOURLY_USAGE_EXPORTER.labels(self.dbname, ResUsageType.MEMORY.name).set(current_mem_usage)
PROMETHEUS_HOURLY_USAGE_EXPORTER.labels(self.dbname, ResUsageType.CONSOLIDATED.name).set(current_consolidated_usage)
return (','.join(res_usage[ResUsageType.CPU]),
','.join(res_usage[ResUsageType.MEMORY]),
','.join(res_usage[ResUsageType.CONSOLIDATED]),
','.join(res_usage[ResUsageType.CUMULATED_COST]))
return '', '', '', ''
def dump_histogram_data(self, period, step_in):
end_ts_in = int(int(calendar.timegm(time.gmtime()) * step_in) / step_in)
start_ts_in = int(end_ts_in - int(period))
result = rrdtool.fetch(self.rrd_location, 'AVERAGE', '-r', str(step_in), '-s', str(start_ts_in), '-e', str(end_ts_in))
periodic_cpu_usage = collections.defaultdict(lambda:0.0)
periodic_mem_usage = collections.defaultdict(lambda:0.0)
periodic_consolidated_usage = collections.defaultdict(lambda:0.0)
valid_rows = collections.defaultdict(lambda:0.0)
start_ts_out, _, step = result[0]
current_ts = start_ts_out
for _, cdp in enumerate( result[2] ):
current_ts += step
if len(cdp) == 4:
try:
datetime_utc = time.gmtime(current_ts)
date_group = self.get_date_group(datetime_utc, period)
current_cpu_usage = round(100*float(cdp[0]))/100
current_mem_usage = round(100*float(cdp[1]))/100
current_consolidated_usage = round(100*float(cdp[2]))/100
periodic_cpu_usage[date_group] += current_cpu_usage
periodic_mem_usage[date_group] += current_mem_usage
periodic_consolidated_usage[date_group] += current_consolidated_usage
valid_rows[date_group] += 1
except:
pass
return periodic_cpu_usage, periodic_mem_usage, periodic_consolidated_usage, valid_rows
@staticmethod
def dump_trend_analytics(dbfiles):
res_usage = collections.defaultdict(list)
for _, dbfile in enumerate(dbfiles):
dbfile_splitted=os.path.splitext(dbfile)
if len(dbfile_splitted) == 2 and dbfile_splitted[1] == '.rrd':
ns = dbfile_splitted[0]
rrd = Rrd(db_files_location=KOA_DB_LOCATION, dbname=ns)
analytics = rrd.dump_trends_data(period=RrdPeriod.PERIOD_7_DAYS_SEC, step_in=3600)
# analytics = rrd.dump_trends_data(period=RrdPeriod.PERIOD_14_DAYS_SEC, step_in=3600)
for usage_type in range(4):
if analytics[usage_type]:
res_usage[usage_type].append(analytics[usage_type])
with open(str('%s/cpu_usage_trends.json' % FRONTEND_DATA_LOCATION), 'w') as fd:
fd.write('['+','.join(res_usage[0])+']')
with open(str('%s/memory_usage_trends.json' % FRONTEND_DATA_LOCATION), 'w') as fd:
fd.write('['+','.join(res_usage[1])+']')
with open(str('%s/consolidated_usage_trends.json' % FRONTEND_DATA_LOCATION), 'w') as fd:
fd.write('['+','.join(res_usage[2])+']')
with open(str('%s/estimated_usage_trends.json' % FRONTEND_DATA_LOCATION), 'w') as fd:
fd.write('['+','.join(res_usage[3])+']')
@staticmethod
def dump_histogram_analytics(dbfiles, period):
res_usage = collections.defaultdict(list)
for _, dbfile in enumerate(dbfiles):
dbfile_splitted=os.path.splitext(dbfile)
if len(dbfile_splitted) == 2 and dbfile_splitted[1] == '.rrd':
ns = dbfile_splitted[0]
rrd = Rrd(db_files_location=KOA_DB_LOCATION, dbname=ns)
analytics = rrd.dump_histogram_data(period=period, step_in=3600)
for usage_type in range(3):
for date_group, value in analytics[usage_type].items():
if value > 0.0:
PROMETHEUS_PERIODIC_USAGE_EXPORTER.labels(RrdPeriod(period).name, ns, ResUsageType(usage_type).name, date_group).set(value)
res_usage[usage_type].append('{"stack":"%s","usage":%f,"date":"%s"}' % (ns, value, date_group))
with open(str('%s/cpu_usage_period_%d.json' % (FRONTEND_DATA_LOCATION, period)), 'w') as fd:
fd.write('['+','.join(res_usage[0])+']')
with open(str('%s/memory_usage_period_%d.json' % (FRONTEND_DATA_LOCATION, period)), 'w') as fd:
fd.write('['+','.join(res_usage[1])+']')
with open(str('%s/consolidated_usage_period_%d.json' % (FRONTEND_DATA_LOCATION, period)), 'w') as fd:
fd.write('['+','.join(res_usage[2])+']')
def pull_k8s(api_context):
data = None
api_endpoint = '%s%s' % (KOA_K8S_API_ENDPOINT, api_context)
try:
http_req = requests.get(api_endpoint, verify=KOA_K8S_API_VERIFY_SLL)
if http_req.status_code == 200:
data = http_req.text
else:
logger.error("call to %s returned error (%s)" % (api_endpoint, http_req.text))
except requests.exceptions.RequestException as ex:
logger.error("HTTP exception requesting %s (%s)" % (api_endpoint, ex))
except:
logger.error("unknown exception requesting %s" % api_endpoint)
return data
def create_metrics_puller():
while True:
k8s_usage = K8sUsage()
k8s_usage.extract_namespaces_and_initialize_usage( pull_k8s('/api/v1/namespaces') )
k8s_usage.extract_nodes( pull_k8s('/api/v1/nodes') )
k8s_usage.extract_node_metrics( pull_k8s('/apis/metrics.k8s.io/v1beta1/nodes') )
k8s_usage.extract_pods( pull_k8s('/api/v1/pods') )
k8s_usage.extract_pod_metrics( pull_k8s('/apis/metrics.k8s.io/v1beta1/pods') )
k8s_usage.consolidate_ns_usage()
k8s_usage.dump_nodes()
if k8s_usage.cpuCapacity > 0.0 and k8s_usage.memCapacity > 0.0:
probe_ts = calendar.timegm(time.gmtime())
rrd = Rrd(db_files_location=KOA_DB_LOCATION, dbname='non-allocatable')
cpu_usage = compute_usage_percent_ratio(k8s_usage.cpuCapacity - k8s_usage.cpuAllocatable, k8s_usage.cpuCapacity)
mem_usage = compute_usage_percent_ratio(k8s_usage.memCapacity - k8s_usage.memAllocatable, k8s_usage.memCapacity)
consolidated_usage = (cpu_usage + mem_usage) / 2.0
estimated_cost = consolidated_usage * (KOA_POLLING_INTERVAL_SEC * KOA_BILLING_HOURLY_RATE) / 36
rrd.add_value(probe_ts, cpu_usage=cpu_usage, mem_usage=mem_usage, consolidated_usage=consolidated_usage, estimated_cost=estimated_cost)
for ns, nsUsage in k8s_usage.nsResUsage.items():
rrd = Rrd(db_files_location=KOA_DB_LOCATION, dbname=ns)
cpu_usage = compute_usage_percent_ratio(nsUsage.cpuUsage, k8s_usage.cpuCapacity)
mem_usage = compute_usage_percent_ratio(nsUsage.memUsage, k8s_usage.memCapacity)
consolidated_usage = (cpu_usage + mem_usage) / 2
estimated_cost = consolidated_usage * (KOA_POLLING_INTERVAL_SEC * KOA_BILLING_HOURLY_RATE) / 36
rrd.add_value(probe_ts, cpu_usage=cpu_usage, mem_usage=mem_usage, consolidated_usage=consolidated_usage, estimated_cost=estimated_cost)
time.sleep(int(KOA_POLLING_INTERVAL_SEC))
def dump_analytics():
export_interval = round(1.5 * KOA_POLLING_INTERVAL_SEC)
while True:
dbfiles = []
for (_, _, filenames) in os.walk(KOA_DB_LOCATION):
dbfiles.extend(filenames)
break
Rrd.dump_trend_analytics(dbfiles)
Rrd.dump_histogram_analytics(dbfiles=dbfiles, period=RrdPeriod.PERIOD_14_DAYS_SEC)
Rrd.dump_histogram_analytics(dbfiles=dbfiles, period=RrdPeriod.PERIOD_YEAR_SEC)
time.sleep(export_interval)
# parse CLI options
parser = argparse.ArgumentParser(description='Kubernetes Opex Analytics Backend.')
parser.add_argument('-v', '--version', action='version', version='%(prog)s '+KOA_VERSION)
args = parser.parse_args()
# create dump directory if not exist
create_directory_if_not_exists(FRONTEND_DATA_LOCATION)
# create workers
th_puller = threading.Thread(target=create_metrics_puller)
th_exporter = threading.Thread(target=dump_analytics)
th_puller.start()
th_exporter.start()
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5483)