![]() |
|
|||||||
| OpenBSD General Other questions regarding OpenBSD which do not fit in any of the categories below. |
![]() |
|
|
Thread Tools | Display Modes |
|
|||
|
Hello all,
I am trying to find a simple solution to a seemingly common requirement. We need to keep track of total bandwidth by month on our external interface as our new ISP charges $15/gb over our monthly cap ![]() We also don't need fancy graphs just text is fine. If anyone can recommend something it would be greatly appreciated. |
|
||||
|
Have a look at the following and see if any one suits your needs:
/usr/ports/net/bwm-ng <= probably your best bet /usr/ports/net/ifstat /usr/ports/net/iftop There's also ipband (Google it), but it's not in OpenBSD ports. See if you can compile it anyway. Good luck.
__________________
BSD, Eggdrop and the random Blah |
|
|||
|
Thanks for the suggestions. I have set them all up and will see what i get after they have a chance to run for a bit. The camomel script seems to have the best output. I put it in the daily cron so I am emailed the stats every day. The only issue i can see is that the statistics get reset on reboot.
I am going to keep searching as ideally i would like the output to look like: Today's Bandwidth: x/gb Yesterday's Bandwidth: x/gb Current Month: x/gb Previous Month: x/gb Don't know if i am expecting too much...... Anyways I will give an update tomorrow |
|
|||
|
This is a raw extraction of the byte-counting code from the netstat command.. this may be useful to someone who's attempting to write their own application.
Code:
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/route.h>
int
main(void)
{
struct if_msghdr ifm;
int mib[6] = { CTL_NET, AF_ROUTE, 0, 0, NET_RT_IFLIST, 0 };
struct rt_msghdr *rtm;
struct if_data *ifd;
struct sockaddr *sa, *rti_info[RTAX_MAX];
struct sockaddr_dl *sdl;
char *buf, *next, *lim;
char name[IFNAMSIZ];
size_t len;
int loop_index;
if (sysctl(mib, 6, NULL, &len, NULL, 0) == -1)
err(1, "sysctl");
if ((buf = (char *)malloc(len)) == NULL)
err(1, NULL);
if (sysctl(mib, 6, buf, &len, NULL, 0) == -1)
err(1, "sysctl");
lim = buf + len;
for (next = buf; next < lim; next += rtm->rtm_msglen) {
rtm = (struct rt_msghdr *)next;
if (rtm->rtm_version != RTM_VERSION)
continue;
if (rtm->rtm_type == RTM_IFINFO) {
bcopy(next, &ifm, sizeof(ifm));
ifd = &ifm.ifm_data;
sa = (struct sockaddr *)(next + rtm->rtm_hdrlen);
for (loop_index = 0; loop_index < RTAX_MAX; loop_index++) {
if (ifm.ifm_addrs & (1 << loop_index)) {
rti_info[loop_index] = sa;
sa = (struct sockaddr *)((char *)(sa) +
roundup(sa->sa_len, sizeof(long)));
} else {
rti_info[loop_index] = NULL;
}
}
sdl = (struct sockaddr_dl *)rti_info[RTAX_IFP];
if (sdl == NULL || sdl->sdl_family != AF_LINK)
continue;
bzero(name, sizeof(name));
if (sdl->sdl_nlen >= IFNAMSIZ) {
memcpy(name, sdl->sdl_data, IFNAMSIZ - 1);
} else if (sdl->sdl_nlen > 0) {
memcpy(name, sdl->sdl_data, sdl->sdl_nlen);
}
if(!strcmp(name, "rl0")) {
printf("%10llu %10llu\n", ifd->ifi_ibytes, ifd->ifi_obytes);
}
}
}
}
The equivalent command is: $ netstat -ibTake care. Last edited by BSDfan666; 12th June 2008 at 03:43 AM. |
|
|||
|
Whoo, ephemera used my alterations to the netstat code.
![]() EDIT: While you're not likely to run into this yet, consider changing the size of the interface variable to IFNAMSIZ. ![]() EDIT2: You're using OpenBSD, also consider using strlcpy/strlcat instead of strncpy/strncat.. ![]() EDIT3: I'm nagging now, ignore me.. -pedantic, anyway, consider making this daemon privsep, there shouldn't be a need to run this as root. Last edited by BSDfan666; 15th June 2008 at 04:36 PM. |
|
||||
|
Quote:
Code:
#!/usr/bin/perl
# Copyright: ephemera @ daemonforums.org
use POSIX("mktime");
die "Usage: $0 interface\n" if $#ARGV;
$logfile="/var/log/bwmond.$ARGV[0]";
open LOG,"< $logfile" or die "$logfile: $!\n";
@abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
($min,$date,$mon,$year)=(localtime)[1,3..5];
($ydate,$ymon,$yyear)=(localtime mktime 0, 0, 0, $date-1, $mon, $year)[3..5];
($ldate,$lmon,$lyear)=(localtime mktime 0, 0, 0, $date, $mon-1, $year)[3..5];
$year+=1900; $yyear+=1900; $lyear+=1900;
%what=(d=>'Today',yd=>'Yesterday',m=>'This Month',lm=>'Last Month');
for (reverse <LOG>) {
$end=1;
if (!defined $lent) {
/(.*):.*/;
$lent=$1;
}
tr/:/ /;
@F = split;
if ($F[5] == $year && $F[0] eq $abbr[$mon]) {
push @m, [@F];
if ($F[1] == $date) {
push @d, [@F];
}
$end=0;
}
if ($F[5] == $yyear && $F[0] eq $abbr[$ymon] && $F[1]==$ydate) {
push @yd,[@F]; $end=0;
}
if ($F[5] == $lyear && $F[0] eq $abbr[$lmon]) {
push @lm,[@F]; $end=0;
}
last if $end;
}
printf "\n%23s%10s\n", '| Dnload(MB)', '| Upload(MB)|';
printf "%s\n", '-----------+-----------+-----------+';
for (('d','yd','m','lm')) {
if (defined @{"$_"}) {
$up = ${"$_"}[0]->[12] - ${"$_"}[-1]->[12] + ${"$_"}[-1]->[6];
$down = ${"$_"}[0]->[14] - ${"$_"}[-1]->[14] + ${"$_"}[-1]->[9];
printf "%-10s | %8.1f | %8.1f |\n",$what{"$_"},$down/1024,$up/1024;
} else {
printf("%-10s | - | - |\n", $what{"$_"});
}
}
print "\nLast updated on $lent\n";
Last edited by ephemera; 22nd October 2008 at 12:36 PM. |
|
|||
|
Quote:
It should be noted, Copyright (c) 1983, 1988, 1993 - The Regents of the University of California. All rights reserved.
|
|
|||
|
BTW running a caching nameserver and/or a proxy like squid are great for reducing your bandwidth and thus your costs.
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump |
|
|||
|
Wow I am truly blown away by the responses. Thanks a bunch!
Ephemera: "I have written a daemon for my own amusement." that's amazing I will be trying this out asap.J65nko: All of our "regular" traffic goes out an unlimited connection so there is no worry there. We are already running a proxy to filer out ad's and other various crap. If anyone was wondering, I ran the script s0xxx suggested for a few days and something is messed up with it. Here is the output: External interface bandwidth usage: uptime 10 days ExtIf in total -1 GBytes ExtIf out total 1 GBytes ExtIf in/day 0 MBytes/day ExtIf out/day 0 MBytes/day ExtIf in/30day 0 GBytes/month ExtIf out/30day 0 GBytes/month |
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Web interface for rTorrent | Beastie | FreeBSD Ports and Packages | 0 | 24th August 2009 11:53 AM |
| Total Success | divadgnol67 | OpenBSD General | 0 | 6th August 2009 07:15 PM |
| CARP interface with DHClient | xinform3n | OpenBSD General | 5 | 22nd July 2009 12:41 PM |
| NAT with only one interface | zapov | General software and network | 4 | 16th February 2009 02:45 AM |
| Web interface for pf? | windependence | OpenBSD Security | 4 | 20th May 2008 03:58 AM |