It’s a common use that we want to know the VM Ips and other NIC card details in an ovirt setup. Most of the time if you got to know the VM IPs it’s pretty easy to manage those VMS or pulling some data out of it.
Here is an example program that can be used to list the nic details, IP address ..etc of ovirt VMs.
Please note that you need a guest agent package installed in the VM to correctly fetch VMs IP addresses.
Below program version may be not properly intended, so download the program from github ..
[terminal]
#! /usr/bin/python
# Author: Humble Chirammal
#
# 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.
# This program can be used to list vm nics details and the ips in an ovirt Dc ,
# Please make sure you are changing below parameters in api :
#url =”Your ovirt engine api url”
#username =”user to access the api”
#password =”password of above user”
#ca_file =”location of ca cert file”
from ovirtsdk.api import API
from ovirtsdk.xml import params
import time
try:
api = API(url=”https://myrhevm.humblec.com/api”,
username=”admin@internal”,
password=”redhat”,
ca_file=”/root/ca.crt”)
try:
vmsList = api.vms.list(max=500)
for instance in vmsList:
print ‘\t \t\t\tVM :%s’ % (instance.name.upper())
address=[]
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 ‘Net.ID:%s \t Name:%s \t MacAddress:%s \t Interface:%s \t Plugged:%s \t Linked:%s ‘ % (card.network.id, card.name, card.mac.address, card.interface, card.plugged, card.linked)
for ip in ips:
address.append(ip.get_address())
print ‘\t IP : %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
[/terminal]
The Sample output would like this:
[terminal]
VM :MYSERVER
Net.ID:7c4a2a35-016e-4a8d-bc9f-4fc622484e43 Name:nic1 MacAddress:00:16:3e:74:7b:97 Interface:virtio Plugged:True Linked:True
IP : X.X.X.191
IP : 192.168.122.1
VM :OPENSTACK
Net.ID:7c4a2a35-016e-4a8d-bc9f-4fc622484e43 Name:nic1 MacAddress:00:16:3e:74:53:aa Interface:virtio Plugged:True Linked:True
Net.ID:7c4a2a35-016e-4a8d-bc9f-4fc622484e43 Name:nic2 MacAddress:00:16:3e:74:53:ae Interface:virtio Plugged:True Linked:True
IP : X.X.X.74
VM: WINDOWS
Net.ID:7c4a2a35-016e-4a8d-bc9f-4fc622484e43 Name:nic2 MacAddress:00:16:3e:74:87:15 Interface:virtio Plugged:True Linked:True
Net.ID:7c4a2a35-016e-4a8d-bc9f-4fc622484e43 Name:e1000 MacAddress:00:16:3e:74:87:2e Interface:e1000 Plugged:True Linked:True
Net.ID:7c4a2a35-016e-4a8d-bc9f-4fc622484e43 Name:nic1 MacAddress:00:16:3e:74:8d:10 Interface:rtl8139 Plugged:True Linked:True
IP : X.X.X.250
IP : X.X.X.102
IP : X.X.X.232
[/terminal]
How can we list Vms details like virtual cpu core and virtual cpu memory to each vm in above script
Hi Deepesh,
It should be easy,
vmsList = api.vms.list(max=500)
for instance in vmsList:
In “instance” you have the VM, just list the properties of this ‘instance’ wrt vcpu and v memory , you should be able to make it.
For ex:
instance.get_guest_info():
Please let me know if you need help.
How can set ip address of vm?