EM CLI with Scripting Option in EM12cR3

Posted in: Technical Track

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:

[[email protected] ~]$ echo $JAVA_HOME
 [[email protected] ~]$ 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)
 [[email protected] emcli]$ pwd
 [[email protected] 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'
Oracle Enterprise Manager 12c EMCLI Version

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:

[[email protected] emcli]$ cat vals.txt
[[email protected] emcli]$ cat f.py
import smtplib
from email.mime.text import MIMEText
me = '[email protected]'
you = '[email protected]'
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[0].split(':')
        r = get_targets_in_group(s[0], s[1])
        for o in r.out()['data']:
            cnt = o['CNT']
            if cnt == '0':
                msg = MIMEText('Please check target ' + s[0] + ' in group ' + s[1])
                msg['Subject'] = 'Target ' + s[0] + ' not in a group ' + s[1]
                msg['From'] = me
                msg['To'] = you
                s = smtplib.SMTP('localhost')
                s.sendmail(me, [you], msg.as_string())

Have a good day, and happy EM CLI and Jython scripting!

Want to talk with an expert? Schedule a call with our team to get the conversation started.

No comments

Leave a Reply

Your email address will not be published. Required fields are marked *