From: Teemu Toivola Date: Fri, 7 Mar 2014 17:51:57 +0000 (+0200) Subject: rename --dumpdb -> --exportdb and --restoredb -> --importdb, use file parameter for... X-Git-Tag: v1.12~66 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8c9104cbf9810c5a77e9d6caa11c3dceb09a097e;p=vnstat rename --dumpdb -> --exportdb and --restoredb -> --importdb, use file parameter for import instead of pipe --- diff --git a/man/vnstat.1 b/man/vnstat.1 index 0f7a600..d2cbf9d 100644 --- a/man/vnstat.1 +++ b/man/vnstat.1 @@ -25,14 +25,17 @@ vnStat \- a console-based network traffic monitor ] [ .B \-\-disable ] [ -.B \-\-dumpdb -] [ .B \-\-enable ] [ +.B \-\-exportdb +] [ .B \-\-help ] [ .B \-\-hours ] [ +.B \-\-importdb +.I file +] [ .B \-i .I interface ] [ @@ -64,8 +67,6 @@ vnStat \- a console-based network traffic monitor ] [ .B \-\-reset ] [ -.B \-\-restoredb -] [ .B \-ru ] [ .B \-\-savemerged @@ -142,7 +143,19 @@ Show additional debug output. Delete the database for the selected interface and stop monitoring it. .TP -.BI "--dumpdb" +.BI "--enable, --disable" +Enable or disable updates for selected interface. Useful for +interfaces that aren't always available, like ppp0. If the interface +goes down it should be disabled in order to avoid errors. Add something +like +.B "vnstat -r --disable -i ppp0" +to the script that's executed when +the interface goes down and +.B "vnstat --enable -i ppp0" +to the up script. These two options aren't needed when the daemon is used. + +.TP +.BI "--exportdb" Instead of showing the database with a formated output, this output will dump the whole database in a format that should be easy to parse with most script languages. Use this for example with PHP, Perl or Python to make a @@ -181,22 +194,23 @@ m = months, t = top10 and h = hours, all other fields are in the same order as i except hours that doesn't have a separate KiB value. For hours the forth and fifth fields have values in KiB. -.TP -.BI "--enable, --disable" -Enable or disable updates for selected interface. Useful for -interfaces that aren't always available, like ppp0. If the interface -goes down it should be disabled in order to avoid errors. Add something -like -.B "vnstat -r --disable -i ppp0" -to the script that's executed when -the interface goes down and -.B "vnstat --enable -i ppp0" -to the up script. These two options aren't needed when the daemon is used. - .TP .BI "-h, --hours" Show traffic for the last 24 hours. +.TP +.BI "--importdb " file +Import a database from +.I file +which was previously exported using the +.BI "--exportdb" +option. This can be used to transfer a database between different architectures and +machines, as the database is architecture dependant and not compatible between +different architectures. First dump the database on one machine, e.g. with +.B "vnstat -i ppp0 --exportdb > ppp0db.txt" +and then import the text file on a different machine using +.B "vnstat -i ppp0 --importdb ppp0db.txt" + .TP .BI "-i, --iface " interface Select one specific @@ -267,17 +281,6 @@ otherwise that interface will get some extra traffic to its database. .BI "--rebuildtotal" Reset the total traffic counters and recount those using recorded months. -.TP -.BI "--restoredb" -Restore a database which was previously dumped using the -.BI "--dumpdb" -option. This can be used to transfer a database between different architectures and -machines, as the database is architecture dependant and not compatible between -different architectures. First dump the database on one machine, e.g. with -.B "vnstat -i ppp0 --dumpdb > ppp0-dump" -and then restore that dump on a different machine using -.B "vnstat -i ppp0 --restoredb < ppp0-dump" - .TP .BI "-ru, --rateunit" Swap the configured rate unit. If rate has been configured to be shown in @@ -364,7 +367,9 @@ Show a command summary. .TP .I /var/lib/vnstat/ This directory contains all databases the program uses. Files are -named according to the monitored interfaces. +named according to the monitored interfaces. A backup copy of each +database is kept in a file starting with a . (dot character) and +otherwise named according to the original file. .TP .I /etc/vnstat.conf diff --git a/src/dbaccess.c b/src/dbaccess.c index 6cb7ea6..f10a6d4 100644 --- a/src/dbaccess.c +++ b/src/dbaccess.c @@ -883,8 +883,9 @@ int validatedb(void) return 1; } -void restoredb(FILE *input) +int importdb(const char *filename) { + FILE *input; char line[512]; int i, count; uint64_t tempint; @@ -892,10 +893,15 @@ void restoredb(FILE *input) MONTH month; HOUR hour; + if ((input=fopen(filename, "r"))==NULL) { + printf("Error: opening file \"%s\" failed. %s.\n", filename, strerror(errno)); + return 0; + } + while (fgets(line, sizeof(line), input) != NULL) { - if (debug) { + if (debug) { printf("parsing %s", line); - } + } if (strlen(line)<9) { continue; @@ -939,20 +945,22 @@ void restoredb(FILE *input) count = sscanf(line, "t;%d;%"PRIu64";%"PRIu64";%"PRIu64";%d;%d;%d", &i, &tempint, &day.rx, &day.tx, &day.rxk, &day.txk, &day.used); - if (count == 7) { + if (count == 7) { if ( i >= 0 && i < (int)sizeof(data.top10) / (int)sizeof(DAY) ) { day.date = (time_t)tempint; data.top10[i] = day; } } - count = sscanf(line, "h;%d;%"PRIu64";%"PRIu64";%"PRIu64, - &i, &tempint, &hour.rx, &hour.tx); - if (count == 4) { + count = sscanf(line, "h;%d;%"PRIu64";%"PRIu64";%"PRIu64, + &i, &tempint, &hour.rx, &hour.tx); + if (count == 4) { if ( i >= 0 && i < (int)sizeof(data.hour) / (int)sizeof(HOUR) ) { hour.date = (time_t)tempint; data.hour[i] = hour; } } } + + return 1; } diff --git a/src/dbaccess.h b/src/dbaccess.h index 150d3df..0944ec3 100644 --- a/src/dbaccess.h +++ b/src/dbaccess.h @@ -15,7 +15,7 @@ void rotatemonths(void); void cleartop10(const char *iface, const char *dirname); void rebuilddbtotal(const char *iface, const char *dirname); int validatedb(void); -void restoredb(FILE *input); +int importdb(const char *filename); /* version 1.0 database format aka db v1 */ typedef struct { diff --git a/src/vnstat.c b/src/vnstat.c index d74ef00..8ec67d3 100644 --- a/src/vnstat.c +++ b/src/vnstat.c @@ -29,10 +29,10 @@ vnStat - Copyright (c) 2002-2014 Teemu Toivola int main(int argc, char *argv[]) { int i; - int currentarg, update=0, query=1, newdb=0, reset=0, sync=0, merged=0, savemerged=0, restore_db=0; + int currentarg, update=0, query=1, newdb=0, reset=0, sync=0, merged=0, savemerged=0, import=0; int active=-1, files=0, force=0, cleartop=0, rebuildtotal=0, traffic=0; int livetraffic=0, defaultiface=1, delete=0, livemode=0; - char interface[32], dirname[512], nick[32]; + char interface[32], dirname[512], nick[32], filename[512]; char definterface[32], cfgfile[512], *ifacelist=NULL; time_t current; DIR *dir=NULL; @@ -111,8 +111,8 @@ int main(int argc, char *argv[]) { printf(" -s, --short use short output\n"); printf(" -ru, --rateunit swap configured rate unit\n"); printf(" --oneline show simple parseable format\n"); - printf(" --dumpdb show database in parseable format\n"); - printf(" --restoredb restore database from previous --dumpdb\n"); + printf(" --exportdb dump database in text format\n"); + printf(" --importdb import previously exported database\n"); printf(" --xml show database in xml format\n"); printf(" Misc:\n"); @@ -241,8 +241,18 @@ int main(int argc, char *argv[]) { query=0; if (debug) printf("Updating database...\n"); - } else if (strcmp(argv[currentarg],"--restoredb")==0) { - restore_db=1; + } else if (strcmp(argv[currentarg],"--importdb")==0) { + if (currentarg+1