#!/bin/bash

# Begin summary section

echo
echo
echo -e "Hostname: $(uname -n)"
echo
echo "SUMMARY"
echo "**********"



# Grab the number of CPUs and the CPU name

CPU_COUNT=$(cat /proc/cpuinfo | grep "physical id" | sort -u | wc -l)
CPU_NAME=$(cat /proc/cpuinfo | grep -e "model name" | uniq | cut -d':' -f 2 | xargs)

# Show a slightly different output depending on if this is single proc or dual

if [ $CPU_COUNT -eq 1 ]
then
    echo "CPU: 1x ${CPU_NAME} processor"
elif [ $CPU_COUNT -eq 2 ]
then
    echo "CPU: 2x ${CPU_NAME} processors"
fi

# Unset the CPU variables as they're not needed anymore

unset CPU_COUNT
unset CPU_NAME

# Display information about the BIOS

echo "BIOS: $(dmidecode -t 2 | grep "Product Name:" | cut -d' ' -f 3) - VERSION: $(dmidecode -s bios-version)"
echo -e "IPMI: `ipmicfg -ver`"
timedatectl|grep Time

# Using the script created by mryndak to display the amount of RAM in the server (in GB) and then verify that the RAM is matching

echo -e "$(dmidecode -t 17 | grep -iE "Part" | grep -ve "NO DIMM" |  grep -ve "CPU" | grep -ve "Not" | grep -ve "PartNum" | grep -ve "Empty" | grep -ve "Series" | sort -u)" > RAM

if [ $(<RAM wc -l) != 1 ];
then
        echo "RAM: $(free --giga -h -t | grep "Mem:" | cut -c 18-22) -- WARNING: NOT MATCHING"
else
       echo "RAM: $(free --giga -h -t | grep "Mem:" | cut -c 18-22) -- GOOD JOB RAM MATCHES"
fi

# Deleting the RAM file used for checking for matching

rm RAM

# Checking to see if the server has a RAID controller

RAID_CARD=$(storcli /c0 show | grep -e "Virtual Drives" | cut -d'=' -f 2 | xargs)
if [ -z $RAID_CARD ]
then
	IS_RAID=false
elif [ $RAID_CARD -gt 0 ]
then
	IS_RAID=true
else
	IS_RAID=false
fi

# If the server has a RAID controller, display info about the RAID.  If not, display hard drive information

if $IS_RAID
then

	# Check to see if a BBU exists

	storcli /c0/bbu show > /dev/null
	IS_BBU=$(echo $?)

        # Check to see if a CacheVault exists

	storcli /c0/cv show > /dev/null
	IS_CV=$(echo $?)

	# Print info about the primary VD

	echo "RAID: $(storcli /c0 show | grep "Product Name" | cut -d' ' -f 7) - VD0: $(storcli /c0 show | grep "0/0" | cut -d' ' -f 4)"

	# Print info about the drives

	echo "Hard Drives: "
	for i in $(storcli /c0 /eall /sall show | awk '{print $2}'|grep '[0-9]'); do smartctl -a -d sat+megaraid,$i /dev/sda | grep "User Capacity" | cut -d'[' -f 2 | cut -d']' -f 1; done

	# Display if the RAID controller has a BBU or not

	if [ $IS_BBU -eq 0 ] || [ $IS_CV -eq 0 ]; then
		echo "BBU/CV: Yes"
	else
		echo "BBU/CV: No"
	fi

	# Delete the temporary bbu variable

	unset IS_BBU
        unset IS_CV
else

	# Set Drive Counter variable to increment through each drive found

	DRIVE_COUNTER=1

	# Loop through results from parted -l and spit out model info and size from each HDD

	for x in $(parted -l 2>/dev/null | grep -E '/dev/sd|/dev/nvme' | cut -d':' -f 1 | cut -d' ' -f 2); do
		echo "Drive ${DRIVE_COUNTER}: - ${x} - $(smartctl -a ${x} | grep -E "Device Model|Model Number" | cut -d":" -f 2 | xargs) - $(parted -l 2>/dev/null | grep "${x}" | cut -d' ' -f 3)"
		DRIVE_COUNTER=$((DRIVE_COUNTER+1))
	done

	# Remove the drive counter variable as it's not needed anymore

	unset DRIVE_COUNTER

fi

# Unset the RAID temp variables created for logic

unset RAID_CARD
unset IS_RAID


# Displaying a shortened name for the OS

echo -e "OS: $(cat /etc/*release | grep -e 'PRETTY_NAME' | cut -d'"' -f 2)"

# List all of the NICs on the system (named eth, eno or bond) and print out their speed

for x in $(ifconfig | grep ": " | grep 'eth|eno|bond' -E | cut -d':' -f 1 | uniq); do 
	echo "${x}: Speed:$(ethtool $x | grep "Speed" | cut -d':' -f 2 | xargs) Link: $(ethtool $x | grep "Link detected:" | cut -d':' -f 2 | xargs)" 
done





# End summary section

echo
#echo "---------------------------------------------------------------------------------------------------"
#echo -e "Hostname: `uname -n`"
#echo -e "Uname information: \n`uname -m;uname -r`"
#echo "--------------------------------------------------------------------------------------------------"
#echo -e "OS information: \n`cat /etc/*release;cat /etc/*version`"
#echo "------------------------------------------[OS INFO]-------------------------------------------"
#hostnamectl
#echo "--------------------------------------------------------------------------------------------------"
echo "Motherboard Information:"
echo -e "`dmidecode -t 2 | grep Manufacturer`"
echo -e "`dmidecode -t 2 | grep 'Product Name'`"
echo -e "BIOS VERSION: `dmidecode -s bios-version`"
echo "--------------------------------------------------------------------------------------------------"
echo -e "CPU `cat /proc/cpuinfo | grep "model name" | uniq`"
echo -e "Physical Count: `cat /proc/cpuinfo | grep "physical id" | sort -u | wc -l`"
echo -e "Total Logical Cores: `cat /proc/cpuinfo | grep "model name" | wc -l`"
echo "--------------------------------------------------------------------------------------------------"
#Start added by mryndak
echo
echo -e "`dmidecode -t 17 | grep -iE "Part" | grep -ve "NO DIMM" | grep -ve "CPU" | grep -ve "Not" | grep -ve "PartNumber" | grep -ve "Empty" | grep -ve "25L3205" | sort -u`" > RAM
if [ `<RAM wc -l` == 1 ];
then
echo "***GOOD JOB, RAM MATCHES***"
echo
else
echo "***RAM MISMATCH DETECTED***"
#awk '{getline x; if(x!=$0) print "***RAM MISMATCH DETECTED***"}' RAM | sort -u
echo
dmidecode -t 17 | grep -iE "Locator|Part" | grep -ve "Bank" | sed -e 's/^[ \t]*//' | sed '0~2 a\\'
fi
rm RAM
#End added by mryndak

echo -e "Total Memory: `free -m | grep Mem | awk '{print $2}'` MB"
echo -e "Swap: `free -m | grep Swap | awk '{print $2}'` MB"
if [[ -x /usr/sbin/dmidecode ]]
then
   STANZA=`dmidecode -t 17 2>/dev/null | awk '
$0 == "" {if (bd!="") for (i=0;i<=n;++i) print s[i];}
/^Memory Device/{ic="";bd=""; n=0}
/Size:/{if ($3 ~ /MB/ ) bd=$3;}
{s[n]=$0;++n} '`
   if [[ $? = 0 ]]
   then
      MEMORY=`echo "${STANZA}" | awk -F: '
/Locator:/ { printf $2 }
/Manufacturer:/ { printf $2 }
/Part Number:/ { printf $2 }
/Size:/ { printf $2 }
/Type:/ { printf $2 }
/Speed:/ { printf $2"\n"}'`
   else
      MEM=`cat /proc/meminfo | grep "MemTotal:" | awk '{print $2$3}'`
      MEMORY=`printf "%28s" ${MEM}`
   fi
fi
echo "${MEMORY}"
echo "--------------------------------------------------------------------------------------------------"
echo -e "Hard Drive(s): \n`fdisk -l | grep Disk | grep -v ident`"
echo "--------------------------------------------------------------------------------------------------"
echo -e "Lsblk: \n`lsblk`"
echo "--------------------------------------------------------------------------------------------------"
echo -e "/sda: \n`smartctl -a /dev/sda | grep ID# && smartctl -a /dev/sda | grep Power_On_Hours && smartctl -a /dev/sda | grep Reallocated_Sector_Ct && smartctl -a /dev/sda | grep Current_Pending_Sector && smartctl -a /dev/sda | grep Wear_Leveling_Count && smartctl -a /dev/sda | grep Media_Wearout_Indicator && smartctl -a /dev/sda | grep Offline_Uncorrectable | smartctl -a /dev/sda | grep %`"
echo "--------------------------------------------------------------------------------------------------"
echo -e "/sdb: \n`smartctl -a /dev/sdb | grep ID# && smartctl -a /dev/sdb | grep Power_On_Hours && smartctl -a /dev/sdb | grep Reallocated_Sector_Ct && smartctl -a /dev/sdb | grep Current_Pending_Sector && smartctl -a /dev/sdb | grep Wear_Leveling_Count && smartctl -a /dev/sdb | grep Media_Wearout_Indicator && smartctl -a /dev/sdb | grep Offline_Uncorrectable | smartctl -a /dev/sdb | grep %`"
echo "--------------------------------------------------------------------------------------------------"
echo -e "/sdc: \n`smartctl -a /dev/sdc | grep ID# && smartctl -a /dev/sdc | grep Power_On_Hours && smartctl -a /dev/sdc | grep Reallocated_Sector_Ct && smartctl -a /dev/sdc | grep Current_Pending_Sector && smartctl -a /dev/sdc | grep Wear_Leveling_Count && smartctl -a /dev/sdc | grep Media_Wearout_Indicator && smartctl -a /dev/sdc | grep Offline_Uncorrectable | smartctl -a /dev/sdc | grep %`"
echo "--------------------------------------------------------------------------------------------------"
echo -e "/sdd: \n`smartctl -a /dev/sdd | grep ID# && smartctl -a /dev/sdd | grep Power_On_Hours && smartctl -a /dev/sdd | grep Reallocated_Sector_Ct && smartctl -a /dev/sdd | grep Current_Pending_Sector && smartctl -a /dev/sdd | grep Wear_Leveling_Count && smartctl -a /dev/sdd | grep Media_Wearout_Indicator && smartctl -a /dev/sdd | grep Offline_Uncorrectable | smartctl -a /dev/sdd | grep %`"
echo "--------------------------------------------------------------------------------------------------"
echo -e "Disk Firmware Info /sda: \n`smartctl -a /dev/sda | grep Model && smartctl -a /dev/sda | grep Firmware &&  dmesg | grep -i sata | grep 'link up'`"
echo -e "Disk Firmware Info /sdb: \n`smartctl -a /dev/sdb | grep Model && smartctl -a /dev/sdb | grep Firmware &&  dmesg | grep -i sata | grep 'link up'`"
echo "--------------------------------------------------------------------------------------------------"
# Start added by mryndak

VSIZE=$(storcli /c0 show | grep "Virtual Drives" | awk '{print $4}')
PSIZE=$(storcli /c0 show | grep "Physical Drives" | awk '{print $4}')
let VD=7+$VSIZE+1
let PD=7+$PSIZE+1
echo "----------------------------------------[RAID INFO]-----------------------------------------"
echo
storcli /call show | grep -i "Product Name"
storcli /call show | grep -i "FW Package Build"
echo
echo "-----------------------------------------[PD INFO]-------------------------------------------"
echo
storcli /call show | grep -A $PD "Physical Drives"
echo
echo "-----------------------------------------[VD INFO]-------------------------------------------"
echo
storcli /call show | grep -A $VD "Virtual Drives"

unset PD
unset VD
unset PSIZE
unset VSIZE
# End added by mryndak

#echo -e "-----------------------------------LSI Disks Health--------------------------------------------------"
#for i in $(megacli -pdlist -a0| grep 'Device Id' |awk '{print $3}'); do smartctl -d sat+megaraid,$i -a /dev/sda; done
echo "---------------------------------------------------------------------------------------------------"
echo -e "IP Addresses: \n`ip addr | grep "inet " | grep -v "127.0.0.1" | cut -d" " -f 6,11-12`";
echo "----------------------------------------------------------------------------------------------------"
echo -e "IPMI: \n`ipmicfg -m | grep IP`"
echo -e "`ipmicfg -g`"
echo -e "`ipmicfg -k`"
echo -e "`ipmicfg -m | grep MAC`"
echo -e "`ipmicfg -ver`"
echo -e "`ipmicfg -fan | head -n 1`"
echo "---------------------------------------------------------------------------------------------------"
#echo "Resolvers"
#cat /etc/resolv.conf
#timedatectl|grep Time
#echo "----------------------------------------------------------------------------------------------------"
echo -e "Network Speed:"
if [ -f /sbin/ethtool ]
then
echo -e "Eth0`ethtool eth0 | grep Speed`"
echo -e "Eth1`ethtool eth1 | grep Speed`"
else
echo -e "Ethtool is not installed."
fi
echo -e '====================================================================================='
ipmicfg -sdr 
echo -e '====================================================================================='
ipmicfg -sel list
echo -e '====================================================================================='


for i in $(storcli /c0 /eall /sall show | awk '{print $2}'|grep '[0-9]'); do smartctl -a -d sat+megaraid,$i /dev/sda | grep -i "firmware version"; done &&

for i in $(storcli /c0 /eall /sall show | awk '{print $2}'|grep '[0-9]'); do smartctl -a -d sat+megaraid,$i  /dev/sda | awk '/Media_Wearout_Indicator/ {print $4}'; done 

ipmicfg -clrint


exit
