[ovirt-users] Script to determine which Hypervisor a VM is running on
John Gardeniers
jgardeniers at objectmastery.com
Tue Jun 17 21:57:03 UTC 2014
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 at 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 at ovirt.org
> http://lists.ovirt.org/mailman/listinfo/users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ovirt.org/pipermail/users/attachments/20140618/a1a57ec9/attachment-0001.html>
More information about the Users
mailing list