def getList(node): if core.cfg['QUIET']: msg = "" for vm in sorted(node.get_vms(),key=lambda x: x.name): msg += vm.name + "\n" else: # Max 80 columns wide msg = "\nOn %s :\n" % (node.get_hostname()) msg += "-----" + "-" * len(node.get_hostname()) + '\n' msg += '\n %-60s %4s %5s %6s\n' % ("Name","Mem", "VCPUs","State") for vm in sorted(node.get_vms(),key=lambda x: x.name): msg += ' %-60s %4d %5d %6s\n' % (vm.name, vm.ram, vm.vcpu, vm.state) return (node.get_hostname(), msg)
def loadbalance(self): """ Run the loadbalancer on the cluster and migrate vm accordingly. See cxm.loadbalancer module for details about algorithm. """ if not core.cfg['QUIET']: print "Recording metrics..." current_state = {} vm_metrics = {} node_metrics = {} for node in self.get_nodes(): node.metrics.init_cache( ) # Early call to increase timeslice used to compute rates vms = node.get_vms() # Get current cluster state current_state[node.get_hostname()] = [vm.name for vm in vms] # Get node's metrics node_metrics[node.get_hostname()] = { 'ram': node.metrics.get_available_ram() } # Get VM's metrics cpu = node.metrics.get_vms_cpu_usage() io = node.metrics.get_vms_disk_io_rate() for vm in vms: vm_metrics[vm.name] = {} vm_metrics[vm.name]['ram'] = vm.get_ram() vm_metrics[vm.name]['cpu'] = cpu[vm.name] vm_metrics[ vm.name]['io'] = io[vm.name]['Read'] + io[vm.name]['Write'] # Initialize loadbalancer lb = loadbalancer.LoadBalancer(current_state) lb.set_metrics(vm_metrics, node_metrics) solution = lb.get_solution() if not solution: print "No better solution found with a minimal gain of %s%%." % core.cfg[ 'LB_MIN_GAIN'] else: # Ask the user for a confirmation if not core.cfg['QUIET']: print "Here is the proposed migration plan:" for path in solution.get_path(): print " -> Migrate", path['vm'], "from", path[ 'src'], "to", path['dst'] if (raw_input("Proceed ? [y/N]:").upper() != "Y"): print "Aborded by user." return # Do migrations to put the cluster in the selected state for path in solution.get_path(): if not core.cfg['QUIET']: print "Migrating", path['vm'], "from", path[ 'src'], "to", path['dst'], "..." self.migrate(path['vm'], path['src'], path['dst'])
def loadbalance(self): """ Run the loadbalancer on the cluster and migrate vm accordingly. See cxm.loadbalancer module for details about algorithm. """ if not core.cfg["QUIET"]: print "Recording metrics..." current_state = {} vm_metrics = {} node_metrics = {} for node in self.get_nodes(): node.metrics.init_cache() # Early call to increase timeslice used to compute rates vms = node.get_vms() # Get current cluster state current_state[node.get_hostname()] = [vm.name for vm in vms] # Get node's metrics node_metrics[node.get_hostname()] = {"ram": node.metrics.get_available_ram()} # Get VM's metrics cpu = node.metrics.get_vms_cpu_usage() io = node.metrics.get_vms_disk_io_rate() for vm in vms: vm_metrics[vm.name] = {} vm_metrics[vm.name]["ram"] = vm.get_ram() vm_metrics[vm.name]["cpu"] = cpu[vm.name] vm_metrics[vm.name]["io"] = io[vm.name]["Read"] + io[vm.name]["Write"] # Initialize loadbalancer lb = loadbalancer.LoadBalancer(current_state) lb.set_metrics(vm_metrics, node_metrics) solution = lb.get_solution() if not solution: print "No better solution found with a minimal gain of %s%%." % core.cfg["LB_MIN_GAIN"] else: # Ask the user for a confirmation if not core.cfg["QUIET"]: print "Here is the proposed migration plan:" for path in solution.get_path(): print " -> Migrate", path["vm"], "from", path["src"], "to", path["dst"] if raw_input("Proceed ? [y/N]:").upper() != "Y": print "Aborded by user." return # Do migrations to put the cluster in the selected state for path in solution.get_path(): if not core.cfg["QUIET"]: print "Migrating", path["vm"], "from", path["src"], "to", path["dst"], "..." self.migrate(path["vm"], path["src"], path["dst"])
def cxm_list(cluster, options): """List started VM on all nodes.""" if options.node: nodes=[cluster.get_node(options.node)] else: nodes=cluster.get_nodes() for node in nodes: print "\nOn", node.get_hostname(), ":" if not core.cfg['QUIET']: print "-----" + "-" * len(node.get_hostname()) print '\n %-40s %4s %5s %6s' % ("Name","Mem", "VCPUs","State") for vm in sorted(node.get_vms(),key=lambda x: x.name): print ' %-40s %4d %5d %6s' % (vm.name, vm.ram, vm.vcpu, vm.state)
def check(self): """Perform a sanity check of the cluster. Return a corresponding exit code (0=success, 0!=error) """ if not core.cfg["QUIET"]: print "Checking for duplicate VM..." safe = True # Get cluster wide VM list vm_by_node = dict() for node in self.get_nodes(): vm_by_node[node.get_hostname()] = node.get_vms() if core.cfg["DEBUG"]: print "DEBUG vm_by_node=", vm_by_node # Invert key/value of the dict node_by_vm = dict() for node, vms in vm_by_node.items(): for vm in vms: try: node_by_vm[vm.name].append(node) except KeyError: node_by_vm[vm.name] = [node] if core.cfg["DEBUG"]: print "DEBUG node_by_vm =", node_by_vm # Check duplicate VM for vm, nodes in node_by_vm.items(): if len(nodes) > 1: print " ** WARNING : " + vm + " is running on " + " and ".join(nodes) safe = False # Check bridges if not self.check_bridges(): safe = False # Other checks for node in self.get_nodes(): # Check (non)activation of LVs if not node.check_lvs(): safe = False # Check autostart link if not node.check_autostart(): safe = False return safe
def check(self): """Perform a sanity check of the cluster. Return a corresponding exit code (0=success, 0!=error) """ if not core.cfg['QUIET']: print "Checking for duplicate VM..." safe = True # Get cluster wide VM list vm_by_node = dict() for node in self.get_nodes(): vm_by_node[node.get_hostname()] = node.get_vms() if core.cfg['DEBUG']: print "DEBUG vm_by_node=", vm_by_node # Invert key/value of the dict node_by_vm = dict() for node, vms in vm_by_node.items(): for vm in vms: try: node_by_vm[vm.name].append(node) except KeyError: node_by_vm[vm.name] = [node] if core.cfg['DEBUG']: print "DEBUG node_by_vm =", node_by_vm # Check duplicate VM for vm, nodes in node_by_vm.items(): if len(nodes) > 1: print " ** WARNING : " + vm + " is running on " + " and ".join( nodes) safe = False # Check bridges if not self.check_bridges(): safe = False # Other checks for node in self.get_nodes(): # Check (non)activation of LVs if not node.check_lvs(): safe = False # Check autostart link if not node.check_autostart(): safe = False return safe