From 5c6ff422544d7d8e426673aaa9864042ea7ce43c Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Tue, 27 Aug 2013 12:27:21 +0200 Subject: [PATCH] examples: Add script to parse topology from capabilities output Add a demo script originally written by Amador Pahim to parse topology of the host from data provided in the capabilities XML. --- examples/python/Makefile.am | 1 + examples/python/topology.py | 45 +++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100755 examples/python/topology.py diff --git a/examples/python/Makefile.am b/examples/python/Makefile.am index 2cacfa127d..d5d867cc5f 100644 --- a/examples/python/Makefile.am +++ b/examples/python/Makefile.am @@ -17,4 +17,5 @@ EXTRA_DIST= \ README \ consolecallback.py \ + topoology.py \ dominfo.py domrestore.py domsave.py domstart.py esxlist.py diff --git a/examples/python/topology.py b/examples/python/topology.py new file mode 100755 index 0000000000..62effe3811 --- /dev/null +++ b/examples/python/topology.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python +# Parse topology information from the capabilities XML and use +# them to calculate host topology +# +# Authors: +# Amador Pahim +# Peter Krempa + +import libvirt +import sys +from xml.dom import minidom + +try: + conn = libvirt.openReadOnly(None) +except libvirt.libvirtError: + print 'Failed to connect to the hypervisor' + sys.exit(1) + +try: + capsXML = conn.getCapabilities() +except libvirt.libvirtError: + print 'Failed to request capabilities' + sys.exit(1) + +caps = minidom.parseString(capsXML) +host = caps.getElementsByTagName('host')[0] +cells = host.getElementsByTagName('cells')[0] +total_cpus = cells.getElementsByTagName('cpu').length + +socketIds = [] +siblingsIds = [] + +socketIds = [ proc.getAttribute('socket_id') + for proc in cells.getElementsByTagName('cpu') + if proc.getAttribute('socket_id') not in socketIds ] + +siblingsIds = [ proc.getAttribute('siblings') + for proc in cells.getElementsByTagName('cpu') + if proc.getAttribute('siblings') not in siblingsIds ] + +print "Host topology" +print "NUMA nodes:", cells.getAttribute('num') +print " Sockets:", len(set(socketIds)) +print " Cores:", len(set(siblingsIds)) +print " Threads:", total_cpus -- 2.47.2