I have this script that notifies me via Telegram when someone logs into the device and increments a global variable so that at the end of the day it shows me the number of logins in another report.
I think the counter isn't working correctly because when I have multiple logins, it doesn't increment the value of the global variable. Any help would be appreciated.
:global tgFunc
:global lastLogLogged
:global loginsToday
:if ([:typeof $loginsToday] = "nothing") do={:set loginsToday 0 }
:if ([:typeof $lastLogLogged] != "num") do={:set lastLogLogged 0}
:if ([:len [/user active find]] > 0) do={
:local id2num do={:return [:tonum "0x$[:pick $1 1 [:len $1]]"]}
:local loginCount 0
/log
:foreach rlog in=[find where (([$id2num $".id"] > $lastLogLogged) and (topics~"account") and \
(message~"((25[0-5]|(2[0-4]|[01]\?[0-9]\?)[0-9])\\.){3}(25[0-5]|(2[0-4]|[01]\?[0-9]\?)[0-9])"))] do={
:local datetime [get $rlog time]
:local msg [get $rlog message]
:set lastLogLogged [$id2num $rlog]
:if ($msg~"logged in") do={
# message: user admin logged in from 192.168.88.20 via winbox
:local IP [:pick $msg ([:find $msg "from "]+5) [:find $msg " via"]]
:local user [:pick $msg ([:find $msg "user "]+5) [:find $msg " logged"]]
:local service [:pick $msg ([:find $msg "via "]+4) [:len $msg]]
:local sysname [/system identity get name]
:set loginCount ($loginCount + 1)
$tgFunc ("\E2\84\B9 <b>$sysname:</b> IP $IP ( $user via $service ) has been connect at $datetime")
:set loginsToday $loginCount
}
}
}