Add notify-on-dhcp-lease script

This commit is contained in:
Anatoly Bubenkov 2022-11-05 17:14:34 +01:00
parent 7594345da8
commit 686a5c37b4
3 changed files with 115 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

View file

@ -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)

68
notify-on-dhcp-lease Normal file
View file

@ -0,0 +1,68 @@
#!rsc by RouterOS
# RouterOS script: notify-on-dhcp-lease
# Copyright (c) 2018-2022 Christian Hesse <mail@eworm.de>
# 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);
}
}