IBM Support

How to configure auto complete for Netcool OMNIbus commands

Technical Blog Post


Abstract

How to configure auto complete for Netcool OMNIbus commands

Body

Do you use the command line and want to save some typing, are you fed up of having to remember the name of servers and options you don't use very much, or simply want the thrill of pressing the TAB key and seeing it fill in an OMNIbus server name?  In this article I will demonstrate how to customize the UNIX shell command line to  auto complete for the ObjectServer and associated command line utilities.  

Prerequisites

You will need use the bash shell and have an OMNIbus install with the OMNIHOME and NCHOME variables set. We will make use of the 'complete' and 'compgen' bash functions.' Complete' allows you to specify a shell script function to be run when auto-complete is requested for a specific command and 'compgen' generates lists of possible commands from a partial name. This example was coded on Linux.

Basic auto complete for the ObjectServer

Let's build up a worked example step-by-step adding features and support for more utilities as we go. The example is a bash script which demonstrates how you could create a personalized command auto-completion for the ObjectServer

First up we will get auto complete working for 'nco_objserv'. In order to do this we need to generate a list of all the available command line options. We could manually code each parameter. However we would need to do this for every utility we wanted to support. A more flexible approach is to scrape the options from the -help option. A OMNIbus tools provide int with the format:

  <HEADER>  -<option1>    description  -<option2>    description


We can use a combination of grep and awk to filter out and format the command options
 

$OMNIHOME/bin/nco_objserv -help 2>&1 | grep "-" | awk '{print $1" "}' | tr -d '\n'

We then put the results into an array and pass it into the 'compgen' command. Here's a script that
puts it all together and autogenerates commands for the ObjectServer

  _NcoObjservComplete()  {          local current ALLOPTS             ALLOPTS=$($OMNIHOME/bin/nco_objserv -help 2>&1 | grep "-" | awk '{print $1" "}' | tr -d '\n')                 current="${COMP_WORDS[COMP_CWORD]}"          COMPREPLY=($(compgen -W "${ALLOPTS}" -- ${current}))            return 0  }    complete -F _NcoObjservComplete nco_objserv


Some explanation of how the bash autocomple configuration works is useful to help you see what the script is doing. The 'complete -F' command specifies that the '_NcoObjservComplete' function is to be run when a user tries to autocomplete options for the 'nco_objserv' command. The bash shell sets the COMP_WORDS and COMP_CWORD variables when it calls _NcoObjservComplete. COMP_WORDS is an array of all options the user specified and COMP_CWORD is an index to the current command that the user is trying to complete. If we return an array of commands in an array variable called COMPREPLY, the shell will present that to the user as  options to auto complete.  

To try it out, put the above script (including the complete) into a file and run it using the 'source' command.This loads it into the current shell , e.g.

  source _NcoObjServComplete.bash

Here's some examples of what is does (if the shell is configured to cycle through options on TAB)

  $OMNIHOME/bin/nco_objserv -nh <TAB>  $OMNIHOME/bin/nco_objserv -nhttpd_accesslog <TAB>  $OMNIHOME/bin/nco_objserv -nhttpd_authdomain <TAB>

 

Adding auto complete for option values


Now we've got basic auto complete of options working, let's look at getting auto complete of values. Server name is an interesting one to try. Server names are typically specified by an option called '-name' or '-server' in OMNIbus command line tools. To auto complete we need to get an array of all servers and pass it to compgen. All the configured servers are defined in the file $NCHOME/etc/interfaces.linux2x86. Looking at the file we can see server names are left-justified with details tabbed across and comment lines starting with '#' . I filtered out the server names using the following quick and dirty regex.

  egrep "^[^[:space:]#]" $NCHOME/etc/interfaces.linux2x86


which outputs something similar to the following:
NCOMS
NCO_GATE
NCO_PA
NCO_PROXY

We only want to complete on the server name if the option is preceded by -name or -server. We saw that the current parameter the user is trying to complete is referenced by COMP_CWORD. So we simply base our auto complete action on the contents of COMP_WORDS[COMP_CWORD-1]

  current="${COMP_WORDS[COMP_CWORD]}"  previous="${COMP_WORDS[COMP_CWORD-1]}"          case "$previous" in          -name)                  #Obtain a list of all the current servers                  ALLOPTS=$(egrep "^[^[:space:]#]" $NCHOME/etc/interfaces.linux2x86)                  COMPREPLY=($(compgen -W "${ALLOPTS}" -- ${current}))                  ;;  


Compgen can also autocomplete files. OMNIbus options which require files are often suffixed 'file'.
This will autogenerate file names if the previous option ended in 'file'

    -*file)                  #Assume this wants a file, and allow autogeneration of file names                  COMPREPLY=($(compgen -f ${current}))  

 

Putting this all together (and some refactoring to allow the script to be used for any OMNIbus command) we come out with :

  _NcoComplete()  {          cmd_name=$1          local current ALLOPTS          current="${COMP_WORDS[COMP_CWORD]}"          previous="${COMP_WORDS[COMP_CWORD-1]}"          case "$previous" in          -name)                  #Obtain a list of all the current servers                  ALLOPTS=$(egrep "^[^[:space:]#]" $NCHOME/etc/interfaces.linux2x86)                  COMPREPLY=($(compgen -W "${ALLOPTS}" -- ${current}))                  ;;          -server)                  #Obtain a list of all the current servers                  ALLOPTS=$(egrep "^[^[:space:]#]" $NCHOME/etc/interfaces.linux2x86)                  COMPREPLY=($(compgen -W "${ALLOPTS}" -- ${current}))                  ;;          -*file)                  #Assume this wants a file, and allow autogeneration of file names                  COMPREPLY=($(compgen -f ${current}))                  ;;          *)                  #Assume we want an option, so scrape all the allowed option names from the help output                  ALLOPTS=$($cmd_name -help 2>&1 | grep "-" | awk '{print $1" "}' | tr -d '\n')                  COMPREPLY=($(compgen -W "${ALLOPTS}" -- ${current}))                  ;;          esac          return 0  }  _NcoObjservComplete()  {          _NcoComplete $OMNIHOME/bin/nco_objserv  }  _NcoSqlComplete()  {          _NcoComplete $OMNIHOME/bin/nco_sql  }  _NcoDbinitComplete()  {          _NcoComplete $OMNIHOME/bin/nco_dbinit  }    complete -F _NcoObjservComplete nco_objserv  complete -F _NcoSqlComplete nco_sql  complete -F _NcoDbinitComplete nco_dbinit      

The above example shows how you can provide option completion, server name completion and file name completion for nco_objserv, nco_sql and nco_dbinit.
You can try it out using 'source' again. Here's an example of what it does (with cycling through options on tab not set)
 

  bash-4.1$ $OMNIHOME/bin/nco_objserv -n<TAB>          -name                      -nhttpd_enablefs           -nhttpd_sslcert            -nrestososlcuipls  -nhttpd_accesslog          -nhttpd_enablehttp         -nhttpd_sslenable          -nrestososlcuipss  -nhttpd_authdomain         -nhttpd_exptimeout         -nhttpd_sslport            -nrestososlcuipwu  -nhttpd_basicauth          -nhttpd_hostname           -norestrictionupdatecheck  -nrestosscfgfile  -nhttpd_configfile         -nhttpd_numworkthrs        -nrestosenable              -nhttpd_docroot            -nhttpd_port               -nrestososlcrescfg          bash-4.1$ $OMNIHOME/bin/nco_objserv -name

 

(and with tabbing round set)

    $OMNIHOME/bin/nco_dbinit -ser<TAB>  $OMNIHOME/bin/nco_dbinit -server<SPACE><TAB>  $OMNIHOME/bin/nco_dbinit -server  NCO_GATE <TAB>  $OMNIHOME/bin/nco_dbinit -server  NCOMS  $OMNIHOME/bin/nco_dbinit -server  NCOMS -aut<TAB>  $OMNIHOME/bin/nco_dbinit -server  NCOMS -authenticate<TAB>  $OMNIHOME/bin/nco_dbinit -server  NCOMS -automationfile<TAB>  $OMNIHOME/bin/nco_dbinit -server  NCOMS -automationfile $OMNIHOME/etc/aut<TAB>  $OMNIHOME/bin/nco_dbinit -server  NCOMS -automationfile /opt/ibm/tivoli/omnibus/etc/automation.sql  etc.

 

What next?

Hopefully this example demonstrates how to start developing a script to auto complete OMNIbus command line options. It could be expanded it to include other OMNIbus tools by adding a new 'complete' statement and a wrapper function for each command name. After testing, the source command could be included in your .bashrc script so it's loaded automatically on shell startup.

[{"Business Unit":{"code":"BU053","label":"Cloud & Data Platform"},"Product":{"code":"","label":""},"Component":"","Platform":[{"code":"","label":""}],"Version":"","Edition":"","Line of Business":{"code":"","label":""}}]

UID

ibm11082181