From 1d96ce6c1994dab79f5da461d600280a711889ca Mon Sep 17 00:00:00 2001 From: Teemu Toivola Date: Thu, 26 Jan 2017 21:39:20 +0200 Subject: [PATCH] implement --exportdb, add previously disabled output tests, remove db merge related files --- Makefile.am | 22 +--- Makefile.in | 88 +++++++++++----- README.md | 2 - src/dbjson.c | 4 +- src/dbmerge.c | 223 ----------------------------------------- src/dbmerge.h | 9 -- src/dbshow.c | 48 +++++++-- src/dbshow.h | 1 + src/dbxml.c | 4 +- src/vnstat.c | 6 +- src/vnstatd.c | 2 +- src/vnstati.c | 3 +- tests/database_tests.c | 129 ++++++++---------------- 13 files changed, 158 insertions(+), 383 deletions(-) delete mode 100644 src/dbmerge.c delete mode 100644 src/dbmerge.h diff --git a/Makefile.am b/Makefile.am index 1c33ddd..a3f8879 100644 --- a/Makefile.am +++ b/Makefile.am @@ -42,7 +42,6 @@ vnstatd_SOURCES = src/vnstatd.c src/vnstatd.h \ # src/image.c src/image.h \ # src/dbsql.c src/dbsql.h \ # src/dbaccess.c src/dbaccess.h \ -# src/dbmerge.c src/dbmerge.h \ # src/common.c src/common.h \ # src/misc.c src/misc.h \ # src/fs.c src/fs.h \ @@ -78,6 +77,9 @@ check_vnstat_SOURCES += src/cfg.c src/cfg.h \ src/dbsql.c src/dbsql.h \ src/dbaccess.c src/dbaccess.h \ src/datacache.c src/datacache.h \ + src/dbshow.c src/dbshow.h \ + src/dbjson.c src/dbjson.h \ + src/dbxml.c src/dbxml.h \ src/ibw.c src/ibw.h \ src/ifinfo.c src/ifinfo.h \ src/misc.c src/misc.h \ @@ -85,24 +87,6 @@ check_vnstat_SOURCES += src/cfg.c src/cfg.h \ src/id.c src/id.h endif -#check_vnstat_SOURCES += src/cfg.c src/cfg.h \ -# src/common.c src/common.h \ -# src/daemon.c src/daemon.h \ -# src/dbsql.c src/dbsql.h \ -# src/dbaccess.c src/dbaccess.h \ -# src/datacache.c src/datacache.h \ -# src/dbjson.c src/dbjson.h \ -# src/dbmerge.c src/dbmerge.h \ -# src/dbshow.c src/dbshow.h \ -# src/dbxml.c src/dbxml.h \ -# src/ibw.c src/ibw.h \ -# src/ifinfo.c src/ifinfo.h \ -# src/misc.c src/misc.h \ -# src/fs.c src/fs.h \ -# src/id.c src/id.h \ -# src/traffic.c src/traffic.h -#endif - setdebug: CFLAGS='-Wall -Wextra -g' ./configure diff --git a/Makefile.in b/Makefile.in index a5b8aa1..10bef23 100644 --- a/Makefile.in +++ b/Makefile.in @@ -112,9 +112,10 @@ am__check_vnstat_SOURCES_DIST = tests/vnstat_tests.c \ tests/id_tests.h tests/daemon_tests.c tests/daemon_tests.h \ src/cfg.c src/cfg.h src/common.c src/common.h src/daemon.c \ src/daemon.h src/dbsql.c src/dbsql.h src/dbaccess.c \ - src/dbaccess.h src/datacache.c src/datacache.h src/ibw.c \ - src/ibw.h src/ifinfo.c src/ifinfo.h src/misc.c src/misc.h \ - src/fs.c src/fs.h src/id.c src/id.h + src/dbaccess.h src/datacache.c src/datacache.h src/dbshow.c \ + src/dbshow.h src/dbjson.c src/dbjson.h src/dbxml.c src/dbxml.h \ + src/ibw.c src/ibw.h src/ifinfo.c src/ifinfo.h src/misc.c \ + src/misc.h src/fs.c src/fs.h src/id.c src/id.h am__dirstamp = $(am__leading_dot)dirstamp @HAVE_CHECK_TRUE@am_check_vnstat_OBJECTS = \ @HAVE_CHECK_TRUE@ tests/check_vnstat-vnstat_tests.$(OBJEXT) \ @@ -134,6 +135,9 @@ am__dirstamp = $(am__leading_dot)dirstamp @HAVE_CHECK_TRUE@ src/check_vnstat-dbsql.$(OBJEXT) \ @HAVE_CHECK_TRUE@ src/check_vnstat-dbaccess.$(OBJEXT) \ @HAVE_CHECK_TRUE@ src/check_vnstat-datacache.$(OBJEXT) \ +@HAVE_CHECK_TRUE@ src/check_vnstat-dbshow.$(OBJEXT) \ +@HAVE_CHECK_TRUE@ src/check_vnstat-dbjson.$(OBJEXT) \ +@HAVE_CHECK_TRUE@ src/check_vnstat-dbxml.$(OBJEXT) \ @HAVE_CHECK_TRUE@ src/check_vnstat-ibw.$(OBJEXT) \ @HAVE_CHECK_TRUE@ src/check_vnstat-ifinfo.$(OBJEXT) \ @HAVE_CHECK_TRUE@ src/check_vnstat-misc.$(OBJEXT) \ @@ -642,10 +646,11 @@ vnstatd_SOURCES = src/vnstatd.c src/vnstatd.h \ @HAVE_CHECK_TRUE@ src/common.c src/common.h src/daemon.c \ @HAVE_CHECK_TRUE@ src/daemon.h src/dbsql.c src/dbsql.h \ @HAVE_CHECK_TRUE@ src/dbaccess.c src/dbaccess.h src/datacache.c \ -@HAVE_CHECK_TRUE@ src/datacache.h src/ibw.c src/ibw.h \ -@HAVE_CHECK_TRUE@ src/ifinfo.c src/ifinfo.h src/misc.c \ -@HAVE_CHECK_TRUE@ src/misc.h src/fs.c src/fs.h src/id.c \ -@HAVE_CHECK_TRUE@ src/id.h +@HAVE_CHECK_TRUE@ src/datacache.h src/dbshow.c src/dbshow.h \ +@HAVE_CHECK_TRUE@ src/dbjson.c src/dbjson.h src/dbxml.c \ +@HAVE_CHECK_TRUE@ src/dbxml.h src/ibw.c src/ibw.h src/ifinfo.c \ +@HAVE_CHECK_TRUE@ src/ifinfo.h src/misc.c src/misc.h src/fs.c \ +@HAVE_CHECK_TRUE@ src/fs.h src/id.c src/id.h all: all-recursive .SUFFIXES: @@ -864,6 +869,12 @@ src/check_vnstat-dbaccess.$(OBJEXT): src/$(am__dirstamp) \ src/$(DEPDIR)/$(am__dirstamp) src/check_vnstat-datacache.$(OBJEXT): src/$(am__dirstamp) \ src/$(DEPDIR)/$(am__dirstamp) +src/check_vnstat-dbshow.$(OBJEXT): src/$(am__dirstamp) \ + src/$(DEPDIR)/$(am__dirstamp) +src/check_vnstat-dbjson.$(OBJEXT): src/$(am__dirstamp) \ + src/$(DEPDIR)/$(am__dirstamp) +src/check_vnstat-dbxml.$(OBJEXT): src/$(am__dirstamp) \ + src/$(DEPDIR)/$(am__dirstamp) src/check_vnstat-ibw.$(OBJEXT): src/$(am__dirstamp) \ src/$(DEPDIR)/$(am__dirstamp) src/check_vnstat-ifinfo.$(OBJEXT): src/$(am__dirstamp) \ @@ -928,7 +939,10 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@src/$(DEPDIR)/check_vnstat-daemon.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@src/$(DEPDIR)/check_vnstat-datacache.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@src/$(DEPDIR)/check_vnstat-dbaccess.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@src/$(DEPDIR)/check_vnstat-dbjson.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@src/$(DEPDIR)/check_vnstat-dbshow.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@src/$(DEPDIR)/check_vnstat-dbsql.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@src/$(DEPDIR)/check_vnstat-dbxml.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@src/$(DEPDIR)/check_vnstat-fs.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@src/$(DEPDIR)/check_vnstat-ibw.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@src/$(DEPDIR)/check_vnstat-id.Po@am__quote@ @@ -1216,6 +1230,48 @@ src/check_vnstat-datacache.obj: src/datacache.c @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(check_vnstat_CFLAGS) $(CFLAGS) -c -o src/check_vnstat-datacache.obj `if test -f 'src/datacache.c'; then $(CYGPATH_W) 'src/datacache.c'; else $(CYGPATH_W) '$(srcdir)/src/datacache.c'; fi` +src/check_vnstat-dbshow.o: src/dbshow.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(check_vnstat_CFLAGS) $(CFLAGS) -MT src/check_vnstat-dbshow.o -MD -MP -MF src/$(DEPDIR)/check_vnstat-dbshow.Tpo -c -o src/check_vnstat-dbshow.o `test -f 'src/dbshow.c' || echo '$(srcdir)/'`src/dbshow.c +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) src/$(DEPDIR)/check_vnstat-dbshow.Tpo src/$(DEPDIR)/check_vnstat-dbshow.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='src/dbshow.c' object='src/check_vnstat-dbshow.o' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(check_vnstat_CFLAGS) $(CFLAGS) -c -o src/check_vnstat-dbshow.o `test -f 'src/dbshow.c' || echo '$(srcdir)/'`src/dbshow.c + +src/check_vnstat-dbshow.obj: src/dbshow.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(check_vnstat_CFLAGS) $(CFLAGS) -MT src/check_vnstat-dbshow.obj -MD -MP -MF src/$(DEPDIR)/check_vnstat-dbshow.Tpo -c -o src/check_vnstat-dbshow.obj `if test -f 'src/dbshow.c'; then $(CYGPATH_W) 'src/dbshow.c'; else $(CYGPATH_W) '$(srcdir)/src/dbshow.c'; fi` +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) src/$(DEPDIR)/check_vnstat-dbshow.Tpo src/$(DEPDIR)/check_vnstat-dbshow.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='src/dbshow.c' object='src/check_vnstat-dbshow.obj' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(check_vnstat_CFLAGS) $(CFLAGS) -c -o src/check_vnstat-dbshow.obj `if test -f 'src/dbshow.c'; then $(CYGPATH_W) 'src/dbshow.c'; else $(CYGPATH_W) '$(srcdir)/src/dbshow.c'; fi` + +src/check_vnstat-dbjson.o: src/dbjson.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(check_vnstat_CFLAGS) $(CFLAGS) -MT src/check_vnstat-dbjson.o -MD -MP -MF src/$(DEPDIR)/check_vnstat-dbjson.Tpo -c -o src/check_vnstat-dbjson.o `test -f 'src/dbjson.c' || echo '$(srcdir)/'`src/dbjson.c +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) src/$(DEPDIR)/check_vnstat-dbjson.Tpo src/$(DEPDIR)/check_vnstat-dbjson.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='src/dbjson.c' object='src/check_vnstat-dbjson.o' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(check_vnstat_CFLAGS) $(CFLAGS) -c -o src/check_vnstat-dbjson.o `test -f 'src/dbjson.c' || echo '$(srcdir)/'`src/dbjson.c + +src/check_vnstat-dbjson.obj: src/dbjson.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(check_vnstat_CFLAGS) $(CFLAGS) -MT src/check_vnstat-dbjson.obj -MD -MP -MF src/$(DEPDIR)/check_vnstat-dbjson.Tpo -c -o src/check_vnstat-dbjson.obj `if test -f 'src/dbjson.c'; then $(CYGPATH_W) 'src/dbjson.c'; else $(CYGPATH_W) '$(srcdir)/src/dbjson.c'; fi` +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) src/$(DEPDIR)/check_vnstat-dbjson.Tpo src/$(DEPDIR)/check_vnstat-dbjson.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='src/dbjson.c' object='src/check_vnstat-dbjson.obj' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(check_vnstat_CFLAGS) $(CFLAGS) -c -o src/check_vnstat-dbjson.obj `if test -f 'src/dbjson.c'; then $(CYGPATH_W) 'src/dbjson.c'; else $(CYGPATH_W) '$(srcdir)/src/dbjson.c'; fi` + +src/check_vnstat-dbxml.o: src/dbxml.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(check_vnstat_CFLAGS) $(CFLAGS) -MT src/check_vnstat-dbxml.o -MD -MP -MF src/$(DEPDIR)/check_vnstat-dbxml.Tpo -c -o src/check_vnstat-dbxml.o `test -f 'src/dbxml.c' || echo '$(srcdir)/'`src/dbxml.c +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) src/$(DEPDIR)/check_vnstat-dbxml.Tpo src/$(DEPDIR)/check_vnstat-dbxml.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='src/dbxml.c' object='src/check_vnstat-dbxml.o' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(check_vnstat_CFLAGS) $(CFLAGS) -c -o src/check_vnstat-dbxml.o `test -f 'src/dbxml.c' || echo '$(srcdir)/'`src/dbxml.c + +src/check_vnstat-dbxml.obj: src/dbxml.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(check_vnstat_CFLAGS) $(CFLAGS) -MT src/check_vnstat-dbxml.obj -MD -MP -MF src/$(DEPDIR)/check_vnstat-dbxml.Tpo -c -o src/check_vnstat-dbxml.obj `if test -f 'src/dbxml.c'; then $(CYGPATH_W) 'src/dbxml.c'; else $(CYGPATH_W) '$(srcdir)/src/dbxml.c'; fi` +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) src/$(DEPDIR)/check_vnstat-dbxml.Tpo src/$(DEPDIR)/check_vnstat-dbxml.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='src/dbxml.c' object='src/check_vnstat-dbxml.obj' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(check_vnstat_CFLAGS) $(CFLAGS) -c -o src/check_vnstat-dbxml.obj `if test -f 'src/dbxml.c'; then $(CYGPATH_W) 'src/dbxml.c'; else $(CYGPATH_W) '$(srcdir)/src/dbxml.c'; fi` + src/check_vnstat-ibw.o: src/ibw.c @am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(check_vnstat_CFLAGS) $(CFLAGS) -MT src/check_vnstat-ibw.o -MD -MP -MF src/$(DEPDIR)/check_vnstat-ibw.Tpo -c -o src/check_vnstat-ibw.o `test -f 'src/ibw.c' || echo '$(srcdir)/'`src/ibw.c @am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) src/$(DEPDIR)/check_vnstat-ibw.Tpo src/$(DEPDIR)/check_vnstat-ibw.Po @@ -1981,24 +2037,6 @@ uninstall-man: uninstall-man1 uninstall-man5 uninstall-man1 uninstall-man5 uninstall-sbinPROGRAMS -#check_vnstat_SOURCES += src/cfg.c src/cfg.h \ -# src/common.c src/common.h \ -# src/daemon.c src/daemon.h \ -# src/dbsql.c src/dbsql.h \ -# src/dbaccess.c src/dbaccess.h \ -# src/datacache.c src/datacache.h \ -# src/dbjson.c src/dbjson.h \ -# src/dbmerge.c src/dbmerge.h \ -# src/dbshow.c src/dbshow.h \ -# src/dbxml.c src/dbxml.h \ -# src/ibw.c src/ibw.h \ -# src/ifinfo.c src/ifinfo.h \ -# src/misc.c src/misc.h \ -# src/fs.c src/fs.h \ -# src/id.c src/id.h \ -# src/traffic.c src/traffic.h -#endif - setdebug: CFLAGS='-Wall -Wextra -g' ./configure diff --git a/README.md b/README.md index 61f54cd..4cfcbb1 100644 --- a/README.md +++ b/README.md @@ -46,8 +46,6 @@ configurable durations. Yearly and 5 minute resolution statistics are now includ * `grep TODO src/* tests/*` * continue daemon refactoring * add missing sanity checks to daemon - * console outputs - * export? * image outputs * all * use of 5 minute resolution statistics diff --git a/src/dbjson.c b/src/dbjson.c index bdb125b..b9612c7 100644 --- a/src/dbjson.c +++ b/src/dbjson.c @@ -66,11 +66,9 @@ void jsondump(const interfaceinfo *interface, const char *tablename, const int d dbdatalist *datalist = NULL, *datalist_i = NULL; dbdatalistinfo datainfo; - /* TODO: add table check */ - if (!db_getdata(&datalist, &datainfo, interface->name, tablename, -1)) { /* TODO: match with other output style */ - printf("Error: failed to fetch monthly data\n"); + printf("Error: failed to fetch %s data\n", tablename); return; } diff --git a/src/dbmerge.c b/src/dbmerge.c deleted file mode 100644 index 8351c8e..0000000 --- a/src/dbmerge.c +++ /dev/null @@ -1,223 +0,0 @@ -#include "common.h" -#include "dbaccess.h" -#include "dbmerge.h" - -int mergedb(char iface[], char dirname[]) -{ - DATA mergedata; - char *ifaceptr; - - if (!strstr(iface, "+")) { - return 0; - } - - /* create empty database */ - emptydb(&mergedata); - strncpy_nt(mergedata.interface, iface, 32); - strncpy_nt(mergedata.nick, mergedata.interface, 32); - - if (debug) - printf("iface merge: %s\n", iface); - - ifaceptr = strtok(iface, "+"); - - /* merge all databases in given string */ - while (ifaceptr != NULL) { - if (debug) - printf("merging %s:\n", ifaceptr); - - if (readdb(ifaceptr, dirname, 0)!=0) { - printf("Merge \"%s\" failed.\n", mergedata.interface); - return 0; - } - - if (!mergewith(&mergedata)) { - printf("Merge \"%s\" failed for interface \"%s\".\n", mergedata.interface, ifaceptr); - return 0; - } - - ifaceptr = strtok(NULL, "+"); - } - - /* clean possible glitches */ - cleanmerged(&mergedata); - - /* replace active data with merged */ - if (memcpy(&data, &mergedata, sizeof(data)) != NULL) { - return 1; - } else { - return 0; - } -} - -void emptydb(DATA *dat) -{ - int i; - struct tm *d; - time_t current; - - current = time(NULL); - - dat->version = DBVERSION; - dat->active = 1; - dat->totalrx = 0; - dat->totaltx = 0; - dat->currx = 0; - dat->curtx = 0; - dat->totalrxk = 0; - dat->totaltxk = 0; - dat->lastupdated = 0; - dat->created = current; - - /* days */ - d = localtime(¤t); - for (i=0;i<=29;i++) { - dat->day[i].rx = 0; - dat->day[i].tx = 0; - dat->day[i].rxk = 0; - dat->day[i].txk = 0; - dat->day[i].date = mktime(d); - dat->day[i].used = 1; - d->tm_mday--; - } - - /* months */ - d = localtime(¤t); - for (i=0;i<=11;i++) { - dat->month[i].rx = 0; - dat->month[i].tx = 0; - dat->month[i].rxk = 0; - dat->month[i].txk = 0; - dat->month[i].month = mktime(d); - dat->month[i].used = 1; - d->tm_mon--; - } - - /* top10 */ - for (i=0;i<=9;i++) { - dat->top10[i].rx = 0; - dat->top10[i].tx = 0; - dat->top10[i].rxk = 0; - dat->top10[i].txk = 0; - dat->top10[i].date = 0; - dat->top10[i].used = 0; - } - - /* hours */ - for (i=0;i<=23;i++) { - dat->hour[i].rx = 0; - dat->hour[i].tx = 0; - dat->hour[i].date = 0; - } - - dat->btime = 0; -} - -int mergewith(DATA *dat) -{ - int i, j, orig, merged; - struct tm *d; - - /* merge totals */ - dat->totalrx += data.totalrx; - dat->totaltx += data.totaltx; - dat->totalrxk += data.totalrxk; - dat->totaltxk += data.totaltxk; - - if (data.created < dat->created) { - dat->created = data.created; - } - - if (data.lastupdated > dat->lastupdated) { - dat->lastupdated = data.lastupdated; - } - - /* clean hours from loaded db */ - cleanhours(); - - /* merge hours */ - for (i=0;i<=23;i++) { - if (data.hour[i].date!=0) { - dat->hour[i].rx += data.hour[i].rx; - dat->hour[i].tx += data.hour[i].tx; - dat->hour[i].date = data.hour[i].date; - } - } - - /* merge days */ - for (i=0;i<=29;i++) { - if (data.day[i].used) { - d = localtime(&data.day[i].date); - orig = d->tm_year * 1000 + d->tm_yday; - - for (j=0;j<=29;j++) { - d = localtime(&dat->day[j].date); - merged = d->tm_year * 1000 + d->tm_yday; - - if (orig == merged) { - dat->day[j].rx += data.day[i].rx; - dat->day[j].tx += data.day[i].tx; - dat->day[j].rxk += data.day[i].rxk; - dat->day[j].txk += data.day[i].txk; - if (dat->day[j].date > data.day[i].date) { - dat->day[j].date = data.day[i].date; - } - } else if (merged < orig) { - break; - } - } - - } - } - - /* merge months */ - for (i=0;i<=11;i++) { - if (data.month[i].used) { - d = localtime(&data.month[i].month); - orig = d->tm_year * 100 + d->tm_mon; - - for (j=0;j<=11;j++) { - d = localtime(&dat->month[j].month); - merged = d->tm_year * 100 + d->tm_mon; - - if (orig == merged) { - dat->month[j].rx += data.month[i].rx; - dat->month[j].tx += data.month[i].tx; - dat->month[j].rxk += data.month[i].rxk; - dat->month[j].txk += data.month[i].txk; - if (dat->month[j].month > data.month[i].month) { - dat->month[j].month = data.month[i].month; - } - } else if (merged < orig) { - break; - } - } - - } - } - - return 1; -} - -void cleanmerged(DATA *dat) -{ - int i; - - /* clean days */ - for (i=29;i>=0;i--) { - if ((dat->day[i].rx == 0) && (dat->day[i].tx == 0) && (dat->day[i].rxk == 0) && (dat->day[i].txk == 0)) { - dat->day[i].used = 0; - } else { - break; - } - } - - /* clean days */ - for (i=11;i>=0;i--) { - if ((dat->month[i].rx == 0) && (dat->month[i].tx == 0) && (dat->month[i].rxk == 0) && (dat->month[i].txk == 0)) { - dat->month[i].used = 0; - } else { - break; - } - } -} diff --git a/src/dbmerge.h b/src/dbmerge.h deleted file mode 100644 index e6c53fa..0000000 --- a/src/dbmerge.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef DBMERGE_H -#define DBMERGE_H - -int mergedb(char iface[], char dirname[]); -void emptydb(DATA *dat); -int mergewith(DATA *dat); -void cleanmerged(DATA *dat); - -#endif diff --git a/src/dbshow.c b/src/dbshow.c index 99e20c1..5c2e982 100644 --- a/src/dbshow.c +++ b/src/dbshow.c @@ -33,9 +33,9 @@ void showdb(const char *interface, int qmode) case 3: showlist(&info, "top"); break; -/* case 4: - exportdb(); - break; */ + case 4: + exportdb(&info); + break; case 5: showsummary(&info, 1); break; @@ -572,8 +572,8 @@ void showhours(const interfaceinfo *interface) char matrix[24][81]; /* width is one over 80 so that snprintf can write the end char */ char unit[4]; struct tm *d; - dbdatalist *datalist = NULL, *datalist_i = NULL; - dbdatalistinfo datainfo; + dbdatalist *datalist = NULL, *datalist_i = NULL; + dbdatalistinfo datainfo; HOURDATA hourdata[24]; for (i=0; i<24; i++) { @@ -583,7 +583,7 @@ void showhours(const interfaceinfo *interface) if (!db_getdata(&datalist, &datainfo, interface->name, "hour", 24)) { /* TODO: match with other output style */ - printf("\nError: failed to fetch monthly data\n"); + printf("\nError: failed to fetch hourly data\n"); return; } @@ -713,6 +713,42 @@ void showhours(const interfaceinfo *interface) } } +void exportdb(const interfaceinfo *interface) +{ + int i; + dbdatalist *datalist = NULL, *datalist_i = NULL; + dbdatalistinfo datainfo; + char *datatables[] = {"hour", "day", "month", "year", "top"}; + + printf("version;%s\n", db_getinfo("dbversion")); + printf("vnstat;%s\n", db_getinfo("vnstatversion")); + printf("active;%d\n", interface->active); + printf("interface;%s\n", interface->name); + printf("alias;%s\n", interface->alias); + printf("created;%"PRIu64"\n", (uint64_t)interface->created); + printf("updated;%"PRIu64"\n", (uint64_t)interface->updated); + + printf("totalrx;%"PRIu64"\n", interface->rxtotal); + printf("totaltx;%"PRIu64"\n", interface->rxtotal); + printf("currx;%"PRIu64"\n", interface->rxcounter); + printf("curtx;%"PRIu64"\n", interface->txcounter); + printf("btime;%s\n", db_getinfo("btime")); + + for (i=0; i<5; i++) { + + if (!db_getdata(&datalist, &datainfo, interface->name, datatables[i], -1)) { + printf("\nError: failed to fetch %s data\n", datatables[i]); + return; + } + datalist_i = datalist; + while (datalist_i != NULL) { + printf("%c;%"PRId64";%"PRIu64";%"PRIu64";%"PRIu64"\n", datatables[i][0], datalist_i->rowid, (uint64_t)datalist_i->timestamp, datalist_i->rx, datalist_i->tx); + datalist_i = datalist_i->next; + } + dbdatalistfree(&datalist); + } +} + int showbar(const uint64_t rx, const uint64_t tx, const uint64_t max, const int len) { int i, l, width = len; diff --git a/src/dbshow.h b/src/dbshow.h index 4b8286c..caf8a2b 100644 --- a/src/dbshow.h +++ b/src/dbshow.h @@ -13,6 +13,7 @@ void showsummary(const interfaceinfo *interface, const int shortmode); void showlist(const interfaceinfo *interface, const char *listname); void showoneline(const interfaceinfo *interface); void showhours(const interfaceinfo *interface); +void exportdb(const interfaceinfo *interface); int showbar(uint64_t rx, uint64_t tx, uint64_t max, const int len); void indent(int i); diff --git a/src/dbxml.c b/src/dbxml.c index bbd14c0..e57b2fc 100644 --- a/src/dbxml.c +++ b/src/dbxml.c @@ -61,11 +61,9 @@ void xmldump(const interfaceinfo *interface, const char *tablename, const int da dbdatalist *datalist = NULL, *datalist_i = NULL; dbdatalistinfo datainfo; - /* TODO: add table check */ - if (!db_getdata(&datalist, &datainfo, interface->name, tablename, -1)) { /* TODO: match with other output style */ - printf("Error: failed to fetch monthly data\n"); + printf("Error: failed to fetch %s data\n", tablename); return; } diff --git a/src/vnstat.c b/src/vnstat.c index 982a38c..e411849 100644 --- a/src/vnstat.c +++ b/src/vnstat.c @@ -184,8 +184,8 @@ int main(int argc, char *argv[]) { cfg.qmode=6; } else if ((strcmp(argv[currentarg],"-h")==0) || (strcmp(argv[currentarg],"--hours")==0)) { cfg.qmode=7; -/* } else if ((strcmp(argv[currentarg],"--exportdb")==0) || (strcmp(argv[currentarg],"--dumpdb")==0)) { - cfg.qmode=4; */ + } else if ((strcmp(argv[currentarg],"--exportdb")==0) || (strcmp(argv[currentarg],"--dumpdb")==0)) { + cfg.qmode=4; } else if (strcmp(argv[currentarg],"--oneline")==0) { cfg.qmode=9; if (currentarg+1 +vnStat daemon - Copyright (c) 2008-2017 Teemu Toivola This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/src/vnstati.c b/src/vnstati.c index 2343619..8fd0359 100644 --- a/src/vnstati.c +++ b/src/vnstati.c @@ -1,5 +1,5 @@ /* -vnStat image output - Copyright (c) 2007-2016 Teemu Toivola +vnStat image output - Copyright (c) 2007-2017 Teemu Toivola This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -19,7 +19,6 @@ vnStat image output - Copyright (c) 2007-2016 Teemu Toivola #include "image.h" #include "cfg.h" #include "dbaccess.h" -#include "dbmerge.h" #include "vnstati.h" int main(int argc, char *argv[]) diff --git a/tests/database_tests.c b/tests/database_tests.c index f162482..0ae0345 100644 --- a/tests/database_tests.c +++ b/tests/database_tests.c @@ -3,9 +3,10 @@ #include "common.h" #include "dbaccess.h" #include "ifinfo.h" -//#include "dbshow.h" -//#include "dbxml.h" -//#include "dbjson.h" +#include "dbsql.h" +#include "dbshow.h" +#include "dbxml.h" +#include "dbjson.h" #include "cfg.h" #include "ibw.h" #include "fs.h" @@ -216,76 +217,50 @@ START_TEST(validatedb_with_top10_use) ck_assert_int_eq(validatedb(&data), 0); } END_TEST -/* + START_TEST(database_outputs_do_not_crash) { - int i; + int ret, i; - initdb(); defaultcfg(); - strcpy(data.interface, "something"); - strcpy(data.nick, "nothing"); - data.totalrx = 1; - data.totaltx = 2; - data.currx = 3; - data.curtx = 4; - data.totalrxk = 5; - data.totaltxk = 6; - data.btime = 7; - - for (i=0; i<30; i++) { - data.day[i].date = i+1; - data.day[i].rx = data.day[i].tx = i*100; - data.day[i].rxk = data.day[i].txk = i; - data.day[i].used = 1; - } - - for (i=0; i<10; i++) { - data.top10[i].date = i+1; - data.top10[i].rx = data.top10[i].tx = i*100; - data.top10[i].rxk = data.top10[i].txk = i; - data.top10[i].used = 1; - } - for (i=0; i<12; i++) { - data.month[i].month = i+1; - data.month[i].rx = data.month[i].tx = i*100; - data.month[i].rxk = data.month[i].txk = i; - data.month[i].used = 1; - } + ret = db_open(1); + ck_assert_int_eq(ret, 1); + ret = db_addinterface("something"); + ck_assert_int_eq(ret, 1); - for (i=0; i<24; i++) { - data.hour[i].date = i+1; - data.hour[i].rx = data.hour[i].tx = i*100; + for (i=1; i<100; i++) { + ret = db_addtraffic_dated("something", i*1234, i*2345, i*85000); + ck_assert_int_eq(ret, 1); } suppress_output(); - showdb(0); - showdb(1); - showdb(2); - showdb(3); - showdb(4); - showdb(5); - showdb(6); - showdb(7); - showdb(8); - showdb(9); + showdb("something", 0); + showdb("something", 1); + showdb("something", 2); + showdb("something", 3); + showdb("something", 4); + showdb("something", 5); + showdb("something", 6); + showdb("something", 7); + showdb("something", 8); + showdb("something", 9); xmlheader(); - showxml('d'); - showxml('m'); - showxml('t'); - showxml('h'); - showxml('a'); + showxml("something", 'd'); + showxml("something", 'm'); + showxml("something", 't'); + showxml("something", 'h'); + showxml("something", 'a'); xmlfooter(); jsonheader(); - showjson(0, 'd'); - showjson(0, 'm'); - showjson(0, 't'); - showjson(0, 'h'); - showjson(0, 'a'); + showjson("something", 0, 'd'); + showjson("something", 0, 'm'); + showjson("something", 0, 't'); + showjson("something", 0, 'h'); + showjson("something", 0, 'a'); jsonfooter(); } END_TEST @@ -294,7 +269,7 @@ START_TEST(showbar_with_zero_len_is_nothing) { int len; suppress_output(); - len = showbar(1, 0, 2, 0, 3, 0); + len = showbar(1, 2, 3, 0); ck_assert_int_eq(len, 0); } END_TEST @@ -303,7 +278,7 @@ START_TEST(showbar_with_big_max_and_small_numbers) { int len; suppress_output(); - len = showbar(0, 1, 0, 2, 1000, 10); + len = showbar(1, 2, 1000, 10); ck_assert_int_eq(len, 0); } END_TEST @@ -318,7 +293,7 @@ START_TEST(showbar_with_all_rx) cfg.rxchar[0] = 'r'; cfg.txchar[0] = 't'; pipe = pipe_output(); - len = showbar(0, 1, 0, 0, 1, 10); + len = showbar(1, 0, 1, 10); ck_assert_int_eq(len, 10); fflush(stdout); @@ -337,7 +312,7 @@ START_TEST(showbar_with_all_tx) cfg.rxchar[0] = 'r'; cfg.txchar[0] = 't'; pipe = pipe_output(); - len = showbar(0, 0, 0, 1, 1, 10); + len = showbar(0, 1, 1, 10); ck_assert_int_eq(len, 10); fflush(stdout); @@ -356,7 +331,7 @@ START_TEST(showbar_with_half_and_half) cfg.rxchar[0] = 'r'; cfg.txchar[0] = 't'; pipe = pipe_output(); - len = showbar(0, 1, 0, 1, 2, 10); + len = showbar(1, 1, 2, 10); ck_assert_int_eq(len, 10); fflush(stdout); @@ -375,7 +350,7 @@ START_TEST(showbar_with_one_tenth) cfg.rxchar[0] = 'r'; cfg.txchar[0] = 't'; pipe = pipe_output(); - len = showbar(0, 1, 0, 9, 10, 10); + len = showbar(1, 9, 10, 10); ck_assert_int_eq(len, 10); fflush(stdout); @@ -394,7 +369,7 @@ START_TEST(showbar_with_small_rx_shows_all_tx) cfg.rxchar[0] = 'r'; cfg.txchar[0] = 't'; pipe = pipe_output(); - len = showbar(0, 1, 0, 1000, 1001, 10); + len = showbar(1, 1000, 1001, 10); ck_assert_int_eq(len, 10); fflush(stdout); @@ -407,30 +382,11 @@ START_TEST(showbar_with_max_smaller_than_real_max) { int len; suppress_output(); - len = showbar(0, 1, 0, 2, 1, 10); + len = showbar(1, 2, 1, 10); ck_assert_int_eq(len, 0); } END_TEST -START_TEST(showbar_can_also_do_mb_calculations) -{ - int pipe, len; - char buffer[512]; - memset(&buffer, '\0', sizeof(buffer)); - - defaultcfg(); - cfg.rxchar[0] = 'r'; - cfg.txchar[0] = 't'; - pipe = pipe_output(); - len = showbar(0, 1024, 0, 1024, 2048, 2); - ck_assert_int_eq(len, 2); - fflush(stdout); - - len = read(pipe, buffer, 512); - ck_assert_str_eq(buffer, " rt"); -} -END_TEST -*/ void add_database_tests(Suite *s) { TCase *tc_db = tcase_create("Database"); @@ -449,7 +405,7 @@ void add_database_tests(Suite *s) tcase_add_test(tc_db, validatedb_with_initdb); tcase_add_test(tc_db, validatedb_with_invalid_totals); tcase_add_test(tc_db, validatedb_with_top10_use); - /*tcase_add_test(tc_db, database_outputs_do_not_crash); + tcase_add_test(tc_db, database_outputs_do_not_crash); tcase_add_test(tc_db, showbar_with_zero_len_is_nothing); tcase_add_test(tc_db, showbar_with_big_max_and_small_numbers); tcase_add_test(tc_db, showbar_with_all_rx); @@ -458,7 +414,6 @@ void add_database_tests(Suite *s) tcase_add_test(tc_db, showbar_with_one_tenth); tcase_add_test(tc_db, showbar_with_small_rx_shows_all_tx); tcase_add_test(tc_db, showbar_with_max_smaller_than_real_max); - tcase_add_test(tc_db, showbar_can_also_do_mb_calculations);*/ suite_add_tcase(s, tc_db); } -- 2.40.0