/system resource> :put [get uptime];
{
:local getUptime [/system resource get uptime]
:put $getUptime
}
thank you my friend, but with this script is the moment when the equipment is turned off due to the power cut and also when the electricity is restored?To retrieve the uptime:
And in into a variable:Code: Select all/system resource> :put [get uptime];
Code: Select all{ :local getUptime [/system resource get uptime] :put $getUptime }
You can use the firewall-protocols 7 as a place to store a variable value.thank you my friend, but with this script is the moment when the equipment is turned off due to the power cut and also when the electricity is restored?To retrieve the uptime:
And in into a variable:Code: Select all/system resource> :put [get uptime];
Code: Select all{ :local getUptime [/system resource get uptime] :put $getUptime }
is there some way for the acript to record in a .txt file. So I know the time when the electric service was interrupted and the time when the service was restored. Thank you.
:global persistVar do={
:local varName $1;
:local varValue $2;
:local varID [/ip firewall layer7-protocol find name="$varName"];
:if ([:typeof $varValue] = "nothing") do={
:if ($varID != "") do={
:set $varValue [/ip firewall layer7-protocol get $varID value-name=regexp];
}
} else={
:if ($varID = "") do={
/ip firewall layer7-protocol add name="$varName" regexp="$varValue"
} else={
/ip firewall layer7-protocol set $varID regexp="$varValue"
}
}
return $varValue
}
Armando, your script runs but doesn't even give the regex a nameYou can use the firewall-protocols 7 as a place to store a variable value.thank you my friend, but with this script is the moment when the equipment is turned off due to the power cut and also when the electricity is restored?To retrieve the uptime:
And in into a variable:Code: Select all/system resource> :put [get uptime];
Code: Select all{ :local getUptime [/system resource get uptime] :put $getUptime }
is there some way for the acript to record in a .txt file. So I know the time when the electric service was interrupted and the time when the service was restored. Thank you.
You create a global variable which defines the mechanism to write/read to variables into layer7 protocol:
Then in order to call this "function" and write the value into it, just call:Code: Select all:global persistVar do={ :local varName $1; :local varValue $2; :local varID [/ip firewall layer7-protocol find name="$varName"]; :if ([:typeof $varValue] = "nothing") do={ :if ($varID != "") do={ :set $varValue [/ip firewall layer7-protocol get $varID value-name=regexp]; } } else={ :if ($varID = "") do={ /ip firewall layer7-protocol add name="$varName" regexp="$varValue" } else={ /ip firewall layer7-protocol set $varID regexp="$varValue" } } return $varValue }
$persistVar Uptime $getUptime; <-- where Uptime will be written as a variable in Layer7 protocol.
So in this case even during outage such value will be kept there.
It's an equivalent of writing/reading to file.
Armando.
The script that i have shown you is only a function to write and ready from layer7 protocol used as storage place.Armando, your script runs but doesn't even give the regex a nameYou can use the firewall-protocols 7 as a place to store a variable value.thank you my friend, but with this script is the moment when the equipment is turned off due to the power cut and also when the electricity is restored?To retrieve the uptime:
And in into a variable:Code: Select all/system resource> :put [get uptime];
Code: Select all{ :local getUptime [/system resource get uptime] :put $getUptime }
is there some way for the acript to record in a .txt file. So I know the time when the electric service was interrupted and the time when the service was restored. Thank you.
You create a global variable which defines the mechanism to write/read to variables into layer7 protocol:
Then in order to call this "function" and write the value into it, just call:Code: Select all:global persistVar do={ :local varName $1; :local varValue $2; :local varID [/ip firewall layer7-protocol find name="$varName"]; :if ([:typeof $varValue] = "nothing") do={ :if ($varID != "") do={ :set $varValue [/ip firewall layer7-protocol get $varID value-name=regexp]; } } else={ :if ($varID = "") do={ /ip firewall layer7-protocol add name="$varName" regexp="$varValue" } else={ /ip firewall layer7-protocol set $varID regexp="$varValue" } } return $varValue }
$persistVar Uptime $getUptime; <-- where Uptime will be written as a variable in Layer7 protocol.
So in this case even during outage such value will be kept there.
It's an equivalent of writing/reading to file.
Armando.
This script will write into firewall -> layer7 protocol a value of val_uptime with the uptime from your router.ARMANDO, it is very difficult for me to make this script could you help me. Thank you.
:global persistVar do={
:local varName $1;
:local varValue $2;
:local varID [/ip firewall layer7-protocol find name="$varName"];
:if ([:typeof $varValue] = "nothing") do={
:if ($varID != "") do={
:set $varValue [/ip firewall layer7-protocol get $varID value-name=regexp];
}
} else={
:if ($varID = "") do={
/ip firewall layer7-protocol add name="$varName" regexp="$varValue"
} else={
/ip firewall layer7-protocol set $varID regexp="$varValue"
}
}
return $varValue
}
:do {
$persistVar val_uptime [/system resource get uptime];
} on-error={ :log error "Script error "};
friend ARMANDO, the script records the RB's activity time, but what I need is that in one regexp he tells me the activity time and in another regexp the new activity time so that we know at what time the electricity was interrupted and at what time They connected the electricity again, I don't know if you understand me. Thank you.This script will write into firewall -> layer7 protocol a value of val_uptime with the uptime from your router.ARMANDO, it is very difficult for me to make this script could you help me. Thank you.
You can start from here and change it to your needs.
Code: Select all:global persistVar do={ :local varName $1; :local varValue $2; :local varID [/ip firewall layer7-protocol find name="$varName"]; :if ([:typeof $varValue] = "nothing") do={ :if ($varID != "") do={ :set $varValue [/ip firewall layer7-protocol get $varID value-name=regexp]; } } else={ :if ($varID = "") do={ /ip firewall layer7-protocol add name="$varName" regexp="$varValue" } else={ /ip firewall layer7-protocol set $varID regexp="$varValue" } } return $varValue } :do { $persistVar val_uptime [/system resource get uptime]; } on-error={ :log error "Script error "};
As MK support told you, router won't know when power outage.
friend ARMANDO, the script records the RB's activity time, but what I need is that in one regexp he tells me the activity time and in another regexp the new activity time so that we know at what time the electricity was interrupted and at what time They connected the electricity again, I don't know if you understand me. Thank you.
:global persistVar do={
:local varName $1;
:local varValue $2;
:local varID [/ip firewall layer7-protocol find name="$varName"];
:if ([:typeof $varValue] = "nothing") do={
:if ($varID != "") do={
:set $varValue [/ip firewall layer7-protocol get $varID value-name=regexp];
}
} else={
:if ($varID = "") do={
/ip firewall layer7-protocol add name="$varName" regexp="$varValue"
} else={
/ip firewall layer7-protocol set $varID regexp="$varValue"
}
}
return $varValue
}
:do {
:local day ([:pick [/system clock get date] 4 6]);
:local mon ([:pick [/system clock get date] 0 3]);
:local year ([:pick [/system clock get date] 7 11]);
:local date "$day-$mon-$year";
:local time (:put [/system clock get time]);
:local data1 ("Uptime is " . [/system resource get uptime] . " --- " . $date . " --- " . $time);
$persistVar val_uptime $data1;
} on-error={ :log error "Script1 error"};
:global persistVar do={
:local varName $1;
:local varValue $2;
:local varID [/ip firewall layer7-protocol find name="$varName"];
:if ([:typeof $varValue] = "nothing") do={
:if ($varID != "") do={
:set $varValue [/ip firewall layer7-protocol get $varID value-name=regexp];
}
} else={
:if ($varID = "") do={
/ip firewall layer7-protocol add name="$varName" regexp="$varValue"
} else={
/ip firewall layer7-protocol set $varID regexp="$varValue"
}
}
return $varValue
}
:do {
:local day ([:pick [/system clock get date] 4 6]);
:local mon ([:pick [/system clock get date] 0 3]);
:local year ([:pick [/system clock get date] 7 11]);
:local date "$day-$mon-$year";
:local time (:put [/system clock get time]);
:local data1 ("Router restarted on " . $date . " --- " . $time);
$persistVar val_restarted $data1;
} on-error={ :log error "Script2 error"};
Well because the logic was simply to record the data into a permanent variable, nothing more.ARMANDO, thanks for the scripts, but the first one doesn't work since when restarted the RB takes the same time as the second script
:global persistVar do={
:local varName $1;
:local varValue $2;
:local varID [/ip firewall layer7-protocol find name="$varName"];
:if ([:typeof $varValue] = "nothing") do={
:if ($varID != "") do={
:set $varValue [/ip firewall layer7-protocol get $varID value-name=regexp];
}
} else={
:if ($varID = "") do={
/ip firewall layer7-protocol add name="$varName" regexp="$varValue"
} else={
/ip firewall layer7-protocol set $varID regexp="$varValue"
}
}
return $varValue
}
:global stopScript false;
:do {
:local day ([:pick [/system clock get date] 4 6]);
:local mon ([:pick [/system clock get date] 0 3]);
:local year ([:pick [/system clock get date] 7 11]);
:local date "$day-$mon-$year";
:local time (:put [/system clock get time]);
:local data1 ("Uptime is " . [/system resource get uptime] . " --- " . $date . " --- " . $time);
$persistVar val_uptime $data1;
:delay 1;
} while=(!$stopScript)
:set $stopScript true;
Change the delay to your needs: 60 will be 1 minute
:delay 60;
I'm glad that this little script helped you out as well.Well because the logic was simply to record the data into a permanent variable, nothing more.
You can try this new approach by storing the current uptime + date and time to a permanent variable.
The script will run as a loop and will update the variable on Layer7 protocol every 1 second.
Well, it helped a lot in my situation.
If you want to write the info to a file instead of layer7 protocol, you can use this version.that was the script dear brother. Thank you.
just out of curiosity, is there a way that instead of layer protocol 7 it's copied to a file
:global stopRouterRun false;
:do {
:local file "flash/uptime.txt";
:local day ([:pick [/system clock get date] 4 6]);
:local mon ([:pick [/system clock get date] 0 3]);
:local year ([:pick [/system clock get date] 7 11]);
:local date "$day-$mon-$year";
:local time (:put [/system clock get time]);
:local data1 ("Uptime is " . [/system resource get uptime] . " --- Stored on " . $date . " at " . $time);
/file print file="$file"
/file set $file contents=$data1;
:delay 60;
} while=(!$stopRouterRun)
:global stopRouterRun false;
:do {
:local file "uptime.txt";
:local day ([:pick [/system clock get date] 4 6]);
:local mon ([:pick [/system clock get date] 0 3]);
:local year ([:pick [/system clock get date] 7 11]);
:local date "$day-$mon-$year";
:local time (:put [/system clock get time]);
:local data1 ("Uptime is " . [/system resource get uptime] . " --- Stored on " . $date . " at " . $time);
/file print file="$file"
/file set $file contents=$data1;
:delay 60;
} while=(!$stopRouterRun)
If you want to write the info to a file instead of layer7 protocol, you can use this version.that was the script dear brother. Thank you.
just out of curiosity, is there a way that instead of layer protocol 7 it's copied to a file
However you should read this info about writing to disk, which depends on the router and disk type that you are using: https://wiki.mikrotik.com/wiki/Manual:System/File
Read about warning and notes.
If your router does have a flash disk you can use this version that writes the info every 1 minute.Code: Select all:global stopRouterRun false; :do { :local file “flash/uptime.txt”; :local day ([:pick [/system clock get date] 4 6]); :local mon ([:pick [/system clock get date] 0 3]); :local year ([:pick [/system clock get date] 7 11]); :local date "$day-$mon-$year"; :local time (:put [/system clock get time]); :local data1 ("Uptime is " . [/system resource get uptime] . " --- Stored on " . $date . " at " . $time); /file print file=“$file” /file set $file contents=$data1; :delay 60; } while=(!$stopRouterRun)
Otherwise if your router uses NAND type of memory storage, you can use this.
This will write data into file and overwrites its content with the latest info every minute. Same principle as before, when you will experience a power outage the script will stop writing into file and you will see the last timestamp in there. However as indicated in the notes, if your router does not use flash type memory, you might have up to 40 seconds delay before data is written from memory to disk, so you might loose some event within the minute.Code: Select all:global stopRouterRun false; :do { :local file “uptime.txt”; :local day ([:pick [/system clock get date] 4 6]); :local mon ([:pick [/system clock get date] 0 3]); :local year ([:pick [/system clock get date] 7 11]); :local date "$day-$mon-$year"; :local time (:put [/system clock get time]); :local data1 ("Uptime is " . [/system resource get uptime] . " --- Stored on " . $date . " at " . $time); /file print file=“$file” /file set $file contents=$data1; :delay 60; } while=(!$stopRouterRun)
So you can decide to change delay 60 to something less so that your reading is more accurate.
Armando
Dear Frank, are you sure ?
Dear ARMANDO, the text is created in the file, but it is not overwritten every minute, which will happen there. Thank you.
Dear Frank, are you sure ?
Dear ARMANDO, the text is created in the file, but it is not overwritten every minute, which will happen there. Thank you.
Please check better, because the text inside the file is overwritten every minute.
Let it run for few minutes and check at the end when you will reboot your router.
I have tested it with my router and it did update the text each minute.
Armando
So I guess we can conclude that our routers are behaving differently.
I just tested it and it does not overwrite the data, it creates the file yes, but it does not overwrite the data every minute.
So I guess we can conclude that our routers are behaving differently.
I just tested it and it does not overwrite the data, it creates the file yes, but it does not overwrite the data every minute.
Now it's up to you what solution you will adopt.
I gave you all the help possible, now it's on you.
Take care,
Armando.
Friend ARMANDO, if the two scripts worked for me, the one for layer 7 and the one for text in the file, very grateful for your collaboration and sorry for so much inconvenience.I don't understand what you are asking. There is nothing to put on layer7.
Use the solution that I gave you with reply #13 on this message, where I used the persistent variable in layer7.
And in case you wanted to combine both persistent variable and files, you can use this final version with both options in the same script.Friend ARMANDO, if the two scripts worked for me, the one for layer 7 and the one for text in the file, very grateful for your collaboration and sorry for so much inconvenience.
thank you very much, god bless you.
:global persistVar do={
:local varName $1;
:local varValue $2;
:local varID [/ip firewall layer7-protocol find name="$varName"];
:if ([:typeof $varValue] = "nothing") do={
:if ($varID != "") do={
:set $varValue [/ip firewall layer7-protocol get $varID value-name=regexp];
}
} else={
:if ($varID = "") do={
/ip firewall layer7-protocol add name="$varName" regexp="$varValue"
} else={
/ip firewall layer7-protocol set $varID regexp="$varValue"
}
}
return $varValue
}
:global stopRouterRun false;
:do {
:local file "uptime.txt";
:local day ([:pick [/system clock get date] 4 6]);
:local mon ([:pick [/system clock get date] 0 3]);
:local year ([:pick [/system clock get date] 7 11]);
:local date "$day-$mon-$year";
:local time (:put [/system clock get time]);
:local data1 ("Uptime is " . [/system resource get uptime] . " - Stored on " . $date . " at " . $time);
$persistVar val_uptime $data1;
/file print file="$file"
/file set $file contents=$data1;
:delay 60;
} while=(!$stopRouterRun)
When you check the message that solved your question, let's say replay 26, if you see in the top-right corner where you can quote/reply there is a function to mark the topic as solved. Once you pass with your mouse you will see the help-pop up telling you what each function does.ARMANDO, How can I leave this topic as resolved?
Click on the circled button in right top corner of the posting with the solution:ARMANDO, How can I leave this topic as resolved?
Hi Armando, this script looks interesting as it could be used also for other such cases.And in case you wanted to combine both persistent variable and files, you can use this final version with both options in the same script.
Armando
Hi mutluit,
Hi Armando, this script looks interesting as it could be used also for other such cases.
Can you briefly comment the code of what it does and how it does, maybe on top of the code itself.
I must admit I've not understood the role of that said layer-7 stuff w.r.t. to this issue.
And is this true persistence? I mean, will the value still be there also if the device reboots?
Thx
:global persistVar do={
:local varName $1;
:local varValue $2;
:local varID [/ip firewall layer7-protocol find name="$varName"];
:if ([:typeof $varValue] = "nothing") do={
:if ($varID != "") do={
:set $varValue [/ip firewall layer7-protocol get $varID value-name=regexp];
}
} else={
:if ($varID = "") do={
/ip firewall layer7-protocol add name="$varName" regexp="$varValue"
} else={
/ip firewall layer7-protocol set $varID regexp="$varValue"
}
}
return $varValue
}
Yes, now it's clear. Thanks also for this explanation.Hi mutluit,
Hi Armando, this script looks interesting as it could be used also for other such cases.
Can you briefly comment the code of what it does and how it does, maybe on top of the code itself.
I must admit I've not understood the role of that said layer-7 stuff w.r.t. to this issue.
And is this true persistence? I mean, will the value still be there also if the device reboots?
Thx
beside the many replies on this topic, the first message that I sent was the main part of this script.
Basically it's a simple way to get benefit of using /ip firewall Layer7Protocol as a persistent storage for variable/value, instead of using disk.
If you check the main code, that goes as global variable, you can see how it works.
So in short, you use this like a function in your main script for when you want to save values even after reboot/crash of your router.
In order to write a value you do: $persistValue <variable> <value>
If you check with the code, when both VarName and VarValue have something, you get to the point where you write your record into regex for layer7-protocol.
If VarValue is not provided is when you use this function to read the value from the existing variable.
Therefore to read a value you do: $persistValue <variable> and the procedure will return its value.
This because you will enter in the first part of IF, where you don't provide a value, then if the variable has been set, you will get its value.
So at the end this is a simple alternative to write values into file, without loosing the capability to store such value permanently into that regex field of layer7 protocol.
So it's an unintended use of layer7 protocol part of the router feature, but since it's there, why not.
Hope this clarified more about the script.
Armando.
I am very new to the forum, is there something where they explain the use of the forum?
How can I send private messages?
How do you copy this image?I am very new to the forum, is there something where they explain the use of the forum?
How can I send private messages?
For PM click on the user: if the user allows PM (in his profile settings) then there will be a link for sending.
Have a look at your own profile page below, right-bottom side.
On "The Team" link ( memberlist.php?mode=team ) at bottom of each page you can find the forum moderators and the administrators.
Btw, you better should mark posting #13 or #17 or #31 as the solution b/c the solution is in these postings, not in the currently marked posting #28.