Community discussions

MikroTik App
 
operat0r
newbie
Topic Author
Posts: 32
Joined: Mon May 29, 2017 9:18 pm

Sending Mail when BGP is down Script error

Mon Mar 26, 2018 3:34 pm

Hello,

I've recently implemented a script (found on the internet), to monitor my BGP peers, so when one bgp peer goes down, I will get informed with a mail. The problem is that the script seems to not working (not sending mail). The mail tool has been configured and is working when I manually send an email.

The script is shown bellow:
# EDIT HERE
:local arrEmails [:toarray "mail@mail.com"];
: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 "";

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 start-tls=yes;
            }        

         } 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 start-tls=yes;
            }            
            
         }
         
         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===============" )
}
The error shown in the logs is the following :
Topics
script
error
Message BGP Peer status is idle! Sending Email alert to mail@mail.com.
Can someone please help me identify the issue?
Model of router: CCR1072-1G-8S+
Version of RouterOS: 6.39.2
 
operat0r
newbie
Topic Author
Posts: 32
Joined: Mon May 29, 2017 9:18 pm

Re: Sending Mail when BGP is down Script error

Fri Mar 30, 2018 3:48 pm

I'm still trying to figure out what is wrong with the script.
So if anyone has any insight, please do provide any kind of feedback.

Thank you.
 
JB172
Member
Member
Posts: 304
Joined: Fri Jul 24, 2015 3:12 pm
Location: AWMN

Re: Sending Mail when BGP is down Script error

Fri Mar 30, 2018 8:11 pm

Did you change the email address in 2nd line?
:local arrEmails [:toarray "mail@mail.com"];
 
operat0r
newbie
Topic Author
Posts: 32
Joined: Mon May 29, 2017 9:18 pm

Re: Sending Mail when BGP is down Script error

Mon Apr 02, 2018 4:33 pm

Yes I did. I have already tested the script but cannot find the reason to why when i manually send an email , all is good, but when the script runs i get no email.
 
deanp
just joined
Posts: 2
Joined: Tue Nov 17, 2020 9:19 pm

Re: Sending Mail when BGP is down Script error

Tue Nov 17, 2020 9:27 pm

Hi

Your script worked perfectly for me, only thing I changed:

# Send Email
/tool e-mail send to=$mailToX subject=$mailSubjectX body=$mailBodyX start-tls=yes;

To

# Send Email
/tool e-mail send to=$mailToX subject=$mailSubjectX body=$mailBodyX start-tls=tls-only;
 
deanp
just joined
Posts: 2
Joined: Tue Nov 17, 2020 9:19 pm

Re: Sending Mail when BGP is down Script error

Sun Mar 26, 2023 8:01 pm

Hi, does anyone have a BGP is down script error for v7?
 
chubbs596
Frequent Visitor
Frequent Visitor
Posts: 90
Joined: Fri Dec 06, 2013 6:07 pm

Re: Sending Mail when BGP is down Script error

Mon May 29, 2023 4:58 pm

Hi,

Im also looking for a similar script for V7
 
kerfuffle
just joined
Posts: 15
Joined: Wed Feb 22, 2023 6:50 am
Location: San Francisco, CA
Contact:

Re: Sending Mail when BGP is down Script error

Mon Jun 19, 2023 7:39 am

I wrote something, in Python to run off the router on my monitoring host. This will also alert you if there is a session defined in /routing/bgp/connection but it doesn't appear in /routing/bgp/sessions as well as sessions which are not established in /routing/bgp/sessions.

https://github.com/edgenative/mikrotik-bgpmon
 
ditud
just joined
Posts: 4
Joined: Wed Jan 27, 2021 10:09 am

Re: Sending Mail when BGP is down Script error

Mon Sep 11, 2023 10:32 am

Hello, could someone advise with solution to:
- I'm using this script and it sends warnings to email address;
- I'm experiece BGP sessions drops from time to time and if so I had to switch BGP peer down and up to restore BGP session;
- I'd like this script to do peer off/on automaticly, when it detect broken BGP session;

any sugestion?

Thaks in advance for help.

Daniel
Last edited by ditud on Mon Sep 11, 2023 10:33 am, edited 1 time in total.

Who is online

Users browsing this forum: akkurad, Google [Bot] and 20 guests