JavaScript must be enabled in order for you to see "WP Copy Data Protect" effect. However, it seems JavaScript is either disabled or not supported by your browser. To see full result of "WP Copy Data Protector", enable JavaScript by changing your browser options, then try again.

Analyze the script for querying the information of VMs under the ESXi


Well, I haven’t posted a rubbish for a long time, and somebody push me need to post the article, so I can’t let him down. The topic of today is Analyzing the script for querying the information of VMs under the ESXi, and I think there have be some scripts for the same features on the Internet, but I still want to share how do we query the information about the VMs under the ESXi. Let’s keep reading:

1) Setup the exchange of keypair about the ssh between the remote client and ESXi’ server
#ssh-keygen -t rsa => Copy the public key to the /etc/ssh/keys-root/authorized_keys

2) Setup the variable of datetime and capture the info of VMs through vim-cmd vmsvc/getallvms
Tems=`date -d now "+%Y%m%d%H%M%S"`
echo "VMID:VMName:Datastore:VCPU:VMEM:VDisk:VNET:PWR" > Report_$1_$Tems
# $1 is IP/FQDN of VMware ESXi Host, $2 is the port of SSH for VMware ESXi Host
ssh $1 -p $2 vim-cmd vmsvc/getallvms | tail -n +2 &> getallvms

3) Transfer the special character from ‘[' and ' ' to ':' or '_'
# Sed's meaning: 1st-From ' [' to ':' before datastore, 2nd-From ' ' to ':' after vmid and 3rd-From ' ' to '_" about VMName
cat getallvms | awk -F\] ‘{ print $1 }’ | tr -s ‘ ‘ | sed ‘s/\ \[/:/' | sed 's/\ /:/' | sed 's/\ /_/g' &> report
# Can not solve the VMName that's included w/ [ and ].
# Solve the VNName that is [SM2] DNS_CentOS that will impact to catch the information of datastore
sed -i -e ‘/DNS_CentOS/ s/\[/+/' -e '/DNS_CentOS/ s/\]/+/’ getallvms

4) Produce the vmxpath through the result of getallvms
# Replace the [ and ] w/ ! and # for solving the problem that's included w/ [ and ].
# Sed's meaning: 1st-Add '/vmfs/volumes/' at the head per lines, 2nd-From '] ' to '/' before folder of VMName, 3rd-Add '.vmx' at the end per lines, 4th-From ' ' to '\ ' for escape, 5th-From '[' to '\[' for VMName, 6th-From ']' to '\]' for VMName, 7th-Add " at the head per lines and 8th-Add " at the end per lines
cat getallvms | sed 's/\[/\!/' | sed 's/\]/\#/' | awk -F\! '{ print $2 }' | awk -F\.vmx '{ print $1 }' | sed 's/^/\/vmfs\/volumes\//' | sed 's/\#\ /\//' | sed 's/$/\.vmx/' | sed 's/\ /\\\ /g' | sed 's/\[/\\\[/g' | sed 's/\]/\\\]/g' | sed 's/^/"/' | sed 's/$/"/' &> vmxpath
# Produce the file w/ the complete commands that are included ssh and others, but this's be skip.
#cat getallvms | sed 's/\[/\!/' | sed 's/\]/\#/' | awk -F\! '{ print $2 }' | awk -F\.vmx '{ print $1 }' | sed 's/^/\/vmfs\/volumes\//' | sed 's/\#\ /\//' | sed 's/$/\.vmx/' | sed 's/\ /\\\ /g' | sed 's/\[/\\\[/g' | sed 's/\]/\\\]/g' | sed 's/^/"/' | sed 's/$/"/' | sed "s/^/ssh\ $1\ -p\ $2\ cat\ /" | sed 's/$/\ \|\ grep\ numvcpus/' &> vmxpath

5) Compose the info about the CPU/Mem/VmxPath/Disk/VMName/IP and remove the temporary files
#IFS=':'
for (( c=1; c<=`cat vmxpath | wc -l`; c++ ))
do
VSP=`sed -n "$c"p vmxpath`
SCU=`ssh $1 -p $2 cat \"$VSP\" | grep -i numvcpus | wc -l`
# Because there is no string in the .vmx when vcpus is 1.
if [ $SCU -lt 1 ] ; then
VCPU='1'
else
# Sample:numvcpus = "2"
VCPU=`ssh $1 -p $2 cat \"$VSP\" | grep -i numvcpus | awk -F\" '{ print $2 }'`
fi
echo "$VCPU" >> vmxvcpu
done
for (( e=1; e<=`cat vmxpath | wc -l`; e++ ))
do
VSP=`sed -n "$e"p vmxpath`
VMEM=`ssh $1 -p $2 cat \"$VSP\" | grep -i memSize | awk -F\" '{ print $2 }'`
echo "$VMEM" >> vmxvmem
done
for (( f=1; f<=`cat vmxpath | wc -l`; f++ ))
do
VSP=`sed -n "$f"p vmxpath | awk -F\/ '{ print "/"$2"/"$3"/"$4"/"$5"/" }'`
VDISK=`ssh $1 -p $2 "cd $VSP && du -sh"`
VDISK=`echo $VDISK | awk '{ print $1 }'`
echo $VDISK >> vmxvdisk
done
for (( g=1; g<=`cat report | wc -l`; g++ ))
do
VSI=`sed -n "$g"p report | awk -F\: '{ print $1 }'`
VNET=`ssh $1 -p $2 vim-cmd vmsvc/get.guest $VSI | grep -i ipaddr | head -1 | awk -F\" '{ print $2 }'`
echo $VNET >> vmxvnet
done
for (( h=1; h<=`cat report | wc -l`; h++ ))
do
VSI=`sed -n "$h"p report | awk -F\: '{ print $1 }'`
VPWR=`ssh $1 -p $2 vim-cmd vmsvc/power.getstate $VSI | tail -1`
echo $VPWR >> vmxvpwr
done
for (( d=1; d<=`cat vmxpath | wc -l`; d++ ))
do
PSP=`sed -n "$d"p vmxvpwr`
NSP=`sed -n "$d"p vmxvnet`
DSP=`sed -n "$d"p vmxvdisk`
MSP=`sed -n "$d"p vmxvmem`
VSP=`sed -n "$d"p vmxvcpu`
RSP=`sed -n "$d"p report`
echo "$RSP:$VSP:$MSP:$DSP:$NSP:$PSP" >> Report_$1_$Tems
done
# Delete the all temp files
rm -rf vmxvcpu
rm -rf vmxvmem
rm -rf vmxvdisk
rm -rf vmxvnet
rm -rf vmxvpwr
rm -rf getallvms
rm -rf vmxpath
rm -rf report

6) Usage about the script for querying the VMs under the ESXi's Server
#./Script_Name.sh ESXi's IP SSH_Port => Will produce the file of Report_ESXi'sIP_Datetime

◎、Above information is about analyzing the script for querying the information of VMs under the ESXi, and you can add additional info about the VMs you want to query in this scripts. That's all for today, see you next time!

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 
This site is protected by WP-CopyRightPro