Small script on OpenBSD

Discussion in 'Perl Misc' started by sukovic@gmail.com, Jul 23, 2007.

  1. Guest

    Hi,

    This script works on Linux, but on OpenBSD there is no /proc/net/dev,
    any tip how to solve this?
    Thanks.


    ----------------------------

    #!/usr/bin/perl

    use strict;

    my $if = $ARGV[0] || mrtg_die();

    open(F, "</proc/net/dev") || mrtg_die();
    my @LINES = <F>;
    close(F);

    foreach (grep(/\s+$if\:/, @LINES)) {
    /\s+$if\:(\s*\d*){1}/;
    my $recv = $1;
    $recv =~ s/\s+//g;

    /\s+$if\:(\s*\d*){9}/;
    my $sent = $1;
    $sent =~ s/\s+//g;

    print "$recv\n$sent\n0\n0\n";
    }

    sub mrtg_die() {
    print "0\n0\n0\n0\n";
    }
    , Jul 23, 2007
    #1
    1. Advertising

  2. wrote:
    > This script works on Linux, but on OpenBSD there is no /proc/net/dev,
    > any tip how to solve this?


    > #!/usr/bin/perl


    > use strict;


    > my $if = $ARGV[0] || mrtg_die();


    > open(F, "</proc/net/dev") || mrtg_die();
    > my @LINES = <F>;
    > close(F);


    > foreach (grep(/\s+$if\:/, @LINES)) {
    > /\s+$if\:(\s*\d*){1}/;
    > my $recv = $1;
    > $recv =~ s/\s+//g;


    > /\s+$if\:(\s*\d*){9}/;
    > my $sent = $1;
    > $sent =~ s/\s+//g;


    > print "$recv\n$sent\n0\n0\n";
    > }


    > sub mrtg_die() {
    > print "0\n0\n0\n0\n";
    > }


    Since you only seem to be inteested in the number of received and
    transmitted bytes I guess you can get the same information (and in
    a more system-independent way) from the output of 'ifconfig':

    #!/usr/bin/perl
    use strict;
    use warnings;
    my $found = 0;
    my $if = $ARGV[ 0 ] or die "Missing argument\n";
    open my $f, "/sbin/ifconfig $if 2>/dev/null|" or die "Can't run ifconfig\n"
    while ( <$f> ) {
    next unless /^\s*RX bytes\s*:\s*(\d+).*?TX bytes\s*:\s*(\d+)/;
    print "$1\n$2\n";
    $found = 1;
    }
    close $f;
    die "Invalid interface: $if\n" unless $found;

    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, Jul 23, 2007
    #2
    1. Advertising

  3. Jens Thoms Toerring <> wrote:
    > wrote:
    > > ...
    > > open(F, "</proc/net/dev") || mrtg_die();
    > > ...


    > ...
    > open my $f, "/sbin/ifconfig $if 2>/dev/null|" or die "Can't run ifconfig\n"
    > ...


    On OBSD, I'm using `netstat -b -n -I <interface>`. I poll several
    networking interfaces at regular intervals to monitor usage, and it
    has never caused unfortunate overhead nor been particularly
    incorrect in its return of data.

    --
    Brian Blackmore
    blb8 at po dot cwru dot edu
    Brian Blackmore, Jul 23, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Archevis
    Replies:
    1
    Views:
    864
    John M. Nordgaard
    Oct 16, 2003
  2. Steph L
    Replies:
    2
    Views:
    871
    Alan Coopersmith
    Oct 28, 2003
  3. Yuri Pimenov

    python 2.3.2 and openbsd 3.4

    Yuri Pimenov, Nov 12, 2003, in forum: Python
    Replies:
    2
    Views:
    383
    Anthony Baxter
    Nov 12, 2003
  4. Noah
    Replies:
    3
    Views:
    416
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
    Sep 17, 2004
  5. Evangeline South
    Replies:
    1
    Views:
    90
    Steve Peters
    Oct 22, 2003
Loading...

Share This Page