diff --git a/doc/notify-on-dhcp-lease.d/notification.avif b/doc/notify-on-dhcp-lease.d/notification.avif new file mode 100644 index 00000000..d426e29c Binary files /dev/null and b/doc/notify-on-dhcp-lease.d/notification.avif differ diff --git a/doc/notify-on-dhcp-lease.md b/doc/notify-on-dhcp-lease.md new file mode 100644 index 00000000..11ae5ab3 --- /dev/null +++ b/doc/notify-on-dhcp-lease.md @@ -0,0 +1,47 @@ +Notify on a new DHCP lease +========================== + +[◀ Go back to main README](../README.md) + +> ℹ️ **Info**: This script can not be used on its own but requires the base +> installation. See [main README](../README.md) for details. + +Description +----------- + +This script is run from scheduler periodically, checking for LTE state changes. + +### Sample notification + +![notify-on-dhcp-lease notification](notify-on-dhcp-lease.d/notification.avif) + +Requirements and installation +----------------------------- + +Just install the script: + + $ScriptInstallUpdate notify-on-dhcp-lease; + +Requires lease-script installed: + + $ScriptInstallUpdate lease-script; + +Configuration +------------- + +The configuration goes to `global-config-overlay`, there are no additional parameters. + +Also notification settings are required for +[e-mail](mod/notification-email.md), +[matrix](mod/notification-matrix.md) and/or +[telegram](mod/notification-telegram.md). + +See also +-------- + +* [Notify on RouterOS update](check-routeros-update.md) +* [Install LTE firmware upgrade](unattended-lte-firmware-upgrade.md) + +--- +[◀ Go back to main README](../README.md) +[▲ Go back to top](#top) diff --git a/notify-on-dhcp-lease b/notify-on-dhcp-lease new file mode 100644 index 00000000..2f3fa432 --- /dev/null +++ b/notify-on-dhcp-lease @@ -0,0 +1,68 @@ +#!rsc by RouterOS +# RouterOS script: notify-on-dhcp-lease +# Copyright (c) 2018-2022 Christian Hesse +# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md +# +# provides: lease-script, order=90 +# +# send notification on a new DHCP lease +# https://git.eworm.de/cgit/routeros-scripts/about/doc/notify-on-dhcp-lease.md + +:local 0 "notify-on-dhcp-lease"; +:global GlobalFunctionsReady; +:while ($GlobalFunctionsReady != true) do={ :delay 500ms; } + +:global Identity; + +:global IfThenElse; +:global LogPrintExit2; +:global ScriptLock; +:global SendNotification2; +:global SymbolForNotification; +:global WaitFullyConnected; + +$ScriptLock $0; + +:global CurrentLeases; + +:if ([ :typeof $CurrentLeases ] != "array") do={ + :global CurrentLeases ({}); +} + +$WaitFullyConnected; + +$LogPrintExit2 debug $0 ("Prepared") false; +$LogPrintExit2 debug $0 ("Searching for leases") false; +:local FoundLeases ({}); + +:foreach Lease in=[ /ip/dhcp-server/lease/find where dynamic=yes status=bound ] do={ + :local LeaseVal [ /ip/dhcp-server/lease/get $Lease ]; + :local MacAddress ($LeaseVal->"mac-address"); + :local IpAddress ($LeaseVal->"address"); + :local Hostname ($LeaseVal->"host-name"); + :local Comment ($LeaseVal->"comment"); + :local LeaseName ("$MacAddress::$IpAddress::$Hostname::$Comment"); + :local CurrentLease ($CurrentLeases->$MacAddress); + :local Message ("New DHCP lease: $LeaseName."); + :if ( $CurrentLease = nil ) do={ + $LogPrintExit2 info $0 ($Message) false; + $SendNotification2 ({ origin=$0; \ + subject=([ $SymbolForNotification "sparkles" ] . "New DHCP lease"); \ + message=($Message); silent=true }); + :set ($CurrentLeases->$MacAddress) ($LeaseName); + } else={ + $LogPrintExit2 debug $0 ("Lease $LeaseName already known.") false; + } + :set ($FoundLeases->$MacAddress) ($LeaseName); +} + +:foreach MacAddress,LeaseName in=$CurrentLeases do={ + :if ($FoundLeases->$MacAddress = nil) do={ + :local Message ("A DHCP lease $LeaseName has been released."); + $LogPrintExit2 info $0 ($Message) false; + $SendNotification2 ({ origin=$0; \ + subject=([ $SymbolForNotification "sparkles" ] . "A DHCP lease has been released"); \ + message=($Message); silent=true }); + :set ($CurrentLeases->$MacAddress) (nil); + } +} \ No newline at end of file