shell-计算虚拟机创建时间

时间:2023-03-09 03:22:15
shell-计算虚拟机创建时间

因为要验证虚拟机创建时间,所以写了下面一个脚本


#!/bin/bash
###########################################################################################
#
# This tool is used to test vm create time
#
########################################################################################### VM_NUM=$1
IMAGE_ID=$2 NET_ID=7baf14cb-d2db-4b52-9874-f120363ace34
FLAVOR_ID=bbc2bc57-42cd-484c-986d-82a5f0d83028
#Use the uuid as the vm name
VM_NAME=`cat /proc/sys/kernel/random/uuid` #create vm
nova boot --flavor $FLAVOR_ID --image $IMAGE_ID --nic net-id=$NET_ID --max-count $VM_NUM $VM_NAME #Record start time
starttime=`date +'%Y-%m-%d %H:%M:%S'`
start_seconds=$(date --date="$starttime" +%s); #Calculate the creation time for each vm
get_vm_time() { local endtime
local end_seconds
local vm_time
local status while :;
do
status=`nova show $1 | grep "OS-EXT-STS:vm_state" | awk '{print $4}'` case $status in
"active")
endtime=`date +'%Y-%m-%d %H:%M:%S'`
end_seconds=$(date --date="$endtime" +%s);
echo "create $1 used "$((end_seconds-start_seconds)) "s"
break
;;
"error")
echo "create $1 failed"
break
;;
esac done } #If you create only one vm ,The name of the virtual machine is : $VM_NAME
if [ $VM_NUM -eq 1 ];then
get_vm_time $VM_NAME >> $VM_NAME.txt & #If you create multiple vm.Statistics of vm creation time
else
for i in $(seq 1 ${VM_NUM});do
get_vm_time $VM_NAME-$i >> $VM_NAME.txt & done
fi