#!/bin/bash
# Author: Swift Griggs <swift@parsec.com>
# 
# Description:
#   Basic System information gathering script for Linux
# 
# Log:
#   11-15-2018 - Initial version
#   11-18-2018 - Added some iostat/vmstat data

# Clobber the path with sanity, in case of a sub-par path
PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin:$PATH

# This will be the temporary directory setup for any
# file/report output. Later we'll tar this up. 
REPORT_DIR=/tmp/sysinfo.$$

# Gotta be able to create the report dir
mkdir $REPORT_DIR
if [ $? -ne 0 ]; then
  echo "ERROR: Cannot create temporary directory ($REPORT_DIR)"
  exit 1
fi 

# They really should be root. But we'll let it slide if not
# not everyone can get root on their system. Sad but true. 
MY_EUID=`id -u`
if [ $MY_EUID -ne 0 ]; then
  echo "WARNING: This script cannot gather full information without root privileges."
  echo "         Bravely continuing anyway. You may see some warnings."
  sleep 5
fi 

echo "INFO: Gathering system information. Standby."

# Get hardware info from dmidecode, which is pretty complete.
which dmidecode >/dev/null 2>&1
if [ $? -eq 0 ] ;then
  dmidecode > ${REPORT_DIR}/dmidecode 2>&1
fi

# Pull the CPU information from /proc
if [ -r /proc/cpuinfo ] ;then
  cat /proc/cpuinfo > ${REPORT_DIR}/cpuinfo 2>&1
fi

# Lots of one-off commands. some will work some won't *shrug*.
# we will take whatever we can get.
timeout 10 df -k 		> ${REPORT_DIR}/df_k 		2>&1
timeout 10 df -h           	> ${REPORT_DIR}/df_h 		2>&1
timeout 10 ls -lR /dev/disk 	> ${REPORT_DIR}/disk_paths 	2>&1
ps -efaux                   	> ${REPORT_DIR}/processes_efaux 2>&1
ps auxw 			> ${REPORT_DIR}/processes_auxw 	2>&1
top -n 1 -b 			> ${REPORT_DIR}/top 		2>&1
timeout 10 vgdisplay -v 	> ${REPORT_DIR}/vgdisplay 	2>&1
timeout 10 mount 		> ${REPORT_DIR}/mount 		2>&1
sysctl -a 			> ${REPORT_DIR}/sysctl_a 	2>&1
lsof -i tcp -Pn 		> ${REPORT_DIR}/lsof_tcp 	2>&1
lsof -i udp -Pn 		> ${REPORT_DIR}/lsof_udp 	2>&1
timeout 15 lsof 		> ${REPORT_DIR}/lsof 		2>&1
netstat -na 			> ${REPORT_DIR}/netstat_na 	2>&1
netstat -rn 			> ${REPORT_DIR}/netstat_rn 	2>&1
w 				> ${REPORT_DIR}/w 		2>&1
last -100 			> ${REPORT_DIR}/last_100 	2>&1
ipcs -m 			> ${REPORT_DIR}/ipcs_m		2>&1


echo "INFO: Checking for iostat & vmstat."

which vmstat >/dev/null 2>&1
if [ $? -eq 0 ]; then
  echo "INFO: Collecting 10 seconds of vmstat data. Standby."
  vmstat -SM 1 5 			> ${REPORT_DIR}/vmstat_SM_1_5	2>&1
fi

which iostat >/dev/null 2>&1
if [ $? -eq 0 ]; then
  echo "INFO: Collecting 10 seconds of iostat data. Standby."
  iostat -x 1 10 			> ${REPORT_DIR}/vmstat_SM_1_5	2>&1
fi

NOW=`date +%s`
echo "INFO: Trying to create tar archive for system information /tmp/system_info_${NOW}.tar.gz "
export GZIP=-9
tar czf /tmp/system_info_${NOW}.tar.gz "${REPORT_DIR}" >/dev/null 2>&1
if [ $? -eq 0 ]; then
  echo "INFO: Successfully created archive. Please pass the archive to tech support."
  echo "/tmp/system_info_${NOW}.tar.gz"
  rm -rf ${REPORT_DIR} 2>/dev/null 
  exit 0
else
  echo "ERROR: Something went wrong with creating the archive! Out of disk space?"
  exit 1
fi
