Small script on OpenBSD

S

sukovic

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";
}
 
J

Jens Thoms Toerring

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

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
 
B

Brian Blackmore

...
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top