import json from django.shortcuts import render from dashboard.openstack_utils.connect import get_connection from dashboard.prometheus_utils.query import query_prometheus _BASE = { "region_name": "ct3k1ldt" } def collect_context(): connection = get_connection() hosts_total = int( query_prometheus( query="count(node_exporter_build_info{job='node_exporter_compute'})" ) ) pcpu_total = int( query_prometheus( query="sum(count(node_cpu_seconds_total{job='node_exporter_compute', mode='idle'}) without (cpu,mode))" ) ) pcpu_usage = float( query_prometheus( query="" ) ) vcpu_total = int ( query_prometheus( query="(sum(count(node_cpu_seconds_total{job='node_exporter_compute', mode='idle'}) without (cpu,mode)))*(avg(openstack_placement_resource_allocation_ratio{resourcetype='VCPU'}))" ) ) vcpu_allocated = int( query_prometheus( query="sum(libvirt_domain_info_virtual_cpus)" ) ) vcpu_overcommit_ratio = float( query_prometheus( query="sum(libvirt_domain_info_virtual_cpus)/(sum(count(node_cpu_seconds_total{job='node_exporter_compute', mode='idle'}) without (cpu,mode)))" ) ) vcpu_overcommit_max = float( query_prometheus( query="avg(openstack_placement_resource_allocation_ratio{resourcetype='VCPU'})" ) ) vm_count = int ( query_prometheus( query="sum(libvirt_domain_state_code)" ) ) vm_active = int ( query_prometheus( query="sum(libvirt_domain_state_code{stateDesc='the domain is running'})" ) ) return { "region_name": "", # <--- Hosts data ---> "hosts_total": hosts_total, # <--- CPU data ---> # pCPU data "pcpu_total": pcpu_total, "pcpu_usage": pcpu_usage, "pcpu_free": pcpu_total - pcpu_usage, # vCPU data "vcpu_total": vcpu_total, "vcpu_allocated": vcpu_allocated, "vcpu_free": vcpu_total - vcpu_allocated, "vcpu_overcommit_ratio": vcpu_overcommit_ratio, "vcpu_overcommit_max": vcpu_overcommit_max, # <--- RAM data ---> # pRAM data # vRAM data # <--- VM data ---> "vm_count": vm_count, "vm_active": vm_active, "vm_stopped": vm_count - vm_active, "vm_error": "", "avg_cpu_per_vm": vcpu_allocated / vm_count, "avg_ram_per_vm": "", "vm_density": vm_count / hosts_total, } def index(request): context = {**_BASE, # CPU and RAM utilization data 'cpu_total': 160, 'cpu_used': 45, 'cpu_free': 66, 'cpu_used_percentage': 42.0, 'ram_used': 128, 'ram_free': 256, 'ram_used_percentage': 33.3, # Resource allocation data 'cpu_allocated': 94, 'cpu_total': 160, 'cpu_overcommit_ratio': 1.5, 'ram_allocated': 384, 'ram_total': 512, 'ram_overcommit_ratio': 1.2, # Instance summary data 'vm_count': 47, 'vm_active': 42, 'vm_stopped': 5, 'vm_error': 0, 'common_flavor': 'm1.medium', 'common_flavor_count': 18, 'second_common_flavor': { 'name': 'm1.small', 'count': 12 }, 'third_common_flavor': { 'name': 'm1.large', 'count': 8 }, # Quick stats 'avg_cpu_per_vm': 2.0, 'avg_ram_per_vm': 8.2, 'vm_density': 9.4, # Audit data 'audits': [ { 'id': 'audit_001', 'name': 'Weekly Optimization', 'created_at': '2024-01-15', 'cpu_weight': 1.2, 'ram_weight': 0.6, 'scope': 'Full Cluster', 'strategy': 'Load Balancing', 'goal': 'Optimize CPU distribution across all hosts', 'migrations': [ { 'instanceName': 'web-server-01', 'source': 'compute-02', 'destination': 'compute-05', 'flavor': 'm1.medium', 'impact': 'Low' }, { 'instanceName': 'db-replica-03', 'source': 'compute-01', 'destination': 'compute-04', 'flavor': 'm1.large', 'impact': 'Medium' }, { 'instanceName': 'api-gateway', 'source': 'compute-03', 'destination': 'compute-06', 'flavor': 'm1.small', 'impact': 'Low' }, { 'instanceName': 'cache-node-02', 'source': 'compute-01', 'destination': 'compute-07', 'flavor': 'm1.small', 'impact': 'Low' }, { 'instanceName': 'monitoring-server', 'source': 'compute-04', 'destination': 'compute-02', 'flavor': 'm1.medium', 'impact': 'Low' } ], 'host_labels': ['compute-01', 'compute-02', 'compute-03', 'compute-04', 'compute-05', 'compute-06', 'compute-07'], 'cpu_current': [78, 65, 42, 89, 34, 56, 71], 'cpu_projected': [65, 58, 45, 72, 48, 61, 68] }, { 'id': 'audit_002', 'name': 'Emergency Rebalance', 'created_at': '2024-01-14', 'cpu_weight': 1.0, 'ram_weight': 1.0, 'scope': 'Overloaded Hosts', 'strategy': 'Hotspot Reduction', 'goal': 'Reduce load on compute-01 and compute-04', 'migrations': [ { 'instanceName': 'app-server-02', 'source': 'compute-01', 'destination': 'compute-06', 'flavor': 'm1.medium', 'impact': 'Medium' }, { 'instanceName': 'file-server-01', 'source': 'compute-04', 'destination': 'compute-07', 'flavor': 'm1.large', 'impact': 'High' } ], 'host_labels': ['compute-01', 'compute-02', 'compute-03', 'compute-04', 'compute-05', 'compute-06', 'compute-07'], 'cpu_current': [92, 65, 42, 85, 34, 56, 71], 'cpu_projected': [72, 65, 42, 65, 34, 66, 81] }, { 'id': 'audit_003', 'name': 'Pre-Maintenance Planning', 'created_at': '2024-01-10', 'cpu_weight': 0.8, 'ram_weight': 1.5, 'scope': 'Maintenance Zone', 'strategy': 'Evacuation', 'goal': 'Empty compute-03 for maintenance', 'migrations': [ { 'instanceName': 'test-vm-01', 'source': 'compute-03', 'destination': 'compute-02', 'flavor': 'm1.small', 'impact': 'Low' }, { 'instanceName': 'dev-server', 'source': 'compute-03', 'destination': 'compute-05', 'flavor': 'm1.medium', 'impact': 'Low' }, { 'instanceName': 'staging-db', 'source': 'compute-03', 'destination': 'compute-07', 'flavor': 'm1.large', 'impact': 'High' } ], 'host_labels': ['compute-01', 'compute-02', 'compute-03', 'compute-04', 'compute-05', 'compute-06', 'compute-07'], 'cpu_current': [78, 65, 56, 89, 34, 56, 71], 'cpu_projected': [78, 75, 0, 89, 54, 56, 81] } ] } # Serialize lists for JavaScript for audit in context['audits']: audit['migrations'] = json.dumps(audit['migrations']) audit['host_labels'] = json.dumps(audit['host_labels']) audit['cpu_current'] = json.dumps(audit['cpu_current']) audit['cpu_projected'] = json.dumps(audit['cpu_projected']) return render(request, 'index.html', context)