Community discussions

MikroTik App
 
upower3
Member
Member
Topic Author
Posts: 425
Joined: Thu May 07, 2015 11:46 am

Get uptime in easy-to-parse form

Sun Jun 25, 2017 12:06 am

I'd like to get device uptime from within script or API call. If I query the device via SNMP I can get uptime via .1.3.6.1.2.1.1.3.0 OID, and the reply will be in seconds (e.g. 1558752). But when I try to get that via CLI or script or API, I got reply in human-readable form, like
3w6d15h29m11s
.

What's bad, I don't know the format (and no description is available), and the format may be just any, (say, like "years-months-THEN-weeks-days-hours-minutes-seconds" or simple "years-months-OR-weeks-etc" - and I have no a single device that's up for years to get full form to think about. I don't like that kind of inconsistency (the OID above and the /system resource print should return the same value, but it won't), and I'm afraid the human-readable format might be changed one day.

So, maybe there is another CLI command to get that info directly, as seconds? If so, I'd really appreciate any help with that!

Thank you!
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Get uptime in easy-to-parse form

Sun Jun 25, 2017 1:21 am

Weeks is the biggest component of time values. Anything above that (months, years) varies based on the date, and to keep the data type simple, scripting doesn't go as far as keeping track of a date. It's just a float formatted in descending order from weeks down to seconds, with seconds having the fraction part. Speaking of which, after a little trial & error, I can tell you the biggest time value you can have before things go out of whack (due to specifics of floats) is 15250w1d23h47m16s854ms775us807ns.

There is no scripting (and by extension, no API) command that can translate that into a number or other sort of a useful value.

My PHP API client includes a function that can parse it, but it does so "outside" of the API protocol... It's included in the library to provide extra ease of use (and then people tell me the library is heavy...). More specifically it parses times into PHP's DateInterval object (an analog to RouterOS' time type; in general, said function transforms strings into native PHP types), and from there you can use it in conjunction with PHP's DateTime to do some useful date and time calculations.

e.g.
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2_Net_RouterOS-1.0.0b6.phar';

$now = new DateTimeImmutable();
$interval = RouterOS\Script::parseValue('3w6d15h29m11s');
$later = $now->add($interval);

echo $later->format(DateTime::W3C);
(see also an actual example with "uptime" in the library's wiki)

If you're using an API client in another language, you'd need to have an analogous function implemented in that language. Here's the relevant source if you're willing to re-implement it.
 
upower3
Member
Member
Topic Author
Posts: 425
Joined: Thu May 07, 2015 11:46 am

Re: Get uptime in easy-to-parse form

Thu Jun 29, 2017 3:46 pm

Thank you, this was what I needed to know!
 
User avatar
maximan
Trainer
Trainer
Posts: 543
Joined: Sat May 29, 2004 12:10 am
Location: Rio Cuarto, Argentina
Contact:

Re: Get uptime in easy-to-parse form

Fri Jun 30, 2017 6:24 pm

 
upower3
Member
Member
Topic Author
Posts: 425
Joined: Thu May 07, 2015 11:46 am

Re: Get uptime in easy-to-parse form

Sat Jul 01, 2017 3:09 pm

I found this on python
Nice, thank you!

Who is online

Users browsing this forum: No registered users and 36 guests