I have shutdown script for each ESXI when the UPS is on battery. I need to make this script better to read "stopDelay" which can be set in ESXI host UI. This value can be found in console with command vim-cmd. I need script which will return "stopDelay" for desired VM ID as input param.
e.g.:
[root@localhost:~] vim-cmd hostsvc/autostartmanager/get_autostartseq
(vim.host.AutoStartManager.AutoPowerInfo) [
(vim.host.AutoStartManager.AutoPowerInfo) {
key = 'vim.VirtualMachine:1',
startOrder = 1,
startDelay = 10,
waitForHeartbeat = "systemDefault",
startAction = "powerOn",
stopDelay = 13,
stopAction = "systemDefault"
},
(vim.host.AutoStartManager.AutoPowerInfo) {
key = 'vim.VirtualMachine:2',
startOrder = 2,
startDelay = -1,
waitForHeartbeat = "systemDefault",
startAction = "powerOn",
stopDelay = 5,
stopAction = "systemDefault"
},
(vim.host.AutoStartManager.AutoPowerInfo) {
key = 'vim.VirtualMachine:3',
startOrder = 3,
startDelay = -1,
waitForHeartbeat = "systemDefault",
startAction = "powerOn",
stopDelay = -1,
stopAction = "systemDefault"
}
]
How to parse this output to get VM ID and its stopDelay value? e.q.:
./getVMStopDelay 1 => 13
./getVMStopDelay 2 => 5
./getVMStopDelay 3 => -1
I would harness GNU
AWKfor this task following way, letfile.txtcontent bethen
gives output
Explanation: I use
indexfunction to detect line where(vim.host.AutoStartManager.AutoPowerInfo)is indented, i.e. start is after 1st character, if it is so I do increasecntvalue by 1. When I find line withstopDelayI replace,using empty string i.e. delete it and print in format compliant with your requirements using value ofcntand content of last field ($NF).(tested in GNU Awk 5.1.0)