After the presentation about EMCLI at DOAG 2015, someone asked me how we can list database targets based on their operating systems. In my presentation, I told that there are more than 70 verbs which starts with “get_” and help you fetch information about EM objects, and if you can’t find what you’re looking for, you can use the “list” verb which is probably the most powerful verb of EMCLI. Now I try to show how we can use the list verb to fetch detailed information about EM targets.
Let’s say we want to list the databases running on Linux. The original question was how we can list the databases running on Windows but thanks God, I don’t have any databases running on Windows. So I changed the question. First let’s see what we can get by using “get_targets”:
emcli get_targets -target=”oracle_database”:
As we can see, there’s no information about the operating systems. There’s not even any information about which host these databases are running on. Now let’s try the “list verb” for any of the target databases such as DEVDB:
emcli list -resource="Targets" -search="TARGET_NAME='DEVDB'" -format="name:csv" TARGET_NAME,TARGET_TYPE,TARGET_GUID,TYPE_VERSION,TYPE_QUALIFIER1,TYPE_QUALIFIER2, TYPE_QUALIFIER3,TYPE_QUALIFIER4,TYPE_QUALIFIER5,EMD_URL,TIMEZONE_REGION,DISPLAY_NAME, HOST_NAME,LAST_METRIC_LOAD_TIME,TYPE_DISPLAY_NAME,BROKEN_REASON,BROKEN_STR,OWNER, LAST_LOAD_TIME_UTC,CREATION_DATE DEVDB,oracle_database,102890E057665EC4E053E03210AC4FA3,5.4,11gR202,2,DB,,FullLLFile, https://db03-test.bilyoner.com:3872/emd/main/,Europe/Istanbul, DEVDB,db03-test.bilyoner.com,2015-11-12 10:28:55.0,Database Instance,0,,SYSMAN, 2015-11-12 08:28:55.0,2015-02-28 17:49:51.0
Using “list” verb, we found the host name. Now we can use the same command to get more information for that host:
emcli list -resource="Targets" -search="TARGET_NAME='db03-test.bilyoner.com'" TARGET_NAME,TARGET_TYPE,TARGET_GUID,TYPE_VERSION,TYPE_QUALIFIER1,TYPE_QUALIFIER2, TYPE_QUALIFIER3,TYPE_QUALIFIER4,TYPE_QUALIFIER5,EMD_URL,TIMEZONE_REGION, DISPLAY_NAME,HOST_NAME,LAST_METRIC_LOAD_TIME,TYPE_DISPLAY_NAME,BROKEN_REASON, BROKEN_STR,OWNER,LAST_LOAD_TIME_UTC,CREATION_DATE db03-test.bilyoner.com,host,FCBE4FFC339F6CA7E043E03210ACE0D9,4.5,Linux, Red Hat Enterprise Linux Server release 5.11 (Tikanga),Generic,,, https://db03-test.bilyoner.com:3872/emd/main/,Europe/Istanbul,db03-test.bilyoner.com, db03-test.bilyoner.com,2015-11-12 10:15:41.0,Host,0,,SYSMAN,2015-11-12 08:15:41.0, 2014-06-26 16:40:17.0
The “TYPE_QUALIFIER1” column holds the name of OS if the target is a host. It holds the database version if the target is an oracle database. Unfortunately there’s not an easy way to combine this information when querying the “resources”. So we need to get all oracle databases and then get the host information for each database. Of course, it won’t be a practical approach but here’s a sample code to do it using EMCLI Python (Jython) scripting engine:
if (len(sys.argv) <> 1 ): print "Usage: emcli @list_targets.py OSname" exit() login( username="EMuser", password="EMpassword") myDBs = list( resource="Targets", search="TARGET_TYPE='oracle_database'" ) for DB in myDBs.out()['data']: # we get the host info for the oracle database target hostinfo = list( resource="Targets", search="TARGET_NAME='" + DB['HOST_NAME'] + "'" ) # the above one should return only one item, so we use the first item OS = hostinfo.out()['data'][0]['TYPE_QUALIFIER1'] # we print only the targets if their OS name equal to the first argument of script if ( OS.lower() == sys.argv[0].lower() ): print DB['TARGET_NAME'],hostinfo.out()['data'][0]['TYPE_QUALIFIER1']
Save the above code as list_targets.py, then run it:
We got the desired result but the method we used is not efficient. Let’s try to get the same info using a different method. The list verb can also be used to execute SQL on EM repository. It will login as MGMT_VIEW, and run the query that you give in “-sql” parameter. So instead of querying “predefined” resources, we can directly query MGMT$TARGET view:
emcli list -sql="select t.target_name DBANME, os.TYPE_QUALIFIER1 OSNAME from MGMT\$TARGET t, MGMT\$TARGET os where t.HOST_NAME = os.TARGET_NAME and t.target_type='oracle_database' and os.TYPE_QUALIFIER1 = 'Linux'"
And this is the result:
We get the desired result with a simple EMCLI command. It’s much faster and cleaner.