Bump !
has this script been tested on v7?
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.
Hi,
Now that BFD has been added in V7.10beta8, will you possible start looking at converting this script for V7?
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.
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.
You mean in v6, right? The peer gets stuck in ‘opensent’ status?
I haven’t seen this behavior in v7.
In v6 you can use the following script in a scheduler every 1 minute (or however fast you want).
/routing bgp peer {
:foreach peer in [find state="opensent"] do={
:log warning "Restart stuck BGP Peer: $([get $peer name])"
disable $peer
enable $peer
}
}
I’ve integrated the above script into my mail alert script but I haven’t tested it.
BGP PEERS STATUS MAIL ALERT SCRIPT for RouterOS v6:
###################################################################
# BGP PEERS STATUS MAIL ALERT SCRIPT (for RouterOS v6 only) #
# Original Author: Cha0s, circa 2015, with improvements by JB172 #
# Info: http://forum.mikrotik.com/t/email-me-if-bgp-session-status-in-not-established/58669/15 #
###################################################################
# 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 haven’t tested the script extensively, so use at your own risk.
BGP PEERS STATUS MAIL ALERT SCRIPT for RouterOS v7:
###################################################################
# BGP PEERS STATUS MAIL ALERT SCRIPT (for RouterOS v7 only) #
# Original Author: Cha0s, circa 2015, with improvements by JB172 #
# Info: http://forum.mikrotik.com/t/email-me-if-bgp-session-status-in-not-established/58669/15 #
###################################################################
# 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 run script in this form but nothing happend. I also put lines below in my aold script:
# 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
}
but after run, nothing happend. Log on mikrotik shows no new lines.
It’s routeros v6 and exactly this is opensent state situation.
[/quote]
There was a syntax error.
I’ve edited my post with the correct syntax. Please try again, it should work now.
Three times this night one session went down and three times it established again after next script run. It seems that it works.
Thank you, very useful!
But I think, that peers must be declared from /routing/bgp/connection and then compared with /routing/bgp/session for v7