[ovirt] Find out hosts and clusters where vm is running. its status, ids, storage domain details in an ovirt dc with python/ovirt-sdk [part 3]

This program can be used to fetch information about :

VMs
Clusters
Hosts
Storage domains
Data center..
in an ovirt dc. You will have “component” name, id, state along with other information.

The program can be downloaded from github

How-ever here it is without proper indentation.

#! /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=”redhat” CAFILE=”/root/ca.crt.2″ LOGFILENAME=”/tmp/list_setup.log”

s1= ‘.’ greet = s1 * 30 objs=[“datacenters”, “hosts”, “vms”, “clusters”, “storagedomains”] list=’True’ logging.basicConfig(level=logging.DEBUG, format=’%(asctime)s %(levelname)s %(message)s’, filename=LOGFILENAME, filemode=’w’)

def getObjInfo (obje, objlist):

print ‘\n %s %s %s \n’ % (greet,obje, greet) for obj in objlist: if obje==”vms” or “hosts” or “datacenters”: 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 obje ==”vms” and obj.status.state != ‘down’: vmonhost = api.hosts.get(id=api.vms.get(obj.name).get_host().get_id()).get_name() print ‘\n \t\t\t \ The VM: %s is running on host %s \n’ % (obj.name , vmonhost) logging.info( ‘VM: %s is running on host %s ‘ % (obj.name , vmonhost)) if obje ==”hosts” and obj.status.state != ‘down’: hostoncluster = api.clusters.get(id=api.hosts.get(obj.name).get_cluster().get_id()).get_name() print ‘\n\t\t\t\t\t\\ %20s is part of Cluster: %s\n ‘ % (obj.name , hostoncluster) logging.info( ‘host: %s is on cluster %s ‘ % (obj.name , hostoncluster)) 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())

if ob==”clusters”: print ‘\n %s %s %s \n’ % (greet,ob, greet) for cluster in api.clusters.list(): print ‘%s \t %s’ % ( cluster.name, cluster.get_id()) logging.info( ‘%s \t %s’ % ( cluster.name, cluster.get_id())) if ob==”storagedomains”: print ‘\n %s %s %s \n’ % (greet,ob, greet) print ‘\t\tName \t\t ID \t\t\t MASTER \t\t\t\tSD.FORMAT \t\t SD.TYPE’ for sd in api.storagedomains.list(): print ‘%20s %20s :%20s %20s %20s’ % (sd.name, sd.get_id(), sd.get_master(), sd.get_storage_format(), sd.get_type() ) logging.info(‘%20s %20s :%20s %20s %20s’ % (sd.name, sd.get_id(), sd.get_master(), sd.get_storage_format(), sd.get_type() )) 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:

I am logging in /tmp/list_setup.log

………………………… datacenters …………………………

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

………………………… hosts …………………………

HOST : NON_RESPONSIVE : e89ef9cc-bb46-43be-806f-721026933b76

\ host is part of Cluster: Default-Cluster

HOST-1 : NON_RESPONSIVE : 55649f85-b783-45f4-a35d-5a1a3af6b216

\ Host-1 is part of Cluster: Default-Cluster

RHEL_H : UP : 3876cffa-0762-4101-bdd7-d772cde70cb7

\ RHEL_H is part of Cluster: locl_Cluster

HOST115 : UP : 1621ea04-aa82-4742-84ab-3d15c3f78a0c

\ host115 is part of Cluster: Default-Cluster

………………………… vms …………………………

RHEL6 : UP : 53fa8e2b-3992-4cce-9c19-aed7f5499982

\ The VM: rhel6 is running on host HOST115

IPA : DOWN : 8224c931-0f6f-4a84-9d92-ed23f4b19162 RHEL6 : DOWN : fc0c8595-4d49-4a53-a72d-f83dc2c39226 TEST_VM_ON_LOCAL_SD : DOWN : cf309bb3-5ac8-45a0-819c-1ffae6521b1c

………………………… clusters …………………………

Default 99408929-82cf-4dc7-a532-9d998063fa95 Default-Cluster bac82894-af57-4dff-a7c7-94bd84632681 locl_Cluster b085aec4-182d-4010-87ce-acd937e02039

………………………… storagedomains …………………………

Name ID MASTER SD.FORMAT SD.TYPE Data e2d26dcc-aa1c-4dbf-b690-fbbdd0033397 : True v3 data DATA-1 fb1ffe88-1b66-49f4-9493-f07b8534866c : False v3 data RHEL_H-Local ad35f95d-ba8e-4fcf-bb08-7cc401d31605 : True v3 data

let me know your feedback.