Based on another I just finish:
http://forum.mikrotik.com/t/help-with-this-script/157065/1
{
:global arrMonths {jan="01";feb="02";mar="03";apr="04";may="05";jun="06";jul="07";aug="08";sep="09";oct="10";nov="11";dec="12"}
:local dhcpName "dhcp-laboratorio"
:local filename "leasehistory.txt"
:local ndate [/system clock get date]; :local ntime [/system clock get time]
:local now "$[:pick $ndate 7 11]-$($arrMonths->[:pick $ndate 0 3])-$[:pick $ndate 4 6] $[/system clock get time]"
/file
:if ([:len [find where name="flash" and type="disk"]] = 1) do={:set filename "flash/$filename"}
:if ([:len [find where name=$filename]] = 0) do={print file="$filename"; :delay 5s; set $filename contents=""}
:local currentLease [:len [/ip dhcp-server lease find where server=$dhcpName]]
:delay 4s
:local filecontent [get $filename contents]
:local newfilecontent "$currentLease\t$now\r\n$filecontent"
set $filename contents=$newfilecontent
}
The update time is based on scheduling interval,
keep an eye on file, when reach ~64K the excedence are deleted (is why is writed “last first”)
if scheduled every 30 min:
~64K are near 2700 record, sufficient for more than 1 month with one value for each half hour on each day
you can copy & paste the content of the txt file inside excel, and you obtain a table with two colums
the date are stored on ISO-like format, can be sorted
27 2022/04/05 15:19:30
42 2022/04/05 18:26:10