SADMIN Section of script

5 minute read

Posted 2021-05-07 - Updated 2021-05-
Supported on Linux, Aix, MacOS

Code section for Shell script

The code below MUST appear in the beginning of every shell scripts using the SADMIN tools. You must also call at the beginning of your script the ‘sadm_start()’ function and the ‘sadm_stop()’ function at the end of your script before it finish.

# ---------------------------------------------------------------------------------------
# SADMIN CODE SECTION 1.50
# Setup for Global Variables and load the SADMIN standard library.
# To use SADMIN tools, this section MUST be present near the top of your code.    
# ---------------------------------------------------------------------------------------

# MAKE SURE THE ENVIRONMENT 'SADMIN' VARIABLE IS DEFINED, IF NOT EXIT SCRIPT WITH ERROR.
if [ -z $SADMIN ] || [ ! -r "$SADMIN/lib/sadmlib_std.sh" ]    
    then printf "\nPlease set 'SADMIN' environment variable to the install directory.\n"
         EE="/etc/environment" ; grep "SADMIN=" $EE >/dev/null 
         if [ $? -eq 0 ]                                   # Found SADMIN in /etc/env.
            then export SADMIN=`grep "SADMIN=" $EE |sed 's/export //g'|awk -F= '{print $2}'`
                 printf "'SADMIN' environment variable temporarily set to ${SADMIN}.\n"
            else exit 1                                    # No SADMIN Env. Var. Exit
         fi
fi 

# USE VARIABLES BELOW, BUT DON'T CHANGE THEM (Used by SADMIN Standard Library).
export SADM_PN=${0##*/}                                    # Script name(with extension)
export SADM_INST=`echo "$SADM_PN" |cut -d'.' -f1`          # Script name(without extension)
export SADM_TPID="$$"                                      # Script Process ID.
export SADM_HOSTNAME=`hostname -s`                         # Host name without Domain Name
export SADM_OS_TYPE=`uname -s |tr '[:lower:]' '[:upper:]'` # Return LINUX,AIX,DARWIN,SUNOS 

# USE & CHANGE VARIABLES BELOW TO YOUR NEEDS (They influence execution of SADMIN Library).
export SADM_VER='1.0'                                      # Script Version
export SADM_EXIT_CODE=0                                    # Script Default Exit Code
export SADM_LOG_TYPE="B"                                   # Log [S]creen [L]og [B]oth
export SADM_LOG_APPEND="N"                                 # Y=AppendLog, N=CreateNewLog
export SADM_LOG_HEADER="Y"                                 # Y=IncludeHeader N=NoHeader
export SADM_LOG_FOOTER="Y"                                 # Y=IncludeFooter N=NoFooter
export SADM_MULTIPLE_EXEC="N"                              # Run Simultaneous copy ?
export SADM_PID_TIMEOUT=7200                               # Sec. before PID Lock expire
export SADM_LOCK_TIMEOUT=3600                              # Sec. before Del. Lock File
export SADM_USE_RCH="Y"                                    # Update RCH HistoryFile 
export SADM_DEBUG=0                                        # Debug Level(0-9) 0=NoDebug
export SADM_TMP_FILE1="${SADMIN}/tmp/${SADM_INST}_1.$$"    # Tmp File1 for you to use
export SADM_TMP_FILE2="${SADMIN}/tmp/${SADM_INST}_2.$$"    # Tmp File2 for you to use
export SADM_TMP_FILE3="${SADMIN}/tmp/${SADM_INST}_3.$$"    # Tmp File3 for you to use

# LOAD SADMIN SHELL LIBRARY AND SET SOME O/S VARIABLES.
. ${SADMIN}/lib/sadmlib_std.sh                             # LOAD SADMIN Shell Library
export SADM_OS_NAME=$(sadm_get_osname)                     # O/S Name in Uppercase
export SADM_OS_VERSION=$(sadm_get_osversion)               # O/S Full Ver.No. (ex: 9.0.1)
export SADM_OS_MAJORVER=$(sadm_get_osmajorversion)         # O/S Major Ver. No. (ex: 9)

# VALUES OF VARIABLES BELOW ARE LOADED FROM SADMIN CONFIG FILE ($SADMIN/cfg/sadmin.cfg)
# THEY CAN BE OVERRIDDEN HERE, ON A PER SCRIPT BASIS (IF NEEDED).
#export SADM_ALERT_TYPE=1                                   # 0=No 1=OnError 2=OnOK 3=Always
#export SADM_ALERT_GROUP="default"                          # Alert Group to advise
#export SADM_MAIL_ADDR="your_email@domain.com"              # Email to send log
#export SADM_MAX_LOGLINE=500                                # Nb Lines to trim(0=NoTrim)
#export SADM_MAX_RCLINE=35                                  # Nb Lines to trim(0=NoTrim)
#export SADM_SSH_CMD="${SADM_SSH} -qnp ${SADM_SSH_PORT} "   # SSH CMD to Access Server
# ---------------------------------------------------------------------------------------

Code section for Python script

The code below MUST appear in the beginning of every Python scripts using the SADMIN tools. You must also call at the beginning of your script the ‘setup_sadmin()’ function. This function include the call to the ‘st.start()’, so you don’t have to worry about calling it. But before the end of your script you MUST call the ‘st.close(exitcode)’.

# ---------------------------------------------------------------------------------------
# SADMIN CODE SECTION 1.49
# Setup for Global Variables and import SADMIN Python module
# To use SADMIN tools, this section MUST be present near the top of your code.    
# ---------------------------------------------------------------------------------------
def setup_sadmin():

    # Load SADMIN Standard Python Library Module ($SADMIN/lib/sadmlib_std.py).
    try :
        SADM = os.environ.get('SADMIN')                                 # Getting SADMIN Root Dir.
        sys.path.insert(0,os.path.join(SADM,'lib'))                     # Add SADMIN to sys.path
        import sadmlib_std as sadm                                      # Import SADMIN Python Libr.
    except ImportError as e:                                            # If Error importing SADMIN 
        print ("Error Importing SADMIN Module: %s " % e)                # Advise User of Error
        sys.exit(1)                                                     # Go Back to O/S with Error
    
    # Create [S]ADMIN [T]ools instance (Setup Dir.,Var.)
    st = sadm.sadmtools()                       

    # You can use variable below BUT DON'T CHANGE THEM - They are used by SADMIN Module.
    st.pn               = os.path.basename(sys.argv[0])                 # Script name with extension
    st.inst             = os.path.basename(sys.argv[0]).split('.')[0]   # Script name without Ext
    st.tpid             = str(os.getpid())                              # Get Current Process ID.
    st.exit_code        = 0                                             # Script Exit Code (Use it)
    st.hostname         = socket.gethostname().split('.')[0]            # Get current hostname

    # CHANGE THESE VARIABLES TO YOUR NEEDS - They influence execution of SADMIN standard library.    
    st.ver              = "2.1"                 # Current Script Version
    st.pdesc            = "Type short description v%s" % st.ver  # Short description of script
    st.log_type         = 'B'                   # Output goes to [S]creen to [L]ogFile or [B]oth
    st.log_append       = False                 # Append Existing Log(True) or Create New One(False)
    st.log_header       = True                  # Show/Generate Header in script log (.log)
    st.log_footer       = True                  # Show/Generate Footer in script log (.log)
    st.multiple_exec    = "N"                   # Allow running multiple copy at same time ?
    st.use_rch          = True                  # Generate entry in Result Code History (.rch) 
    st.debug            = 0                     # Increase Verbose from 0 to 9 
    st.usedb            = True                  # Open/Use Database(True) or Don't Need DB(False)
    st.dbsilent         = False                 # When DB Error, False=ShowErrMsg and True=NoErrMsg 
                                                # But Error Code always returned (0=ok else error)

    # Override Default define in $SADMIN/cfg/sadmin.cfg
    #st.cfg_alert_type   = 1                    # 0=NoMail 1=OnlyOnError 2=OnlyOnSuccess 3=Always
    #st.cfg_alert_group  = "default"            # Valid Alert Group are defined in alert_group.cfg
    #st.cfg_mail_addr    = ""                   # This Override Default Email Address in sadmin.cfg
    #st.cfg_cie_name     = ""                   # This Override Company Name specify in sadmin.cfg
    #st.cfg_max_logline  = 500                  # When Script End Trim log file to 500 Lines
    #st.cfg_max_rchline  = 35                   # When Script End Trim rch file to 35 Lines
    #st.ssh_cmd = "%s -qnp %s " % (st.ssh,st.cfg_ssh_port) # SSH Command to Access Server 

    # Start SADMIN Tools - Initialize 
    st.start()                                  # Init. SADMIN Env. (Create dir.,Log,RCH, Open DB..)
    return(st)                                  # Return Instance Object To Caller

ENVIRONMENT

  • The “$SADMIN” environment variable must be defined and contains the root directory of the SADMIN tools (normally /opt/sadmin). It should be already done, the setup script have updated the ‘/etc/profile.d/sadmin.sh’ and the ‘/etc/environment’ files.
  • The SADMIN configuration file, is needed and loaded in memory at the beginning of every scripts. This file should already exist and contains your SADMIN configuration and preference setting.
  • For Shell script the Shell Library is used and for Python script the Python Library is used.

AUTHOR

Jacques Duplessis
Any suggestions or bug report can be submitted at the support page

Copyright © 2021 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later
This is free software, you are free to change and redistribute it.
There is NO WARRANTY to the extent permitted by law.

SEE ALSO

sadm_requirements.sh - List/Install required SADMIN Tools packages
sadm_template.sh - Using SADMIN Shell script template
sadm_template.py - Using SADMIN Python script template
sadmlib_std_demo.sh - SADMIN Shell Library Functions Demo
sadmlib_std_demo.py - SADMIN Python Library Functions Demo