-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmeasure_loadbalancers.py
More file actions
62 lines (56 loc) · 2.25 KB
/
measure_loadbalancers.py
File metadata and controls
62 lines (56 loc) · 2.25 KB
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
import uuid
import time
import logging
import ipaddress
from django.utils import timezone
from django.core.management.base import BaseCommand
from django.conf import settings
from api import monitor
from api.models.app import App
from api.tasks import send_measurements
logger = logging.getLogger(__name__)
class Command(BaseCommand):
"""Management command for push data to manager"""
def _get_measure_name(self, ip):
address = ipaddress.ip_address(ip)
prefix = "intranet" if address.is_private else "internet"
return f"{prefix}:{address.version}"
def _measure_loadbalancers(self, app_map, timestamp):
stop = timestamp - (timestamp % 3600)
start = stop - 3600
loadbalancers = []
for item in monitor.query_loadbalancer(app_map.keys(), start, stop):
ip = item["ip"]
namespace = item["namespace"]
owner_id = app_map[namespace].owner_id
loadbalancers.append({
"app_id": str(app_map[namespace].uuid),
"owner": owner_id,
"name": self._get_measure_name(ip),
"type": "loadbalancer",
"unit": "number",
"usage": 1,
"kwargs": {
"ip": ip,
"node": item["node"],
"service": item["service"],
"instance": item["instance"],
},
"timestamp": start
})
send_measurements.delay(loadbalancers)
def handle(self, *args, **options):
if settings.WORKFLOW_MANAGER_URL and settings.DRYCC_PROMETHEUS_URL:
timestamp = int(time.time())
task_id = uuid.uuid4().hex
logger.info(f"pushing {task_id} limits to workflow_manager when {timezone.now()}")
app_map = {}
for app in App.objects.all():
app_map[app.id] = app
if len(app_map) % 1000 == 0:
self._measure_loadbalancers(app_map, timestamp)
app_map = {}
if len(app_map) > 0:
self._measure_loadbalancers(app_map, timestamp)
logger.info(f"pushed {task_id} limits to workflow_manager when {timezone.now()}")
self.stdout.write("done")