#! /bin/bash
#
# System Report
# Copyright (c) 1999-2005 Red Hat, Inc. All rights reserved.
#
# Written by:
#     Wil Harris <wil@redhat.com>
#     Peter Jones <pjones@redhat.com>
#     Preston Brown <pbrown@redhat.com>
#     Than Ngo <than@redhat.com>

umask 0077
ROOT=/tmp/sysreport.$$
UTILDIR=/usr/share/sysreport
VER=`/bin/uname -r`

if [ -f $UTILDIR/functions ]; then
  . $UTILDIR/functions
else 
  echo "Sysreport is not properly installed"
  exit 1
fi
if [ $UID != 0 ]; then
  echo "You must be root to use this utility"
  exit 1
fi
rm -rf $ROOT
mkdir $ROOT
PATH=""

echo
echo "This utility will go through and collect some detailed information "
echo "about the hardware and setup of your Red Hat Linux system."
echo "This information will be used to diagnose problems with your system"
echo "and will be considered confidential information.  Red Hat will use"
echo "this information for diagnostic purposes ONLY. "
echo
echo "Please wait while we collect information about your system."
echo
echo "This process may take a while to complete...."
echo "No changes will be made to your system during this process."
echo
echo "NOTE: You can safely ignore a failed message. This only means a file"
echo "we were checking for did not exist."
echo
echo "If your system hangs while gathering rpm information, please abort"
echo "the script with CTRL-C and run it again after adding -norpm to the"
echo "sysreport command line"
echo
echo "Press ENTER to continue, or CTRL-C to quit."
read IGNORE

echo
echo "Getting system configuration information."
echo
STATUS="Determining Red Hat Linux version:"
catiffile "/etc/redhat-release"
STATUS="Determinding your current hostname: " 
catifexec "/bin/hostname"
STATUS="Getting the date:"
catifexec "/bin/date"
STATUS="Checking your systems current uptime and load average:"
catifexec "/usr/bin/uptime"
STATUS="Checking available memory:"
catifexec "/usr/bin/free"
STATUS="Checking free disk space:"
catifexec "/bin/df" "-al"
STATUS="Checking currently running processes:"
catifexec "/bin/ps" "auxww"
STATUS="Checking current process tree:"
catifexec "/usr/bin/pstree"
STATUS="Collecting information about ld.so:"
catiffile "/etc/ld.so.conf"
STATUS="Collecting information about chkconfig --list:"
catifexec "/sbin/chkconfig" "--list"
STATUS="Collecting information about /etc/rc.d:"
catiffile "/etc/rc.d"
/bin/ls /etc/rc.d/rc*.d/ > $ROOT/etc/rc.d/ls-output
STATUS="Lists information about files opened (lsof)"
catifexec "/usr/sbin/lsof" "-b +M -n -l"

# check to see if we want to run rpm, under some circumstances
# rpm can hang and never complete
# yes, command line parsing needs some work
if [ ${1}s != "-norpms" ]; then
  echo "Collecting information about currently installed packages:"
  echo -n "This may take several minutes...."
  /bin/rpm -qa --qf "%{NAME}-%{VERSION}-%{RELEASE}-%{ARCH}\n" > $ROOT/installed-rpms
  /bin/rpm -Va > $ROOT/rpm-Va 2>&1
  echo_success
fi

STATUS="Getting bootloader information:"
/bin/ls -alR /boot > $ROOT/ls-boot 2>&1
# This covers sparc, alpha, and intel (respectively)
# updated for grub -mpg
if [ -f /etc/silo.conf ]; then
  STATUS="Collecting information about the boot process (silo):"
  catiffile "/etc/silo.conf"
fi
if [ -f /etc/milo.conf ]; then
  STATUS="Collecting information about the boot process (milo):"
  catiffile "/etc/milo.conf"
fi
if [ -f /etc/lilo.conf ]; then
  STATUS="Collecting information about the boot process (lilo):"
  catiffile "/etc/lilo.conf"
  catifexec "/sbin/lilo" "-q"
fi
if [ -d /boot/grub ]; then
  STATUS="Collecting information about the boot process (grub):"
  catiffile "/boot/grub/grub.conf"
  catiffile "/boot/grub/device.map"
fi
if [ -f /etc/cluster.conf -o -f /etc/cluster.xml ] ; then
  STATUS="Gathering information on cluster setup"
  # 2.1 AS
  if [ -f /etc/cluster.conf ] ; then
    catiffile "/etc/cluster.conf"
  fi
  # Taroon
  if [ -f /etc/cluster.xml ] ; then
    catiffile "/etc/cluster.xml"
  fi
fi
STATUS="Collecting information about X:"
catiffile "/etc/X11"
STATUS="Gathering sysctl information:"
catiffile "/proc/sys" 2>/dev/null
catiffile "/etc/sysctl.conf"
for x in `/bin/ls -d /etc/cron*` ; do
  STATUS="Collecting information about cron ($(/bin/basename $x)):"
  catiffile "$x"
done
STATUS="Gathering Syslog config information:"
catiffile "/etc/syslog.conf"
STATUS="Gathering ntp configuration:"
catiffile "/etc/ntp.conf"
catiffile "/etc/ntp/step-tickers"
catiffile "/etc/ntp/ntpservers"
STATUS="Gathering IP information (/sbin/ifconfig):"
catifexec "/sbin/ifconfig" "-a"
STATUS="Checking network routes:"
catifexec "/sbin/route" "-n"
STATUS="Collecting Name Service Switch config information:"
catiffile "/etc/nsswitch.conf"
STATUS="Collecting system configuration information:"
catiffile "/etc/sysconfig"
STATUS="Collecting information about system authentication (pam):"
catiffile "/etc/pam.d"

echo
echo "Getting information about the kernel."
echo
STATUS="Getting kernel version:"
catifexec "/bin/uname" "-a"
STATUS="Checking module information:"
catifexec "/sbin/lsmod"
for x  in $(/sbin/lsmod | /bin/cut -f1 -d" " 2>/dev/null | /bin/grep -v Module 2>/dev/null ) ; do
  STATUS="Checking module information $x:"
  catifexec "/sbin/modinfo" "$x"
done
STATUS="Currently getting ksysms information:"
catifexec "/sbin/ksyms"
STATUS="Gathering information about your filesystems:"
catiffile "/proc/filesystems" 2>/dev/null
STATUS="Gathering information about your system stat:"
catiffile "/proc/stat" 2>/dev/null
STATUS="Gathering information about your partitions (/proc/partitions):"
catiffile "/proc/partitions" 2>/dev/null
STATUS="Gathering information about your ksysms:"
catiffile "/proc/ksyms" 2>/dev/null
STATUS="Gathering information about slabinfo:"
catiffile "/proc/slabinfo" 2>/dev/null
# Added support to cover for the new modules.conf layout in Red Hat 7
STATUS="Collecting information regarding kernel modules"
catiffile "/lib/modules/$VER/modules.dep"
if [ -f /etc/conf.modules ]; then
  STATUS="Collecting information regarding kernel modules (/etc/conf.modules)"
  catiffile "/etc/conf.modules"
elif [ -f /etc/modules.conf ]; then
  STATUS="Collecting information regarding kernel modules (/etc/modules.conf)"
  catiffile "/etc/modules.conf"
fi

# dkms status
STATUS="Gathering current status of modules, versions and kernels (dkms):"
if [ -x /usr/sbin/dkms ] ; then
  catifexec "/usr/sbin/dkms" "status"
fi

if [ -f /etc/sysconfig/isdncard ] ; then
  STATUS="Gathering information about ISDN:"
  catiffile "/etc/sysconfig/isdncard"
fi

STATUS="Collecting information from the proc directory:"
catiffile "/proc/pci" 2>/dev/null
STATUS="Getting kernel command line"
catiffile "/proc/cmdline" 2>/dev/null
STATUS="Gathering information about your CPU:"
catiffile "/proc/cpuinfo" 2>/dev/null
STATUS="Gathering information about your Ram:"
catiffile "/proc/meminfo" 2>/dev/null
STATUS="Gathering information about your ioports:"
catiffile "/proc/ioports" 2>/dev/null
STATUS="Gathering information about your interrupts:"
catiffile "/proc/interrupts" 2>/dev/null
STATUS="Gathering information about your scsi devices:"
catiffile "/proc/scsi" 2>/dev/null
STATUS="Gathering information about your dma:"
catiffile "/proc/dma" 2>/dev/null
STATUS="Gathering information about your devices (/proc/devices):"
catiffile "/proc/devices" 2>/dev/null
STATUS="Gathering information about your rtc:"
catiffile "/proc/rtc" 2>/dev/null
STATUS="Gathering information about your ide drivers:"
catiffile "/proc/ide" 2>/dev/null
STATUS="Gathering information about your bus:"
getpciinfo > $ROOT/lspci
catiffile "/proc/bus" 2>/dev/null

echo
echo "Getting disk and filesystem information."
echo
STATUS="Collecting information from /etc/fstab:"
catiffile "/etc/fstab"
STATUS="Collecting disk partition information"
PATH=/bin getpartinfo > $ROOT/fdisk-l
STATUS="Checking mounted file systems (mount)"
catifexec "/bin/mount"
STATUS="Checking mounted file systems (/proc/mounts)"
catiffile "/proc/mounts" 2>/dev/null
STATUS="Collecting Software RAID information (/proc/mdstat)"
catiffile "/proc/mdstat" 2>/dev/null
STATUS="Collecting Software RAID information (/etc/raidtab)"
catiffile "/etc/raidtab"
STATUS="Collecting Software RAID information (/etc/mdadm.conf)"
catiffile "/etc/mdadm.conf"
STATUS="Collecting Automount information (auto.master)"
catiffile "/etc/auto.master"
STATUS="Collecting Automount information (auto.misc)"
catiffile "/etc/auto.misc"
STATUS="Collecting Automount information (auto.net)"
catiffile "/etc/auto.net"

echo
echo "collecting information about commonly used network services"
echo
# This covers backwards compatiablity for systems with inetd while keeping 
# support for the new 7.x xinetd
if [ -f /etc/inetd.conf ]; then
  STATUS="Collecting information about system services (inetd.conf)"
  catiffile "/etc/inetd.conf"
elif [ -f /etc/xinetd.conf ]; then
  STATUS="Collecting information about system services (xinetd.conf)"
  catiffile "/etc/xinetd.conf"
  for x in `/bin/ls -d /etc/xinetd.d/*`; do
    STATUS="Collecting information about system services ($(/bin/basename $x))"
    catiffile "$x"
  done
fi
for x in `/bin/ls -d /etc/host*` ; do
  STATUS="Collecting information from $x:"
  catiffile "$x"
done
STATUS="Collecting information about the ftp servers:"
ftpconfigfiles=`/bin/ls -d /etc/ftp* 2>/dev/null`  
for x in $ftpconfigfiles; do
  STATUS="Collecting information about the ftp servers ($(/bin/basename $x)):"
  catiffile "$x"
done
STATUS="Collecting information about ypbind configuration:"
catiffile "/etc/yp.conf"

KERNELMIN=`/bin/uname -r | /bin/sed -e 's,[^\.]*\.,,' -e 's,\..*,,'`
ipchainsmod=`/sbin/lsmod 2>/dev/null| /bin/grep ipchains`

if [ "$KERNELMIN" -lt 3 ] || [ -n "${ipchainsmod}" ] ; then
  STATUS="Getting ipchains information:"
  catifexec "/sbin/ipchains" "-nvL"
elif [ "$KERNELMIN" -gt 3 ]; then
  STATUS="Getting iptables information:"
  if [ -f /etc/sysconfig/iptables-config ] ; then
     catiffile "/etc/sysconfig/iptables-config"
  fi
  /bin/echo "<--iptables -t filter -nvL-->" > $ROOT/iptables
  /sbin/iptables -t filter -nvL >> $ROOT/iptables
  /bin/echo "<--iptables -t mangle -nvL-->" >> $ROOT/iptables
  /sbin/iptables -t mangle -nvL >> $ROOT/iptables
  /bin/echo "<--iptables -t nat -nvL-->" >> $ROOT/iptables
  /sbin/iptables -t nat -nvL >> $ROOT/iptables
  echo_success
fi

# ldap client and server config
if [ -f /etc/ldap.conf ] ; then
  STATUS="Getting LDAP configuration (ldap.conf)"
  catiffile "/etc/ldap.conf"
fi
if [ -d /etc/openldap ] ; then
  for x in `/bin/ls -d /etc/openldap/*` ; do
    if [ -f $x ] ; then
      STATUS="Getting LDAP configuration ($(/bin/basename $x))"
      catiffile "$x"
    fi
  done
fi

# sendmail
SENDMAIL_CF_FILE=/etc/mail/sendmail.cf
SENDMAIL_CW_FILE=/etc/mail/sendmail.cw

if [ -f /etc/sendmail.cf ] ; then
   SENDMAIL_CF_FILE=/etc/sendmail.cf
fi

if [ -f /etc/sendmail.cw ] ; then
   SENDMAIL_CW_FILE=/etc/sendmail.cw
fi

STATUS="Sendmail Configuration ($SENDMAIL_CF_FILE):"
catiffile "$SENDMAIL_CF_FILE"
if [ -f $SENDMAIL_CW_FILE ]; then
  STATUS="Sendmail Machine aliases ($SENDMAIL_CW_FILE):"
  catiffile "$SENDMAIL_CW_FILE"
fi

STATUS="Sendmail User aliases (/etc/aliases):"
catiffile "/etc/aliases"

for f in `/bin/ls -d /etc/mail/* | /bin/grep -v \.db`; do
  STATUS="Getting files from ($(/bin/basename $f))"
  catiffile $f
done

STATUS="Collecting Postfix Configuration (main.cf)"
catiffile "/etc/postfix/main.cf"
STATUS="Collecting Postfix Configuration (master.cf)"
catiffile "/etc/postfix/master.cf"

STATUS="Collecting host resolution information"
catiffile "/etc/resolv.conf"
# Make sure we have support for both versions of bind
if [ -f /etc/named.boot ]; then
  STATUS="Collecting information about the nameserver (named.boot)"
  catiffile "/etc/named.boot"
elif [ -f /etc/named.conf ]; then
  STATUS="Collecting information about the nameserver (named.conf)"
  catiffile "/etc/named.conf"
fi

DNSDIR=""
if [ -f /etc/named.conf ]; then
  DNSDIR=$(/bin/grep -i directory /etc/named.conf |/bin/gawk '{ print $2 ;}'|\
    /bin/sed s/\"//g|/bin/sed s/\;//g)
elif [ -f /etc/named.boot ]; then
  DNSDIR=$(/bin/grep -i directory /etc/named.boot|/bin/gawk '{ print $2 ;}')
fi

if [ -n $DNSDIR ]; then
  for x in $DNSDIR ; do
    STATUS="Collecting information about the nameserver ($DNSDIR)"
    catiffile "$DNSDIR"
  done
fi

if [ -f /etc/exports ] ; then
  STATUS="Collecting information about the NFS:"
  catiffile "/etc/exports"
fi

SAMBADIR="/etc/samba"
if [ -d $SAMBADIR ]; then
  for x in `/bin/ls -d $SAMBADIR/* | /bin/grep -vE 'smbpasswd|secrets\.tdb|\.bak|\.rpmnew'` ; do
    STATUS="Getting samba informations ($(/bin/basename $x))"
    catiffile "$x"
  done
  /bin/ls -alR $SAMBADIR > $ROOT/ls-samba 2>&1
  if [ -x /usr/bin/wbinfo ] ; then
    STATUS="Getting samba informations (wbinfo -g):"
    catifexec "/usr/bin/wbinfo" "-g"
    STATUS="Getting samba informations (wbinfo -u):"
    catifexec "/usr/bin/wbinfo" "-u"
  fi
fi

STATUS="Getting Kerberos informations (/etc/krb5.conf)"
catiffile "/etc/krb5.conf"
STATUS="Getting Kerberos informations (/etc/krb.conf)"
catiffile "/etc/krb.conf"
STATUS="Getting Kerberos informations (/etc/krb.realms)"
catiffile "/etc/krb.realms"

STATUS="Getting information about apache"
catiffile "/etc/httpd/conf/httpd.conf"
for x in `/bin/ls -d /etc/httpd/conf.d/*.conf 2>/dev/null` ; do
  STATUS="Getting information about apache ($(/bin/basename $x))"
  catiffile "$x"
done

STATUS="Getting information about squid"
catiffile "/etc/squid/squid.conf"

STATUS="Getting information about CUPS"
for x in `/bin/ls -d /etc/cups/*.conf 2>/dev/null` ; do
  STATUS="Getting information about CUPS ($x)"
  catiffile "$x"
done

echo
echo "Gathering information from system logs"
echo
STATUS="Collecting information from dmesg:"
catiffile "/var/log/dmesg"
STATUS="Collecting information from /proc/sysrq-trigger"
if [ -f /proc/sysrq-trigger ] ; then
  echo m > /proc/sysrq-trigger
fi
STATUS="Collecting information (/var/log/messages)"
catiffile "/var/log/messages"
STATUS="Collecting information (/var/log/sa)"
catiffile "/var/log/sa"
if [ -f /var/log/Xorg.0.log ] ; then
  STATUS="Collecting information (/var/log/Xorg.0.log)"
  catiffile "/var/log/Xorg.0.log"
fi
if [ -f /var/log/XFree86.0.log ] ; then
  STATUS="Collecting information (/var/log/XFree86.0.log)"
  catiffile "/var/log/XFree86.0.log"
fi
STATUS="Collecting information (/var/log/rpmpkgs)"
catiffile "/var/log/rpmpkgs"
STATUS="Collecting information (/var/log/boot.log)"
catiffile "/var/log/boot.log"
STATUS="Collecting information (/var/log/boot.log)"
catiffile "/var/log/up2date"
TATUS="Collecting information (/var/log/secure)"
catiffile "/var/log/secure"
STATUS="Collecting log files from Apache"
catiffile "/var/log/httpd"
STATUS="Collecting log files from RHN"
if `/bin/ls /var/log/rhn* >& /dev/null` ; then
  catiffile "/var/log/rhn*"
fi

# General monitoring moc logs
if [ -d /opt/notification ] ; then
  for x in `/bin/ls -d /opt/notification/var/*.log* 2>/dev/null` ; do
    STATUS="Collecting monitoring moc logs ($(/bin/basename $x))"
    catiffile "$x"
  done
  for x in `/bin/ls -d /var/tmp/ack_handler.log* 2>/dev/null` ; do
    STATUS="Collecting monitoring moc logs ($(/bin/basename $x))"
    catiffile "$x"
  done
  for x in `/bin/ls -d /var/tmp/enqueue.log* 2>/dev/null` ; do
    STATUS="Collecting monitoring moc logs ($(/bin/basename $x))"
    catiffile "$x"
  done
fi

# monitoring scout logs
if [ -d /home/nocpulse/var ] ; then
  for x in `/bin/ls -d /home/nocpulse/var/*.log* 2>/dev/null` ; do
    STATUS="Collecting monitoring scout logs ($(/bin/basename $x))"
    catiffile "$x"
  done
  for x in `/bin/ls -d /home/nocpulse/var/commands/*.log* 2>/dev/null` ; do
    STATUS="Collecting monitoring scout logs ($(/bin/basename $x))"
    catiffile "$x"
  done
fi

/bin/echo
/bin/echo "Getting information about RHN."
/bin/echo
RHNDIR=/etc/sysconfig/rhn
/bin/mkdir -p $ROOT/$RHNDIR 2>/dev/null
catiffile "/etc/rhn"
if [ -f $RHNDIR/systemid ] ; then
  echo -n "Gathering systemid information:"
  if [ -x /usr/bin/xsltproc ] ; then
    /usr/bin/xsltproc $UTILDIR/text.xsl $RHNDIR/systemid > $ROOT/$RHNDIR/systemid 2>/dev/null
    echo_success
  else
    catiffile "$RHNDIR/systemid"
  fi
fi
# Get hardware profile information (for verification with system state and RHN)
if [ -x /usr/share/rhn/up2date_client/hardware.py ] ; then
  STATUS="Gathering RHN hardware profile information"
  catifexec /usr/share/rhn/up2date_client/hardware.py
fi
if [ -x /usr/bin/rhn-schema-version ] ; then
  STATUS="Gathering schema version"
  catifexec "/usr/bin/rhn-schema-version"
fi
if [ -x /usr/bin/rhn-charsets ] ; then
  STATUS="Gathering database charactersets"
  catifexec "/usr/bin/rhn-charsets"
fi


cd /tmp
/bin/echo
/bin/echo -n "Enter your first initial and last name with no spaces (example: jsmith): "
/bin/echo 
read NAME
if [ ! $NAME ]; then
  NAME=$LOGNAME.$HOSTNAME
fi
/bin/rm -Rf $NAME.$$
/bin/mv sysreport.$$ $NAME.$$
PATH=/bin tar cf $NAME.$$.tar $NAME.$$
if [ -x /usr/bin/bzip2 ]; then
  /usr/bin/bzip2 $NAME.$$.tar
  SUFFIX="tar.bz2"
else 
  /bin/gzip -9 $NAME.$$.tar
  SUFFIX="tar.gz"
fi

/bin/rm -Rf $NAME.$$
/bin/echo 
/bin/echo "Please send /tmp/${NAME}.${$}.${SUFFIX} to your support"
/bin/echo "representative."
/bin/echo 

