Friday, May 05, 2017

TSM Menu - Bash Shell Scripting

For years I have maintained a shell script on our jump server that was a menu for accessing all the TSM servers in our environment. It was a nice interface and great for noobies. It allowed for you to set your password and ID variables before the menu loaded so anyone could run the menu under their ID, then presented them with a nice menu to pick which TSM server they wished to access. It was nice and easy. The only problem was when servers were added or decommissioned. When that occurred I would then have to go out and revise the menu. This was a pain and soon to be more painful as we plan to add a lot more TSM servers in the near future. I remember seeing someone post that they had created a menu system based off of the dsm.sys file so I decided to write my own.

Before:

 ################################################################## 
 #                                                                # 
 #                        Infocrossing                            # 
 #                    UNIX ADMINISTRATION                         # 
 #                         TSM  MENU                              # 
 #                                                                # 
 ################################################################## 
                            Omaha                                   
     0)   ouax000                         1)  ouax001               
     2)   ouax002                         3)  ouax003               
     4)   OTSM01                          5)  OTSM02                
 ------------------------------------------------------------------ 
                           Phoenix                                  
     6)   puax001                         7)  puax002               
     8)   puax008                         9)  PTSM01                
     10)  PTSM02                         11)  PTSM20                
 ------------------------------------------------------------------ 
                           Singapore                                
     20)  SIN01ZZ                        99)  STSM01                
 ------------------------------------------------------------------ 
                            Germany                                 
     21)  DTSM01                         22)  DTSM02                
     23)  DTSM03                         24)  DTSM20                
 ------------------------------------------------------------------ 
                            Kings Mtn                               
     25)  CTSM01                         26)  CTSM20                
     27)  CTSM30                         28)  CTSM40                
 ------------------------------------------------------------------ 
                                                                    
                     X/x)  Cross Server                             
                                                                    
                     S/s)  Cmd Prompt                               
                                                                    
                   DD/dd)  Data Domain Admin                        
                                                                    
                     Q/q)  Return to Main Menu                      
                                                                    
                                                                    
     Selection:                                                    

 After (Notice servers listed here that were not in above menu):

 ################################################################
 #                                                              #
 #                         Infocrossing                         #
 #                       Recovery Services                      #
 #                           TSM  MENU                          #
 #                                                              #
 ################################################################
                                                                 
 1) C1IF            12) LABTSM            23) PTSM01             
 2) C1KDC2          13) LUAX001           24) PTSM02             
 3) CTSM01          14) LUAX002           25) PTSM20             
 4) CTSM20          15) LTSM01            26) PTSM25-Arch        
 5) CTSM30          16) NTSM01            27) SIN01ZZ            
 6) CTSM40          17) OUAX003           28) STSM01             
 7) CTSM46-Arch     18) OUAX000           29) Cross Server       
 8) DTSM01          19) OUAX001           30) Refresh Menu       
 9) DTSM02          20) OTSM01            31) Exit               
10) DTSM03          21) OTSM02                                   
11) DTSM20          22) PESTSM01                                   
                                                                 
 Please enter your choice:     

Ok, so the Before is more organized by region but as new servers come in I wanted the menu to update dynamically. So this new menu is built around the BASH shell's select menu function. The script pulls all the TSM server names from the dsm.sys and populates an array that the select statement reads and generates this menu. Make sure the variables the script declares work for you and your dsm.sys. My dsm.sys looks like this if I grep servername:

SERVERNAME         DTSM01          * Germany      LIB-MAN
SERVERNAME         DTSM02          * Germany             
SERVERNAME         DTSM03          * Germany             
SERVERNAME         DTSM20          * Germany      LIB-MAN
SERVERNAME         CTSM01          * Kings_Mnt    LIB-MAN
SERVERNAME         C1KDC2          * Kings_Mnt           
SERVERNAME         C1IF            * Kings_Mnt           
SERVERNAME         CTSM20          * Kings_Mnt           
SERVERNAME         CTSM30          * Kings_Mnt           
SERVERNAME         CTSM40          * Kings_Mnt           
SERVERNAME         CTSM46-Arch     * Kings_Mnt           
SERVERNAME         LUAX001         * Leonia       LIB-MAN
SERVERNAME         LUAX002         * Leonia              
SERVERNAME         LTSM01          * Leonia              
SERVERNAME         NTSM01          * Norcross     LIB-MAN
SERVERNAME         OUAX000         * Omaha        LIB-MAN
SERVERNAME         OUAX001         * Omaha               
SERVERNAME         OUAX003         * Omaha               
SErvername         OTSM01          * Omaha        LIB-MAN
SErvername         OTSM02          * Omaha               
SERVERNAME         PESTSM01        * PEST         LIB-MAN
SERVERNAME         PTSM01          * Tempe        LIB-MAN
SERVERNAME         PTSM02          * Tempe               
SERVERNAME         PTSM20          * Tempe               
SERVERNAME         PTSM25-Arch     * Tempe               
SERVERNAME         STSM01          * Singapore           
SERVERNAME         SIN01ZZ         * Singapore           

SERVERNAME         LABTSM                                

With the dsm.sys file formated this way I can use grep and awk to get specific data. This allows the script to build my menus according to my needs. Especially with the Cross Server sub menu that allows me to run a command across multiple servers at once. In the sub-menu it groups by distinct location which I placed in the servername line. Leave me a comment if you have any comments, questions, or suggestions.

See the script below:

#!/bin/bash
#set -x

### Set Color Variables ###

NORMAL=`echo "\033[0m"`
WHITE=`echo "\033[37m"` #Blue
YELLOW=`echo "\033[33m"` #yellow
GREEN=`echo "\033[32m"`
RED=`echo "\033[31m"`
PURPLE=`echo "\033[35m"`

##### Declare Variables & Arrays ############

prompt="
 Please enter your choice: "

TSM_SYS="/opt/tivoli/tsm/client/ba/bin/dsm.sys"
all_servers=(`cat $TSM_SYS | grep -i servername | awk -v OFS='\t' '{print $2}' | sort`)
xservers=(`cat $TSM_SYS | grep -i servername | awk -v OFS='\t' '{print $4}' | sort -u`)
Germany=(`cat $TSM_SYS | grep -i servername | grep -i germany |awk -v OFS='\t' '{print $2}'`)
Omaha=(`cat $TSM_SYS | grep -i servername | grep -i omaha |awk -v OFS='\t' '{print $2}'`)
Norcross=(`cat $TSM_SYS | grep -i servername | grep -i norcross |awk -v OFS='\t' '{print $2}'`)
Tempe=(`cat $TSM_SYS | grep -i servername | grep -i tempe |awk -v OFS='\t' '{print $2}'`)
PEST=(`cat $TSM_SYS | grep -i servername | grep -i pest |awk -v OFS='\t' '{print $2}'`)
Leonia=(`cat $TSM_SYS | grep -i servername | grep -i leonia |awk -v OFS='\t' '{print $2}'`)
Kings_Mnt=(`cat $TSM_SYS | grep -i servername | grep -i kings_mnt |awk -v OFS='\t' '{print $2}'| sort`)
Singapore=(`cat $TSM_SYS | grep -i servername | grep -i singapore |awk -v OFS='\t' '{print $2}'`)
lib_man=(`cat $TSM_SYS | grep -i servername | grep -i lib-man | awk -v OFS='\t' '{print $2}'`)

export COLUMNS=70
########################

######################
function clearlogin
######################
{
echo "Please wait while I clear all login information."|while read x;do for((i=0;i<${#x};i++));do echo -n "${x:$i:1}";sleep .03;done;echo;done;
unset TSMID
unset TSMPA
unset DDID
echo "."|while read x;do for((i=0;i<${#x};i++));do echo -n "${x:$i:1}";sleep .16;done;echo;done;
echo "."|while read x;do for((i=0;i<${#x};i++));do echo -n "${x:$i:1}";sleep .16;done;echo;done;
echo "Logins have been cleared! "|while read x;do for((i=0;i<${#x};i++));do echo -n "${x:$i:1}";sleep .03;done;echo;done;
sleep 1
}
######################
#  end clearlogin    #
######################

###############
#END MAIN-MENU#
###############

#########################
function tsmadmin
#########################
{
clear
if [ -z "${TSMID}" ]; then
echo ""
echo -e "$YELLOW Enter your TSM Admin ID: $GREEN \c"
read TSMID
echo ""
echo -e "$YELLOW Enter your TSM Admin Password: \c "
#stty -echo
read -s TSMPA
#stty $stty_orig
fi
clear
  tsm-menu
exit
}
####################
#  end tsmadmin    #
####################

#####################
function tsm-menu
#####################
{
all_done=0
export COLUMNS=70
while ((!all_done )); do
clear
echo -e "$WHITE ################################################################$NORMAL "
echo -e "$WHITE #                                                              #$NORMAL "
echo -e "$WHITE #                       $YELLOW  Infocrossing                        $WHITE #"
echo -e "$WHITE #                       Recovery Services                      #"
echo -e "$WHITE #                          $RED TSM  MENU                         $WHITE #"
echo -e "$WHITE #                                                              #$NORMAL "
echo -e "$WHITE ################################################################$NORMAL "
echo -e " "
length=`echo "$((${#all_servers[@]} + 4))"`
PS3="$prompt "
 select host in "${all_servers[@]}" "Cross Server" "Reset Login" "Refresh Menu" "Exit"; do
   if [ "$host" = "Exit" ]; then
      all_done=1; clear; exit
   elif [ "$host" = "Refresh Menu" ]; then
      all_done=1; tsm-menu
   elif [ "$host" = "Reset Login" ]; then
      all_done=1; clearlogin; tsmadmin
   elif [ "$host" = "Cross Server" ]; then
      all_done=1; xmenu
   elif [ "$REPLY" -gt "$length" ]; then
      echo ""
      echo "You chose option $REPLY which is invalid. Try again! "
      echo ""
   else
       clear
       echo -e "$GREEN "
       dsmadmc -id=$TSMID -pa=$TSMPA -servern="$host"
       all_done=1; tsm-menu
   fi
 done
done
}
##########################
# end function tsm-menu  #
##########################

#####################
function xmenu
#####################
{
all_done=0
export COLUMNS=50
while ((!all_done )); do
clear
echo -e "$WHITE ################################################################$NORMAL "
echo -e "$WHITE #                                                              #$NORMAL "
echo -e "$WHITE #                       $YELLOW  Infocrossing                        $WHITE #"
echo -e "$WHITE #                       Recovery Services                      #"
echo -e "$WHITE #            $RED     TSM Cross Server Command MENU               $WHITE # "
echo -e "$WHITE #                                                              #$NORMAL "
echo -e "$WHITE ################################################################$NORMAL "
echo -e " "
length=`echo "$((${#xservers[@]} + 4))"`
PS3="$prompt "
 select host in "${xservers[@]}" "All Servers" "Library Managers" "Refresh Menu" "Exit"; do
   if [ "$host" = "Exit" ]; then
      all_done=1; tsm-menu
   elif [ "$host" = "Refresh Menu" ]; then
      all_done=1; xmenu
   elif [ "$host" = "Library Managers" ]; then
       clear
       echo -e "$GREEN "
       xserver $TSMID $TSMPA lib_man
       all_done=1; xmenu
   elif [ "$host" = "All Servers" ]; then
       clear
       echo -e "$GREEN "
       xserver $TSMID $TSMPA all_servers
       all_done=1; xmenu
   elif [ "$REPLY" -gt "$length" ]; then
       echo ""
       echo "You chose option $REPLY which is invalid. Try again! "
       echo ""
   else
       clear
       echo -e "$GREEN "
       xserver $TSMID $TSMPA "$host"
       all_done=1; xmenu
   fi
 done
done
}
##########################
# end function xmenu     #
##########################


###################
function xserver
###################
{
  DC=$3
  echo -e " "
  echo -e "     $YELLOW Enter the command to process: $NORMAL \c"
  read CMD
  echo ""
    echo -e "     $YELLOW Do you want to save the data to a commadelimited csv file? (Yes or No [N])  $NORMAL \c"
    read ANSWER
    case $ANSWER
     in
      y|Y|YES|Yes|yes)
       echo ""
       echo -e "     $YELLOW Enter the file name to save the data: $NORMAL \c"
       read FILE
       echo ""
       touch $FILE
       cat /dev/null > $FILE
        eval varAlias=\${$DC[@]}
        for Server in ${varAlias[@]}
        do
         echo ""
          echo -e "Processing TSM server $Server "
          echo ""
          dsmadmc -id=$1 -pa=$2 -dataonly=yes -commadelimited -servern=$Server $CMD | grep -v ANR | grep -v ANS >> $FILE
         echo ""
        done
       xmenu
      ;;
      n|N|*)
       echo ""
        eval varAlias=\${$DC[@]}
        for Server in ${varAlias[@]}
        do
         echo ""
          echo -e "Processing TSM server $Server "
          echo ""
          dsmadmc -id=$1 -pa=$2 -dataonly=yes -servern=$Server $CMD
         echo ""
        done
        sleep 2
       xmenu
      ;;
      *)
       echo ""
        eval varAlias=\${$DC[@]}
        for Server in ${varAlias[@]}
        do
         echo ""
          echo -e "Processing TSM server $Server "
          echo ""
          dsmadmc -id=$1 -pa=$2 -dataonly=yes -servern=$Server $CMD
         echo ""
        done
        sleep 2
       xmenu
      ;;
    esac
}
########################
# end xserver function #
########################
clear
echo -e "$GREEN"
echo "Please Wait Loading Menu....."|while read x;do for((i=0;i<${#x};i++));do echo -n "${x:$i:1}";sleep .03;done;echo;done;
sleep 2

tsmadmin