def _get_vms_cpu_usage(): cpu = dict() # Get domains' infos doms = self.node.legacy_server.xend.domains(True) log.debug("[Legacy-API]", self.node.get_hostname(), "doms=", doms) # Timestamp used to compute CPU percentage timestamp = time.time() # Initialize result with 0 for all vm # This is because legacy api do not report paused vm for vm in self.node.get_vms( nocache ): # 5s of cache is ok, this func is designed to be run every 60s cpu[vm.name] = 0 for dom in doms: dom_info = main.parse_doms_info(dom) try: # String version with one digit after dot # See http://stackoverflow.com/questions/56820/round-in-python-doesnt-seem-to-be-rounding-properly for reasons. #cpu[dom_info['name']]="%.1f" % round( # (dom_info['cpu_time']-self.cpu_cache[dom_info['name']])*100/(timestamp-self.cpu_cache['timestamp']),1 #) cpu[dom_info['name']] = ( dom_info['cpu_time'] - self.cpu_cache[dom_info['name']] ) * 100 / (timestamp - self.cpu_cache['timestamp']) except KeyError: # First call: return zero values cpu[dom_info['name']] = 0 except ZeroDivisionError: cpu[dom_info['name']] = 0 # In case of reboot, remove negative values if cpu[dom_info['name']] < 0: cpu[dom_info['name']] = 0 # Update cpu_cache with the new value self.cpu_cache[dom_info['name']] = dom_info['cpu_time'] # Update timestamp self.cpu_cache['timestamp'] = timestamp return cpu
def get_vms_cpu_usage(self): """ Return a dict with the computed CPU usage for all runing VMs. Values are floats with 16 digit of precision (python standard's binary float) If you want a string with less precision, you can use "%.1f" % round(xxx). If you want a number with less precision, you can use the Decimal module. """ cpu = dict() # Get domains' infos doms = self.node.legacy_server.xend.domains(True) if core.cfg["DEBUG"]: print "DEBUG Legacy-Api: ", doms # Timestamp used to compute CPU percentage timestamp = time.time() for dom in doms: dom_info = main.parse_doms_info(dom) try: # String version with one digit after dot # See http://stackoverflow.com/questions/56820/round-in-python-doesnt-seem-to-be-rounding-properly for reasons. # cpu[dom_info['name']]="%.1f" % round( # (dom_info['cpu_time']-self.cpu_cache[dom_info['name']])*100/(timestamp-self.cpu_cache['timestamp']),1 # ) cpu[dom_info["name"]] = ( (dom_info["cpu_time"] - self.cpu_cache[dom_info["name"]]) * 100 / (timestamp - self.cpu_cache["timestamp"]) ) except KeyError: # First call: return zero values cpu[dom_info["name"]] = 0 except ZeroDivisionError: cpu[dom_info["name"]] = 0 # Update cpu_cache with the new value self.cpu_cache[dom_info["name"]] = dom_info["cpu_time"] # Update timestamp self.cpu_cache["timestamp"] = timestamp return cpu
def _get_vms_cpu_usage(): cpu=dict() # Get domains' infos doms=self.node.legacy_server.xend.domains(True) log.debug("[Legacy-API]", self.node.get_hostname(), "doms=", doms) # Timestamp used to compute CPU percentage timestamp=time.time() # Initialize result with 0 for all vm # This is because legacy api do not report paused vm for vm in self.node.get_vms(nocache): # 5s of cache is ok, this func is designed to be run every 60s cpu[vm.name]=0 for dom in doms: dom_info=main.parse_doms_info(dom) try: # String version with one digit after dot # See http://stackoverflow.com/questions/56820/round-in-python-doesnt-seem-to-be-rounding-properly for reasons. #cpu[dom_info['name']]="%.1f" % round( # (dom_info['cpu_time']-self.cpu_cache[dom_info['name']])*100/(timestamp-self.cpu_cache['timestamp']),1 #) cpu[dom_info['name']]=(dom_info['cpu_time']-self.cpu_cache[dom_info['name']])*100/(timestamp-self.cpu_cache['timestamp']) except KeyError: # First call: return zero values cpu[dom_info['name']]=0 except ZeroDivisionError: cpu[dom_info['name']]=0 # In case of reboot, remove negative values if cpu[dom_info['name']] < 0: cpu[dom_info['name']]=0 # Update cpu_cache with the new value self.cpu_cache[dom_info['name']]=dom_info['cpu_time'] # Update timestamp self.cpu_cache['timestamp']=timestamp return cpu
def get_vms_cpu_usage(self): """ Return a dict with the computed CPU usage for all runing VMs. Values are floats with 16 digit of precision (python standard's binary float) If you want a string with less precision, you can use "%.1f" % round(xxx). If you want a number with less precision, you can use the Decimal module. """ cpu = dict() # Get domains' infos doms = self.node.legacy_server.xend.domains(True) if core.cfg['DEBUG']: print "DEBUG Legacy-Api: ", doms # Timestamp used to compute CPU percentage timestamp = time.time() for dom in doms: dom_info = main.parse_doms_info(dom) try: # String version with one digit after dot # See http://stackoverflow.com/questions/56820/round-in-python-doesnt-seem-to-be-rounding-properly for reasons. #cpu[dom_info['name']]="%.1f" % round( # (dom_info['cpu_time']-self.cpu_cache[dom_info['name']])*100/(timestamp-self.cpu_cache['timestamp']),1 #) cpu[dom_info['name']] = ( dom_info['cpu_time'] - self.cpu_cache[dom_info['name']] ) * 100 / (timestamp - self.cpu_cache['timestamp']) except KeyError: # First call: return zero values cpu[dom_info['name']] = 0 except ZeroDivisionError: cpu[dom_info['name']] = 0 # Update cpu_cache with the new value self.cpu_cache[dom_info['name']] = dom_info['cpu_time'] # Update timestamp self.cpu_cache['timestamp'] = timestamp return cpu