This repository has been archived on 2025-12-03. You can view files and clone it, but cannot push or open issues or pull requests.
scripts_ros/monitor_bgp.rsc
Halbe Bruno f8cb39c579 monitor_bgp.rsc
monitor_bgp.rsc ver.1.02
2025-02-10 07:43:54 -03:00

64 lines
2.6 KiB
Plaintext

# Script que monitorar o status de um determinado peer bgp. Em caso que o status
# do peer seja diferente de established, o RoS desativa e ativa o peer na tentativa de
# restabelecer a conexão.
# Uma notificação é enviada via telegram quando o peer cai e quando o mesmo é reestabelecido.
#
# Author: Halbe Bruno
# V1.02 - 2025-02-07
# Variáveis para o Telegram
:local TelegramBotToken "318510879:AAFMQ7-GojYTcLRMFEIcwAJ21M2iFhrINxs"
:local TelegramChatID "172198732"
:local TelegramMessageDown "Peer VIVO (telefonia) DOWN"
:local TelegramMessageUp "Peer VIVO (telefonia) UP"
# Variáveis para o peer BGP
:local peerName "VIVO"
# Variáveis globais para rastrear o estado do peer
:global PeerDownNotificationSent
:global PeerUpNotificationSent
# Verifica o estado do peer BGP
:local peerState [/routing bgp peer get [find where name=$peerName] state]
# Lógica principal
:if ($peerState != "established") do={
/log warning message="O peer BGP $peerName está DOWN. Reiniciando conexão do peer..."
# Reinicia o peer
/routing bgp peer disable $peerName
:delay 2
/routing bgp peer enable $peerName
# Verifica se a mensagem de DOWN já foi enviada
:if ([:typeof $PeerDownNotificationSent] = "nothing" || $PeerDownNotificationSent = false) do={
# Envia a mensagem de DOWN pelo Telegram
/tool fetch url="https://api.telegram.org/bot$TelegramBotToken/sendMessage?chat_id=$TelegramChatID&text=$TelegramMessageDown" keep-result=no
# Marca que a mensagem de DOWN foi enviada
:set PeerDownNotificationSent true
/log info "Mensagem de notificação (DOWN) enviada."
}
# Reseta a variável de notificação de UP
:if ([:typeof $PeerUpNotificationSent] != "nothing" && $PeerUpNotificationSent = true) do={
:set PeerUpNotificationSent false
}
} else={
# Verifica se a mensagem de UP já foi enviada
:if ([:typeof $PeerUpNotificationSent] = "nothing" || $PeerUpNotificationSent = false) do={
# Envia a mensagem de UP pelo Telegram
/tool fetch url="https://api.telegram.org/bot$TelegramBotToken/sendMessage?chat_id=$TelegramChatID&text=$TelegramMessageUp" keep-result=no
# Marca que a mensagem de UP foi enviada
:set PeerUpNotificationSent true
/log info "Mensagem de notificação (UP) enviada."
}
# Reseta a variável de notificação de DOWN
:if ([:typeof $PeerDownNotificationSent] != "nothing" && $PeerDownNotificationSent = true) do={
:set PeerDownNotificationSent false
/log info "Peer BGP $peerName reestabelecido. Variável de notificação (DOWN) resetada."
}
}