Script to determine which Hypervisor a VM is running on

Hi, I'm checking to see if someone has a script to determine where a VM is running preferably with the shell? Thanks, VS

This is a multi-part message in MIME format. --------------050306040406020008040501 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Hi Citros, I have this Python script I wrote to get VM information from Red Hat Virtualization which I believe should work for Ovirt as well. I should point out that I'm not a Python user normally and actually hate the language. If the formatting gets screwed I can email you a copy. To use it either run it without parameters to get a list of all VMs or run it with "-n vm_name" (case sensitive) to list just a single VM. [code] #! /usr/bin/python from __future__ import division from ovirtsdk.api import API from ovirtsdk.xml import params from optparse import OptionParser import os import sys import urllib ## Edit the following three items as necesary rhevman = 'https://your_rhev_manager' user = 'admin@internal' password = 'user_password' cert = '/tmp/rhevm.cer' ## Format numbers def factor(value): str = 'Bytes' if (value >= 1024): value /= 1024 str = 'KB' if (value >= 1024): value /= 1024 str = 'MB' if (value >= 1024): value /= 1024 str = 'GB' return '%s %s' % (('%f' % value).rstrip('0').rstrip('.'), str) ## Ensure we have the server ca certificate if (not os.path.isfile(cert)): print "Certificate file not found. Trying to download it." urllib.urlretrieve (rhevman + '/ca.crt', cert) ## Find out what is being asked for vmname = '' parser = OptionParser() parser.add_option('-n', '--name', dest='vmname', default='', action='store', help='Specify the name of a single VM (case sensitive)') (options, args) = parser.parse_args() vmname = options.vmname hostnames = {} try: ## Connect to the API api = API (url=rhevman, username=user, password=password, ca_file=cert) try: ## Get the list of hypervisors for use later on for host in api.hosts.list(): hostnames[host.id] = host.name ## Are we after a single VM or the entire list? if (len(vmname) > 0): print 'Retrieving information for %s' % vmname vmsList = api.vms.list(query = 'name=%s' % vmname) else: print 'Retrieving information for all VMs' vmsList = api.vms.list(max = -1) print 'Found %d VMs' % len(vmsList) # Get the VM details for instance in vmsList: address=[] print '\nVM: %s' % instance.name host = instance.get_host() print '\tState: %s\n\tDescription: %s\n\tComment: %s' % (instance.status.state, instance.description, instance.comment) if (host): print '\tHost: %s' % hostnames[host.id] createtime = instance.get_creation_time() print '\tCreated: %s' % createtime starttime = instance.get_start_time() print '\tStarted: %s' % starttime cpu = instance.get_cpu() print '\tCPU: %s (%s sockets, %s cores)' % (cpu.topology.sockets*cpu.topology.cores, cpu.topology.sockets, cpu.topology.cores) ram = instance.get_memory() print '\tRAM: %s' % factor(ram) disks = instance.get_disks().list() for disk in disks: print '\tDisk: %s %s' % (disk.name, factor(disk.size)) if ((instance.status.state == 'up') and instance.get_guest_info()): vmnics= instance.get_nics().list() ips = instance.get_guest_info().get_ips().get_ip() for card in vmnics: print '\tInterface: %s, MAC address: %s' % (card.name, card.mac.address) for ip in ips: address.append(ip.get_address()) print '\tIP: %s' % ( ip.get_address()) except Exception as e: print ' Exception:\n%s' % str(e) api.disconnect() except Exception as ex: print 'Unexpected error: %s' % ex [/code] regards, John On 18/06/14 00:59, Citros Airv wrote:
Hi,
I'm checking to see if someone has a script to determine where a VM is running preferably with the shell?
Thanks,
VS
______________________________________________________________________ This email has been scanned by the Symantec Email Security.cloud service. For more information please visit http://www.symanteccloud.com ______________________________________________________________________
_______________________________________________ Users mailing list Users@ovirt.org http://lists.ovirt.org/mailman/listinfo/users
--------------050306040406020008040501 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit <html> <head> <meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"> </head> <body text="#000000" bgcolor="#FFFFFF"> Hi Citros,<br> <br> I have this Python script I wrote to get VM information from Red Hat Virtualization which I believe should work for Ovirt as well. I should point out that I'm not a Python user normally and actually hate the language. If the formatting gets screwed I can email you a copy.<br> <br> To use it either run it without parameters to get a list of all VMs or run it with "-n vm_name" (case sensitive) to list just a single VM.<br> <br> [code]<br> #! /usr/bin/python<br> <br> from __future__ import division<br> from ovirtsdk.api import API<br> from ovirtsdk.xml import params<br> from optparse import OptionParser<br> import os<br> import sys<br> import urllib<br> <br> ## Edit the following three items as necesary<br> rhevman = '<a class="moz-txt-link-freetext" href="https://your_rhev_manager">https://your_rhev_manager</a>'<br> user = 'admin@internal'<br> password = 'user_password'<br> <br> cert = '/tmp/rhevm.cer'<br> <br> ## Format numbers<br> def factor(value):<br> str = 'Bytes'<br> if (value >= 1024):<br> value /= 1024<br> str = 'KB'<br> if (value >= 1024):<br> value /= 1024<br> str = 'MB'<br> if (value >= 1024):<br> value /= 1024<br> str = 'GB'<br> return '%s %s' % (('%f' % value).rstrip('0').rstrip('.'), str)<br> <br> ## Ensure we have the server ca certificate<br> if (not os.path.isfile(cert)):<br> print "Certificate file not found. Trying to download it."<br> urllib.urlretrieve (rhevman + '/ca.crt', cert)<br> <br> ## Find out what is being asked for<br> vmname = ''<br> parser = OptionParser()<br> parser.add_option('-n', '--name', dest='vmname', default='', action='store', help='Specify the name of a single VM (case sensitive)')<br> (options, args) = parser.parse_args()<br> vmname = options.vmname<br> hostnames = {}<br> <br> try:<br> ## Connect to the API<br> api = API (url=rhevman, username=user, password=password, ca_file=cert)<br> <br> try:<br> ## Get the list of hypervisors for use later on<br> for host in api.hosts.list():<br> hostnames[host.id] = host.name<br> <br> ## Are we after a single VM or the entire list?<br> if (len(vmname) > 0):<br> print 'Retrieving information for %s' % vmname<br> vmsList = api.vms.list(query = 'name=%s' % vmname)<br> else:<br> print 'Retrieving information for all VMs'<br> vmsList = api.vms.list(max = -1)<br> print 'Found %d VMs' % len(vmsList)<br> <br> # Get the VM details<br> for instance in vmsList:<br> address=[]<br> print '\nVM: %s' % instance.name<br> host = instance.get_host()<br> print '\tState: %s\n\tDescription: %s\n\tComment: %s' % (instance.status.state, instance.description, instance.comment)<br> <br> if (host):<br> print '\tHost: %s' % hostnames[host.id]<br> <br> createtime = instance.get_creation_time()<br> print '\tCreated: %s' % createtime<br> starttime = instance.get_start_time()<br> print '\tStarted: %s' % starttime<br> cpu = instance.get_cpu()<br> print '\tCPU: %s (%s sockets, %s cores)' % (cpu.topology.sockets*cpu.topology.cores, cpu.topology.sockets, cpu.topology.cores)<br> ram = instance.get_memory()<br> print '\tRAM: %s' % factor(ram)<br> disks = instance.get_disks().list()<br> <br> for disk in disks:<br> print '\tDisk: %s %s' % (disk.name, factor(disk.size))<br> <br> if ((instance.status.state == 'up') and instance.get_guest_info()):<br> vmnics= instance.get_nics().list()<br> ips = instance.get_guest_info().get_ips().get_ip()<br> <br> for card in vmnics:<br> print '\tInterface: %s, MAC address: %s' % (card.name, card.mac.address)<br> <br> for ip in ips:<br> address.append(ip.get_address())<br> print '\tIP: %s' % ( ip.get_address())<br> <br> except Exception as e:<br> print ' Exception:\n%s' % str(e)<br> <br> api.disconnect()<br> <br> except Exception as ex:<br> print 'Unexpected error: %s' % ex<br> [/code]<br> <br> regards,<br> John<br> <br> <br> <div class="moz-cite-prefix">On 18/06/14 00:59, Citros Airv wrote:<br> </div> <blockquote cite="mid:CAGiV8-yQ_RJy_0umpowH8SBDRNs0rC4531GmYMmNtc=9rFJHuw@mail.gmail.com" type="cite"> <div dir="ltr">Hi, <div><br> </div> <div>I'm checking to see if someone has a script to determine where a VM is running preferably with the shell?</div> <div><br> </div> <div>Thanks,</div> <div><br> </div> <div>VS</div> </div> <br clear="all"> ______________________________________________________________________<br> This email has been scanned by the Symantec Email Security.cloud service.<br> For more information please visit <a class="moz-txt-link-freetext" href="http://www.symanteccloud.com">http://www.symanteccloud.com</a><br> ______________________________________________________________________<br> <br> <fieldset class="mimeAttachmentHeader"></fieldset> <br> <pre wrap="">_______________________________________________ Users mailing list <a class="moz-txt-link-abbreviated" href="mailto:Users@ovirt.org">Users@ovirt.org</a> <a class="moz-txt-link-freetext" href="http://lists.ovirt.org/mailman/listinfo/users">http://lists.ovirt.org/mailman/listinfo/users</a> </pre> </blockquote> <br> </body> </html> --------------050306040406020008040501--
participants (2)
-
Citros Airv
-
John Gardeniers