#!/bin/ksh
###################################################################################################
#
#   Disclaimer: This Tool is for IBM support team diagnostics.
#       It is provided as is and is not supported as an IBM product 
#       under any type of software maintenance agreement. 
#   Author: Wayne Willcox
#   Created: 01 Apr 2015
###################################################################################################
DIR="/tmp/ibmsupt/testcase"
LOG=${DIR}/disable_fcs_ent_adapters.$$
Version="1.0"

Usage () {
cat << EOF
    This command removes all unused ethernet and/or fibre channel devices as well as 
    disables the perfprovider command.
    $0 [fep]
        -f = fibre channel only
        -e = ethernet only
        -p = only disable perfprovider
        -i = specify the bogus ipaddress that you want the adapters to be assigned.
EOF

Disclaimer
exit 1

}
# This function formats and properly displays the standard discliamer.
Disclaimer () {

        echo |awk 'BEGIN {
                                LINES = "#########################################################################";
                                Seperator = "#";
                } {
                printf("%s\n",LINES);
                printf("%-71s %s\n",Seperator,Seperator);
                printf("%-4s Disclaimer: This Tool is for IBM support team diagnostics. %9s\n",Seperator,Seperator);
                printf("%-4s It is provided as is and is not supported as an IBM product %8s\n",Seperator,Seperator);
                printf("%-4s under any type of software maintenance agreement. %18s\n",Seperator,Seperator);
                printf("%s\n",LINES);

        }'

}

FCS="FALSE"
ENT="FALSE"
PPROVIDER="FALSE"
ALL="TRUE"
IP="1.1.1.1"

while getopts "feph?i:" opt;do
    case "$opt" in
        f) FCS="TRUE"; ALL="FALSE";;
        e) ENT="TRUE"; ALL="FALSE";;
        p) PPROVIDER="TRUE"; ALL="FALSE";;
        i) IP=$OPTARG;;
        h) Usage;;
        ?) Usage;;
    esac

done
shift $((OPTIND-1))

if [ ! -d "${DIR}" ];then
    echo "mkdir ${DIR}"
    mkdir -p "${DIR}"
fi

echo "truncate any existing log file"
> ${LOG}

echo "$FCS $ENT $PPROVIDER $ALL"
# build a list of fibre channel devices that are available.
# once we have this list we need to check if it is connected or not
if [ "${FCS}" = "TRUE" ] || [ "${ALL}" = "TRUE" ];then
   fcdevices=`lsdev -Cc adapter |awk '{
    # now only get the list of fibre channel or ethernet adapters.
        if ($1 ~ "fcs") {
        # only get the adapters that are currently available
        if (/Available/)
            print $1;
        }
    
   }'`

   echo "\tcheck the fcs adapters:" |tee -a ${LOG}
   echo "\t\t"$fcdevices |tee -a ${LOG}
   # build an array of all the fscsi -> fcs mappings 
   # so we only have to run the odmget once.
   set -A fcsmap `odmget CuDv |grep -p fcs |awk '{
        gsub("\"","");
        if (/name/)
                printf("%s:",$3);
        if (/parent/)
                print $3;
    }'`


   for dev in $fcdevices;do
      echo 
      echo  >> $LOG
      echo $dev | grep fcs  > /dev/null 2>&1
      if [ $? -eq 0 ];then
          fscsi_dev=`echo $dev |sed s/fcs/fscsi/`
          # need to map back the fscsi device to the parent fcs
          # this is kind of ugly and we have to make sure to exclude the vio 
          # devices.
          fscsi_dev=`for i in ${fcsmap[@]};do
                       echo $i | grep -w $dev |grep fscsi> /dev/null
                       if [ $? -eq 0 ];then
                          echo $i |awk 'FS=":"{print $1}'
                       fi
                   done`

          lsattr -El $fscsi_dev  |awk '{
            if(/attach/)  {
                if (/none/) {
                    print "calling rmdev -Rl " fscsi_dev ":"dev >> LOG;
                    print "calling rmdev -Rl " fscsi_dev ":"dev;
                    system("rmdev -Rl "dev);
                    print "calling chdev -l "fscsi_dev " -a autoconfig=defined" >> LOG;
                    print "calling chdev -l "fscsi_dev " -a autoconfig=defined" ;
                    system("chdev -l "fscsi_dev" -a autoconfig=defined");
                }
            }
          }' fscsi_dev=$fscsi_dev dev=$dev LOG=$LOG
        continue
      fi
    done
fi

if [ "${ENT}" = "TRUE" ] || [ "${ALL}" = "TRUE" ];then
    set -A entmap `odmget CuDep | awk '{
        gsub ("\"","");
        if (/name =/) {
                if(/en/) {
                        printf("%s:", $3);
                        getline;
                        gsub ("\"","");
                        printf("%s\n",$3);
                }
        }
    }'`

    endevices=`lsdev -Cc adapter |awk '{
    # now get the list of ethernet adapters.
        if ($1 ~ "ent")  {
            # only get the adapters that are currently available
            if ((/Available/) && (!/Virtual/) && (!/EtherChannel/) && (!/Shared/))
                print $1;
        }
   }'`
   # only look for sea devices if this is a vio server.
   if [ -x /usr/ios/cli/ioscli ];then
       seadevices="$seadevices "`/usr/ios/cli/ioscli lsmap -all -net |awk '{if (/Backing device/) print $3}'`
   else
       seadevices=""
   fi
   etherchannel=`lsdev -Cc adapter | awk '{if(/EtherChannel/) print $1}'`
   for f in $etherchannel;do
       et="$et "`lsattr -El $f |awk '{if ((/backup_adapter/) || (/adapter_names/)) print $2; }'`
   done
   seadevices="${seadevices} ${et}"
   echo |tee -a ${LOG}
   echo "\tcheck the ethernet adapters" |tee -a ${LOG}
   echo "\t\t"$endevices |tee -a ${LOG}

   for dev in $endevices;do
        echo 
        echo  >> $LOG
        echo $seadevices |grep -w $dev > /dev/null 2>&1
        if [ $? -eq 0 ];then
            # found a SEA device so skip it.
            echo "\tskip the SEA adapter $dev" |tee -a ${LOG}
            continue
        fi
        #edev=`echo $dev |sed s/ent/en/`
        edev=`for i in ${entmap[@]};do
                       echo $i | grep -w $dev |grep ent> /dev/null
                       if [ $? -eq 0 ];then
                          echo $i |awk 'FS=":"{print $1}'
                       fi
                   done`
        netstat -in |grep -w $edev  > /dev/null 2>&1
        if [ $? -ne 0 ];then
            #echo "now disable the ethernet device :$dev:$edev:" |tee -a $LOG
            echo "now set bogus address on the ethernet device :$dev:$edev:" |tee -a $LOG
            #echo "rmdev -Rl $dev" |tee -a $LOG
            echo "chdev -l $edev -a netaddr=$IP -a netmask=255.255.255.255 -a state=up" |tee -a $LOG
            chdev -l $edev -a netaddr=$IP -a netmask=255.255.255.254 -a state=up |tee -a $LOG
            #rmdev -Rl $dev
        fi
   done
fi

if [ "${PPROVIDER}" = "TRUE" ] ;then
    perfprovider=`lssrc -s perfprovider |awk '{if(/active/) print "active";}'`

    if [ "${perfprovider}" = "active" ];then
        echo "perfprovider is running running" |tee -a $LOG
        stopsrc -s perfprovider |tee -a $LOG
        lsitab perfprovider |tee -a $LOG
        if [ $? -eq 0 ];then
             echo "remove perfprovider from the inittab? [y/n]"
             read answer
             if [ "${answer}" = "y" ];then
                echo "removing perfprovider from the inittab" |tee -a $LOG
                /usr/sbin/rmitab perfprovider |tee -a $LOG
             fi
        fi
    fi
fi
