Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add notify-on-dhcp-lease script #32

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added doc/notify-on-dhcp-lease.d/notification.avif
Binary file not shown.
47 changes: 47 additions & 0 deletions doc/notify-on-dhcp-lease.md
Original file line number Diff line number Diff line change
@@ -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 changes: 68 additions & 0 deletions notify-on-dhcp-lease
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!rsc by RouterOS
# RouterOS script: notify-on-dhcp-lease
# Copyright (c) 2018-2022 Christian Hesse <[email protected]>
# 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);
}
}