#!/bin/bash # AllYourBase.sh by Meryll Larkin 2/29/08 # 12/1/08 updated to have a dynamic servername $SERVERNAME # Script to take possession of server # This script customized for RHE3, RHE4, RHE5 with standard architecture # Parts of this script may need to be rewritten for different distros and architectures # To use this script, login to the server as root and run it there # Okay to run from the /root /root/bin or /tmp directory. # Major update 11/12/2014 # reorganized into hardware/software # reformatted output for multiple lines # 1/4/2015 added function for cpu analysis # 5/12/2015 redid get_totalspace function for readability and accuracy # 2/17/2020 refactored for versions RH6,RH7,RH8 # Ensure script is run as root user if [[ $EUID -ne 0 ]]; then echo echo "This script must be run as root. Quitting." echo exit 1 fi # Determine the OS Distro and Version DISTRO=`cat /etc/*release* |grep NAME |head -n1 |cut -f2 -d= | tr -d \" | cut -f1 -d" " ` VERSION=`cat /etc/*release* |grep VERSION |head -n1 |cut -f2 -d= |tr -d \" | cut -f1 -d" " ` TODAY=$(date +"%Y%m%d") SERVERNAME=`hostname -s` if [ -z $SERVERNAME ] then echo "Quitting, SERVERNAME not set." echo "" exit fi echo "SERVERNAME = $SERVERNAME" RESULTSDIR=/root/bin/${SERVERNAME}_${TODAY} mkdir -p $RESULTSDIR TEMPDF=${RESULTSDIR}/df_${TODAY} df -lk > $TEMPDF function get_totalspace { totaltotal=0 totalused=0 totalavail=0 persistentFilesystem='' while read label total used available percent mount do count=$((count+1)) if(`test "$persistentFilesystem" != ''` ); then # This is for the case where Filesystem is on the previous line # So all labels are off kilter by one # fix that mount=$percent percent=$available available=$used used=$total total=$label label=$persistentFilesystem persistentFilesystem='' fi if( `test "$label" = "Filesystem"` ); then # ignore first line of df - it only contains column headers continue fi if( `test "$label" = "tmpfs"` ); then # ignore /dev/shm, /dev/vx/, tmpfs, swap continue fi if( `test "$mount" = "swap*"` ); then # ignore /dev/shm, /dev/vx/, tmpfs, swap continue fi if( `test "$mount" = "/dev/*"` ); then # ignore /dev/shm, /dev/vx/, tmpfs, swap continue elif(`test "$total" = ""` ); then # This is for the case where Filesystem is the only value on the line persistentFilesystem=$label continue else totaltotal=$((totaltotal + total)) totalused=$((totalused + used)) totalavail=$((totalavail + available)) fi done <$TEMPDF totaltotal=$((totaltotal/1024/1024)) totalused=$(((totalused/1024/1024) + 1)) totalavail=$(((totalavail/1024/1024) -1)) echo " Totalspace = $totaltotal G" echo " Totalused = $totalused G" echo " Totalavail = $totalavail G" } function get_cpuinfo { # for hyperthreading dmidecode, lscpu, cpuid but don't look for those till RH6 cores=`grep "core id" /proc/cpuinfo | sort | uniq | wc -l` thread_file="$RESULTSDIR/threads.txt" proc_line_file="$RESULTSDIR/processor_lines.txt" touch $thread_file $proc_line_file >$proc_line_file >$thread_file grep -n processor /proc/cpuinfo |awk -F: '{ print $1 }'>>$proc_line_file processors=`grep processor /proc/cpuinfo | wc -l` hyperthreading=`grep '^flags' /proc/cpuinfo |grep ' ht' | wc -l` # zero indicates no hyperthreading, equal to number of processors indicates all cores hyperthreaded # A processor entry due to hypherthreading will get its own processor, but share core id and physical id with another. if [ $(( ${cores} )) -gt 0 ]; then if [ $(( ${hyperthreading} )) -eq 0 ]; then # Hyperthreading is not enabled # CPU eval is straightforward, just cores and processors if [ $(( ${processors} % ${cores} )) -eq 0 ]; then physical_procs=$(( $processors / ${cores} )) else echo "Manual evaluation required of /proc/cpuinfo" fi elif [ $(( ${hyperthreading} - ${processors} )) -eq 0 ]; then # threads equal the number of cores lastline=0 thisline=0 increment=0 while read line do thisline=$line if [ $(( ${lastline} )) -eq 0 ]; then lastline=$thisline continue fi if [ $(( ${increment} )) -eq 0 ]; then increment=$(( ${thisline} - ${lastline} )) fi physical_id=`cat /proc/cpuinfo| head -n $thisline |tail -n $increment |grep "physical id"` core_id=`cat /proc/cpuinfo| head -n $thisline |tail -n $increment |grep "core id"` echo "$physical_id $core_id" >>$thread_file lastline=$thisline done < $proc_line_file thread_number=`cat $thread_file |sort |uniq | wc -l` physical_procs=$(( processors / thread_number )) cores=$(( thread_number / physical_procs )) threds=$(( cores / physical_procs )) else echo "Do a manual evaluation of /proc/cpu" fi else # number of cores = 0 physical_procs=`grep processor /proc/cpuinfo | wc -l` fi echo " PROCESSORS=$physical_procs" echo " CORES=$cores" if [ $(( ${threds} )) -gt 0 ]; then echo " THREADS=$threds per core" fi echo ' PROC SPEED='`grep MHz /proc/cpuinfo | sort |uniq` # cleanup rm $proc_line_file rm $thread_file } # START HERE echo "Taking possession of server $SERVERNAME" cd $RESULTSDIR { echo 'HOSTNAME='`hostname` # echo 'IPADDR='`ifconfig |grep "inet "` echo -n 'IPADDR=' IPADDR_ARRAY=( $(ifconfig |grep 'inet ' | grep -v '127.0.0.1') ) for var in "${IPADDR_ARRAY[@]}" do if( `test "$var" = "inet"` ); then echo echo -n " ${var}" continue fi echo -n " ${var}" done echo echo 'REBOOT NOTIFY=' echo 'FUNCTIONS=' echo echo "*** HARDWARE ***" echo " HARDWARE MAKE AND MODEL" dmidecode -t system lshw -C system |head echo memory_total=`grep MemTotal /proc/meminfo |awk '{ print $2 }'` mem_total=$((memory_total/1024/1024)) echo " MEMORY TOTAL= $mem_total G" echo ' -- CPU CORES AND THREADS EVALUATION --' get_cpuinfo echo ' MACHINE ARCH='`uname -m` echo ' -- HARDRIVE SPACE AND PARTITION EVALUATION --' # Below is just to create headers for the df and totalspace output newline=0 for dfvar in `df -k` do if [ $dfvar == "Mounted" ]; then echo " Mounted_on" newline=0 continue fi if [ $dfvar == "on" ] ; then continue fi indexpercent=`expr index "$dfvar" %` echo -n " ${dfvar}" if [ "$indexpercent" -gt "0" ] ; then newline=1 continue fi if [ "$newline" -gt "0" ] ; then echo newline=0 fi done echo echo ' Drive Totals on Local Machine only:' get_totalspace echo echo "*** SOFTWARE ***" echo " OS_VERSION=$DISTRO $VERSION" echo ' KERNEL RELEASE='`uname -r` echo ' KERNEL ARCH='`uname -i` echo ' CPU ARCH='`uname -p` if [[ -f /var/log/rpmkgs ]]; then echo ' LAST UPDATE='`date -r /var/log/rpmpkgs +'%Y-%m-%d'` fi if [[ -f "/var/log/dnf.rpm.log" ]]; then echo ' LAST UPDATE='`date -r /var/log/dnf.rpm.log +'%Y-%m-%d'` fi echo ' LAST REBOOT='`who -b` echo echo 'CUSTOM INSTALLS /usr/local =' ls -1 /usr/local/ echo echo 'CUSTOM INSTALLS /usr/local/bin =' ls -1 /usr/local/bin echo echo 'CUSTOM INSTALLS /opt =' ls -1 /opt/ echo '' } >>$RESULTSDIR/Basics.txt echo "Creating additional files of information" cp /root/.bash_history $RESULTSDIR/root_history.txt if [[ -f /var/spool/mail/root ]]; then cp /var/spool/mail/root $RESULTSDIR/root_email.txt fi cp /etc/hosts $RESULTSDIR/hosts.txt cp /etc/fstab $RESULTSDIR/fstab.txt cp /etc/nsswitch.conf $RESULTSDIR/nsswitch.txt if [[ -d /etc/sysconfig/network-scripts ]]; then mkdir -p $RESULTSDIR/network-scripts/ cp /etc/sysconfig/network-scripts/ifcfg-* $RESULTSDIR/network-scripts/ fi if [[ -d /etc/netplan ]]; then mkdir -p $RESULTSDIR/netplan/ cp /etc/netplan/* $RESULTSDIR/netplan/ fi if [[ -d /etc/network ]]; then mkdir -p $RESULTSDIR/network/ cp /etc/network/interfaces $RESULTSDIR/network/ fi grep -R -v '^#' /var/spool/cron/ > $RESULTSDIR/cronjobs.txt ls /home >$RESULTSDIR/Home.txt echo "df -hl" >$RESULTSDIR/df.txt df -hl >>$RESULTSDIR/df.txt echo "" >>$RESULTSDIR/df.txt echo "df -h" >>$RESULTSDIR/df.txt df -h >>$RESULTSDIR/df.txt PACKDIR=${RESULTSDIR}/PACKAGES mkdir -p ${PACKDIR} # Are we RedHat family or Ubuntu? # services installed and when running if [[ -f /usr/bin/rpm ]]; then # assume RedHat family commands will work echo 'rpm -qa |sort' >${PACKDIR}/rpm.txt echo '' >>${PACKDIR}/rpm.txt rpm -qa |sort >>${PACKDIR}/rpm.txt if [[ $VERSION < 7 ]] ; then echo "chkconfig --list" > ${PACKDIR}/chkconfig.txt echo "" >> ${PACKDIR}/chkconfig.txt chkconfig --list >> ${PACKDIR}/chkconfig.txt fi if [[ $VERSION > 6 ]] ; then echo "systemctl list-unit-files" > ${PACKDIR}/unit_files.txt echo "" >> ${PACKDIR}/unit_files.txt systemctl list-unit-files >> ${PACKDIR}/unit_files.txt fi elif [[ -f /usr/bin/dpkg ]]; then # assume Debian/Ubuntu family commands will work /usr/bin/dpkg --get-selections | grep -v deinstall > ${PACKDIR}/installed_dpkg.txt fi echo "netstat -antup" >${PACKDIR}/network_services.txt echo "" >>${PACKDIR}/network_services.txt netstat -antup >>${PACKDIR}/network_services.txt # Package it for easy export: mkdir -p /tmp/${SERVERNAME}_${TODAY} cp -a ${RESULTSDIR}/* /tmp/${SERVERNAME}_${TODAY} cd /tmp tar -cvf ${SERVERNAME}_${TODAY}.tar ${SERVERNAME}_${TODAY} mv /tmp/${SERVERNAME}_${TODAY}.tar ${RESULTSDIR}/ # clean-up unlink $TEMPDF rm -rf /tmp/${SERVERNAME}_${TODAY} # report results echo echo "Done. Results in $RESULTSDIR" echo