Thursday, January 9, 2014

Using EM CLI for mass update of Lifecycle Status Property Value

I co-presented at Oracle Open World in September, Manage Beyond Limits: Enterprise Manager CLI and Other Extensibility Features. I focused on the enhancements to Enterprise Manager Command Line Interface, EM CLI. I enthused about the two new modes, Interactive and Script mode and how they compare to the standard mode of previous releases, from the SQL*Plus like environment of Interactive mode to the scalable, JSON formatted output of script mode. I highlighted the ease of use and the scalable power of EM CLI.
After my session a number of you asked me for a copy of the scripts that I demoed. This is one. 
Why do we take on the extra task involved in learning something new? …because we know it will lead to personal growth, ultimately solve a problem or two, and maybe even look good on our resume. Learning Jython scripting will tick all of those boxes. Plus, it’s fun!
This script tries to solve the problem of mass updates to the Lifecycle Status property value. This is a new property introduced in Oracle Enterprise Manager 12c, and can be used to indicate the importance of a target, e.g. “Mission Critical", or to determine where a target is in its life cycle, e.g. “Stage”, “Test” or “Production”. Consider a new deployment of several hundred Oracle Databases, half of which are Mission Critical and the other half are in “Test”, but are about to go “Production”.
What is the best way to transition from “Test” to “Production”?
EM CLI in script mode!
EM CLI in script mode takes advantage of the Jython scripting language to use Enterprise Manager in a programmatic way, allowing task automation. The EM CLI Jython script below automates the setting of the Lifecycle Status Property Value, and uses standard programming constructs to make itterating through several Targets simpler, more robust and less error prone.
At a high level, every EM CLI Jython script can effectively be broken down into two parts:
Step 1: The setting and defining the necessary variables such as, which OMS URL to connect to, how secure you want your communication channel and which Administrator to log into the OMS.
Step 2: The calling or manipulation of EM CLI 12c procedures. Procedures were called verbs in previous releases, verb options are now procedure arguments in script and interactive mode.You can explore the on-line verb reference for more information.
Let’s break the script down further in to the major functional blocks of code.
Line 19: Sets the variable EMCLI_OMS_URL, which determines which OMS URL we shall connect too.
Line 21: Sets the variable EMCLI_TRUSTALL, which determines the level of security associated with the communication channel between the EM CLI and the OMS. We are choosing the lowest level of security.
Both of these variables could also have been set as environment variables.
Line 26 – 40: Between the if – else loop, we are checking for arguments that are passed to the script. We are passing two arguments into this script. Following, is what it looks like when calling an EM CLI Jython script, with arguments, on the command line:
$>./emcli OWUSER Production
Where: - is the name of our Jython Script.
OWUSER - is the username used to log into the OMS, the script will prompt for a password, to authenticate this user. The mode of authentication is the same as is configured for the Console. Authentication modes supported are Repository, SSO or LDAP.
Production - is the Lifecycle Status property Value we shall set.
Line 27: We log into the OMS.
Line 29: We search through all targets where the version, “DBVersion” is greater than or equal to 12.1. This is passed to an internal procedure defined in Line 10.
Line 11: We construct the SQL command, based on the arguments passed in, then use the EM CLI list()procedure to convert the returned output to an easily parse-able JSON formatted syntax (line 15) . We then return the Response Object, obj (line 16). The information returned are all the targets of the appropriate version.
Line 37: We then take the information and parse it, filtering further on oracle_database Target types. Finally we parse and print TARGET_NAME, TARGET_TYPE, PROPERTY_NAME and PROPERTY_VALUE for all databases which fit our criteria.
Line 39: We call the set_target_property_value() procedure which accepts a colon separated list of property records, in the form, TARGET_NAME:TARGET_TYPE:PROPERTY_NAME:PROPERTY_VALUE.
Please copy the code, save it with the *.py extension and change the EMCLI_OMS_URL value to the valid OMS URL for your environment.
Play around with it, and take your Jython scripting knowledge from Test to Production.


Anonymous said...

copy the code ? These are images.

Porus Homi Havewala (પોરસ હોમી હવેવાલા) said...

Yes, images only. Cut and paste not possible for the code, due to the nature of the posting.


Opinions expressed in this blog are entirely the opinions of the writers of this blog, and do not reflect the position of Oracle corporation. No responsiblity will be taken for any resulting effects if any of the instructions or notes in the blog are followed. It is at the reader's own risk and liability.

Blog Archive