Change in vdsm[master]: hooks: Add hook for fake VM stats

dkuznets at redhat.com dkuznets at redhat.com
Thu Mar 20 09:50:40 UTC 2014


Dima Kuznetsov has uploaded a new change for review.

Change subject: hooks: Add hook for fake VM stats
......................................................................

hooks: Add hook for fake VM stats

Added hook to generate random VmStats for fake (or real) VMs.

Added before and after get VM stats hook. Currently there is no use for
before hook and I cannot think of any way it can be useful, but added
for symmetry.

Change-Id: I6a6d13f43495170a5cb86ae0b0efc1ae85ef05ab
Signed-off-by: Dima Kuznetsov <dkuznets at redhat.com>
---
M configure.ac
M vdsm.spec.in
M vdsm/hooking.py
M vdsm/hooks.py
M vdsm/vm.py
M vdsm_hooks/Makefile.am
A vdsm_hooks/fakevmstats/Makefile.am
A vdsm_hooks/fakevmstats/after_vm_stats.py
8 files changed, 102 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/27/25927/1

diff --git a/configure.ac b/configure.ac
index 55879e7..6d73711 100644
--- a/configure.ac
+++ b/configure.ac
@@ -279,6 +279,7 @@
 	vdsm_hooks/checkimages/Makefile
 	vdsm_hooks/directlun/Makefile
 	vdsm_hooks/extnet/Makefile
+	vdsm_hooks/fakevmstats/Makefile
 	vdsm_hooks/faqemu/Makefile
 	vdsm_hooks/fileinject/Makefile
 	vdsm_hooks/floppy/Makefile
diff --git a/vdsm.spec.in b/vdsm.spec.in
index 041deac..d6f5a71 100644
--- a/vdsm.spec.in
+++ b/vdsm.spec.in
@@ -584,6 +584,12 @@
 %description hook-vmdisk
 Hook adds additional disk image for a VM (raw or qcow2)
 
+%package hook-fakevmstats
+Summary:	Generate random VM statistics
+BuildArch:	noarch
+
+%description hook-fakevmstats
+Hook intercepts VM's stats and randomizes various fields.
 %if 0%{?with_gluster}
 %package gluster
 Summary:        Gluster Plugin for VDSM
@@ -1331,6 +1337,11 @@
 %{_libexecdir}/%{vdsm_name}/hooks/before_device_create/50_vmfex
 %{_libexecdir}/%{vdsm_name}/hooks/before_device_migrate_destination/50_vmfex
 %{_libexecdir}/%{vdsm_name}/hooks/before_nic_hotplug/50_vmfex
+
+%files hook-fakevmstats
+%defattr(-, root, root, -)
+%{_libexecdir}/%{vdsm_name}/hooks/after_vm_stats/10_fakevmstats
+
 %endif
 
 %files debug-plugin
diff --git a/vdsm/hooking.py b/vdsm/hooking.py
index b6be7ca..a49032c 100644
--- a/vdsm/hooking.py
+++ b/vdsm/hooking.py
@@ -36,6 +36,7 @@
 >2 - reserved
 """
 
+import json
 import os
 import sys
 from xml.dom import minidom
@@ -56,6 +57,14 @@
     file(os.environ['_hook_domxml'], 'w').write(domxml.toxml(encoding='utf-8'))
 
 
+def read_json():
+    return json.loads(file(os.environ['_hook_json']).read())
+
+
+def write_json(data):
+    file(os.environ['_hook_json'], 'w').write(json.dumps(data))
+
+
 def exit_hook(message, return_code=2):
     """
     Exit the hook with a given message, which will be printed to the standard
diff --git a/vdsm/hooks.py b/vdsm/hooks.py
index 75f33f9..1689eb9 100644
--- a/vdsm/hooks.py
+++ b/vdsm/hooks.py
@@ -339,6 +339,15 @@
                         raiseError=False, hookType=_JSON_HOOK)
 
 
+def before_vm_stats():
+    pass
+
+
+def after_vm_stats(stats):
+    return _runHooksDir(stats, 'after_vm_stats', raiseError=False,
+                        hookType=_JSON_HOOK)
+
+
 def _getScriptInfo(path):
     try:
         with file(path) as f:
diff --git a/vdsm/vm.py b/vdsm/vm.py
index c53f1d4..60e9e8c 100644
--- a/vdsm/vm.py
+++ b/vdsm/vm.py
@@ -2813,6 +2813,7 @@
         stats['hash'] = self._devXmlHash
         if self._watchdogEvent:
             stats["watchdogEvent"] = self._watchdogEvent
+        stats = hooks.after_vm_stats(stats)
         return stats
 
     def _getStatsInternal(self):
diff --git a/vdsm_hooks/Makefile.am b/vdsm_hooks/Makefile.am
index ce4aa46..f6cffe6 100644
--- a/vdsm_hooks/Makefile.am
+++ b/vdsm_hooks/Makefile.am
@@ -29,6 +29,7 @@
 	directlun \
 	extnet \
 	fileinject \
+	fakevmstats \
 	floppy \
 	hostusb \
 	hugepages \
diff --git a/vdsm_hooks/fakevmstats/Makefile.am b/vdsm_hooks/fakevmstats/Makefile.am
new file mode 100644
index 0000000..8e416b0
--- /dev/null
+++ b/vdsm_hooks/fakevmstats/Makefile.am
@@ -0,0 +1,35 @@
+#
+# Copyright 2011-2012 Red Hat, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+#
+# Refer to the README and COPYING files for full details of the license
+#
+
+include $(top_srcdir)/build-aux/Makefile.subs
+
+CLEANFILES = \
+	config.log
+
+EXTRA_DIST = \
+	after_vm_stats.py
+
+install-data-local:
+	$(MKDIR_P) $(DESTDIR)$(vdsmhooksdir)/after_vm_stats
+	$(INSTALL_SCRIPT) $(srcdir)/after_vm_stats.py \
+		$(DESTDIR)$(vdsmhooksdir)/after_vm_stats/10_fakevmstats
+
+uninstall-local:
+	$(RM) $(DESTDIR)$(vdsmhooksdir)/after_vm_stats/10_fakevmstats
diff --git a/vdsm_hooks/fakevmstats/after_vm_stats.py b/vdsm_hooks/fakevmstats/after_vm_stats.py
new file mode 100644
index 0000000..6ff0468
--- /dev/null
+++ b/vdsm_hooks/fakevmstats/after_vm_stats.py
@@ -0,0 +1,35 @@
+#!/usr/bin/python
+import hooking
+import random
+
+
+def randomizeRuntimeStats(stats):
+    cpuTotal = random.random() * 100.0
+    cpuUser = random.random() * cpuTotal
+    stats['cpuUser'] = '%.2f' % (cpuUser)
+    stats['cpuSys'] = '%.2f' % (cpuTotal - cpuUser)
+    stats['memUsage'] = str(random.randint(0, 100))
+
+    # Disks
+    for disk in stats['disks'].iterkeys():
+        # Simulate some supersonic disks:
+        stats['disks'][disk]['readRate'] = str(random.randint(0, 2**32))
+        stats['disks'][disk]['writeRate'] = str(random.randint(0, 2**32))
+        stats['disks'][disk]['readLatency'] = str(random.randint(0, 10**9))
+        stats['disks'][disk]['writeLatency'] = str(random.randint(0, 10**9))
+        stats['disks'][disk]['flushLatency'] = str(random.randint(0, 10**9))
+
+    # Network:
+    for net in stats['network'].iterkeys():
+        stats['network'][net]['rxRate'] = '%.1f' % (random.random() * 100)
+        stats['network'][net]['rxDropped'] = str(random.randint(0, 2*32))
+        stats['network'][net]['rxErrors'] = str(random.randint(0, 2*32))
+        stats['network'][net]['txRate'] = '%.1f' % (random.random() * 100)
+        stats['network'][net]['txDropped'] = str(random.randint(0, 2*32))
+        stats['network'][net]['txErrors'] = str(random.randint(0, 2*32))
+
+
+if __name__ == '__main__':
+    stats = hooking.read_json()
+    randomizeRuntimeStats(stats)
+    hooking.write_json(stats)


-- 
To view, visit http://gerrit.ovirt.org/25927
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I6a6d13f43495170a5cb86ae0b0efc1ae85ef05ab
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Dima Kuznetsov <dkuznets at redhat.com>


More information about the vdsm-patches mailing list