Ovirt- List Datacenter, hypervisors/host,vms in an ovirt DC with its name,status,id..etc using python/ovirt-sdk [Part 2]

Its a common use that, fetching Data center, hypervisors and VMs status in a ovirt Dc periodically.. Instead of connecting to OVirt GUI ( web admin portal) and seeing its status, you can automate this task via a python script. I wrote a program to just fetch the name & its status in a DC using python sdk.

Please download the program from github as below indentation is broken..

#! /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 datacenters, hypervisors, vms in an ovirt Dc , #with its status..

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

import sys from ovirtsdk.api import API from ovirtsdk.xml import params from threading import Thread import time import logging

#Configure

APIURL=”https://myrhevm.humblec.com/api” APIUSER=”admin@internal” APIPASS=”somepassword” CAFILE=”/root/ca.crt” LOGFILENAME=”/tmp/list_setup.log”

s1= ‘.’ objs=[“datacenters”, “hosts”, “vms”]

logging.basicConfig(level=logging.DEBUG, format=’%(asctime)s %(levelname)s %(message)s’, filename=LOGFILENAME, filemode=’w’)

def getObjInfo (obje, objlist): greet = s1 * 15 print ‘\n %s %s %s \n’ % (greet,obje, greet) for obj in objlist: print ‘ %30s : %30s : %30s ‘ % ((obj.name).upper(), (obj.status.state).upper(),obj.get_id()) logging.info (‘%13s- : %30s : %30s : %30s’ % (obje, (obj.name).upper(), (obj.status.state).upper(), obj.get_id()))

if __name__ == “__main__”: try: api = API(url=APIURL, username=APIUSER, password=APIPASS, ca_file=CAFILE) try: print ‘ \n I am logging in %s \n’ % LOGFILENAME

for ob in objs: if ob==”datacenters”: getObjInfo(ob, api.datacenters.list()) if ob==”hosts”: getObjInfo(ob, api.hosts.list()) if ob==”vms”: getObjInfo(ob, api.vms.list())

except Exception as e: logging.debug(‘Error:\n%s’ % str(e))

api.disconnect()

except Exception as ex: logging.debug(‘Unexpected error: %s’ % ex)

The output will look like this:

[root@humbles-lap ~]# python objlist.py

I am logging in /tmp/list_setup.log

…………… datacenters ……………

DEFAULT-DC : UP : c88a1735-f4c9-440e-becc-e0d16bd9a5b7 LOCAL_DC : UP : cc85d605-8a24-4606-96b1-bd4a436487b8

…………… hosts ……………

HOST : NON_RESPONSIVE : e89ef9cc-bb46-43be-806f-721026933b76 HOST-1 : NON_RESPONSIVE : 55649f85-b783-45f4-a35d-5a1a3af6b216 RHEL_H : UP : 3876cffa-0762-4101-bdd7-d772cde70cb HOST115 : UP : 1621ea04-aa82-4742-84ab-3d15c3f78a0c

…………… vms …………… RHEL6 : UP : 53fa8e2b-3992-4cce-9c19-aed7f5499982 IPA : DOWN : 8224c931-0f6f-4a84-9d92-ed23f4b19162 RHEL6 : DOWN : fc0c8595-4d49-4a53-a72d-f83dc2c39226 TEST_VM_ON_LOCAL_SD : DOWN : cf309bb3-5ac8-45a0-819c-1ffae6521b1

The log file will have contents similar to:

[root@humbleslap ~]# cat /tmp/list_setup.log 2013-10-13 07:46:22,744 INFO datacenters- Name: DEFAULT-DC Status: UP 2013-10-13 07:46:22,745 INFO datacenters- Name: LOCAL_DC Status: UP 2013-10-13 07:46:23,381 INFO hosts- Name: HOST Status: NON_RESPONSIVE 2013-10-13 07:46:23,381 INFO hosts- Name: HOST-1 Status: NON_RESPONSIVE 2013-10-13 07:46:23,382 INFO hosts- Name: RHEL_H Status: UP 2013-10-13 07:46:23,382 INFO hosts- Name: HOST115 Status: UP 2013-10-13 07:46:24,017 INFO vms- Name: RHEL6 Status: DOWN 2013-10-13 07:46:24,017 INFO vms- Name: IPA Status: DOWN 2013-10-13 07:46:24,018 INFO vms- Name: RHEL6 Status: DOWN 2013-10-13 07:46:24,018 INFO vms- Name: TEST_VM_ON_LOCAL_SD Status: DOWN You have new mail in /var/spool/mail/root [root@humbleslap ~]#

I hope this will help someone. Please let me know your feedback.