summaryrefslogtreecommitdiff
path: root/others/vm3/compute/ls.sh
diff options
context:
space:
mode:
Diffstat (limited to 'others/vm3/compute/ls.sh')
-rwxr-xr-xothers/vm3/compute/ls.sh45
1 files changed, 45 insertions, 0 deletions
diff --git a/others/vm3/compute/ls.sh b/others/vm3/compute/ls.sh
new file mode 100755
index 0000000..b62ef14
--- /dev/null
+++ b/others/vm3/compute/ls.sh
@@ -0,0 +1,45 @@
1
2#!/bin/bash
3printf "%-10s %-15s %-8s %-6s %-8s %-12s %-10s\n" "Network" "IP" "State" "vCPUs" "RAM(GB)" "Disk(GB)" "Name"
4printf "%-10s %-15s %-8s %-6s %-8s %-12s %-10s\n" "----------" "---------------" "--------" "-----" "-------" "-----------" "----------"
5
6# Get all VMs
7vms=$(sudo virsh list --name --all)
8
9# Cache the network leases once
10default_leases=$(sudo virsh net-dhcp-leases default 2>/dev/null)
11
12for vm in $vms; do
13 # Get XML once and use it multiple times
14 xml=$(sudo virsh dumpxml "$vm" 2>/dev/null)
15
16 # Extract all data from the cached XML
17 mac=$(echo "$xml" | grep "mac address" | awk -F\' '{ print $2}')
18 net=$(echo "$xml" | grep "<source network" | awk -F\' '{print $2}')
19 if [ -z "$net" ]; then
20 net="default"
21 fi
22
23 # Use cached leases
24 ip=$(echo "$default_leases" | grep "$mac" | awk '{print $5}' | cut -f1 -d'/')
25
26 # Run commands in background and save to temp files
27 sudo virsh domstate "$vm" 2>/dev/null > /tmp/state.$$ &
28 echo "$xml" | grep "<vcpu" | awk -F'[<>]' '{print $3}' > /tmp/vcpus.$$ &
29 echo "$xml" | grep "<memory" | awk -F'[<>]' '{print $3}' | awk '{ printf "%.2f", $1/1048576 }' > /tmp/ram.$$ &
30 sudo du -sk "/var/lib/libvirt/images/${vm}/${vm}.qcow2" 2>/dev/null | awk '{ printf "%.2f", $1/1024/1024 }' > /tmp/disk.$$ &
31
32 wait
33
34 # Read from temp files
35 state=$(cat /tmp/state.$$ 2>/dev/null)
36 vcpus=$(cat /tmp/vcpus.$$ 2>/dev/null)
37 ram=$(cat /tmp/ram.$$ 2>/dev/null)
38 disk=$(cat /tmp/disk.$$ 2>/dev/null)
39
40 # Clean up temp files
41 rm -f /tmp/state.$$ /tmp/vcpus.$$ /tmp/ram.$$ /tmp/disk.$$
42
43 printf "%-10s %-15s %-8s %-6s %-8s %-12s %-10s\n" \
44 "$net" "$ip" "$state" "$vcpus" "$ram" "$disk" "$vm"
45done