:put [:serialize to=json [/user-manager/session/print as-value ]]
/user-manager/session/print as-value where [:put "$".id",$user,$"acct-session-id",$"nas-ip-address",$"calling-station-id",$download,$started,$uptime,$($status->0),$"last-accounting-packet"" ]
*1,test,84000018,127.0.0.1,192.168.xx.148,0,2024-06-21 14:29:12,00:00:00,start,2024-06-21 14:29:12
*2,test,84000019,127.0.0.1,192.168.xx.148,0,2024-06-21 14:30:24,00:00:00,start,2024-06-21 14:30:24
{
:local filename "session_data.csv"
:local fileExists ""
:set fileExists [/file find where name=$filename]
:if ($fileExists = "") do={
/tool fetch url="http://127.0.0.1/" dst-path=$filename mode=http
/file set $filename contents="ID,User,Acct Session ID,NAS IP Address,Calling Station ID,Download (MB),Started,Uptime (s),Status,Last Accounting Packet\r\n"
}
:foreach session in=[/user-manager/session/print as-value] do={
:local id ($session->"id")
:local user ($session->"user")
:local acctSessionId ($session->"acct-session-id")
:local nasIpAddress ($session->"nas-ip-address")
:local callingStationId ($session->"calling-station-id")
:local download ($session->"download")
:local started ($session->"started")
:local uptime ($session->"uptime")
:local status ($session->"status")
:local lastAccountingPacket ($session->"last-accounting-packet")
# Formatta i dati in formato CSV
:local csvLine "$id,$user,$acctSessionId,$nasIpAddress,$callingStationId,$download,$started,$uptime,$status,$lastAccountingPacket\r\n"
# Aggiungi la nuova riga al contenuto esistente del file
:local currentContent [/file get $filename contents]
/file set $filename contents=($currentContent . $csvLine)
}
}
I was hoping the new [:serialize $array to=dsv delim=","] introduced in v7.16 would help. But it does not like the data from [/user-manager/session/print].I'm resuming this discussion after a long time as the project had been put on hold.
{
:local infile "sessions.csv"
:local csvstr ""
:onerror e in={ :set csvstr [/file get $infile contents] } do={
/file add name=$infile
/file set $infile contents="ID,User,Acct Session ID,NAS IP Address,Calling Station ID,Download (MB),Started,Uptime (s),Status,Last Accounting Packet\r\n"
}
:local rows ""
:foreach session in=[/user-manager/session/print show-ids as-value] do={
:local id ($session->".id")
:local user ($session->"user")
:local acctSessionId ($session->"acct-session-id")
:local nasIpAddress ($session->"nas-ip-address")
:local callingStationId ($session->"calling-station-id")
:local download ($session->"download")
:local started ($session->"started")
:local uptime ($session->"uptime")
:local status ($session->"status")
:local lastAccountingPacket ($session->"last-accounting-packet")
# Formatta i dati in formato CSV
:local csvLine "$[:tostr $id],$[:tostr $user],$[:tostr $acctSessionId],$[:tostr $nasIpAddress],$[:tostr $callingStationId],$[:tostr $download],$[:tostr $started],$[:tostr $uptime],$[:tostr $status],$[:tostr $lastAccountingPacket]\r\n"
:set rows "$rows$csvLine"
}
/file set $infile contents="$csvstr$rows"
:put [/file get $infile contents]
}
{
:local infile "sessions.csv"
:local csvstr ""
:onerror e in={ :set csvstr [/file get $infile contents] } do={
/file add name=$infile
:delay 2s;
/file set $infile contents="ID,User,Acct Session ID,NAS IP Address,Calling Station ID,User Address,Started,Ended,Terminate Cause,Uptime (s),Download (MiB),Upload (MiB)\r\n"
:set csvstr [/file get $infile contents]
}
:delay 2s;
# Controlla se l'intestazione esiste già
:if ([:len $csvstr] <= 0) do={
/file set $infile contents="ID,User,Acct Session ID,NAS IP Address,Calling Station ID,User Address,Started,Ended,Terminate Cause,Uptime (s),Download (MiB),Upload (MiB)\r\n"
:set csvstr [/file get $infile contents]
}
:local rows ""
:foreach session in=[/user-manager/session/print show-ids as-value] do={
:local id ($session->".id")
:local user ($session->"user")
:local acctSessionId ($session->"acct-session-id")
:local nasIpAddress ($session->"nas-ip-address")
:local callingStationId ($session->"calling-station-id")
:local useraddress ($session->"user-address")
:local started ($session->"started")
:local ended ($session->"ended")
:local terminatecause ($session->"terminate-cause")
:local uptime ($session->"uptime")
:local download ($session->"download")
:local upload ($session->"upload")
:local status ($session->"status")
:local lastAccountingPacket ($session->"last-accounting-packet")
:local downloadMib ($download / 1048576)
:local uploadMib ($upload / 1048576)
# Formatta i dati in formato CSV
:local csvLine "$[:tostr $id],$[:tostr $user],$[:tostr $acctSessionId],$[:tostr $nasIpAddress],$[:tostr $callingStationId],$[:tostr $useraddress],$[:tostr $started],$[:tostr $ended],$[:tostr $terminatecause],$[:tostr $uptime],$[:tostr $downloadMib],$[:tostr $uploadMib]\r\n"
:set rows "$rows$csvLine"
}
# Aggiungi nuove righe al file CSV senza sovrascrivere
/file set $infile contents="$csvstr$rows"
:put [/file get $infile contents]
}