Community discussions

MikroTik App
 
eldoncito2019
Member
Member
Topic Author
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

Register file. txt uptime

Sun Apr 17, 2022 7:39 pm

hello guys, you can make a .txt file to record the time the routerboard was started for the first time with the following data:

Date: Apr/17/2022
Time: 13:10:10
Uptime: 1d 00:09:20

Thanks for your help.




EL DONCITO:
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Register file. txt uptime

Tue Apr 19, 2022 7:10 pm

hello guys, you can make a .txt file to record the time the routerboard was started for the first time with the following data:

Date: Apr/17/2022
Time: 13:10:10
Uptime: 1d 00:09:20

Thanks for your help.

EL DONCITO:
Try this:
:local newdate [/system clock get date]
:local newtime [/system clock get time]
:local newuptime [/system resource get uptime]
:local filename "infosystem.txt"

/file
:if ([:len [find where name="flash" and type="disk"]] = 1) do={:set filename "flash/$filename"}
:if ([:len [find where name=$filename]] = 0) do={print file="$filename"; :delay 5s; set $filename contents=""}

:delay 5s
:local filecontent [get $filename contents]
:local newfilecontent "Info: $newdate\t$newtime\t$newuptime\r\n$filecontent"
set $filename contents=$newfilecontent
File content:
Info: apr/19/2022	18:06:50	5d04:52:13
Info: apr/19/2022	18:06:40	5d04:52:03
Info: apr/19/2022	18:06:25	5d04:51:48
Info: apr/19/2022	18:06:02	5d04:51:26
BR.
 
eldoncito2019
Member
Member
Topic Author
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

Re: Register file. txt uptime

Tue Apr 19, 2022 7:58 pm

Thanks friend, but it is not what I need, what I need is that according to the uptime, you can give me the information at what time the routerboard reboot.




EL DONCITO.
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Register file. txt uptime

Tue Apr 19, 2022 9:03 pm

Thanks friend, but it is not what I need, what I need is that according to the uptime, you can give me the information at what time the routerboard reboot.
You will have to get in your WAN interface -> Status, the date in the "Last Link UP Time" field in a global variable.

BR.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Register file. txt uptime

Tue Apr 19, 2022 9:05 pm

@diamuxin
Why all that?
And why you post the script than I have already maked for @eldoncito2019?


@eldoncito2019
Again with "started for the fisrt time"?
How many topic you want open for the same identical thing?
viewtopic.php?p=924455#p924455
viewtopic.php?p=925553#p925553
viewtopic.php?p=926634#p925553
 
eldoncito2019
Member
Member
Topic Author
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

Re: Register file. txt uptime

Tue Apr 19, 2022 10:10 pm

I need your help friends.




EL DONCITO.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Register file. txt uptime

Tue Apr 19, 2022 11:02 pm

It will keep the same errors on the other scripts:
do not write the right time until the time is not syncronized,
but until you do not want use (S)NTP, you do not know when the time is sincronized
and if you put a delay of 10 min or less for be sure the time is synced, surely you also want calculate the uptime / time set differencies for write in the file the right time.
Nothing familiar?
Everytime the same things...


And, yes, I help you again, but not now, I must go home,
I write later.
If I forgot this, write here.
Thanks.


Some hint for myself later:
Check if http://worldclockapi.com/api/json/cet/now is reachable,
when reachable read http://worldclockapi.com/api/json/cet/now
the datetime is after currentDateTime":" and before ","utcOffset
split date and time at the T
remove TimeZone after time
write the string in the file with uptime
done.

EL DONCITO: what are your time zone name? (not +08:00, but name like CET, Central Europe Time)
 
eldoncito2019
Member
Member
Topic Author
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

Re: Register file. txt uptime

Wed Apr 20, 2022 12:08 am

if I can go to: http://worldclockapi.com/api/json/cet/now
I can read; http://worldclockapi.com/api/json/cet/now
and my time zone is: america/ caracas and the GMT offset: -04:00





EL DONCITO.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Register file. txt uptime

Wed Apr 20, 2022 10:10 am

Venezuela Standard Time / VST
http://worldclockapi.com/api/json/vst/now

+

viewtopic.php?f=2&t=178355&p=878643#p878643

=

Step 1)
Check if the URL is available, and display what is get from URL:
/file remove [find where name="testfetch.txt"]
{
    # seconds of timeout, after that time stop try get the time from url
    :local timeout 120
    :local jobid [:execute file=testfetch.txt script="/tool fetch url=\"http://worldclockapi.com/api/json/vst/now\""]
    :put "\r\nWaiting the end of process for file testfetch.txt to be ready, max $timeout seconds..."
    :global Gltesec 0
    :while (([:len [/sys script job find where .id=$jobid]] = 1) && ($Gltesec < $timeout)) do={
        :set Gltesec ($Gltesec + 1)
        :delay 1s
        :put "waiting... $Gltesec"
    }
    :put "Done. Elapsed Seconds: $Gltesec\r\n"
    :if ([:len [/file find where name="testfetch.txt"]] = 1) do={
        :local filecontent [/file get [/file find where name="testfetch.txt"] contents]
        :put "Result of Fetch:\r\n****************************\r\n$filecontent\r\n****************************"
        :if ([:len [/file find where name="now"]] = 1) do={
            :local filecontent [/file get [/file find where name="now"] contents]
            :put "\r\nFile content:\r\n****************************\r\n$filecontent\r\n****************************"
        } else={
            :put "\r\nURL not reached."
        }
    } else={
        :put "Generic error, Test file not created."
    }
}
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Register file. txt uptime

Wed Apr 20, 2022 10:41 am

Step 2)
Elaborate file content and get date and time:
/file remove [find where name="testfetch.txt"]
{
    # seconds of timeout, after that time stop try get the time from url
    :local timeout 120
    :local jobid [:execute file=testfetch.txt script="/tool fetch url=\"http://worldclockapi.com/api/json/vst/now\""]
    :put "\r\nWaiting the end of process for file testfetch.txt to be ready, max $timeout seconds..."
    :local Gltesec 0
    :while (([:len [/sys script job find where .id=$jobid]] = 1) && ($Gltesec < $timeout)) do={
        :set Gltesec ($Gltesec + 1)
        :delay 1s
        :put "waiting... $Gltesec"
    }
    :put "Done. Elapsed Seconds: $Gltesec\r\n"
    :if ([:len [/file find where name="testfetch.txt"]] = 1) do={
        :local filecontent [/file get [/file find where name="testfetch.txt"] contents]
        :put "Result of Fetch:\r\n****************************\r\n$filecontent\r\n****************************"
        :if ([:len [/file find where name="now"]] = 1) do={
            :local filecontent [/file get [/file find where name="now"] contents]
            :put "\r\nFile content:\r\n****************************\r\n$filecontent\r\n****************************"
            :local dstart ([:find $filecontent "currentDateTime\":\"" -1] + 18)
            :local year  [:pick $filecontent $dstart ($dstart + 4)]
            :local month [:pick $filecontent ($dstart + 5 ) ($dstart + 7)]
            :local day   [:pick $filecontent ($dstart + 8 ) ($dstart + 10)]
            :local hm    [:pick $filecontent ($dstart + 11 ) ($dstart + 16)]
            :put "\r\nTime readed from the URL: $year-$month-$day $hm\r\n"
        } else={
            :put "\r\nURL not reached."
        }
    } else={
        :put "Generic error, Test file not created."
    }
}
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Register file. txt uptime

Wed Apr 20, 2022 10:52 am

Step 3)
Add file creation
/file remove [find where name="testfetch.txt"]
{
    :local filename "lastupurl.txt"
    # seconds of timeout, after that time stop try get the time from url
    :local timeout 120
    :local jobid [:execute file=testfetch.txt script="/tool fetch url=\"http://worldclockapi.com/api/json/vst/now\""]
    :put "\r\nWaiting the end of process for file testfetch.txt to be ready, max $timeout seconds..."
    :local Gltesec 0
    :while (([:len [/sys script job find where .id=$jobid]] = 1) && ($Gltesec < $timeout)) do={
        :set Gltesec ($Gltesec + 1)
        :delay 1s
        :put "waiting... $Gltesec"
    }
    :put "Done. Elapsed Seconds: $Gltesec\r\n"
    :if ([:len [/file find where name="testfetch.txt"]] = 1) do={
        :local filecontent [/file get [/file find where name="testfetch.txt"] contents]
        :put "Result of Fetch:\r\n****************************\r\n$filecontent\r\n****************************"
        :if ([:len [/file find where name="now"]] = 1) do={
            :local filecontent [/file get [/file find where name="now"] contents]
            :put "\r\nFile content:\r\n****************************\r\n$filecontent\r\n****************************"
            :local dstart ([:find $filecontent "currentDateTime\":\"" -1] + 18)
            :local year  [:pick $filecontent $dstart ($dstart + 4)]
            :local month [:pick $filecontent ($dstart + 5 ) ($dstart + 7)]
            :local day   [:pick $filecontent ($dstart + 8 ) ($dstart + 10)]
            :local hm    [:pick $filecontent ($dstart + 11 ) ($dstart + 16)]
            :put "\r\nTime readed from the URL: $year-$month-$day $hm\r\n"
            /file
            :if ([:len [find where name="flash" and type="disk"]] = 1) do={:set filename "flash/$filename"}
            :if ([:len [find where name=$filename]] = 0) do={print file="$filename"; :delay 5s; set $filename contents=""}
            :delay 5s
            :local newfilecontent "Date: $year-$month-$day\r\nTime: $hm\r\nUptime: $[/system resource get uptime]"
            set $filename contents=$newfilecontent
        } else={
            :put "\r\nURL not reached."
        }
    } else={
        :put "Generic error, Test file not created."
    }
    /file remove [find where name="testfetch.txt"]
    /file remove [find where name="now"]
}
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Register file. txt uptime

Wed Apr 20, 2022 11:37 am

Special EL DONCITO version
Date on abbreviated venezuelan montn name/ day / year and Time on AM/PM format
viewtopic.php?p=925177#p925010
viewtopic.php?p=926000#p926000

{
    :local filename "lastupurl.txt"
    :local timeout 60
    :local url "http://worldtimeapi.org/api/timezone/America/Caracas.txt"
    /file
    remove [find where name="fetchtest.txt"]
    remove [find where name="fetchnow.txt"]
    :local jobid [:execute file=fetchtest.txt script="/tool fetch url=\"$url\" dst-path=fetchnow.txt"]
    :local Gltesec 0
    :while (([:len [/sys script job find where .id=$jobid]] = 1) && ($Gltesec < $timeout)) do={:set Gltesec ($Gltesec + 1); :delay 1s}
    :if ([:len [find where name="fetchnow.txt"]] = 1) do={
        :local filecontent [get [find where name="fetchnow.txt"] contents]
        :local dstart ([:find $filecontent "datetime: " -1] + 10)
        :local year  [:pick $filecontent $dstart ($dstart + 4)]
        :local month [:pick $filecontent ($dstart + 5 ) ($dstart + 7)]
        :local day   [:pick $filecontent ($dstart + 8 ) ($dstart + 10)]
        :local hms   [:pick $filecontent ($dstart + 11 ) ($dstart + 19)]
        :if ([:len [find where name="flash" and type="disk"]] = 1) do={:set filename "flash/$filename"}
        :if ([:len [find where name=$filename]] = 0) do={print file="$filename"; :delay 5s; set $filename contents=""}
        :delay 5s
            :global arrMonths {"01"="ENE";"02"="FEB";"03"="MAR";"04"="ABR";"05"="MAY";"06"="JUN";\
                "07"="JUL";"08"="AGO";"09"="SEP";"10"="OCT";"11"="NOV";"12"="DIC"}
            :local mhours [:tonum [:pick $hms 0 2]]
            :local minsec [:pick $hms 2 8]
            :local msymbl "AM"
            :if ($mhours > 11) do={:set msymbl "PM"}
            :set mhours ($mhours % 12)
            :if ($mhours = 0) do={:set mhours 12}
            :if ($mhours < 10) do={:set mhours "0$mhours"}
        set $filename contents="Date: $($arrMonths->$month)/$day/$year\r\nTime: $mhours$minsec $msymbl\r\n\
            Uptime: $[/system resource get uptime]"
    }
    remove [find where name="fetchtest.txt"]
    remove [find where name="fetchnow.txt"]
}

lastupurl.txt code

Date: ABR/20/2022
Time: 04:49:23 AM
Uptime: 00:02:26
Last edited by rextended on Wed Apr 20, 2022 11:57 am, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Register file. txt uptime

Wed Apr 20, 2022 11:55 am

International version
Date on ISO format year-month-day and timezone UTC
{
    :local filename "lastupurl.txt"
    :local timeout 60
    :local url "http://worldtimeapi.org/api/timezone/Etc/UTC.txt"
    /file
    remove [find where name="fetchtest.txt"]
    remove [find where name="fetchnow.txt"]
    :local jobid [:execute file=fetchtest.txt script="/tool fetch url=\"$url\" dst-path=fetchnow.txt"]
    :local Gltesec 0
    :while (([:len [/sys script job find where .id=$jobid]] = 1) && ($Gltesec < $timeout)) do={:set Gltesec ($Gltesec + 1); :delay 1s}
    :if ([:len [find where name="fetchnow.txt"]] = 1) do={
        :local filecontent [get [find where name="fetchnow.txt"] contents]
        :local dstart ([:find $filecontent "datetime: " -1] + 10)
        :local year  [:pick $filecontent $dstart ($dstart + 4)]
        :local month [:pick $filecontent ($dstart + 5 ) ($dstart + 7)]
        :local day   [:pick $filecontent ($dstart + 8 ) ($dstart + 10)]
        :local hms   [:pick $filecontent ($dstart + 11 ) ($dstart + 19)]
        :if ([:len [find where name="flash" and type="disk"]] = 1) do={:set filename "flash/$filename"}
        :if ([:len [find where name=$filename]] = 0) do={print file="$filename"; :delay 5s; set $filename contents=""}
        :delay 5s
        set $filename contents="Date: $year-$month-$day\r\nTime: $hms\r\nUptime: $[/system resource get uptime]"
    }
    remove [find where name="fetchtest.txt"]
    remove [find where name="fetchnow.txt"]
}

lastupurl.txt code

Date: 2022-04-20
Time: 08:49:23
Uptime: 00:02:26

Who is online

Users browsing this forum: eldoncito2019 and 24 guests