write to file

Hello!

I am trying to make a script like this.

:global test;
:global tekst1;
:global tekst2;
:global tekst3;
:global tekst4 ",";
/file set test.txt contents=" " 
:for i from=0 to=10 do={/queue simple find comment=0
:set tekst1 [/queue simple get $i bytes]
:set tekst2 [/queue simple get $i target-addresses]
:set tekst3 ($tekst2 . $tekst4 . $tekst1)
/file set test.txt content=$tekst3}

I can only see the last line
Haw can I make it, so I can see all 10 lines

You’re overwriting the file in every loop iteration with the latest line. Instead you should accumulate lines in another variable and write out after the loop. You also don’t need globals for any of this.

:local newFileContents;
:for i from=0 to=10 do={
  /queue simple find comment=0;
  :local bytes [/queue simple get $i bytes];
  :local address [/queue simple get $i target-addresses];
  :set newFileContents ($newFileContents . "\n" . $bytes . "," . $address);
}
/file set text.txt content=$newFileContents

On a sidenote, I don’t understand what you’re trying to do here. Your code checks strictly simple queues 0 through 10. That’s 11 lines total. What purpose is the ‘/queue simple find’ instruction supposed to have? Are you trying to find specific queues?

Thank you for the reply
I have 400 queues and that is more then 4Kb, so I try to divide in 3 files, but that do not the trick

This is off the cuff and untested but might work. This will remove all files that contain the string “queues” in their filename from the file system, so you may have to adjust the naming conventions.

# keep track of what number file this is
:local fileNumber 1;
# create buffer of lines to put into a file
:local buffer;
# delete all previous files
/file remove [/file find where name~"queues"];
# go through each simple queue
:foreach QUEUE in=[/queue simple find] do={
# record the parameters
	:local bytes [/queue simple get $QUEUE bytes];
	:local address [/queue simple get $QUEUE target-addresses];
# assemble them into a line
	:local line ("\n" . $bytes . "," . $address)
# 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 ("queues" . $fileNumber . ".txt");
# create the file as it doesn't exist yet
		/file print file=$fileName;
# 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 ($bytes . "," . $address);
	} else= {
# variable size limit would not be exceeded. add line to buffer
	:set $buffer ($buffer . $line);
	}
}
# write out the last buffer
:local fileName ("queues" . $fileNumber . ".txt");
/file print file=$fileName;
/file set $fileName contents="$buffer";

Hope that helps (and works).

However, can’t you just collect this information via SNMP? That would scale better, and is pretty much what network monitoring was made for. Also, unless the simple queues are automatically created for PPPoE or Hotspot users you would be better off using PCQ. 400 manual simple queues is too much.

I use P.C.Q, simple queues is only for graphing. I will look at SNMP
In the meantime I will use your script

thanks this works well