Iโll teach you how to monitor virtual machines on Alpine Linux! This helps you keep track of VM performance, resource usage, and spot problems early. Essential for running VMs smoothly!
๐ค What is VM Monitoring?
VM monitoring tracks how your virtual machines are performing. Itโs like having dashboards for each VM showing CPU, memory, disk, and network usage. You can catch issues before they become big problems!
Why monitor VMs?
- Track resource usage
- Prevent performance issues
- Spot problems early
- Plan capacity better
- Ensure VM health
๐ฏ What You Need
Before we start, youโll need:
- Alpine Linux host system
- Running virtual machines
- Root or sudo access
- Basic terminal knowledge
- About 15 minutes
๐ Step 1: Install Monitoring Tools
First, letโs install essential monitoring tools:
# Update packages
apk update
# Install basic monitoring tools
apk add htop iotop nethogs
# Install VM-specific tools
apk add libvirt-client virt-top
# Install data collection tools
apk add collectd collectd-disk collectd-cpu
# For graphing (optional)
apk add rrdtool
Check if tools are working:
# Test virt-top
virt-top --version
# List VMs (if using libvirt)
virsh list --all
๐ Step 2: Monitor with virt-top
Use virt-top for real-time VM monitoring:
# Start virt-top
virt-top
# Useful keys in virt-top:
# 1 - Show CPU stats
# 2 - Show memory stats
# 3 - Show disk stats
# 4 - Show network stats
# s - Set update delay
# q - Quit
Create an alias for common view:
# Add to ~/.profile
alias vmtop='virt-top -d 2 --batch'
๐ Step 3: Create VM Dashboard Script
Build a custom VM monitoring dashboard:
# Create VM monitor script
cat > /usr/local/bin/vm-monitor << 'EOF'
#!/bin/sh
# VM Monitoring Dashboard
clear
echo "๐ฅ๏ธ Virtual Machine Monitor"
echo "=========================="
date
echo ""
# Check if libvirt is available
if command -v virsh >/dev/null 2>&1; then
echo "๐ VM Status:"
virsh list --all | tail -n +3 | head -n -1
echo ""
# For each running VM
for vm in $(virsh list --name); do
if [ -n "$vm" ]; then
echo "๐ธ VM: $vm"
# CPU info
CPU=$(virsh dominfo "$vm" | grep "CPU(s)" | awk '{print $2}')
echo " CPU cores: $CPU"
# Memory info
MEM=$(virsh dominfo "$vm" | grep "Used memory" | awk '{print $3/1024 " MB"}')
echo " Memory: $MEM"
# Disk usage
DISK=$(virsh domblklist "$vm" | grep -v "Target" | head -1 | awk '{print $2}')
if [ -f "$DISK" ]; then
DISK_SIZE=$(du -h "$DISK" | awk '{print $1}')
echo " Disk: $DISK_SIZE"
fi
echo ""
fi
done
fi
# System resources
echo "๐ Host System Resources:"
echo " CPU Load: $(uptime | awk -F'load average:' '{print $2}')"
echo " Memory Free: $(free -h | grep Mem | awk '{print $4}')"
echo " Disk Free: $(df -h / | tail -1 | awk '{print $4}')"
echo ""
echo "Press Ctrl+C to exit"
EOF
chmod +x /usr/local/bin/vm-monitor
๐ Step 4: Set Up Continuous Monitoring
Create a monitoring service:
# Create monitoring script
cat > /usr/local/bin/vm-collect-stats << 'EOF'
#!/bin/sh
# Collect VM statistics
LOG_DIR="/var/log/vm-stats"
mkdir -p "$LOG_DIR"
while true; do
TIMESTAMP=$(date +"%Y-%m-%d %H:%M:%S")
# Collect stats for each VM
for vm in $(virsh list --name); do
if [ -n "$vm" ]; then
LOG_FILE="$LOG_DIR/${vm}.log"
# Get CPU percentage
CPU_TIME=$(virsh domstats "$vm" --cpu-total | grep cpu.time | cut -d'=' -f2)
# Get memory stats
MEM_STATS=$(virsh dommemstat "$vm" | grep actual | awk '{print $2}')
# Get disk stats
DISK_STATS=$(virsh domblkstat "$vm" vda 2>/dev/null | grep rd_bytes | awk '{print $3}')
# Log the stats
echo "$TIMESTAMP,$CPU_TIME,$MEM_STATS,$DISK_STATS" >> "$LOG_FILE"
fi
done
sleep 60
done
EOF
chmod +x /usr/local/bin/vm-collect-stats
# Create service file
cat > /etc/init.d/vm-monitor << 'EOF'
#!/sbin/openrc-run
name="VM Monitor"
command="/usr/local/bin/vm-collect-stats"
command_background=true
pidfile="/run/${RC_SVCNAME}.pid"
depend() {
need libvirtd
}
EOF
chmod +x /etc/init.d/vm-monitor
๐ Step 5: Create Alerts
Set up basic alerting:
# Create alert script
cat > /usr/local/bin/vm-alerts << 'EOF'
#!/bin/sh
# VM Alert System
check_vm_resources() {
for vm in $(virsh list --name); do
if [ -n "$vm" ]; then
# Check CPU usage
CPU_PERCENT=$(virt-top -n 1 --batch | grep "$vm" | awk '{print $7}' | tr -d '%')
if [ "${CPU_PERCENT:-0}" -gt 80 ]; then
echo "โ ๏ธ WARNING: $vm CPU usage is ${CPU_PERCENT}%"
fi
# Check memory
MEM_USED=$(virsh dommemstat "$vm" | grep actual | awk '{print $2}')
MEM_TOTAL=$(virsh dominfo "$vm" | grep "Max memory" | awk '{print $3}')
MEM_PERCENT=$((MEM_USED * 100 / MEM_TOTAL))
if [ "$MEM_PERCENT" -gt 90 ]; then
echo "โ ๏ธ WARNING: $vm memory usage is ${MEM_PERCENT}%"
fi
fi
done
# Check host resources
HOST_CPU=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | tr -d '%us,')
if [ "${HOST_CPU:-0}" -gt 85 ]; then
echo "โ ๏ธ WARNING: Host CPU usage is high!"
fi
}
# Run checks
check_vm_resources
EOF
chmod +x /usr/local/bin/vm-alerts
# Add to cron
echo "*/5 * * * * /usr/local/bin/vm-alerts >> /var/log/vm-alerts.log 2>&1" | crontab -
๐ Step 6: Visualize VM Stats
Create simple visualization:
# Create stats viewer
cat > /usr/local/bin/vm-stats-view << 'EOF'
#!/bin/sh
# View VM statistics
VM="$1"
if [ -z "$VM" ]; then
echo "Usage: $0 <vm-name>"
echo "Available VMs:"
virsh list --name
exit 1
fi
LOG_FILE="/var/log/vm-stats/${VM}.log"
if [ ! -f "$LOG_FILE" ]; then
echo "No stats found for VM: $VM"
exit 1
fi
echo "๐ Statistics for VM: $VM"
echo "========================"
# Show last 10 entries
echo "Time CPU Memory Disk"
echo "---- --- ------ ----"
tail -10 "$LOG_FILE" | while IFS=',' read timestamp cpu mem disk; do
printf "%-20s %8s %8s %8s\n" "$timestamp" "$cpu" "$mem" "$disk"
done
# Simple graph
echo ""
echo "๐ CPU Usage Trend (last hour):"
tail -60 "$LOG_FILE" | awk -F',' '{print $2}' | spark
EOF
chmod +x /usr/local/bin/vm-stats-view
๐ฎ Practice Exercise
Try monitoring your VMs:
- Start monitoring dashboard
- Create some VM load
- Watch resources change
- Check alerts
# Start dashboard
vm-monitor
# In another terminal, create load in VM
virsh console your-vm
# Inside VM: stress --cpu 2 --timeout 30
# Check alerts
vm-alerts
# View statistics
vm-stats-view your-vm
๐จ Troubleshooting Common Issues
Canโt Connect to VMs
If monitoring fails:
# Check libvirt service
rc-service libvirtd status
# Start if needed
rc-service libvirtd start
# Check VM state
virsh list --all
High Resource Usage
VM using too many resources?
# Limit VM CPU
virsh schedinfo vm-name --set vcpu_quota=50000
# Limit memory
virsh setmem vm-name 512M --live
# Check limits
virsh dominfo vm-name
Missing Statistics
No stats being collected?
# Check if service is running
ps aux | grep vm-collect
# Check logs
tail -f /var/log/vm-stats/*.log
# Restart monitoring
rc-service vm-monitor restart
๐ก Pro Tips
Tip 1: Resource Templates
Create VM templates with limits:
# Set default resources
cat > /etc/libvirt/vm-defaults.xml << EOF
<memory unit='GiB'>2</memory>
<currentMemory unit='GiB'>2</currentMemory>
<vcpu>2</vcpu>
<cpu mode='host-passthrough'/>
EOF
Tip 2: Batch Monitoring
Monitor multiple VMs efficiently:
# Check all VMs at once
for vm in $(virsh list --name); do
echo "=== $vm ==="
virsh domstats "$vm"
done
Tip 3: Historical Data
Keep monitoring history:
# Rotate logs monthly
cat > /etc/logrotate.d/vm-stats << EOF
/var/log/vm-stats/*.log {
monthly
rotate 12
compress
missingok
notifempty
}
EOF
โ Verification Steps
Letโs verify monitoring works:
# Check monitoring tools
which virt-top vm-monitor
# Test dashboard
vm-monitor
# Verify stats collection
ls -la /var/log/vm-stats/
# Check alerts
tail /var/log/vm-alerts.log
๐ What You Learned
Excellent work! You can now:
- โ Install VM monitoring tools
- โ Use virt-top for real-time stats
- โ Create monitoring dashboards
- โ Set up alerts
- โ Track VM performance
Your VMs are now properly monitored!
๐ฏ Whatโs Next?
Now that you can monitor VMs, explore:
- Setting up Prometheus monitoring
- Creating Grafana dashboards
- Implementing auto-scaling
- Advanced performance tuning
Remember, good monitoring prevents problems before they happen. Iโve saved many VMs from crashing by catching issues early! Keep those dashboards running.
Happy monitoring! ๐