Script for Usermanager Sessions List in multiple files

Here's my first script for creating multiple files that list User Manager sessions....


#########################################################################################################

User Manager Sessions List

#########################################################################################################

Author: Meneleo Cedeno, Cebu, Philippines

email: mcedeno.gm@gmail.com

Username in MikroTik Forum is meska

#########################################################################################################

Features #

- List user sessions in multiple files to avoid the 4095 length limit

- Just change the variable 'entriesPerFile' to adjust the number of sessions per file.

- Edit the Header to suit your needs

- You can add more columns (i.e Download, Upload, Uptime, etc.), however, you need to adjust the 'entriesPerFile' accordingly.

#########################################################################################################

:local iterator 0
:local entriesPerFile 20
:local thisDate
:local thisYear
:local thisDay
:local thisMonth
:local fname ""

:local username
:local macID
:local startSession
:local finishSession
:local totalUptime
:local dataDownload
:local dataUpload
:local dateUsed
:local fromTime
:local toTime
:local Header ""
:local oldContent ""
:local newContent ""
:local lenContent
:local lenHeader
:local entryCount 0
:local fileIndex 0


:local qtySessions [/tool user-manager session print count-only]
:local fileCount ($qtySessions / $entriesPerFile)

:if ( ($fileCount * $entriesPerFile) != $qtySessions) do={
:set fileCount ($fileCount + 1);
}

Prepare date suffixes for the filename

:set thisDate [/system clock get date]
:set thisYear [:pick $thisDate 7 11]
:set thisDay [:pick $thisDate 4 6]
:set thisMonth [:pick $thisDate 0 3]

:if ($thisMonth="jan") do { :set thisMonth 1}
:if ($thisMonth="feb") do { :set thisMonth 2}
:if ($thisMonth="mar") do { :set thisMonth 3}
:if ($thisMonth="apr") do { :set thisMonth 4}
:if ($thisMonth="may") do { :set thisMonth 5}
:if ($thisMonth="jun") do { :set thisMonth 6}
:if ($thisMonth="jul") do { :set thisMonth 7}
:if ($thisMonth="aug") do { :set thisMonth 8}
:if ($thisMonth="sep") do { :set thisMonth 9}
:if ($thisMonth="oct") do { :set thisMonth 10}
:if ($thisMonth="nov") do { :set thisMonth 11}
:if ($thisMonth="dec") do { :set thisMonth 12}


:for i from=1 to=$fileCount do={
#create the files
:set fname ("sessions_" . $thisMonth . $thisDay. [:pick $thisYear 2 4]. "_" . $i . ".txt")

/file print file=$fname
:delay 2
/file set $fname contents=""
:set Header ("Ezi Wifi User Sessions " )
:set Header ($Header . "\r\n" . "Date Printed : " . $thisDate)
:set Header ($Header . "\r\n" . "Kiosk ID : CEB-002" )
:set Header ($Header . "\r\n" . "Page " . $i . " of " . $fileCount )
:set Header ($Header . "\r\n" . "User, machine ID, Date, Start, Finish" )
/file set $fname contents=$Header
}

:set fname ""

Read and write the session logs

:foreach i in=[/tool user-manager session find] do {
:set username [/tool user-manager session get $iterator user]
:set macID [/tool user-manager session get $iterator calling-station-id]
:set startSession [/tool user-manager session get $iterator from-time]
:set finishSession [/tool user-manager session get $iterator till-time]
:set totalUptime [/tool user-manager session get $iterator uptime]
:set dataDownload [/tool user-manager session get $iterator download]
:set dataUpload [/tool user-manager session get $iterator upload]
:set dateUsed [:pick $startSession 0 6]
:set fromTime [:pick $startSession 12 17]
:set toTime [:pick $finishSession 12 17]
:set entryCount ($entryCount+1)

:set newContent ($newContent . "\r\n" . $username . ", " . $macID . ", " . $dateUsed . ", " . $fromTime . ", " . $toTime)

:if ($entryCount=$entriesPerFile || ($iterator+1)=$qtySessions) do {
:set fileIndex ($fileIndex+1)

Get contents of filename with index

:set fname ("sessions_" . $thisMonth . $thisDay. [:pick $thisYear 2 4]. "_" . $fileIndex . ".txt")
:set oldContent [/file get $fname contents]
:put $oldContent
/file set $fname contents=($oldContent . $newContent)
:set entryCount 0
:set oldContent ""
:set newContent ""
}

:set iterator ($iterator+1)

}