Community discussions

MikroTik App
 
kenkilaw
just joined
Topic Author
Posts: 2
Joined: Wed Feb 24, 2016 10:37 pm

KVM VM Watchdag and auto disable/enable

Sun Oct 04, 2020 2:05 pm

RouterOS Function
# Copyright (c) Kenkilaw
# Version 1.0
# Last update: 4/10/2020
# After detected VM host unable to ping count 4, auto disable/enable VM host and sends email notification

:global sendEMAIL do={

:local targetVMname $c
:local GUser "YYY@YYY.com"
:local GPass "PWPWPWPW"
#寄件人
:local MailFrom "YYY@YYY.com"

#收件人
:local MailTo "XXX@XXX.com"

/tool e-mail set address=smtp.XXXX.com port=587 start-tls=yes
/tool e-mail set user=$GUser
/tool e-mail set password=$GPass
/tool e-mail set from=$MailFrom

/tool e-mail send to=$MailTo subject=($targetVMname . " -- vm_watchdog rebooted " . [:pick [/system clock get date] 7 11] . [:pick [/system clock get date] 0 3] . [:pick [/system clock get date] 4 6]) body=($targetVMname . " -- vm_watchdog rebooted")


:log warning ("vm_watchdog $targetVMname emailed at " . [/system clock get time] . " " . [/system clock get date] . " ")

}


:global VMwatchdog do={
:local targetVM $a
:local targetPing $b
:global sendEMAIL

:if ([/ping $targetPing interval=1 count=4] =0) do={
:if ([/kvm get $targetVM state]="running") do={
/kvm disable $targetVM
delay 10
}

/kvm enable $targetVM
$sendEMAIL c=$targetVM
} else={
:log info ("vm_watchdog $targetVM Health Check okay")
}

#:return ($a . $b)
}

$VMwatchdog a=ubuntu b=192.168.5.226
$VMwatchdog a=openwrt b=192.168.5.253
$VMwatchdog a=openwrt2 b=192.168.5.155

Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot], Google [Bot], oskarsk and 25 guests