EMCLI Rel 3, Post 1
So as the book gets under way on the Enterprise Manager Command Line Interface, (EMCLI) I’m starting to move away from the introduction statements that I commonly was required to repeat to folks, (“it’s the return to the golden age of the DBA 1.0- command line, baby!” :)) and now are onto what has changed in release 3.
The first things we want to talk about is the cool new features with Jython scripting and the the formatted output using JSON, (JavaScript Object Notation). These may not seem like much to most, but for those of us that have been using the EMCLI for some time, they are major enhancements that take the interface to a whole new level. The scripting option offers the DBA the option to build production grade Jython modules for Enterprise Manager. This is cool because, JSON text format is syntactically identical to the code for creating JavaScript objects. Due to this similarity, instead of requiring a parser, a JavaScript program can use the built-in eval() function and execute JSON data to produce native JavaScript objects.
Now I know I’ve heard a couple folks complain, “here we go into the JavaScript maelstrom!” but do give JSON a chance. I haven’t found anyone yet who has not worked with JSON and found it to be easy to learn, use and implement to an environment. This has created an instant following with developers and the success of the scripting language is impressive.
The JSON format is simple and clean- often used in an array, (also lists or maps, too) JSON is a collection of names and values set up in pairs. To understand why JSON has become so popular, here’s a comparison between JSON and XML for the same examples of code:
Example in JSON
{ "glossary": { "title": "example glossary", "GlossDiv": { "title": "S", "GlossList": { "GlossEntry": { "ID": "SGML", "SortAs": "SGML", "GlossTerm": "Standard Generalized Markup Language", "Acronym": "SGML", "Abbrev": "ISO 8879:1986", "GlossDef": { "para": "A meta-markup language, used to create markup languages such as DocBook.", "GlossSeeAlso": ["GML", "XML"] }, "GlossSee": "markup" } } } } }
Same Example in XML
<!DOCTYPE glossary PUBLIC "-//OASIS//DTD DocBook V3.1//EN"> <glossary><title>example glossary</title> <GlossDiv><title>S</title> <GlossList> <GlossEntry ID="SGML" SortAs="SGML"> <GlossTerm>Standard Generalized Markup Language</GlossTerm> <Acronym>SGML</Acronym> <Abbrev>ISO 8879:1986</Abbrev> <GlossDef> <para>A meta-markup language, used to create markup languages such as DocBook.</para> <GlossSeeAlso OtherTerm="GML"> <GlossSeeAlso OtherTerm="XML"> </GlossDef> <GlossSee OtherTerm="markup"> </GlossEntry> </GlossList> </GlossDiv> </glossary> {"menu": { "id": "file", "value": "File", "popup": { "menuitem": [ {"value": "New", "onclick": "CreateNewDoc()"}, {"value": "Open", "onclick": "OpenDoc()"}, {"value": "Close", "onclick": "CloseDoc()"} ] } }}
You can see the full example of comparisons here, but the lesser amount of code and the simplicity makes JSON an obvious choice of the two for those that want clean, simple and effective code.
Installation
To install the EMCLI kit, you now have an option in the Setup drop down menu, (to the top right of your screen…) Before installing, you must ensure your JAVA_HOME is properly set and that it’s part of your PATH. If you don’t wish to use the console to install the EMCLI, you can download it directly from the OMS host and then copy the jar file to where you would like to install it.
Installing from the command line is as simple as the following command:
$<path to where jar installation file was copied to>/java -jar emcliadvancedkit.jar client -install_dir=<input directory if not where jar file resides>
Once you complete the installation, you will need to synchronize with the OMS repository to download the help content. In previous versions, this was done with the setup, but no longer. To perform the sync, the following command is run:
emcli sync -url=https://<EMHost>:<port>/em -username=sysman -trustall
After inputting the password, you should see a successful completion of the sync and you should be ready to go.
Once you’ve set everything up, it’s time to log in and start to script.
Scripting
./emcli login -username=<username> Enter password : ******
For our example today, we’ll start with something simple. We’ll create a simple report on failed jobs-
#chk_failed_jobs.py #Import all emcli verbs to current program from emcli import * #Set the OMS URL to connect to set_client_property('EMCLI_OMS_URL','http://emrephost_orcl.com:7801:/em') #Accept all the certificates set_client_property('EMCLI_TRUSTALL','true') #Login to the OMS login(username='sysman') l_jobs = get_jobs(status_id=4) for jobs in l_jobs.out()['data']: JN = jobs[NAME'] TY = jobs['TYPE'] JI = jobs['JOB_ID'] SD = jobs['SCHEDULED'] ST = jobs['STATUS'] ON = jobs['OWNER'] TT = jobs['TARGET TYPE'] TN = jobs['TARGET NAME'] print "Job Name= "+ JN + " Job Type =" + TY + " JobID =" + JI "Scheduled= "+ SD " Status= "+ ST " Owner= "+ O "Target Type= "+ TT "Target Name= "+ TN
The output will then show a failed job, which I’ve created a dummy job to fail for this demonstration:
./emcli @chk_brkn_jobs.py Enter password : ****** Job Name = TEST_JOB_2 Job Type = SQLScript JobID = E8F8F923012D7516E043200B14ACFD07 E8F8F92301307516E043200B14ACFD07 Scheduled= 2013-10-17 18:14:01 Status= Failed Owner= SYSMAN Target Type= oracle_database Target_Name= emrep12.orcl.com Logout successful
If you’d like to learn more, I would recommend the Oracle Screen Watches and read up on the EMCLI documentation for release 3.
Good Stuff!