Skip to content

Commit

Permalink
add BandwidthDetection keyword to config, reorganize config related f…
Browse files Browse the repository at this point in the history
…unctions and add support for interface bandwidth query (not used yet)
  • Loading branch information
vergoh committed Jan 6, 2015
1 parent 63a5725 commit 476bfa9
Show file tree
Hide file tree
Showing 18 changed files with 424 additions and 224 deletions.
4 changes: 4 additions & 0 deletions cfg/vnstat.conf
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ OutputStyle 3
# used rate unit (0 = bytes, 1 = bits)
RateUnit 1

# try to detect interface maximum bandwidth, 0 = disable feature
# MaxBandwidth will be used as fallback value when enabled
BandwidthDetection 1

# maximum bandwidth (Mbit) for all interfaces, 0 = disable feature
# (unless interface specific limit is given)
MaxBandwidth 100
Expand Down
11 changes: 6 additions & 5 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ CC = gcc
endif
CFLAGS ?= -O2
LDLIBS = -lm
OBJS = vnstat.o ifinfo.o dbxml.o dbshow.o dbaccess.o dbmerge.o common.o misc.o cfg.o traffic.o
DOBJS = vnstatd.o ifinfo.o dbaccess.o dbcache.o common.o misc.o cfg.o daemon.o
OBJS = vnstat.o ifinfo.o dbxml.o dbshow.o dbaccess.o dbmerge.o common.o misc.o cfg.o ibw.o traffic.o
DOBJS = vnstatd.o ifinfo.o dbaccess.o dbcache.o common.o misc.o cfg.o ibw.o daemon.o
IOBJS = vnstati.o image.o dbaccess.o dbmerge.o common.o misc.o cfg.o

default: vnstat vnstatd
Expand All @@ -22,17 +22,18 @@ vnstat.o: vnstat.c vnstat.h common.h ifinfo.h traffic.h dbxml.h dbshow.h dbacces
vnstatd.o: vnstatd.c vnstatd.h common.h dbcache.h cfg.h daemon.h
vnstati.o: vnstati.c vnstati.h common.h image.h cfg.h dbaccess.h dbmerge.h

ifinfo.o: ifinfo.c ifinfo.h common.h dbaccess.h misc.h cfg.h
ifinfo.o: ifinfo.c ifinfo.h common.h dbaccess.h misc.h cfg.h ibw.h
traffic.o: traffic.c traffic.h common.h ifinfo.h misc.h
dbxml.o: dbxml.c dbxml.h common.h
dbshow.o: dbshow.c dbshow.h misc.h common.h
dbaccess.o: dbaccess.c dbaccess.h common.h
dbmerge.o: dbmerge.c dbmerge.h dbaccess.h common.h
dbcache.o: dbcache.c dbcache.h dbaccess.h common.h ifinfo.h cfg.h
dbcache.o: dbcache.c dbcache.h dbaccess.h common.h ifinfo.h cfg.h ibw.h
common.o: common.c common.h
misc.o: misc.c misc.h common.h
cfg.o: cfg.c cfg.h common.h
daemon.o: daemon.c daemon.h common.h ifinfo.h dbaccess.h dbcache.h misc.h cfg.h
ibw.o: ibw.c ibw.h common.h
daemon.o: daemon.c daemon.h common.h ifinfo.h dbaccess.h dbcache.h misc.h cfg.h ibw.h
image.o: image.c image.h vnstati.h common.h misc.h

clean:
Expand Down
210 changes: 13 additions & 197 deletions src/cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ void printcfgfile(void)
printf("# used rate unit (0 = bytes, 1 = bits)\n");
printf("RateUnit %d\n\n", cfg.rateunit);

printf("# try to detect interface maximum bandwidth, 0 = disable feature\n");
printf("# MaxBandwidth will be used as fallback value when enabled\n");
printf("BandwidthDetection %d\n\n", cfg.bwdetection);

printf("# maximum bandwidth (Mbit) for all interfaces, 0 = disable feature\n# (unless interface specific limit is given)\n");
printf("MaxBandwidth %d\n\n", cfg.maxbw);

Expand Down Expand Up @@ -179,6 +183,7 @@ int loadcfg(const char *cfgfile)
{ "UnitMode", 0, &cfg.unit, 0, 0 },
{ "OutputStyle", 0, &cfg.ostyle, 0, 0 },
{ "RateUnit", 0, &cfg.rateunit, 0, 0 },
{ "BandwidthDetection", 0, &cfg.bwdetection, 0, 0 },
{ "MaxBandwidth", 0, &cfg.maxbw, 0, 0 },
{ "Sampletime", 0, &cfg.sampletime, 0, 0 },
{ "QueryMode", 0, &cfg.qmode, 0, 0 },
Expand Down Expand Up @@ -218,8 +223,6 @@ int loadcfg(const char *cfgfile)
{ 0, 0, 0, 0, 0 }
};

ifacebw = NULL;

/* clear buffer */
for (i=0; i<512; i++) {
buffer[i] = '\0';
Expand Down Expand Up @@ -352,14 +355,6 @@ int loadcfg(const char *cfgfile)

} /* while */

/* search for interface specific limits */
ibwcfgread(fd);

fclose(fd);

if (debug)
ibwlist();

/* validate config */
validatecfg();

Expand Down Expand Up @@ -524,11 +519,17 @@ void validatecfg(void)
snprintf(errorstring, 512, "Invalid value for TrafficlessDays, resetting to \"%d\".", cfg.transbg);
printe(PT_Config);
}

if (cfg.bwdetection<0 || cfg.bwdetection>1) {
cfg.bwdetection = BWDETECT;
snprintf(errorstring, 512, "Invalid value for BandwidthDetection, resetting to \"%d\".", cfg.bwdetection);
printe(PT_Config);
}
}

void defaultcfg(void)
{
ibwflush();
ifacebw = NULL;

cfg.bvar = BVAR;
cfg.qmode = DEFQMODE;
Expand All @@ -537,6 +538,7 @@ void defaultcfg(void)
cfg.unit = UNITMODE;
cfg.ostyle = OSTYLE;
cfg.rateunit = RATEUNIT;
cfg.bwdetection = BWDETECT;
cfg.maxbw = DEFMAXBW;
cfg.spacecheck = USESPACECHECK;
cfg.flock = USEFLOCK;
Expand Down Expand Up @@ -583,189 +585,3 @@ void defaultcfg(void)
strncpy_nt(cfg.ctx, CTX, 8);
strncpy_nt(cfg.ctxd, CTXD, 8);
}

int ibwadd(const char *iface, int limit)
{
ibwnode *n, *p = ifacebw;

/* add new node if list is empty */
if (p == NULL) {

n = malloc(sizeof(ibwnode));

if (n == NULL) {
return 0;
}

n->next = ifacebw;
ifacebw = n;
strncpy_nt(n->interface, iface, 32);
n->limit = limit;

} else {

/* update previous value if already in list */
while (p != NULL) {
if (strcmp(p->interface, iface)==0) {
p->limit = limit;
return 1;
}
p = p->next;
}

/* add new node if not found */
n = malloc(sizeof(ibwnode));

if (n == NULL) {
return 0;
}

n->next = ifacebw;
ifacebw = n;
strncpy_nt(n->interface, iface, 32);
n->limit = limit;
}

return 1;
}

void ibwlist(void)
{
int i=1;
ibwnode *p = ifacebw;

if (p == NULL) {
printf("ibw list is empty.\n");
return;
}

printf("ibw:\n");
while (p != NULL) {
printf(" %2d: \"%s\" \"%d\"\n", i, p->interface, p->limit);
p = p->next;
i++;
}
}

int ibwget(const char *iface)
{
ibwnode *p = ifacebw;

/* search for interface specific limit */
while (p != NULL) {
if (strcasecmp(p->interface, iface)==0) {
if (p->limit>0) {
return p->limit;
} else {
return -1;
}
}
p = p->next;
}

/* return default limit if specified */
if (cfg.maxbw>0) {
return cfg.maxbw;
} else {
return -1;
}
}

void ibwflush(void)
{
ibwnode *f, *p = ifacebw;

while (p != NULL) {
f = p;
p = p->next;
free(f);
}

ifacebw = NULL;
}

int ibwcfgread(FILE *fd)
{
char cfgline[512], name[512], value[512];
int i, j, linelen, count = 0, ivalue;

/* start from value search from first line */
rewind(fd);

/* cycle all lines */
while (!feof(fd)) {

cfgline[0] = '\0';

/* get current line */
if (fgets(cfgline, 512, fd)==NULL) {
break;
}

linelen = (int)strlen(cfgline);

if (linelen>8 && cfgline[0]!='#') {

if (strncasecmp(cfgline, "MaxBW", 5)==0) {

/* clear name and value buffers */
for (j=0; j<512; j++) {
name[j]=value[j]='\0';
}

/* get interface name */
j=0;
for (i=5; i<linelen; i++) {
if (cfgline[i]==' ' || cfgline[i]=='=' || cfgline[i]=='\t' || cfgline[i]=='\n' || cfgline[i]=='\r') {
break;
} else {
name[j]=cfgline[i];
j++;
}
}

/* get new line if no usable name was found */
if (strlen(name)==0) {
continue;
}

/* search value */
j=0;
for (i++; i<linelen; i++) {
if (cfgline[i]=='\n' || cfgline[i]=='\r') {
break;
} else if (cfgline[i]=='\"') {
if (j==0) {
continue;
} else {
break;
}
} else {
if (j==0 && (cfgline[i]==' ' || cfgline[i]=='=' || cfgline[i]=='\t')) {
continue;
} else {
value[j]=cfgline[i];
j++;
}
}
}

/* get new line if no usable value was found */
if ((strlen(value)==0) || (!isdigit(value[0])) ) {
continue;
}

/* add interface and limit to list if value is within limits */
ivalue = atoi(value);
if (ivalue<0 || ivalue>10000) {
snprintf(errorstring, 512, "Invalid value \"%d\" for MaxBW%s, ignoring parameter.", ivalue, name);
printe(PT_Config);
} else {
ibwadd(name, ivalue);
}
}
}
}

return count;
}
5 changes: 0 additions & 5 deletions src/cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ void printcfgfile(void);
int loadcfg(const char *cfgfile);
void validatecfg(void);
void defaultcfg(void);
int ibwadd(const char *iface, int limit);
void ibwlist(void);
int ibwget(const char *iface);
void ibwflush(void);
int ibwcfgread(FILE *fd);

struct cfgsetting {
const char *name;
Expand Down
7 changes: 6 additions & 1 deletion src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ and most can be changed later from the config file.
/* default locale */
#define LOCALE "-"

/* bandwidth detection, 0 = feature disabled */
#define BWDETECT 1

/* default maximum bandwidth (Mbit) for all interfaces */
/* 0 = feature disabled */
#define DEFMAXBW 100
Expand Down Expand Up @@ -214,7 +217,7 @@ typedef struct {
char logfile[512], pidfile[512];
char daemonuser[33], daemongroup[33];
short updateinterval, pollinterval, saveinterval, offsaveinterval, savestatus, uselogging;
short createdirs, updatefileowner;
short createdirs, updatefileowner, bwdetection;
} CFG;

/* internal interface information structure */
Expand Down Expand Up @@ -265,6 +268,8 @@ typedef struct {
typedef struct ibwnode {
char interface[32];
int limit;
int fallback;
int retries;
struct ibwnode *next;
} ibwnode;

Expand Down
2 changes: 2 additions & 0 deletions src/daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "dbcache.h"
#include "misc.h"
#include "cfg.h"
#include "ibw.h"
#include "daemon.h"

void daemonize(void)
Expand Down Expand Up @@ -700,6 +701,7 @@ void handleintsignals(DSTATE *s)
if (loadcfg(s->cfgfile)) {
strncpy_nt(s->dirname, cfg.dbdir, 512);
}
ibwloadcfg(s->cfgfile);
break;

case SIGINT:
Expand Down
1 change: 1 addition & 0 deletions src/dbcache.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "dbaccess.h"
#include "dbcache.h"
#include "cfg.h"
#include "ibw.h"

int cacheadd(const char *iface, int sync)
{
Expand Down
Loading

0 comments on commit 476bfa9

Please sign in to comment.