Community discussions

MikroTik App
 
ariaz1
just joined
Topic Author
Posts: 3
Joined: Wed Dec 20, 2017 7:07 pm

Writing TX and RX bytes to file for all interfaces

Wed Dec 20, 2017 7:21 pm

Hello,

I'm using a script to capture the TX and RX bytes for all my router interfaces (ports and VLANs), but can't seem to get it to work. I got the idea of the following script from the forum mentioned below and then tried adjusting it to my needs: viewtopic.php?t=37708

I get the concept, its pretty simple.."Get the respective information for many interfaces as you can, but once you reach the variable limit, flush it down in a text file. Then create another text file and continue where you left off." So not really sure what I'm missing here, perhaps mixing global with local variables is causing the issue?

:global time ([/system clock get time]);
:global hour [:pick $time 0 2];
:global min [:pick $time 3 5];
:global sec [:pick $time 6 8];
:global stamp ($hour . "_" . $min . "_" . $sec);
:global id ([/system identity get name]);
:global numports 100;
:global name " ";
:global rx " ";
:global tx " ";
:global line "Interface Name, Avg RX Byte, Avg TX Byte";
:global line2 "";
:global filenumber 1;
:global buffer;

:foreach INTERFACE in=[/interface find] do={

# record te parameters
:set name [/interface get $INTERFACE name];
:set rx [/interface get $INTERFACE rx-byte];
:set tx [/interface get $INTERFACE tx-byte];

# assemble them into a line
:set line ($line . "\n" . $name . "," . $rx . "," . $tx);

# add the buffer length and the string length
:local newLength ([:len $buffer] + [:len $line]);

# check if the buffer would now be larger than the variable size limit
:if($newLength > 4095) do={

# it is. write out the current line buffer (without the latest line)
# first construct the file name
:local fileName ($id . "_BW_" . $stamp . "_" . $filenumber . ".txt");

# create the file as it doesn't exist yet
/file print file=$fileName;
delay 2s;

# write the buffer into it
/file set $fileName contents="$buffer";

# increase the file number
:set $fileNumber ($fileNumber + 1);

# reset the line buffer to just the last line that hasn't been written to a file yet
:set buffer ($name . "," . $rx . "," . $tx);
} else= {
# variable size limit would not be exceeded. add line to buffer
:set $buffer ($buffer . $line);
}
}

# write out the last buffer
:local fileName ($id . "_BW_" . $stamp . "_" . $filenumber . ".txt");
/file print file=$fileName;
/file set $fileName contents="$buffer";
 
User avatar
ADahi
Member Candidate
Member Candidate
Posts: 209
Joined: Thu Sep 21, 2017 7:16 pm
Location: Iraq, Ninavah
Contact:

Re: Writing TX and RX bytes to file for all interfaces

Sat Dec 23, 2017 10:33 pm

check it now
{
:global time ([/system clock get time]);
:global hour [:pick $time 0 2];
:global min [:pick $time 3 5];
:global sec [:pick $time 6 8];
:global stamp ($hour . "_" . $min . "_" . $sec);
:global id ([/system identity get name]);
:global numports 100;
:global name " ";
:global rx " ";
:global tx " ";
:global line "Interface Name, Avg RX Byte, Avg TX Byte";
:global line2 "";
:global filenumber 1;
:global buffer;

:foreach INTERFACE in=[/interface find] do={

# record te parameters
:set name [/interface get $INTERFACE name];
:set rx [/interface get $INTERFACE rx-byte];
:set tx [/interface get $INTERFACE tx-byte];

# assemble them into a line
:set line ($line . "\n" . $name . "," . $rx . "," . $tx);

# add the buffer length and the string length
:local newLength ([:len $buffer] + [:len $line]);

# check if the buffer would now be larger than the variable size limit
:if ($newLength > 4095) do={

# it is. write out the current line buffer (without the latest line)
# first construct the file name
:local fileName ($id . "_BW_" . $stamp . "_" . $filenumber . ".txt");

# create the file as it doesn't exist yet
/file print file=$fileName;
delay 2s;

# write the buffer into it
/file set $fileName contents="$buffer";

# increase the file number
:set $fileNumber ($fileNumber + 1);

# reset the line buffer to just the last line that hasn't been written to a file yet
:set $buffer ($name . "," . $rx . "," . $tx);
} else= {
# variable size limit would not be exceeded. add line to buffer
:set $buffer ($buffer . $line);
}
}

# write out the last buffer
:local fileName ($id . "_BW_" . $stamp . "_" . $filenumber . ".txt");
/file print file=$fileName;
/file set $fileName contents="$buffer";
}
 
ariaz1
just joined
Topic Author
Posts: 3
Joined: Wed Dec 20, 2017 7:07 pm

Re: Writing TX and RX bytes to file for all interfaces

Tue Dec 26, 2017 6:55 pm

Hey, thanks for getting back to me! I did try running the code with your adjustments; it's able to create a new file now but it doesn't seem to save any of the interface information in it. And the file just continues to be over-written every few seconds, sort of like its stuck in a loop somewhere.
 
ariaz1
just joined
Topic Author
Posts: 3
Joined: Wed Dec 20, 2017 7:07 pm

Re: Writing TX and RX bytes to file for all interfaces  [SOLVED]

Wed Jan 03, 2018 10:31 pm

Hey ADahi!

So thanks to you, I was able to debug my code and I've got a working solution now. :) So, as a token of appreciation, just wanted to post the script here if anyone else has been looking for a way to achieve this or something similar to it.

Once again, thank you for your help!

-------------------------------------------------------------------------------------------------------

{
:global time ([/system clock get time]);
:global hour [:pick $time 0 2];
:global min [:pick $time 3 5];
:global sec [:pick $time 6 8];
:global stamp ($hour . "_" . $min . "_" . $sec);
:global id ([/system identity get name]);
:global numports 100;
:global name "";
:global rx "";
:global tx "";
:global line "Interface Name, Avg RX Byte, Avg TX Byte";
:global line2 "";
:global filenumber 1;
:global buffer "";
:global newLength 0;
:global debug "";

:set $buffer ($line);

:foreach INTERFACE in=[/interface find] do={

delay 2s;

# record the parameters
:set name [/interface get $INTERFACE name];
:set rx [/interface get $INTERFACE rx-byte];
:set tx [/interface get $INTERFACE tx-byte];

# assemble them into a line
:set line2 ($name . "," . $rx . "," . $tx);

# add the buffer length and the string length
:set newLength ([:len $buffer] + [:len $line2]);

# check if the buffer would now be larger than the variable size limit
:if ($newLength > 4095) do={

# it is. write out the current line buffer (without the latest line)
# first construct the file name
:local fileName ($id . "_BW_" . $stamp . "_" . $filenumber . ".txt");

# create the file as it doesn't exist yet
/file print file=$fileName;

delay 2s;

# write the buffer into it
/file set $fileName contents="$buffer";

# increase the file number
:set $filenumber ($filenumber + 1);

# reset the line buffer to just the last line that hasn't been written to a file yet
:set $buffer ($line . "\n" . $name . "," . $rx . "," . $tx);
:set $newLength 0;
} else= {
# variable size limit would not be exceeded. add line to buffer
[:set $buffer ($buffer . "\n" . $line2)]
}
}

# write out the last buffer
:local fileName ($id . "_BW_" . $stamp . "_" . $filenumber . ".txt");
/file print file=$fileName;

delay 2s;

/file set $fileName contents="$buffer";
set $debug ("Im here");
}

-------------------------------------------------------------------------------------------------------
check it now
{
:global time ([/system clock get time]);
:global hour [:pick $time 0 2];
:global min [:pick $time 3 5];
:global sec [:pick $time 6 8];
:global stamp ($hour . "_" . $min . "_" . $sec);
:global id ([/system identity get name]);
:global numports 100;
:global name " ";
:global rx " ";
:global tx " ";
:global line "Interface Name, Avg RX Byte, Avg TX Byte";
:global line2 "";
:global filenumber 1;
:global buffer;

:foreach INTERFACE in=[/interface find] do={

# record te parameters
:set name [/interface get $INTERFACE name];
:set rx [/interface get $INTERFACE rx-byte];
:set tx [/interface get $INTERFACE tx-byte];

# assemble them into a line
:set line ($line . "\n" . $name . "," . $rx . "," . $tx);

# add the buffer length and the string length
:local newLength ([:len $buffer] + [:len $line]);

# check if the buffer would now be larger than the variable size limit
:if ($newLength > 4095) do={

# it is. write out the current line buffer (without the latest line)
# first construct the file name
:local fileName ($id . "_BW_" . $stamp . "_" . $filenumber . ".txt");

# create the file as it doesn't exist yet
/file print file=$fileName;
delay 2s;

# write the buffer into it
/file set $fileName contents="$buffer";

# increase the file number
:set $fileNumber ($fileNumber + 1);

# reset the line buffer to just the last line that hasn't been written to a file yet
:set $buffer ($name . "," . $rx . "," . $tx);
} else= {
# variable size limit would not be exceeded. add line to buffer
:set $buffer ($buffer . $line);
}
}

# write out the last buffer
:local fileName ($id . "_BW_" . $stamp . "_" . $filenumber . ".txt");
/file print file=$fileName;
/file set $fileName contents="$buffer";
}

Who is online

Users browsing this forum: No registered users and 8 guests