#!/bin/sh
#
#	Collect LINUX system info
#
#	Optional parameter:  name
#		If present, <name> will be used to generate the output 
#               directory/file:/tmp/netapp/ntap_linux_name
#		compressed tar file will be called /tmp/ntap_linux_name.tar.gz
#
#	If <name> is not specified, output will be in
#       /tmp/ntap_linux_info.tar.gz and so on.
#
#	$Id: //depot/prod/san_host/main/support/linux_info#16 $
#

USAGE="linux_info fcp|iscsi|all [-d <directory>] [-f] [ -n <name>]"

HELP1="\nCollects LINUX configuration information.\n"
HELP2="This creates a tar file ready to be sent to Network\n"
HELP3="Appliance Customer Support.\n\n"
HELP4="\tfcp\tcollects information for FCP enabled hosts\n"
HELP5="\tiscsi\tcollects information for iSCSI enabled hosts\n"
HELP6="\tall\tcollects information for both FCP and iSCSI enabled hosts\n"
HELP7="\t-d <directory>\tgives the directory to place the files in\n"
HELP8="\t-f force overwriting of output files if they already exist\n"
HELP9="\t\t\t(default is /tmp/netapp)\n\n"
HELPA="\t-n <name>\tgives a name string to be used in the output\n"
HELPB="\t\t\tfile and directory names\n"
HELP="$HELP1$HELP2$HELP3$HELP4$HELP5$HELP6$HELP7$HELP8$HELP9$HELPA$HELPB"


CD=cd
CP=/bin/cp
CAT=/bin/cat
COMPRESS=/usr/bin/gzip
DATE=/bin/date
ECHO="/bin/echo -e"
FDISK=/sbin/fdisk
GREP=/bin/grep
LSMOD=/sbin/lsmod
LSPCI=/sbin/lspci
MKDIR=/bin/mkdir
MODPROBE=/sbin/modprobe
MOUNT=/bin/mount
RM=/bin/rm
RPM=/bin/rpm
STRINGS=/usr/bin/strings
SYSCTL=/sbin/sysctl
TAR=/bin/tar
UNAME=/bin/uname
WHOAMI=/usr/bin/whoami
PSCMD=/bin/ps
LSOF_REDHAT=/usr/sbin/lsof
LSOF_SUSE=/usr/bin/lsof
UPTIME=/usr/bin/uptime

FSTAB=/etc/fstab
MCONF=/etc/modules.conf

NTAP_UTIL_DIR=/opt/sanlun/bin
NTAP_UTIL_PROG=sanlun
#NTAP_UTIL_RPM=whatever_the_rpm_is_called.rpm
SANLUN=$NTAP_UTIL_DIR/$NTAP_UTIL_PROG

QLREMOTE=/usr/local/bin/qlremote

# save a copy of the command line
CMDLINE="$0 $@"

# set default arguements/options
directory=/tmp/netapp
name="info"
FFLAG=0
FCPFLAG=0
ISCSIFLAG=0

# process the first arguement
# at least one parameter from {fcp|isci|all} is required
case "$1" in
	fcp)	FCPFLAG=1
		shift;;
	iscsi)	ISCSIFLAG=1
		shift;;
	all)	ISCSIFLAG=1
		FCPFLAG=1
		shift;;
	*)	$ECHO $USAGE
                exit 1;;
esac

# process remaining options
while getopts d:n:f opt
do
        case $opt in
        d)      directory=$OPTARG;;
	f)	FFLAG=1;;
        n)      name=$OPTARG;;
        \?)     $ECHO $USAGE
		$ECHO $HELP
                exit 1;;
        esac
done

outdir=$directory/ntap_linux_$name
outfile=$directory/ntap_linux_$name.tar
final_file=$outfile.gz

ME=`$WHOAMI`
if [ "$ME" != "root" ]; then
	$ECHO "You must be root to run this script \(current user: $ME\)"
	exit 1
fi

# make sure the directory exists; re-use it if it's already there 
if [ ! -d $directory ] ; then
	$MKDIR -p $directory
fi

if [ $FFLAG -eq 0 ]; then
   if [ -d $outdir -o -f $outdir ]; then
      $ECHO "$outdir already exists; please delete it or specify a different name from \"$name\""
      exit 1
   fi

   if [ -f $outfile ]; then
      $ECHO "$outfile already exists; please delete it or specify a different name from \"$name\""
      exit 1
   fi

   if [ -f $final_file ]; then
      $ECHO "$final_file already exists; please delete it or specify a different name from \"$name\""
      exit 1
   fi
else
   $RM -rf $outdir
   $RM -f $final_file
fi

printf "Gathering RPM information..."
$MKDIR -m 777 $outdir

# Collect NetApp package information
#$RPM -qa | grep $NTAP_UTIL_RPM > $outdir\$NTAP_UTIL_RPM.rpminfo
$RPM -qa > $outdir/packages
printf "...DONE\n"

# 
#	Get the version info for the kit
#
$NTAP_UTIL_DIR/san_version -v > $outdir/san_version 2>&1

#
#	Double check the version in the sanlun binary
#
$SANLUN version > $outdir/sanlun_version 2>&1

printf "Gathering general Linux information..."
#
# 	Get config files from /etc
#
$CP $FSTAB  $outdir/fstab	 2>&1
$CP $MCONF  $outdir/modules.conf 2>&1

#	
#	Get the partition and filesystem information
#
$FDISK -l		> $outdir/fdisk_l	  2>&1
$MOUNT -l		> $outdir/mount_l	  2>&1
$CAT /proc/partitions	> $outdir/proc.partitions 2>&1

#
#	Get the current scsi mid layer view of the world
#
$CAT /proc/scsi/scsi > $outdir/proc.scsi.scsi 2>&1

#
# Get raw and SCSI disk device info
#
raw -qa > $outdir/raw_qa 2>&1
ls -l /dev/sd* > $outdir/dev_sd 2>&1

#
#	Get the latest kernel messages
#	Note:  if the messages file has zero length, try for the next
#		most recent messages file, /var/log/messages.0
#
if [ -s /var/log/messages ]; then
	$CP /var/log/messages $outdir/messages 2>&1
fi
if [ -s /var/log/messages.0 ]; then
	$CP /var/log/messages.0 $outdir/messages.0 2>&1
fi

#
#	Collect OS version and uptime
#
$UNAME -a > $outdir/uname_a 2>&1
$CP /etc/issue.net $outdir/issue.net 2>&1
$UPTIME > $outdir/uptime 2>&1

#
#	Get the full hardware configuration of the system
#
$ECHO "lspci basic info\n===================="		>  $outdir/lspci 2>&1
$LSPCI							>> $outdir/lspci 2>&1
$ECHO "\n\nlspci verbose info\n===================="	>> $outdir/lspci 2>&1
$LSPCI -v						>> $outdir/lspci 2>&1

$CAT /proc/cpuinfo > $outdir/proc.cpuinfo 2>&1
$CAT /proc/meminfo > $outdir/proc.meminfo 2>&1

#
# Get current process and open file information
#
$PSCMD -efl > $outdir/ps_efl 2>&1
if [ "`grep -i suse /etc/issue.net`" ]; then 
	$LSOF_SUSE > $outdir/lsof 2>&1
elif [ "`grep -i \"Red Hat\" /etc/issue.net`" ]; then 
	$LSOF_REDHAT > $outdir/lsof 2>&1
else
	$LSOF_REDHAT > $outdir/lsof 2>&1
fi

#
#	Get the kernel boot parameters and modules info
#
$CAT /proc/cmdline > $outdir/proc.cmdline 2>&1
$SYSCTL -a	   > $outdir/sysctl_a	  2>&1
$LSMOD		   > $outdir/lsmod	  2>&1
printf "...DONE\n"


#
# ----- FCP information section -----
#	Get data that pertains to FCP 
#
if [ $FCPFLAG -eq 1 ]; then
   printf "Gathering FCP information..."

   if [ ! -d /proc/scsi/qla2300 ]; then
      $ECHO "NOTICE:qla2300 driver not loaded" > $outdir/qla2300.X 2>&1
   else
      #
      #		Get a copy of all of the hba data
      #
      for i in `ls -1 /proc/scsi/qla2300 | grep -v HbaApiNode`; do
         $CP /proc/scsi/qla2300/$i $outdir/qla2300.$i 2>&1
      done

      #
      #		Get the module load parameters
      #
      $MODPROBE -ndv qla2300 > $outdir/modprobe_ndv 2>&1

      #
      #		Get the info from the sanlun utility:
      #
      $SANLUN lun show -v all         > $outdir/luns 2>&1
      $SANLUN fcp show adapter -v all > $outdir/adapters 2>&1
      $NTAP_UTIL_DIR/$NTAP_UTIL_PROG lun show -v all         > $outdir/luns 2>&1
      $NTAP_UTIL_DIR/$NTAP_UTIL_PROG fcp show adapter -v all > $outdir/adapters 2>&1

      #
      #		Get info from the qlremote agent:
      #
      if [ ! -f $QLREMOTE ]; then
         $ECHO "NOTICE: qlremote agent not installed in default location" > $outdir/qlremote.X 2>&1
      else
         $QLREMOTE -v > $outdir/qlremote_v
      fi
   fi
   printf "...DONE\n"
fi

#
# ----- iSCSI information section -----
#	Get data that pertains to iSCSI
#
if [ $ISCSIFLAG -eq 1 ]; then

   # make sure the directory exists; re-use it if it's already there 
   if [ ! -d $outdir/iscsi ] ; then
      mkdir -p $outdir/iscsi
   fi
   #
   # Get general TCP/IP info: netstat -an, netstat -rn, ifconfig -a
   #
   printf "Gathering iSCSI networking information..."
   netstat -an > $outdir/iscsi/netstat_an 2>&1
   netstat -rn > $outdir/iscsi/netstat_rn 2>&1
   ifconfig -a > $outdir/iscsi/ifconfig_a 2>&1
   printf "...DONE\n"

   #
   # Get specific iscsi information
   #
   printf "Gathering iSCSI configuration information..."
   if [ -f /sbin/iscsi-ls ]; then
      /sbin/iscsi-ls -l > $outdir/iscsi/iscsi-ls_l 2>&1
      /sbin/iscsi-ls -c > $outdir/iscsi/iscsi-ls_c 2>&1
      /sbin/iscsi-ls    > $outdir/iscsi/iscsi-ls 2>&1
   elif [ -f /usr/local/sbin/iscsi-ls ]; then
      /usr/local/sbin/iscsi-ls -l > $outdir/iscsi/iscsi-ls_l 2>&1
      /usr/local/sbin/iscsi-ls -c > $outdir/iscsi/iscsi-ls_c 2>&1
      /usr/local/sbin/iscsi-ls    > $outdir/iscsi/iscsi-ls 2>&1
   fi
   cat /proc/scsi/iscsi/* > $outdir/iscsi/proc_scsi_iscsi 2>&1
   cp /etc/iscsi.conf $outdir/iscsi/iscsi.conf 2>&1
   cp /etc/initiatorname.iscsi $outdir/iscsi/initiatorname.iscsi 2>&1
   cp /etc/fstab.iscsi $outdir/iscsi/fstab.iscsi 2>&1
   #
   # For 3-4 linux-iscsi versions, grab /dev/iscsi/... symlinks
   #
   if [ -d /dev/iscsi ]; then
      ls -lR /dev/iscsi > $outdir/iscsi/dev_iscsi_ls_lR 2>&1
   elif [ -d /dev/disk/by-id ]; then
      # 
      # For SLES9, get device persistency links
      #
      if [ "`grep -i suse /etc/issue.net`" ]; then 
         ls -l /dev/disk/by-id/iscsi* > $outdir/iscsi/dev_disk_by-id_symlinks
      fi
   fi

   cp /var/lib/iscsi/bindings $outdir/iscsi/bindings 2>&1
   printf "...DONE\n"

   #
   # sanlun
   #
   printf "Gathering iSCSI sanlun information..."
   $SANLUN lun show -v all         > $outdir/iscsi/sanlun_luns 2>&1
   printf "...DONE\n"
fi



printf "Done gathering information\n"
#
#	Save the runtime params for this invocation of linux_info
#
$ECHO "Run on `date`"			>  $outdir/linux_info.runtime
$ECHO "Command line was:\n\n\t$CMDLINE"	>> $outdir/linux_info.runtime

$TAR -cf $outfile -C $directory ntap_linux_$name
$COMPRESS $outfile
#
#	Print out what's where, and exit
#
$ECHO "\n\nLINUX system info is in directory $outdir"
$ECHO "\tCompressed file is $final_file"
$ECHO "\tPlease send this file to Network Appliance for analysis"
exit 0
