#!/bin/bash

export LANG=C
export PATH=/sbin:/usr/sbin:/bin:/usr/bin

# If this script hangs, un-comment the below two entries and note the command that the script hangs on.  Then comment out that command and re-run the script.
# set -x
# set -o verbose

[[ -d /tmp/sosreport ]] && rm -rf /tmp/sosreport
mkdir -p /tmp/sosreport && cd /tmp/sosreport && mkdir -p var/log etc/lvm etc/sysconfig network storage kernel systemd/etc systemd/lib proc

LOGFILE=/tmp/sosreport/error_log

echo -e "Gathering system information..."
hostname &> hostname  
cp -a /etc/redhat-release  etc/ 2>> $LOGFILE
uptime &> uptime 
file /etc/localtime &> localtime

echo -e "Gathering application information..."
chkconfig --list &> chkconfig
top -bn1 &> top_-bn1
which service &> /dev/null && service --status-all &> service_--status-all
date &> date
ps auxww &> ps_auxww
ps axlfwww &> ps_axlfwww
ps -elf &> ps_-elf
ps -efL &> ps_-efL
test -x /usr/bin/lsof && lsof -b +M -n -l -P &> lsof_-bnlP.M
rpm -qa --last &> installed-rpms
rpm --nodigest -qa --qf '%{NAME}~~%{ARCH}~~%{VERSION}~~%{RELEASE}~~%{INSTALLTIME:date}~~%{INSTALLTIME}~~%{VENDOR}~~%{BUILDHOST}~~%{SIGPGP}~~%{SIGPGP:pgpsig}\n' &> rpm_-qa_full
echo -e "  * Running 'rpm -Va'. This can take a while."
rpm -Va &> rpm_-Va
cp -a /etc/yum* etc/ 2>> $LOGFILE

echo -e "Gathering basic service information..."
cp -a /etc/audit/  etc/ 2>> $LOGFILE
cp -a /etc/audisp/  etc/ 2>> $LOGFILE
cp -a /etc/*syslog.* etc/ 2>> $LOGFILE
cp -a /etc/logrotate.* etc/ 2>> $LOGFILE

echo -e "Gathering memory information..."
free -m &> free  
which vmstat &> /dev/null && vmstat 1 10 &> vmstat

echo -e "Gathering network information..."
cp -a /etc/NetworkManager/ etc/NetworkManager/ 2>> $LOGFILE
ifconfig &> network/ifconfig  
netstat -s &> network/netstat_-s
netstat -agn &> network/netstat_-agn
netstat -neopa &> network/netstat_-neopa
route -n &> network/route_-n
ip address show &> ip_address_show
ip -o addr &> network/ip_-o_addr
ip link &> network/ip_link
ip route show table all &> network/ip_route_show_table_all
ip -6 route show table all &> network/ip_-6_route_show_table_all
ip -4 rule &> network/ip_-4_route
ip -6 rule &> network/ip_-6_rule
ip -s -d link &> network/ip_-s_-d_link
ip -d address &> network/ip_-d_address
ifenslave -a &> network/ifenslave_-a
ip mroute show &> network/ip_mroute_show
ip maddr show &> network/ip_maddr_show
ip -s -s neigh show &> network/ip_-s_-s_neigh_show
ip neigh show nud noarp &> network/ip_neigh_show_nud_noarp
biosdevname -d &> network/biosdevname_-d
tc -s qdisc show &> network/tc_-s_qdisk_show
ss -peaonmi &> network/ss_-peaonmi
iptables --list --line-numbers &> network/iptables_--list_--line-numbers
iptables -vnxL &> network/iptables_-vnxL
ip6tables -vnxL &> network/ip6tables_-vnxL
iptables-save &> network/iptables-save
# XXX - need to skip bnx2x
for i in /sys/class/net/*; do ndev=`basename $i`; echo "###### $ndev";
  ethtool $ndev;
  echo "###-d"
  ethtool -d $ndev;
  echo "###-i"
  ethtool -i $ndev;
  echo "###-k"
  ethtool -k  $ndev;
  echo "###-S"
  ethtool -S  $ndev;
  echo "###-T"
  ethtool -T  $ndev;
  echo "###-a"
  ethtool -a  $ndev;
  echo "###-c"
  ethtool -c  $ndev;
  echo "###-g"
  ethtool -g  $ndev;
  echo "###-P"
  ethtool -P  $ndev;
  echo "###-l"
  ethtool -l  $ndev;
  echo "###--phy-statistics"
  ethtool --phy-statistics  $ndev;
  echo "###--show-priv-flags"
  ethtool --show-priv-flags  $ndev;
  echo "###--show-eee"
  ethtool --show-eee  $ndev;
done &> network/ethtool.out
# bond
cat /proc/net/bonding/bond* &> network/proc-net-bonding-bond 2>> $LOGFILE

echo -e "Gathering storage/filesystem information..."
df -l &> df
fdisk -l &> fdisk
parted -l &> parted
cp -a /etc/fstab  etc/ 2>> $LOGFILE
cp -a /etc/lvm/lvm.conf etc/lvm/ 2>> $LOGFILE
cp -a /etc/lvm/backup/ etc/lvm/ 2>> $LOGFILE
cp -a /etc/lvm/archive/ etc/lvm/ 2>> $LOGFILE
cp -a /etc/multipath.conf etc/ 2>> $LOGFILE
cat /proc/mounts &> proc/mounts
iostat -tkx 1 10 &> iostat_-tkx_1_10
parted -l &> storage/parted_-l
vgdisplay -v &> storage/vgdisplay
lvdisplay &> storage/lvdisplay
pvdisplay &> storage/pvdisplay
pvs -a -v &> storage/pvs
vgs -v &> storage/vgs
lvs -o +devices &> storage/lvs
multipath -v4 -ll &> storage/multipath_ll
pvscan -vvvv &> storage/pvscan
vgscan -vvvv &> storage/vgscan
lvscan -vvvv &> storage/lvscan
lsblk &> storage/lsblk
lsblk -t &> storage/lsblk_t
blkid -c /dev/null &> storage/blkid_c
dmsetup info -C &> storage/dmsetup_info_c
dmsetup status &>  storage/dmsetup_status 
dmsetup table &>  storage/dmsetup_table
ls -lanR /dev &> storage/ls_dev
udevadm info --export-db &> storage/udevadm_info_export_db
cat /proc/scsi/scsi &> storage/proc-scsi-scsi
test -x /usr/bin/lsscsi && lsscsi -dg &> storage/lsscsi_dg

echo -e "Gathering kernel information..."
cp -a /etc/security/limits* etc/ 2>> $LOGFILE
cp -a /etc/sysctl* etc/ 2>> $LOGFILE
tar cf ./kernel/$(uname -r)_mod.tar /lib/modules/$(uname -r)/modules* 2>> $LOGFILE
ulimit -a &> ulimit
cat /proc/slabinfo &> proc/slabinfo
cat /proc/interrupts &> proc/interrupts 
cat /proc/iomem &> proc/iomem
cat /proc/ioports &> proc/ioports
cat /proc/cmdline &> proc/cmdline
slabtop -o &> slabtop_-o
uname -a &> uname
sysctl -a &> sysctl_-a
lsmod &> lsmod
cp -a /etc/modprobe* etc/ 2>> $LOGFILE
cp -a  /etc/sysconfig/ etc/sysconfig/ 2>> $LOGFILE
for MOD in `lsmod | grep -v "Used by"| awk '{ print $1 }'`; do modinfo  $MOD 2>&1 >> modinfo; done;
ipcs -a &> ipcs_-a
ipcs -s | awk '/^0x/ {print $2}' | while read semid; do ipcs -s -i $semid; done &> ipcs_-s_verbose
sar -A &> sar_-A
cp -a /var/log/dmesg var/log/dmesg 2>> $LOGFILE
dmesg &> dmesg_now
dmesg -T &> dmesg_T

echo -e "Gathering hardware information..."
dmidecode &> dmidecode
lspci -vvv &> lspci_-vvv
lspci &> lspci
cat /proc/meminfo &> proc/meminfo  
cat /proc/cpuinfo &> proc/cpuinfo

echo -e "Gathering kdump information..."
cp -a /etc/kdump.conf etc/ 2>> $LOGFILE
ls -lanR /var/crash &> ls_lanR-var-crash
ls -1 /var/crash | while read n; do mkdir -p var/crash/${n}; cp -a /var/crash/${n}/vmcore-dmesg* var/crash/${n}/ 2>> $LOGFILE; done

echo -e "Gathering detection software information..."
cp -a /etc/aide.conf etc/ 2>> $LOGFILE
ls -lanR /var/lib/aide &> ls_lanR-var-lib-aide
cp -a /var/log/aide var/log/ 2>> $LOGFILE

echo -e "Gathering abrt information..."
ls -lanR /var/spool/abrt &> ls_lanR-var-spool-abrt
cp -a /etc/abrt etc/ 2>> $LOGFILE

echo -e "Gathering container related information..."
mkdir container
rpm -q podman >/dev/null 2>&1 || alias podman="docker"
which podman &>/dev/null && podman ps | awk '$1!="CONTAINER" {print $1}' | while read id; do podman inspect $id &> container/inspect_${id}; done
which podman &>/dev/null && podman ps &> container/ps
which podman &>/dev/null && podman image list &> container/image_list

echo -e "Gathering library information..."
ldconfig -p -N -X &> ldconfig_-p_-N_-X
cp -a /etc/nsswitch.conf etc 2>> $LOGFILE

echo -e "Gathering python information..."
which python &> which_python && readlink -e `which python` &> readlink_python

echo -e "Gathering systemd information..."
cp -a /etc/systemd systemd/etc 2>> $LOGFILE
cp -a /usr/lib/systemd systemd/lib 2>> $LOGFILE
systemctl status --all --full --no-pager &> systemd/systemctl_status

echo -e "Gathering logs..."
test -x /usr/bin/journalctl && journalctl --no-pager --all --full --boot > var/log/journalctl.out 2>> $LOGFILE
cp -a /var/log/{containers*,message*,secure*,boot*,cron*,yum*,Xorg*,sa,rhsm,audit,dmesg} var/log/ 2>> $LOGFILE

echo -e "Compressing files..."
tar -zpcf /tmp/sosreport-$(uname -n)-$(date +%s).tar.gz ./

echo -e "Script complete."
