in reply to Parsing 'uptime' output

I generally recommend people to try to avoid parsing the output of another program when the information they require can be obtained more directly from other sources. For example, another way which may prove to be a better alternative for obtaining the uptime would be to source if directly from /proc/uptime (assuming that this interface is available to you) - This allows for vagarities of uptime output and regular pattern matching to sidestepped altogether.

For example:

use Date::Calc qw/ Normalize_DHMS /; use IO::File; # get_uptime function - returns uptime from /proc/uptime # as 4-member array representing current system uptime in # days, hours, minutes and seconds sub get_uptime { my $uptime; my $fh = IO::File->new('/proc/uptime', 'r'); $uptime = (split /\s+/, <$fh>)[0] if defined $fh; return Normalize_DHMS(0, 0, 0, $uptime) if defined $uptime; }

A similar approach could be taken to obtain load average information from /proc/loadavg.

 

Replies are listed 'Best First'.
Re: Re: Parsing 'uptime' output
by sauoq (Abbot) on Aug 19, 2002 at 01:29 UTC
    For example, another way which may prove to be a better alternative for obtaining the uptime would be to source if directly from /proc/uptime (assuming that this interface is available to you)

    I understand the sentiment here and often this approach makes sense but I don't think it does in this case.

    The output from Solaris's stock uptime is close enough to GNU's that the regex would work without modification. So would IRIX's. And OpenBSD's. None of those systems has a /proc/uptime though. Using the tool in this case is far more portable.

    As the uptime program is a stable enough tool that its interface is unlikely to change, using it probably should not be regarded as a maintenance risk either.

    Getting your data from another program isn't inherently a bad thing. Nor should it to be shunned in lieu of any alternative. The key is to retrieve your data from an interface that is as stable and portable as possible. In this case, the data source he wanted to use is more standard than the one you suggested.

    -sauoq
    "My two cents aren't worth a dime.";
    

        I read that node and it contains one side of a debate about the merits of using three-argument open over two-argument open. In fact, it is Dominus's reply to tilly's node in which he made the statement that he "would be cautious about suggesting that people use it in any code whose use is meant to be portable."

        It seems to me that tilly and I are actually falling on the same side of completely different debates. I frankly don't even see how that discussion applies to this case and I invite you to enlighten me.

        I think tilly might agree with me if he were still here. Maybe not. I don't know. I wish he was here to speak for himself. Maybe someone should ask Dominus what he thinks as it was his node to which you linked.

        -sauoq
        "My two cents aren't worth a dime.";