As VMware administrators, we often face the challenge of efficiently managing and monitoring our virtual infrastructure. While the vSphere Client provides a user-friendly interface for manual operations, there are compelling reasons to embrace the vSphere API for automation.
In this post, we'll explore why you should consider using the vSphere API and break down a practical script that helps you collect VM configuration data automatically.
What does the script in this blog post do? It will connect to the vSphere REST API and then collect the virtual machines and their configuration settings. Eventually the script exports the data to a csv file.
First, let's create our environment variables.
export VCENTER_SERVER='your-vcenter-server'
export VCENTER_USER='your-username'
export VCENTER_PASSWORD='your-password'
Now, here's our main script. Create a file called vsphere-monitor.sh
:
#!/bin/bash
# Exit on error
set -e
# Check if required environment variables are set
if [ -z "$VCENTER_SERVER" ] || [ -z "$VCENTER_USER" ] || [ -z "$VCENTER_PASSWORD" ]; then
echo "❌ Please set all required environment variables:"
echo " VCENTER_SERVER, VCENTER_USER, VCENTER_PASSWORD"
exit 1
fi
# Output file
OUTPUT_FILE="vsphere_config.csv"
# Function to handle errors
handle_error() {
echo "❌ Error: $1"
exit 1
}
# Function to cleanup session
cleanup() {
if [ ! -z "$SESSION_ID" ]; then
echo "Cleaning up session..."
curl -k -s -X DELETE \
-H "vmware-api-session-id: $SESSION_ID" \
"https://$VCENTER_SERVER/api/session" || true
fi
}
# Set trap for cleanup
trap cleanup EXIT
# Get session ID
echo "Authenticating with vSphere..."
SESSION_ID=$(curl -k -s -X POST \
-u "${VCENTER_USER}:${VCENTER_PASSWORD}" \
"https://$VCENTER_SERVER/api/session" \
-H 'Accept: application/json' \
| tr -d '"') || handle_error "Failed to authenticate"
if [ -z "$SESSION_ID" ]; then
handle_error "Failed to get session ID"
fi
echo "✅ Successfully connected to vSphere"
# Initialize CSV file
echo "timestamp,vm_name,power_state,cpu_count,memory_size_mb,disk_capacity_gb" > "$OUTPUT_FILE"
# Get list of VMs
echo "Fetching VM list..."
VM_LIST=$(curl -k -s \
-H "vmware-api-session-id: $SESSION_ID" \
"https://$VCENTER_SERVER/api/vcenter/vm" \
-H 'Accept: application/json') || handle_error "Failed to get VM list"
# Process each VM
echo "$VM_LIST" | jq -r '.[] | @base64' | while read -r vm_info; do
# Decode VM info
VM_ID=$(echo "$vm_info" | base64 -d | jq -r '.vm')
VM_NAME=$(echo "$vm_info" | base64 -d | jq -r '.name')
POWER_STATE=$(echo "$vm_info" | base64 -d | jq -r '.power_state')
echo "Processing VM: $VM_NAME"
# Get VM details
VM_DETAILS=$(curl -k -s \
-H "vmware-api-session-id: $SESSION_ID" \
"https://$VCENTER_SERVER/api/vcenter/vm/$VM_ID" \
-H 'Accept: application/json') || continue
# Extract configuration details
CPU_COUNT=$(echo "$VM_DETAILS" | jq -r '.cpu.count')
MEMORY_MB=$(echo "$VM_DETAILS" | jq -r '.memory.size_MiB')
# Calculate total disk capacity
DISK_GB=$(echo "$VM_DETAILS" | jq -r '
[.disks | to_entries[] | .value.capacity] |
add / (1024 * 1024 * 1024) |
round |
tostring
')
# Get current timestamp
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
# Write to CSV
echo "$TIMESTAMP,$VM_NAME,$POWER_STATE,$CPU_COUNT,$MEMORY_MB,$DISK_GB" >> "$OUTPUT_FILE"
echo "✅ Collected configuration for VM: $VM_NAME"
done
echo "✅ Data has been written to $OUTPUT_FILE"
Let's break down a practical example: a Bash script that uses the vSphere API to collect configuration details from all VMs and export them to a CSV file.
if [ -z "$VCENTER_SERVER" ] || [ -z "$VCENTER_USER" ] || [ -z "$VCENTER_PASSWORD" ]; then
echo "❌ Please set all required environment variables:"
echo " VCENTER_SERVER, VCENTER_USER, VCENTER_PASSWORD"
exit 1
fi
The script uses environment variables for sensitive credentials, following security best practices by keeping credentials out of the code.
-u "${VCENTER_USER}:${VCENTER_PASSWORD}" \
"https://$VCENTER_SERVER/api/session" \
-H 'Accept: application/json' \
| tr -d '"')
The script establishes a session with the vSphere API, storing the session ID for subsequent requests. It includes proper cleanup through a trap mechanism to ensure sessions are terminated properly.
The script collects essential VM information including:
handle_error() {
echo "❌ Error: $1"
exit 1
}
Robust error handling ensures the script fails gracefully when encountering issues, making it suitable for production environments.
The script generates a CSV file with timestamps, making it perfect for:
export VCENTER_USER="administrator@vsphere.local"
export VCENTER_PASSWORD="your-password"
./vm_config_collector.sh
The script will create a CSV file containing configuration details for all VMs in your environment.
When working with the vSphere API:
This script provides a quick and efficient way to inventory your vSphere environment using just bash and common Unix tools. It's particularly useful for:
You can extend this script to collect additional metrics or modify the output format to suit your needs.