Skip to content
Snippets Groups Projects
Commit ef83bc0c authored by Anael Beutot's avatar Anael Beutot
Browse files

Implemented cpuuse tag for VMs.

parent 71ab2daf
No related branches found
No related tags found
No related merge requests found
...@@ -56,6 +56,9 @@ class VirtualMachine(object): ...@@ -56,6 +56,9 @@ class VirtualMachine(object):
logger.debug('Virtual Machine tags: %s', self.tags) logger.debug('Virtual Machine tags: %s', self.tags)
#: keep record of CPU stats (libev timestamp, cpu time)
self.cpu_stats = (hypervisor.handler.main.evloop.now(), dom.info()[4])
@property @property
def state(self): def state(self):
return self._state return self._state
......
...@@ -76,8 +76,17 @@ def cpu(dom): ...@@ -76,8 +76,17 @@ def cpu(dom):
@ttl(10) @ttl(10)
@refresh(5) @refresh(5)
def cpuuse(): @_vir_tag
pass def cpuuse(dom):
"""Represent CPU use in percent average on 5 seconds."""
state, _, _, vcpu, cpu_time = dom.lv_dom.info()
if state != 1: # if VM is not running
return None
old_cpu_stats = dom.cpu_stats
dom.cpu_stats = (dom.hypervisor.handler.main.evloop.now(), cpu_time)
# calculate CPU percentage
return '%.2f' % max(0., min(100., ((cpu_time - old_cpu_stats[1]) * 100.) / (
(dom.cpu_stats[0] - old_cpu_stats[0]) * 1000. * 1000. * 1000.)))
@_vir_tag @_vir_tag
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment