:if ([/routing bgp peer get [find name="xxx"] state] != "established") do={ .. send email here...}
Thank you for your reply! Highly appreciated!add scheduled scriptCode: Select all:if ([/routing bgp peer get [find name="xxx"] state] != "established") do={ .. send email here...}
add scheduled scriptCode: Select all:if ([/routing bgp peer get [find name="xxx"] state] != "established") do={ .. send email here...}
Script works for me fine. If you do not receive email then check your email settings. It may require username and password or starttls option.
###################################################################
# BGP PEERS STATUS MAIL ALERT SCRIPT (for RouterOS v6 only) #
# Original Author: Cha0s, circa 2015, with improvements by JB172 #
# Info: https://forum.mikrotik.com/viewtopic.php?p=494204#p494204 #
###################################################################
# EDIT HERE
:local arrBgpPeers [:toarray "peer1,peer2,peer3"];
:local arrEmails [:toarray "email@example.net,email2@example.net"];
:local debug true
# EDIT END
# Define Variables
:global arrBgpState;
:global gotKey false;
:local hostNameX ([/system identity get name]);
:local statusX;
:local peerX;
:local peerXarrEl;
:local mailToX;
:local mailSubjectX;
:local mailBodyX;
:local peerStatusX;
if ($debug = true) do={
:log info ("=============BGP PEER STATUS DETECTION STARTED=============" )
}
# Initialize global array - kinda lame way to do it :P
:if ( [:len $arrBgpState] =0 ) do={
:set $arrBgpState {"false"="false"}
}
# Loop through the peers array
:local arrPos
:for arrPos from=0 to=([:len $arrBgpPeers]-1) do={
# Set peerX to current peer name for this iteration
:set peerX [:pick $arrBgpPeers $arrPos];
# Check if peer is enabled and proceed
:if ([/routing bgp peer get [find name=$peerX] disabled ] != true) do={
# Get Peer Status
:set peerStatusX [/routing bgp peer get [find name="$peerX"] state]
# Find peer key in global array
:foreach k,v in=$arrBgpState do={
if ($k != "false") do={
:if ($gotKey = false) do={
:if ($k = $peerX) do={
:set gotKey true
}
}
}
}
# Initialize arrBgpState array element with peerX name if it doesn't already exist
:if ($gotKey = false) do={
:set ($arrBgpState->"$peerX") "up"
}
# Reset this for the next iteration
:set gotKey false
# Previous run peer status
:set statusX ($arrBgpState->"$peerX")
# Prepare Email body
:set mailBodyX ("Router Hostname: " . $hostNameX . "\nBGP Peer Status: " . $peerStatusX . "\nBGP Peer Name: " . $peerX . "\n");
# Check if BGP Peer is not established
:if ($peerStatusX != "established") do={
# Check if this is the first time the peer is doen
:if ( $statusX = "up" ) do={
# Set value to 'down' to peer global var key
:set ($arrBgpState->$peerX) "down"
# Informational Log
if ($debug = true) do={
:log error ("BGP Peer ". $peerX ." state is " . $peerStatusX . ". Sending email alerts!" )
}
# Prepare Email subject
:set mailSubjectX ("BGP Peer ". $peerX ." on ". $hostNameX . " is NOT established!");
# Loop over emails array to send emails to all recipients in array
:local arrPos2
:for arrPos2 from=0 to=([:len $arrEmails]-1) do={
# Set mailToX to recipient for this iteration
:set mailToX [:pick $arrEmails $arrPos2]
# Informational Log
:log error ("BGP Peer " . $peerX . " status is " .$peerStatusX. "! Sending Email alert to " . $mailToX . "." )
# Send Email
/tool e-mail send to=$mailToX subject=$mailSubjectX body=$mailBodyX;
}
} else={
# Peer down. Already sent notification so do nothing.
if ($debug = true) do={
:log info ("BGP Peer ". $peerX ." is already down. Ignoring!")
}
}
} else={
# Check if peer just came back up and send informational email
if ($statusX = "down") do={
# Prepare Email subject
:set mailSubjectX ("BGP Peer ". $peerX ." on ". $hostNameX . " has recovered!");
# Loop over emails array to send emails to all recipients in array
:local arrPos2
:for arrPos2 from=0 to=([:len $arrEmails]-1) do={
# Set mailToX to recepient for this iteration
:set mailToX [:pick $arrEmails $arrPos2]
# Informational Log
:log warning ("BGP Peer " . $peerX . " status has recovered! Sending informational Email to " . $mailToX . "." )
# Send Email
/tool e-mail send to=$mailToX subject=$mailSubjectX body=$mailBodyX;
}
}
if ($debug = true) do={
:log info ("BGP Peer " . $peerX . " is up. Nothing to do!")
}
# Set peer status to up
:set ($arrBgpState->$peerX) "up"
}
} else={
if ($debug = true) do={
:log warning ("BGP Peer " . $peerX . " is disabled. Ignoring!")
}
}
}
if ($debug = true) do={
:log info ("=============BGP PEER STATUS DETECTION ENDED===============" )
}
###################################################################
# BGP PEERS STATUS MAIL ALERT SCRIPT (for RouterOS v6 only) #
# Original Author: Cha0s, circa 2015, with improvements by JB172 #
# Info: https://forum.mikrotik.com/viewtopic.php?p=494204#p494204 #
###################################################################
# EDIT HERE
:local arrEmails [:toarray "email@example.net,email2@example.net"];
:local debug true
# EDIT END
# Define Variables
:global arrBgpState;
:global gotKey false;
:local hostNameX ([/system identity get name]);
:local statusX;
:local peerX;
:local peerXarrEl;
:local mailToX;
:local mailSubjectX;
:local mailBodyX;
:local peerStatusX;
:local arrBgpPeers [:toarray ""];
:local peername "";
:local peerTotal "";
if ($debug = true) do={
:log info ("=============BGP PEER STATUS DETECTION STARTED=============" )
}
# Initialize global array - kinda lame way to do it :P
:if ( [:len $arrBgpState] =0 ) do={
:set $arrBgpState {"false"="false"}
}
# Fill arrBgpPeers array with peers name
:foreach i in=[/routing bgp peer find] do={
:set peername [/routing bgp peer get $i name];
:set peerTotal ($peerTotal . "," .$peername);
}
:set arrBgpPeers [:toarray $peerTotal];
# Loop through the peers array
:local arrPos
:for arrPos from=0 to=([:len $arrBgpPeers]-1) do={
# Set peerX to current peer name for this iteration
:set peerX [:pick $arrBgpPeers $arrPos];
# Check if peer is enabled and proceed
:if ([/routing bgp peer get [find name=$peerX] disabled ] != true) do={
# Get Peer Status
:set peerStatusX [/routing bgp peer get [find name="$peerX"] state]
# Find peer key in global array
:foreach k,v in=$arrBgpState do={
if ($k != "false") do={
:if ($gotKey = false) do={
:if ($k = $peerX) do={
:set gotKey true
}
}
}
}
# Initialize arrBgpState array element with peerX name if it doesn't already exist
:if ($gotKey = false) do={
:set ($arrBgpState->"$peerX") "up"
}
# Reset this for the next iteration
:set gotKey false
# Previous run peer status
:set statusX ($arrBgpState->"$peerX")
# Prepare Email body
:set mailBodyX ("Router Hostname: " . $hostNameX . "\nBGP Peer Status: " . $peerStatusX . "\nBGP Peer Name: " . $peerX . "\n");
# Check if BGP Peer is not established
:if ($peerStatusX != "established") do={
# Check if this is the first time the peer is doen
:if ( $statusX = "up" ) do={
# Set value to 'down' to peer global var key
:set ($arrBgpState->$peerX) "down"
# Informational Log
if ($debug = true) do={
:log error ("BGP Peer ". $peerX ." state is " . $peerStatusX . ". Sending email alerts!" )
}
# Prepare Email subject
:set mailSubjectX ("BGP Peer ". $peerX ." on ". $hostNameX . " is NOT established!");
# Loop over emails array to send emails to all recipients in array
:local arrPos2
:for arrPos2 from=0 to=([:len $arrEmails]-1) do={
# Set mailToX to recipient for this iteration
:set mailToX [:pick $arrEmails $arrPos2]
# Informational Log
:log error ("BGP Peer " . $peerX . " status is " .$peerStatusX. "! Sending Email alert to " . $mailToX . "." )
# Send Email
/tool e-mail send to=$mailToX subject=$mailSubjectX body=$mailBodyX;
}
} else={
# Peer down. Already sent notification so do nothing.
if ($debug = true) do={
:log info ("BGP Peer ". $peerX ." is already down. Ignoring!")
}
}
} else={
# Check if peer just came back up and send informational email
if ($statusX = "down") do={
# Prepare Email subject
:set mailSubjectX ("BGP Peer ". $peerX ." on ". $hostNameX . " has recovered!");
# Loop over emails array to send emails to all recipients in array
:local arrPos2
:for arrPos2 from=0 to=([:len $arrEmails]-1) do={
# Set mailToX to recepient for this iteration
:set mailToX [:pick $arrEmails $arrPos2]
# Informational Log
:log warning ("BGP Peer " . $peerX . " status has recovered! Sending informational Email to " . $mailToX . "." )
# Send Email
/tool e-mail send to=$mailToX subject=$mailSubjectX body=$mailBodyX;
}
}
if ($debug = true) do={
:log info ("BGP Peer " . $peerX . " is up. Nothing to do!")
}
# Set peer status to up
:set ($arrBgpState->$peerX) "up"
}
} else={
if ($debug = true) do={
:log warning ("BGP Peer " . $peerX . " is disabled. Ignoring!")
}
}
}
if ($debug = true) do={
:log info ("=============BGP PEER STATUS DETECTION ENDED===============" )
}
Hi,Hello,
I don't use v7 yet since it's far from production ready (especially when it comes to BGP/BFD), so I haven't tested the script on it.
You mean in v6, right? The peer gets stuck in 'opensent' status?I'm using this script and I'm happy with job it's doing.
I have a BGP session that turn down from time to time and I have to disable it manualy and then enable to establish it again. I wonder how to do a small script upgrade so it would do that automaticly?
I would be very greatful for an example.
/routing bgp peer {
:foreach peer in [find state="opensent"] do={
:log warning "Restart stuck BGP Peer: $([get $peer name])"
disable $peer
enable $peer
}
}
###################################################################
# BGP PEERS STATUS MAIL ALERT SCRIPT (for RouterOS v6 only) #
# Original Author: Cha0s, circa 2015, with improvements by JB172 #
# Info: https://forum.mikrotik.com/viewtopic.php?p=494204#p494204 #
###################################################################
# EDIT HERE
:local arrEmails [:toarray "email@example.net,email2@example.net"];
:local debug true
# EDIT END
# Define Variables
:global arrBgpState;
:global gotKey false;
:local hostNameX ([/system identity get name]);
:local statusX;
:local peerX;
:local peerXarrEl;
:local mailToX;
:local mailSubjectX;
:local mailBodyX;
:local peerStatusX;
:local arrBgpPeers [:toarray ""];
:local peername "";
:local peerTotal "";
if ($debug = true) do={
:log info ("=============BGP PEER STATUS DETECTION STARTED=============" )
}
# Initialize global array - kinda lame way to do it :P
:if ( [:len $arrBgpState] =0 ) do={
:set $arrBgpState {"false"="false"}
}
# Fill arrBgpPeers array with peers name
:foreach i in=[/routing bgp peer find] do={
:set peername [/routing bgp peer get $i name];
:set peerTotal ($peerTotal . "," .$peername);
}
:set arrBgpPeers [:toarray $peerTotal];
# Loop through the peers array
:local arrPos
:for arrPos from=0 to=([:len $arrBgpPeers]-1) do={
# Set peerX to current peer name for this iteration
:set peerX [:pick $arrBgpPeers $arrPos];
# Check if peer is enabled and proceed
:if ([/routing bgp peer get [find name=$peerX] disabled ] != true) do={
# Get Peer Status
:set peerStatusX [/routing bgp peer get [find name="$peerX"] state]
# Find peer key in global array
:foreach k,v in=$arrBgpState do={
if ($k != "false") do={
:if ($gotKey = false) do={
:if ($k = $peerX) do={
:set gotKey true
}
}
}
}
# Initialize arrBgpState array element with peerX name if it doesn't already exist
:if ($gotKey = false) do={
:set ($arrBgpState->"$peerX") "up"
}
# Reset this for the next iteration
:set gotKey false
# Previous run peer status
:set statusX ($arrBgpState->"$peerX")
# Prepare Email body
:set mailBodyX ("Router Hostname: " . $hostNameX . "\nBGP Peer Status: " . $peerStatusX . "\nBGP Peer Name: " . $peerX . "\n");
# Check if BGP Peer is not established
:if ($peerStatusX != "established") do={
# Check if peer is stuck in opensent status an disable/enable to get it unstuck
if ($peerStatusX = "opensent") do={
:log warning "Restart stuck BGP Peer: $peerX"
/routing bgp peer disable $peerX
/routing bgp peer enable $peerX
}
# Check if this is the first time the peer is doen
:if ( $statusX = "up" ) do={
# Set value to 'down' to peer global var key
:set ($arrBgpState->$peerX) "down"
# Informational Log
if ($debug = true) do={
:log error ("BGP Peer ". $peerX ." state is " . $peerStatusX . ". Sending email alerts!" )
}
# Prepare Email subject
:set mailSubjectX ("BGP Peer ". $peerX ." on ". $hostNameX . " is NOT established!");
# Loop over emails array to send emails to all recipients in array
:local arrPos2
:for arrPos2 from=0 to=([:len $arrEmails]-1) do={
# Set mailToX to recipient for this iteration
:set mailToX [:pick $arrEmails $arrPos2]
# Informational Log
:log error ("BGP Peer " . $peerX . " status is " .$peerStatusX. "! Sending Email alert to " . $mailToX . "." )
# Send Email
/tool e-mail send to=$mailToX subject=$mailSubjectX body=$mailBodyX;
}
} else={
# Peer down. Already sent notification so do nothing.
if ($debug = true) do={
:log info ("BGP Peer ". $peerX ." is already down. Ignoring!")
}
}
} else={
# Check if peer just came back up and send informational email
if ($statusX = "down") do={
# Prepare Email subject
:set mailSubjectX ("BGP Peer ". $peerX ." on ". $hostNameX . " has recovered!");
# Loop over emails array to send emails to all recipients in array
:local arrPos2
:for arrPos2 from=0 to=([:len $arrEmails]-1) do={
# Set mailToX to recepient for this iteration
:set mailToX [:pick $arrEmails $arrPos2]
# Informational Log
:log warning ("BGP Peer " . $peerX . " status has recovered! Sending informational Email to " . $mailToX . "." )
# Send Email
/tool e-mail send to=$mailToX subject=$mailSubjectX body=$mailBodyX;
}
}
if ($debug = true) do={
:log info ("BGP Peer " . $peerX . " is up. Nothing to do!")
}
# Set peer status to up
:set ($arrBgpState->$peerX) "up"
}
} else={
if ($debug = true) do={
:log warning ("BGP Peer " . $peerX . " is disabled. Ignoring!")
}
}
}
if ($debug = true) do={
:log info ("=============BGP PEER STATUS DETECTION ENDED===============" )
}
It turns out it was easier than I thought.I don't think that my approach will work on v7 since the sessions are radically different from v6.
It will probably need a complete rewrite to make it work on v7. I am not promising anything.
###################################################################
# BGP PEERS STATUS MAIL ALERT SCRIPT (for RouterOS v7 only) #
# Original Author: Cha0s, circa 2015, with improvements by JB172 #
# Info: https://forum.mikrotik.com/viewtopic.php?p=494204#p494204 #
###################################################################
# EDIT HERE
:local arrEmails [:toarray "email@example.net,email2@example.net"];
:local debug false
# EDIT END
# Define Variables
:global arrBgpState;
:global gotKey false;
:local hostNameX ([/system identity get name]);
:local statusX;
:local peerX;
:local peerXarrEl;
:local mailToX;
:local mailSubjectX;
:local mailBodyX;
:local peerStatusX;
:local arrBgpPeers [:toarray ""];
:local peername "";
:local peerTotal "";
:local notifStatus "";
if ($debug = true) do={
:log info ("=============BGP PEER STATUS DETECTION STARTED=============" )
}
# Initialize global array - kinda lame way to do it :P
:if ( [:len $arrBgpState] =0 ) do={
:set $arrBgpState {"false"="false"}
}
# Fill arrBgpPeers array with peers name
:foreach i in=[/routing bgp session find] do={
:set peername [/routing bgp session get $i name];
:set peerTotal ($peerTotal . "," .$peername);
}
:set arrBgpPeers [:toarray $peerTotal];
# Loop through the peers array
:local arrPos
:for arrPos from=0 to=([:len $arrBgpPeers]-1) do={
# Set peerX to current peer name for this iteration
:set peerX [:pick $arrBgpPeers $arrPos];
# Get Peer Status
:set peerStatusX [/routing bgp session get [find name="$peerX"] established]
if ($peerStatusX = true) do={
:set notifStatus ("up");
} else={
:set notifStatus ("down");
}
# Find peer key in global array
:foreach k,v in=$arrBgpState do={
if ($k != "false") do={
:if ($gotKey = false) do={
:if ($k = $peerX) do={
:set gotKey true
}
}
}
}
# Initialize arrBgpState array element with peerX name if it doesn't already exist
:if ($gotKey = false) do={
:set ($arrBgpState->"$peerX") "up"
}
# Reset this for the next iteration
:set gotKey false
# Previous run peer status
:set statusX ($arrBgpState->"$peerX")
# Prepare Email body
:set mailBodyX ("Router Hostname: " . $hostNameX . "\nBGP Peer Status: " . $notifStatus . "\nBGP Peer Name: " . $peerX . "\n");
# Check if BGP Peer is not established
:if ($peerStatusX != true) do={
# Check if this is the first time the peer is doen
:if ( $statusX = "up" ) do={
# Set value to 'down' to peer global var key
:set ($arrBgpState->$peerX) "down"
# Informational Log
if ($debug = true) do={
:log error ("BGP Peer ". $peerX ." state is " . $notifStatus . ". Sending email alerts!" )
}
# Prepare Email subject
:set mailSubjectX ("BGP Peer ". $peerX ." on ". $hostNameX . " is NOT established!");
# Loop over emails array to send emails to all recipients in array
:local arrPos2
:for arrPos2 from=0 to=([:len $arrEmails]-1) do={
# Set mailToX to recipient for this iteration
:set mailToX [:pick $arrEmails $arrPos2]
# Informational Log
:log error ("BGP Peer " . $peerX . " status is " .$notifStatus. "! Sending Email alert to " . $mailToX . "." )
# Send Email
/tool e-mail send to=$mailToX subject=$mailSubjectX body=$mailBodyX;
}
} else={
# Peer down. Already sent notification so do nothing.
if ($debug = true) do={
:log info ("BGP Peer ". $peerX ." is already down. Ignoring!")
}
}
} else={
# Check if peer just came back up and send informational email
if ($statusX = "down") do={
# Prepare Email subject
:set mailSubjectX ("BGP Peer ". $peerX ." on ". $hostNameX . " has recovered!");
# Loop over emails array to send emails to all recipients in array
:local arrPos2
:for arrPos2 from=0 to=([:len $arrEmails]-1) do={
# Set mailToX to recepient for this iteration
:set mailToX [:pick $arrEmails $arrPos2]
# Informational Log
:log warning ("BGP Peer " . $peerX . " status has recovered! Sending informational Email to " . $mailToX . "." )
# Send Email
/tool e-mail send to=$mailToX subject=$mailSubjectX body=$mailBodyX;
}
}
if ($debug = true) do={
:log info ("BGP Peer " . $peerX . " is up. Nothing to do!")
}
# Set peer status to up
:set ($arrBgpState->$peerX) "up"
}
}
if ($debug = true) do={
:log info ("=============BGP PEER STATUS DETECTION ENDED===============" )
}
I've integrated the above script into my mail alert script but I haven't tested it.
Code: Select all################################################################### # BGP PEERS STATUS MAIL ALERT SCRIPT (for RouterOS v6 only) # # Original Author: Cha0s, circa 2015, with improvements by JB172 # # Info: https://forum.mikrotik.com/viewtopic.php?p=494204#p494204 # ################################################################### [/quote]
There was a syntax error.
I've edited my post with the correct syntax. Please try again, it should work now.