EM CLI with Scripting Option in EM12cR3
Jul 9, 2013 / By Andrey Goryunov
The new release of #EM12c came with advanced EM CLI, providing interactive and script modes to enhance the standard command line functionality. EM CLI includes Jython interpreter and all verbs presented as functions, which allows the use of verb arguments as parameters.
Managing several thousand targets in one of Pythian’s client environments and having different scripts and user-defined metrics to trace specific conditions, I was excited when I saw that functionality. It would allow me to not only join all checks together, but also add more functionality and make it flexible for changes. It is definitely an additional solid argument to migrate from EM 11g to 12c.
I installed the new release of OEM and, using examples adjusted to my environment, installed advanced kit:
[oracle@em12 ~]$ echo $JAVA_HOME /u02/app/oracle/product/em12c3/jdk16/jdk [oracle@em12 ~]$ java -version java version "1.6.0_43" Java(TM) SE Runtime Environment (build 1.6.0_43-b01) Java HotSpot(TM) 64-Bit Server VM (build 20.14-b01, mixed mode) [oracle@em12 emcli]$ pwd /u02/app/oracle/product/em12c3/oms/emcli [oracle@em12 emcli]$ java -jar /u02/app/oracle/product/em12c3/oms/sysman/jlib/emcliadvancedkit.jar client -install_dir=/u02/app/oracle/product/em12c3/oms/emcli Oracle Enterprise Manager 12c Release 3. Copyright (c) 2012, 2013 Oracle Corporation. All rights reserved. EM CLI Advanced install completed successfully.
The execution of first commands using Jython language in EM CLI interactive mode went well:
emcli>print 'Hello EMCLI' Hello EMCLI emcli>version() Oracle Enterprise Manager 12c EMCLI Version 22.214.171.124.0
Surprisingly enough, I was unable to get output when I executed the file emcli_json_processing.py, created by pasting contents. Finally, when I looked through several books about Jython (thank you Pythian for providing subscription to Safari Books Online), I could better understand the language. I also learned that formatting is very important in Python, and prepared the file with proper formatting options. Going further through Python, I was able to do simple manipulations with the file, send e-mails and, using EM CLI functions, retrieve information from the OEM repository database.
I put all these pieces together and created a script that checks which targets belong to which group. The script was based on a text file that defines relations between targets and groups. If a target is not in a group, it sends an e-mail. The functionality is simple, but it is a starting point for further complex, and consequently, necessary checks:
[oracle@em12 emcli]$ cat vals.txt d1:PROD d12:TEST [oracle@em12 emcli]$ cat f.py import smtplib from email.mime.text import MIMEText me = 'firstname.lastname@example.org' you = 'email@example.com' from emcli import * def format(str): if str is None: return "" return str def get_targets_in_group(target_name, group_name): l_sql = "select count(*) cnt from MGMT$TARGET_MEMBERS where AGGREGATE_TARGET_NAME = '" + group_name + "' and MEMBER_TARGET_NAME = '" + target_name + "'" obj = list(sql=l_sql) return obj def check_targets(file_name): for l in open(file_name, 'r').readlines(): s = l.split('n') s = s.split(':') r = get_targets_in_group(s, s) for o in r.out()['data']: cnt = o['CNT'] if cnt == '0': msg = MIMEText('Please check target ' + s + ' in group ' + s) msg['Subject'] = 'Target ' + s + ' not in a group ' + s msg['From'] = me msg['To'] = you s = smtplib.SMTP('localhost') s.sendmail(me, [you], msg.as_string()) set_client_property('EMCLI_OMS_URL','https://em12.home:7799/em') set_client_property('EMCLI_TRUSTALL','true') login(username='sysman',password='sysman_pwd') check_targets('vals.txt')
Have a good day, and happy EM CLI and Jython scripting!
Leave a Reply