def get_host_metrics(request): if request.method == 'POST' and request.is_ajax(): host_id = request.POST.get('host_id', None) if host_id: grafana_endpoint = settings.GRAFANA_ENDPOINT host_id = int(host_id) cloud = common.get_cloud_handler() host = cloud.get_host_repr(host_id) host_name = host['name'] date_to = int(round(time.time() * 1000)) date_from = date_to - 86400000 date_to = str(date_to) date_from = str(date_from) content = render_to_string( 'metrics_host.html', { 'grafana_endpoint': grafana_endpoint, 'date_from': date_from, 'date_to': date_to, 'host_name': host_name }) response = { 'html': content, 'date_from': date_from, 'date_to': date_to } return response_with_messages(messages, response, request) else: return HttpResponse("host_id is empty") else: return HttpResponse("Wrong request")
def get_cluster_info(request): if request.method == 'POST' and request.is_ajax(): cluster_id = request.POST.get('cluster_id', None) if cluster_id: cluster_id = int(cluster_id) cloud = common.get_cloud_handler() cluster = cloud.get_cluster_repr(cluster_id) response = {'id': cluster['id'], 'name': cluster['name']} messages.success( request, "Cluster information was synchronized successfully!") return response_with_messages(messages, response, request) else: return HttpResponse("cluster_id is empty") else: return HttpResponse("Wrong request")
def get_vm_info(request): if request.method == 'POST' and request.is_ajax(): vm_id = request.POST.get('vm_id', None) if vm_id: vm_id = int(vm_id) cloud = common.get_cloud_handler() vm = cloud.get_vm_repr(vm_id) response = { 'id': vm['id'], 'cpu': vm['cpu_req'], 'ram': vm['mem_req'], 'host': vm['hid'] } messages.success(request, "VM information was synchronized successfully!") return response_with_messages(messages, response, request) else: return HttpResponse("vm_id is empty") else: return HttpResponse("Wrong request")
def get_host_info(request): if request.method == 'POST' and request.is_ajax(): host_id = request.POST.get('host_id', None) if host_id: host_id = int(host_id) cloud = common.get_cloud_handler() host = cloud.get_host_repr(host_id) response = { 'id': host['id'], 'cpu': host['usage_cpu']['max'], 'ram': host['usage_mem']['max'], 'cluster_id': host['cluster_id'] } messages.success( request, "Host information was synchronized successfully!") return response_with_messages(messages, response, request) else: return HttpResponse("host_id is empty") else: return HttpResponse("Wrong request")
def load_new_cloud_conf(): cloud = common.get_cloud_handler() clusters = cloud.get_clusters_repr() for cluster in clusters: cluster_record = Cluster(cluster_id=cluster['id'], cluster_name=cluster['name']) cluster_record.save() hosts = cloud.get_hosts_repr() for host in hosts: host_record = Host(host_id=host['id'], cpu=host['usage_cpu']['max'], ram=host['usage_mem']['max'], cluster=host['cluster_id']) host_record.save() vms = cloud.get_vms_repr(vmStateFilter=3) for vm in vms: vm_record = VM(vm_id=vm['id'], cpu=vm['cpu_req'], ram=vm['mem_req'], host=Host.objects.get(host_id=vm['hid'])) vm_record.save()
def get_cloud_conf_changes(): cloud = common.get_cloud_handler() result = {"clusters": {}, "hosts": {}, "vms": {}} hasChanges = False clusters = cloud.get_clusters_repr() clusters_db = Cluster.objects.all() for cluster in clusters: found_match = False for cluster_db in clusters_db: if cluster["id"] == cluster_db.cluster_id: found_match = True if cluster["name"] != cluster_db.cluster_name: hasChanges = True result["clusters"][str(cluster["id"])] = { "cluster_id": cluster['id'], "cluster_name": cluster['name'], "old_cluster_name": cluster_db.cluster_name } if not found_match: hasChanges = True result["clusters"][str(cluster["id"])] = { "cluster_id": cluster['id'], "cluster_name": cluster['name'], "old_cluster_name": "-" } hosts = cloud.get_hosts_repr() hosts_db = Host.objects.all() for host in hosts: found_match = False for host_db in hosts_db: if host["id"] == host_db.host_id: found_match = True if host_db.cpu != host['usage_cpu'][ 'max'] or host_db.ram != host['usage_mem'][ 'max'] or host_db.cluster != host['cluster_id']: hasChanges = True result["hosts"][host["id"]] = { 'host_id': host['id'], 'cpu': host['usage_cpu']['max'], 'ram': host['usage_mem']['max'], 'cluster_id': host['cluster_id'], 'old_cpu': host_db.cpu, 'old_ram': host_db.ram, 'old_cluster_id': host_db.cluster } if not found_match: hasChanges = True result["hosts"][host["id"]] = { 'host_id': host['id'], 'cpu': host['usage_cpu']['max'], 'ram': host['usage_mem']['max'], 'cluster_id': host['cluster_id'], 'old_cpu': "-", 'old_ram': "-", 'old_cluster_id': "-" } vms = cloud.get_vms_repr(vmStateFilter=3) vms_db = VM.objects.all().prefetch_related('host') for vm in vms: found_match = False for vm_db in vms_db: if vm['id'] == vm_db.vm_id: found_match = True if vm_db.cpu != vm['cpu_req'] or vm_db.ram != vm[ 'mem_req'] or vm_db.host.host_id != vm['hid']: hasChanges = True result["vms"][vm["id"]] = { 'vm_id': vm['id'], 'cpu': vm['cpu_req'], 'ram': vm['mem_req'], 'host_id': vm['hid'], 'old_cpu': vm_db.cpu, 'old_ram': vm_db.ram, 'old_host_id': vm_db.host.host_id } if not found_match: hasChanges = True result["vms"][vm["id"]] = { 'vm_id': vm['id'], 'cpu': vm['cpu_req'], 'ram': vm['mem_req'], 'host_id': vm['hid'], 'old_cpu': "-", 'old_ram': "-", 'old_host_id': "-" } result['has'] = hasChanges return result