Hyper-V Clusters

'' AND cluster NOT LIKE '%-WM%' AND cluster NOT LIKE '%-C1MAS%' AND cluster NOT LIKE '%-vrs%' AND (cluster LIKE 'DUN%' OR cluster LIKE 'DMV%') GROUP BY cluster ORDER BY cluster "; $all_clusters_data = Invoke_Infra($cluster_query); // Query 2: Get VM distribution for all relevant clusters at once. $vm_repart_query = " SELECT v.Cluster, v.Owner, count(v.Owner) as vm_count FROM cmdb_vms v INNER JOIN cmdb_HyperV_Hosts h ON v.Cluster = h.cluster WHERE v.decomtime IS NULL AND h.cluster <> '' AND h.cluster NOT LIKE '%-WM%' AND h.cluster NOT LIKE '%-C1MAS%' AND h.cluster NOT LIKE '%-vrs%' AND (h.cluster LIKE 'DUN%' OR h.cluster LIKE 'DMV%') GROUP BY v.Cluster, v.Owner ORDER BY v.Cluster, v.Owner "; $raw_vm_distribution = Invoke_Infra($vm_repart_query); // Process the VM distribution data into an easy-to-use associative array $vm_distribution = []; if (is_array($raw_vm_distribution)) { foreach ($raw_vm_distribution as $repart) { $vm_distribution[$repart['Cluster']][] = [ 'owner' => $repart['Owner'], 'vm_count' => $repart['vm_count'] ]; } } // ================================================================= // STEP 2: Loop through the pre-fetched data and display it // ================================================================= $total_vms_left = 0; $cluster_count = 0; if (is_array($all_clusters_data)) { foreach ($all_clusters_data as $cluster_data) { if ($cluster_count > 0 && $cluster_count % 4 == 0) { echo "
"; } // --- Calculations --- $cluster_name = $cluster_data['cluster']; $node_count = (int)$cluster_data['node_count']; $total_vms = (int)$cluster_data['total_vms']; $total_vm_memory = (int)$cluster_data['total_vm_memory']; // Memory calculation $cluster_usable_mem = (int)$cluster_data['min_node_ram'] - 32; if ($node_count == 4) { $cluster_usable_mem *= 2; } $free_mem = $cluster_usable_mem - $total_vm_memory; $free_mem_percentage = ($cluster_usable_mem > 0) ? round($free_mem / $cluster_usable_mem * 100) : 0; // Storage calculation $capacity_gb = 0; $free_gb = 0; if (!empty($cluster_data['all_csvs'])) { // Explode the big string into an array of all CSV entries (with duplicates) $all_csv_entries = explode("|", $cluster_data['all_csvs']); // Create a new array containing only the unique CSV entries $unique_csv_entries = array_unique($all_csv_entries); // Loop through the UNIQUE entries to perform the calculation foreach ($unique_csv_entries as $csv_string) { if (str_contains($csv_string, ';')) { $parts = explode(";", $csv_string); if(count($parts) >= 3) { $capacity_gb += (int)$parts[1]; // Free space is Total Capacity (parts[1]) - Used Space (parts[2]) $free_gb += (int)$parts[1] - (int)$parts[2]; } } } } $free_gb = max(0, $free_gb); $free_storage_percentage = ($capacity_gb > 0) ? round($free_gb / $capacity_gb * 100) : 0; // IOPS Calculation $avg_io = ($node_count > 0) ? round((float)$cluster_data['total_io_disk'] / $node_count * 1024) : 0; $io_display = ($avg_io == 0) ? "?" : $avg_io; // VMs Left Calculation $vms_left_by_mem = ($free_mem > 0) ? floor($free_mem / 16) : 0; $vms_left_by_storage = ($free_gb > 0) ? floor($free_gb / 110) : 0; $vms_left = min($vms_left_by_mem, $vms_left_by_storage); $total_vms_left += $vms_left; // --- Display Logic --- if ($vms_left > 2) { $vleft_html = "(" . $vms_left . " VMs left)"; } elseif ($vms_left >= 1) { // Handles 1 and 2 $vleft_html = "(" . $vms_left . " VMs left)"; } else { $vleft_html = "(" . $vms_left . " VMs left)"; } ?>


Total VMs Left: