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
Panagiotis Botos
Networking Engineer
 
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.
Panagiotis Botos
Networking Engineer
 
JB172
Member
Member
Posts: 306
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.
Panagiotis Botos
Networking Engineer
 
deanp
just joined
Posts: 1
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;

Who is online

Users browsing this forum: Bing [Bot], elico, eworm and 34 guests