- Script for OPNsense: monitors multi-WAN gateway status and sends notifications via Telegram (or email with Monit service)
This project provides two scripts for monitoring the status of gateways in OPNsense and sending alerts to Telegram.
The gateway_multiwan
script monitors the offline
, packet loss
, online
, or unknown
status of all gateways found in opnSense. If any of them change, it sends an alert using the sendTelegram.sh
file to a group, via a Telegram bot. If the SMTP alert is configured, it will also be sent to your email. For it to work, the script needs to be added to your opnSense and set up to be executed and triggered by events through the already included package called Monit.
- OPNsense Firewall
- SSH terminal
- Download the two files from this GitHub repository (stable version) to your machine with OPNsense
- Telegram Account
- Enable SSH on OpnSense
- Download Files
- Set Permissions
- Configure Telegram
- Edit
sendTelegram.sh
- Enable Monit Service
- Enable Script
- Test
-
Enable SSH on OpnSense and download the files
1.1 Enable SSH on OpnSense: System > Settings > Administration
1.2 Access OpnSense via SSH from terminal: ssh root@192.168.1.1. Replace root with your username and 192.168.1.1 with your OpnSense IP address.
1.3 Navigate to the Monit service folder:cd /usr/local/opnsense/scripts/OPNsense/Monit
1.4 Use fetch to download "gateway_multiwan" and "sendTelegram.sh' from GitHub:
fetch https://github.com/31337-4554551n/opnsense_gateways_status/blob/main/versions/stable/0.7/gateway_multiwan && fetch https://github.com/31337-4554551n/opnsense_gateways_status/blob/main/versions/stable/0.7/sendTelegram.sh
-
Set Permissions:
2.1 Set +x permission on the files:
chmod +x gateway_multiwan sendTelegram.sh
-
Telegram Configuration:
3.1 Create a Telegram group.
3.2 Create a bot with "BotFather".
3.3 Note down the bot token.
3.4 Add the bot and yourself to the group.
3.5 Get the group ID from the URL. -
Configure sendTelegram.sh:
4.1 Open sendTelegram.sh in a text editor.
4.2 Update TOKEN and CHAT_ID with your bot token and group ID. -
Enable Monit Service:
5.1 In OPNsense, go to Services > Monit > Services and click on Enable Monit.
5.2 Set your preferred polling interval. -
Enable Script:
6.1 OPNsense, go to Services > Monit > Services and duplicate the pre-existing service called gateway_alert.
6.2 In the Duplicate Item dialog box, enter a name for the new service, such as "gateway_multiwan".
6.3 In the Path field, enter the path to the shell script, such as/usr/local/opnsense/scripts/OPNsense/Monit/gateway_multiwan
.
6.4 In Tests field, uncheck "NonZeroStatus" and check "ChangedStatus".
6.5 Click Save and Apply.
Testing the Script
To test the script, manually disable a WAN link in OPNsense. You should receive a Telegram alert.
For more details on how to get your bot token in Telegram[^1^][2] or how to get your chat ID in Telegram, you can refer to these links.
(1) How to Generate a Token for Telegram Bot API | https://medium.com/geekculture/generate-telegram-token-for-bot-api-d26faf9bf064
(2) How to Find a Chat ID in Telegram | https://www.alphr.com/find-chat-id-telegram/