Last active
December 25, 2022 03:28
-
-
Save MrDrMcCoy/7c3f3f9af0886f72a9ea1b229f7c8720 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
trap exit SIGINT SIGTERM | |
sshcmd="ssh -q -o StrictHostKeyChecking=no" | |
hostlist="" | |
username="" | |
outfile="" | |
totalhosts=0 | |
while getopts :u:f:o:h arg ; do | |
case $arg in | |
u) | |
echo "Using '$OPTARG' as user name to log in to remote host(s)." | |
username="$OPTARG" | |
echo | |
;; | |
f) | |
echo "Reading host list from file: '$OPTARG'" | |
totalhosts=$(wc -l < "$OPTARG") | |
echo "Found $totalhosts host(s)." | |
hostlist=$(tr '\n' ' ' < "$OPTARG") | |
echo | |
;; | |
h) | |
echo " | |
This script gathers hardware info from remote hosts via SSH. Please use key-based authentication for simplicity. | |
The hosts can be specified in a line-separated file passed as the first argument, or as a space-separated list when prompted. | |
A remote user can be optionally specified. If it is not specfied, the current user will be used. This is currently detected as '$USER'. | |
If you wish to save the output to CSV, you can specify a file with -o. | |
Usage: $0 [-f host-list.txt] [-u remote-user-id] [-o host-info.csv] | |
Press 'q' to quit" | tr -d '\t' | fold -sw "$(tput cols)" | less | |
exit | |
;; | |
o) | |
echo "Will output CSV to file: $OPTARG" | |
outfile="$OPTARG" | |
echo | |
;; | |
*) | |
echo -e "Unknown option: '$OPTARG' \nFor usage, type '$0 -h'" | |
exit | |
;; | |
esac | |
done | |
if [ "$outfile" != "" ]; then | |
csv="$outfile" | |
else | |
csv="$(mktemp)" | |
fi | |
if [ "$hostlist" = "" ]; then | |
read -rp 'Host list empty. Please specify a space-separated list of hosts to gather hardware info from: | |
# ' hostlist | |
totalhosts=$(echo "$hostlist" | tr ' ' '\n' | wc -l) | |
echo -e "Host list: \n$hostlist\n" | |
fi | |
if [ "$username" = "" ]; then | |
echo "Using '$USER' as user name to log in to remote host(s)." | |
username="$USER" | |
echo | |
fi | |
hostprogress=1 | |
echo "Host,OS Info,CPU Model,CPU Speed,CPU Sockets,Cores per Socket,Vendor,Total Memory,Consumed Disk,Provisioned Disk" > "$csv" | |
for host in $hostlist; do | |
(>&2 echo "Gathering info from: $host ($hostprogress/$totalhosts)") | |
osinfo="$($sshcmd "$username@$host" "grep -E 'DISTRIB_DESCRIPTION|release' /etc/*release | head -n 1")" | |
cpumodel="$($sshcmd "$username@$host" "grep -m1 'model name' /proc/cpuinfo | sed 's/^[a-z \t:]*//;s/[CPU @]*[0-9\.]*GHz//'")" | |
cpuspeed="$($sshcmd "$username@$host" "grep -oiPm1 '[\d\.]+GHz' /proc/cpuinfo")" | |
cpusockets="$($sshcmd "$username@$host" "grep 'physical id' /proc/cpuinfo | sort -u | wc -l")" | |
corespersocket="$($sshcmd "$username@$host" "grep -m1 'cpu cores' /proc/cpuinfo | awk '{print \$4}'")" | |
vendor="$($sshcmd "$username@$host" "sed 's/,/،/g' < /sys/class/dmi/id/sys_vendor")" | |
memtotal="$($sshcmd "$username@$host" "grep 'MemTotal' /proc/meminfo | awk '{print \$2}'")" | |
(( memtotal = "$memtotal" / 1000000 )) # to GB | |
diskconsumed="0" | |
for mount in $(printf "df -lkP -x tmpfs -x devtmpfs | awk '{print \$3}' | grep -v Used | tr '\n' ' '" | $sshcmd "$username@$host" bash); do | |
(( diskconsumed = "$diskconsumed" + "$mount" )) | |
done | |
(( diskconsumed = "$diskconsumed" / 1000000 )) # to GB | |
diskprovisioned="0" | |
for mount in $(printf "df -lkP -x tmpfs -x devtmpfs | awk '{print \$2}' | grep -v blocks | tr '\n' ' '" | $sshcmd "$username@$host" bash); do | |
(( diskprovisioned = "$diskprovisioned" + "$mount" )) | |
done | |
(( diskprovisioned = "$diskprovisioned" / 1000000 )) # to GB | |
echo "$host,$osinfo,$cpumodel,$cpuspeed,$cpusockets,$corespersocket,$vendor,$memtotal GB,$diskconsumed GB,$diskprovisioned GB" >> "$csv" | |
(( hostprogress = "$hostprogress" + 1 )) | |
done | |
echo | |
column -s, -t < "$csv" | |
echo | |
if [ "$csv" != "$outfile" ]; then | |
rm "$csv" | |
fi | |
if [ "$outfile" != "" ]; then | |
echo "Output saved as CSV to: $outfile" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When running this in a vSphere VM, the hardware is reported as "VMware, Inc". This obviously causes problems with column alignment in a csv. Probably just need to strip out any commas from those fields.