I was looking for the same script to get alerts when any bgp peer goes down.
I didn't find anything complete so I wrote a script based on what was suggested on this thread.
It's a single script that supports multiple peers and multiple emails for getting alerts.
It assumes that /tools email is properly configured on the router.
The script will periodically (you run it with scheduler - I run it every 10 seconds with no issues so far) check all configured BGP peers.
-If a peer is disabled it will ignore it.
-If a peer is with state 'established' it will ignore it.
-If a peer has any other state than 'established' it will send an informational email to the configured email addresses and log this as an error (red) in the log window.
-If on the next run of the script the peer is still down it will remember it and will not send any emails again for that peer.
-If on the next run the peers that were down go back on state 'established' an informational email will be send to the configured email address and log this as a warning (blue) in the log window.
The script has been tested on v6.29 & v6.30.2. I don't know if it will work properly on older versions. (I know for sure that it doesn't work on 5.x and probably won't work on <=6.3)
Known issues:
If you do not define the bgp peers with their correct name, the script will not run successfully.
To use the script you only need to edit the two first arrays to add the names of your bgp peers and emails separated by commas.
There is also a debug option to enable more info on the log window. Set this to false to disable.
You don't need to edit anything else on the script. By simply running it it should not produce any output (if debug is off) when all peers are on state established.
###################################################################
# 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===============" )
}
I am not very familiar with the RouterOS scripting language so there is definitely room for improvements on my code!
Feel free to edit it as you see fit
