-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesxi.config
97 lines (80 loc) · 3.28 KB
/
esxi.config
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
################################################################################
################################################################################
#
# Common code for ESXi support
#
################################################################################
################################################################################
###############################
# Define variables used later #
###############################
# user and host:
esxiuser=""
esxihost=""
# Record keeping:
totalvms=0
totalvmsshutdown=0
totalvmspowereddown=0
##########################################################
# Edit these settings to suit your needs and environment #
##########################################################
# Test flag: set to 1 to prevent the script from shutting VM guests down.
dryrun=0
# waitretries determines how many times the script will loop while attempting to power down a VM.
# waitdelay specifies how many seconds to sleep during each loop.
waitretries=30
waitdelay=6
# maxwait is the product of waitretries and waitdelay, i.e., the total number of seconds we will
# wait before gracelessly forcing the power off.
maxwait=$((waitretries*waitdelay))
# For tests, force the retry max count to 1
if [ $dryrun -ne 0 ]; then
waitretries=1
fi
################################################################################
#
# Function to shut down a guest virtual machine
#
################################################################################
shutdown_guest_vm()
{
l_try=0
ssh "${esxiuser}"@"${esxihost}" vim-cmd vmsvc/power.getstate $1 | grep -i "off" > /dev/null 2<&1
l_status=$?
# printf "VM [%s] status=[%s]\n" "$1" "${l_status}"
if [ $l_status -eq 0 ]; then
echo "Guest VM ID $1 already powered down..."
else
while [ $l_try -lt $waitretries ] && [ $l_status -ne 0 ]; do
l_try=$((l_try+1))
if [ $dryrun -ne 0 ]; then
echo "TEST MODE: Would issue shutdown command and wait ${waitdelay} seconds for guest VM ID $1 to shutdown (attempt $l_try of $waitretries)..."
else
ssh "${esxiuser}"@"${esxihost}" vim-cmd vmsvc/power.shutdown $1 > /dev/null 2<&1
echo "Waiting ${waitdelay} seconds for guest VM ID $1 to shutdown (attempt $l_try of $waitretries)..."
sleep $waitdelay
ssh "${esxiuser}"@"${esxihost}" vim-cmd vmsvc/power.getstate $1 | grep -i "off" > /dev/null 2<&1
l_status=$?
# printf "VM [%s] status=[%s] try=[%s]\n" "$1" "${l_status}" "${l_try}"
fi
done
if [ $l_status -eq 0 ]; then
echo "Shutdown sucessful on attempt ${l_try}..."
totalvmsshutdown=$((totalvmsshutdown + 1))
else
if [ $dryrun -ne 0 ]; then
echo "TEST MODE: Unable to gracefully shutdown guest VM ID $1, would force power off and wait ${waitdelay} seconds before checking status."
else
echo "Unable to gracefully shutdown guest VM ID $1... forcing power off."
ssh "${esxiuser}"@"${esxihost}" vim-cmd vmsvc/power.off $1 > /dev/null 2<&1
sleep $waitdelay
fi
ssh "${esxiuser}"@"${esxihost}" vim-cmd vmsvc/power.getstate $1 | grep -i "off" > /dev/null 2<&1
l_status=$?
# printf "VM [%s] status=[%s]\n" "$1" "${l_status}"
if [ $l_status -eq 0 ]; then
totalvmspowereddown=$((totalvmspowereddown + 1))
fi
fi
fi
}