salsa has asked for the wisdom of the Perl Monks concerning the following question:

I'm currently working on a little script to go out and query our switches for IP thoughput, broadcast throughput, and basic utilization, and everyhting seems to be going well but for one small thing.. My system uptime that my managers want to see is being returned to me in 'ticks'. I am fairly sure there is an easy way to convert this value to a human readable stamp, but have hit the proverbial brickwall. Does anyone here know how that stamp is formatted so I can write a subroutine to convert it to 'managerese'? I'm assuming it looks something like Hours:Min:Sec:Millisec or somesuch, but I am probably wrong as usual.. I would appreciate any help you can offer! Thanks! Here is a little snippet to fetch the system uptime for those that may not know what I referring to:
#!/usr/local/bin/perl -w use strict; use warnings; use diagnostics; use SNMP; ### Equipment Array ### our @Equipment = ( "10.190.16.200", ); ### Equipment Type Hash ### our %EquipmentType = ( "10.190.16.200" => "switch", ); ### Switch MIB Hash ### our %SwitchMIBs = ( "uptime" => "sysUpTime,0", "description" => "sysDescr,0", ); our $uptime; our $snmp; # Main { my $Equipment; foreach $Equipment(@Equipment) { if ($EquipmentType{$Equipment} =~ "switch") { $snmp = new SNMP::Session(DestHost=> "$Equipment", Community => 'emacs2000', ); $uptime=$snmp->get("$SwitchMIBs{uptime}"); printf ("Uptime for $Equipment is $uptime\n"); } } exit; }

Replies are listed 'Best First'.
Re: Conveting SNMP-returned ticks to
by DrManhattan (Chaplain) on Nov 26, 2001 at 23:53 UTC
    A "tick" is 1/100th of a second.

    -Matt

      Right! So am I to understand that the returned 'tick' time on my switch sysUpTime.0, is an entirely base 10 number, and not directly correlating to a "human time"? Or is there a formatting that can be resolved directly from it via DD:HH:MM:SS:.01(sec) or some such? Or do I need to chop off the last two digits (the partial seconds) and then start reverse engineering the base10 time back to a DD:HH:MM:SS format manually through division by 60, then 3600, etc.? Like: $uptime=returned SNMP value $humanuptime=$uptime/100 Thanks for responding!
        Right. It's just a count of the total number of ticks your device has been up. If the uptime is 2 seconds, sysUpTime.0 will be 200. You can extract a human readable number pretty easily with some math.
        #!/usr/bin/perl use strict; # Calculate ticks per second, minute, hour, and day my $TICKS_PER_SECOND = 100; my $TICKS_PER_MINUTE = $TICKS_PER_SECOND * 60; my $TICKS_PER_HOUR = $TICKS_PER_MINUTE * 60; my $TICKS_PER_DAY = $TICKS_PER_HOUR * 24; sub tick2time { my $ticks = shift; my $seconds = int($ticks / $TICKS_PER_SECOND) % 60; my $minutes = int($ticks / $TICKS_PER_MINUTE) % 60; my $hours = int($ticks / $TICKS_PER_HOUR) % 24; my $days = int($ticks / $TICKS_PER_DAY); return ($days, $hours, $minutes, $seconds); }

        -Matt