add feature reconnect to esxi

This commit is contained in:
tursom 2023-08-07 15:11:15 +08:00
parent 984e306b04
commit dd6d55cca3

View File

@ -68,46 +68,52 @@ func doWatch(watches []*watchdog) {
fmt.Println("in ping tick")
for _, w := range watches {
pinger, err := ping.NewPinger(w.addr)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "ping to target failed: %s\n", err)
continue
}
pinger.Timeout = time.Second * 15
pinger.Count = 4
fmt.Printf("start ping to %s\n", w.addr)
err = pinger.Run()
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "ping to target failed: %s\n", err)
continue
}
fmt.Printf("finished ping to %s\n", w.addr)
statistics := pinger.Statistics()
if statistics.PacketsRecv != 0 {
w.life = 3
continue
}
w.life--
fmt.Printf("ping vm %s(%s) failed, left %d life", w.addr, w.vm.Name(), w.life)
if w.life > 0 {
continue
}
fmt.Printf("restarting vm %s(%s)", w.addr, w.vm.Name())
if err := reset(w.vm); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "restart vm failed: %s\n", err)
}
doWatch0(w)
}
}
}
func doWatch0(w *watchdog) bool {
pinger, err := ping.NewPinger(w.addr)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "ping to target failed: %s\n", err)
return false
}
pinger.Timeout = time.Second * 15
pinger.Count = 4
fmt.Printf("start ping to %s\n", w.addr)
err = pinger.Run()
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "ping to target failed: %s\n", err)
return false
}
fmt.Printf("finished ping to %s\n", w.addr)
statistics := pinger.Statistics()
if statistics.PacketsRecv != 0 {
w.life = 3
return false
}
w.life--
fmt.Printf("ping vm %s(%s) failed, left %d life", w.addr, w.vm.Name(), w.life)
if w.life > 0 {
return false
}
fmt.Printf("restarting vm %s(%s)", w.addr, w.vm.Name())
if err := reset(w.vm); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "restart vm failed: %s\n", err)
return true
}
return false
}
func reset(vm *vmomi.VirtualMachine) error {
task, err := vm.Reset()
if err != nil {