<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>