From: Teemu Toivola Date: Fri, 27 Jan 2017 20:53:00 +0000 (+0200) Subject: fix log buffering visible at least with systemd, reformat error prints, replace creat... X-Git-Tag: v2.0~98^2~38 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c8daed89a6e10857985138182847cbef45558195;p=vnstat fix log buffering visible at least with systemd, reformat error prints, replace create/delete parameters with add/remove --- diff --git a/src/common.c b/src/common.c index dcba7e7..4546738 100644 --- a/src/common.c +++ b/src/common.c @@ -44,7 +44,7 @@ int printe(PrintType type) printf("%d: %s\n", type, errorstring); break; } - + fflush(stdout); } return result; diff --git a/src/dbjson.c b/src/dbjson.c index b9612c7..42faaf5 100644 --- a/src/dbjson.c +++ b/src/dbjson.c @@ -67,8 +67,7 @@ void jsondump(const interfaceinfo *interface, const char *tablename, const int d dbdatalistinfo datainfo; if (!db_getdata(&datalist, &datainfo, interface->name, tablename, -1)) { - /* TODO: match with other output style */ - printf("Error: failed to fetch %s data\n", tablename); + printf("Error: Failed to fetch %s data.\n", tablename); return; } diff --git a/src/dbshow.c b/src/dbshow.c index 5c2e982..f480549 100644 --- a/src/dbshow.c +++ b/src/dbshow.c @@ -122,8 +122,7 @@ void showsummary(const interfaceinfo *interface, const int shortmode) } if (!db_getdata(&datalist, &datainfo, interface->name, "month", 2)) { - /* TODO: match with other output style */ - printf("Error: failed to fetch monthly data\n"); + printf("Error: Failed to fetch month data.\n"); return; } @@ -203,8 +202,7 @@ void showsummary(const interfaceinfo *interface, const int shortmode) strftime(yesterdaystr, DATEBUFFLEN, cfg.dformat, d); if (!db_getdata(&datalist, &datainfo, interface->name, "day", 2)) { - /* TODO: match with other output style */ - printf("Error: failed to fetch daily data\n"); + printf("Error: Failed to fetch day data.\n"); return; } @@ -359,8 +357,7 @@ void showlist(const interfaceinfo *interface, const char *listname) } if (!db_getdata(&datalist, &datainfo, interface->name, listname, limit)) { - /* TODO: match with other output style */ - printf("Error: failed to fetch %s data\n", titlename); + printf("Error: Failed to fetch %s data.\n", titlename); return; } @@ -503,8 +500,7 @@ void showoneline(const interfaceinfo *interface) printf(";"); if (!db_getdata(&datalist, &datainfo, interface->name, "day", 1)) { - /* TODO: match with other output style */ - printf("\nError: failed to fetch daily data\n"); + printf("\nError: Failed to fetch day data.\n"); return; } @@ -529,8 +525,7 @@ void showoneline(const interfaceinfo *interface) dbdatalistfree(&datalist); if (!db_getdata(&datalist, &datainfo, interface->name, "month", 1)) { - /* TODO: match with other output style */ - printf("\nError: failed to fetch monthly data\n"); + printf("\nError: Failed to fetch month data.\n"); return; } @@ -582,8 +577,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 hourly data\n"); + printf("Error: Failed to fetch hour data.\n"); return; } @@ -737,7 +731,7 @@ void exportdb(const interfaceinfo *interface) 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]); + printf("Error: Failed to fetch %s data.\n", datatables[i]); return; } datalist_i = datalist; diff --git a/src/dbsql.c b/src/dbsql.c index 57e8e11..11f3c5d 100644 --- a/src/dbsql.c +++ b/src/dbsql.c @@ -814,7 +814,8 @@ void dbiflistfree(dbiflist **dbifl) int db_getdata(dbdatalist **dbdata, dbdatalistinfo *listinfo, const char *iface, const char *table, const uint32_t resultlimit) { - int ret = 1; + int ret = 1, i; + char *datatables[] = {"fiveminute", "hour", "day", "month", "year", "top"}; char sql[512], limit[64]; sqlite3_int64 ifaceid = 0; sqlite3_stmt *sqlstmt; @@ -829,7 +830,16 @@ int db_getdata(dbdatalist **dbdata, dbdatalistinfo *listinfo, const char *iface, return 0; } - /* TODO: add table validation here */ + ret = 0; + for (i=0; i<6; i++) { + if (strcmp(table, datatables[i]) == 0) { + ret = 1; + break; + } + } + if (!ret) { + return 0; + } if (resultlimit > 0) { snprintf(limit, 64, "limit %"PRIu32"", resultlimit); diff --git a/src/dbxml.c b/src/dbxml.c index e57b2fc..04bf49c 100644 --- a/src/dbxml.c +++ b/src/dbxml.c @@ -62,8 +62,7 @@ void xmldump(const interfaceinfo *interface, const char *tablename, const int da dbdatalistinfo datainfo; if (!db_getdata(&datalist, &datainfo, interface->name, tablename, -1)) { - /* TODO: match with other output style */ - printf("Error: failed to fetch %s data\n", tablename); + printf("Error: Failed to fetch %s data.\n", tablename); return; } diff --git a/src/vnstat.c b/src/vnstat.c index e411849..e6d1a3c 100644 --- a/src/vnstat.c +++ b/src/vnstat.c @@ -162,8 +162,8 @@ int main(int argc, char *argv[]) { printf("Error: Locale for %s missing.\n", argv[currentarg]); return 1; } - } else if (strcmp(argv[currentarg],"--create")==0) { - p.create=1; + } else if (strcmp(argv[currentarg],"--add")==0) { + p.addiface=1; p.query=0; } else if ((strcmp(argv[currentarg],"-u")==0) || (strcmp(argv[currentarg],"--update")==0)) { printf("Error: The \"%s\" parameter is not supported in this version.\n", argv[currentarg]); @@ -280,8 +280,8 @@ int main(int argc, char *argv[]) { } else if (strcmp(argv[currentarg],"--showconfig")==0) { printcfgfile(); return 0; - } else if (strcmp(argv[currentarg],"--delete")==0) { - p.delete=1; + } else if (strcmp(argv[currentarg],"--remove")==0) { + p.removeiface=1; p.query=0; } else if (strcmp(argv[currentarg],"--iflist")==0) { getiflist(&p.ifacelist, 1); @@ -351,8 +351,8 @@ int main(int argc, char *argv[]) { p.interface[31]='\0'; /* parameter handlers */ - handledelete(&p); - handlecreate(&p); + handleremoveinterface(&p); + handleaddinterface(&p); handlesetalias(&p); handleshowdatabases(&p); handletrafficmeters(&p); @@ -363,9 +363,9 @@ int main(int argc, char *argv[]) { /* give more help if there's no database */ if (p.ifcount == 0) { getiflist(&p.ifacelist, 1); - printf("No database found, nothing to do. Use --help for help.\n\n"); - printf("A new database can be created with the following command:\n"); - printf(" %s --create -i eth0\n\n", argv[0]); + printf("No interfaces found in the database, nothing to do. Use --help for help.\n\n"); + printf("Interfaces can be added to the database with the following command:\n"); + printf(" %s --add -i eth0\n\n", argv[0]); printf("Replace 'eth0' with the interface that should be monitored.\n\n"); if (strlen(cfg.cfgfile)) { printf("The default interface can be changed by updating the \"Interface\" keyword\n"); @@ -391,7 +391,7 @@ void initparams(PARAMS *p) debug = 0; /* debug disabled by default */ disableprints = 0; /* let prints be visible */ - p->create = 0; + p->addiface = 0; p->query = 1; p->setalias = 0; p->ifcount = 0; @@ -399,7 +399,7 @@ void initparams(PARAMS *p) p->traffic = 0; p->livetraffic = 0; p->defaultiface = 1; - p->delete=0; + p->removeiface=0; p->livemode = 0; p->ifacelist = NULL; p->cfgfile[0] = '\0'; @@ -430,10 +430,6 @@ void showhelp(PARAMS *p) void showlonghelp(PARAMS *p) { - /* TODO: update */ - /* --create could be replaced with --add as it adds the interface to the database */ - /* --delete could be similarly replaced with --remove */ - printf(" vnStat %s by Teemu Toivola \n\n", getversion()); printf(" Query:\n"); @@ -451,8 +447,8 @@ void showlonghelp(PARAMS *p) printf(" --xml show database in xml format\n"); printf(" Modify:\n"); - printf(" --create create database\n"); - printf(" --delete delete database\n"); + printf(" --add add interface to database\n"); + printf(" --remove remove interface from database\n"); printf(" --setalias set alias for interface\n"); printf(" Misc:\n"); @@ -473,9 +469,9 @@ void showlonghelp(PARAMS *p) printf("See also \"man vnstat\".\n"); } -void handledelete(PARAMS *p) +void handleremoveinterface(PARAMS *p) { - if (!p->delete) { + if (!p->removeiface) { return; } @@ -485,27 +481,27 @@ void handledelete(PARAMS *p) } if (!p->force) { - printf("Warning:\nThe current option would delete all data\nabout interface \"%s\" from the database.\n", p->interface); + printf("Warning:\nThe current option would remove all data\nabout interface \"%s\" from the database.\n", p->interface); printf("Use --force in order to really do that.\n"); exit(EXIT_FAILURE); } if (db_removeinterface(p->interface)) { - printf("Interface \"%s\" deleted from database.\n", p->interface); - printf("The interface will no longer be monitored. Use --create\n"); + printf("Interface \"%s\" removed from database.\n", p->interface); + printf("The interface will no longer be monitored. Use --add\n"); printf("if monitoring the interface is again needed.\n"); exit(EXIT_SUCCESS); } else { - printf("Error: Deleting interface \"%s\" from database failed.\n", p->interface); + printf("Error: Removing interface \"%s\" from database failed.\n", p->interface); exit(EXIT_FAILURE); } } -void handlecreate(PARAMS *p) +void handleaddinterface(PARAMS *p) { char dbfile[512]; - if (!p->create) { + if (!p->addiface) { return; } diff --git a/src/vnstat.h b/src/vnstat.h index a68b89f..4dcc866 100644 --- a/src/vnstat.h +++ b/src/vnstat.h @@ -3,8 +3,8 @@ typedef struct { int query, setalias; - int create, ifcount, force, traffic; - int livetraffic, defaultiface, delete, livemode; + int addiface, ifcount, force, traffic; + int livetraffic, defaultiface, removeiface, livemode; char interface[32], dirname[512], alias[32], filename[512]; char definterface[32], cfgfile[512], *ifacelist, jsonmode, xmlmode; } PARAMS; @@ -12,8 +12,8 @@ typedef struct { void initparams(PARAMS *p); void showhelp(PARAMS *p); void showlonghelp(PARAMS *p); -void handledelete(PARAMS *p); -void handlecreate(PARAMS *p); +void handleremoveinterface(PARAMS *p); +void handleaddinterface(PARAMS *p); void handlesetalias(PARAMS *p); void handleshowdatabases(PARAMS *p); void showoneinterface(PARAMS *p, const char *interface);