vnstat  2.9
About: vnStat is a console-based network traffic monitor (using the /proc filesystem).
  Fossies Dox: vnstat-2.9.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

vnstat.c
Go to the documentation of this file.
1/*
2vnStat - Copyright (C) 2002-2022 Teemu Toivola <tst@iki.fi>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 dated June, 1991.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along
14 with this program; if not, write to the Free Software Foundation, Inc.,
15 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16*/
17
18#include "common.h"
19#include "ifinfo.h"
20#include "dbsql.h"
21#include "misc.h"
22#include "cfg.h"
23#include "ibw.h"
24#include "vnstat_func.h"
25
26int main(int argc, char *argv[])
27{
28 int currentarg;
29 DIR *dir = NULL;
30 PARAMS p;
31
32 initparams(&p);
33
34 /* early check for debug and config parameter */
35 if (argc > 1) {
36 for (currentarg = 1; currentarg < argc; currentarg++) {
37 if ((strcmp(argv[currentarg], "-D") == 0) || (strcmp(argv[currentarg], "--debug") == 0)) {
38 debug = 1;
39 printf("Debug enabled, vnstat %s\n", VERSION);
40 } else if (strcmp(argv[currentarg], "--config") == 0) {
41 if (currentarg + 1 < argc) {
42 strncpy_nt(p.cfgfile, argv[currentarg + 1], 512);
43 if (debug)
44 printf("Used config file: %s\n", p.cfgfile);
45 currentarg++;
46 } else {
47 printf("Error: File for --config missing.\n");
48 return 1;
49 }
50 }
51 }
52 }
53
54 /* load config if available */
55 if (!loadcfg(p.cfgfile, CT_CLI)) {
56 return 1;
57 }
58 if (!ibwloadcfg(p.cfgfile)) {
59 return 1;
60 }
61
63 strncpy_nt(p.interface, "default", 32);
65 strncpy_nt(p.alias, "none", 32);
66
67 parseargs(&p, argc, argv);
68
69 /* open database and see if it contains any interfaces */
70 if (!p.traffic && !p.livetraffic) {
71 if ((dir = opendir(cfg.dbdir)) != NULL) {
72 if (debug)
73 printf("Dir OK\n");
74 closedir(dir);
75 if (!db_open_ro()) {
76 printf("Error: Failed to open database \"%s/%s\" in read-only mode.\n", cfg.dbdir, DATABASEFILE);
77 if (errno == ENOENT) {
78 printf("The vnStat daemon should have created the database when started.\n");
79 printf("Check that it is configured and running. See also \"man vnstatd\".\n");
80 }
81 return 1;
82 }
84 if (debug)
85 printf("%" PRIu64 " interface(s) found\n", p.dbifcount);
86
87 if (p.dbifcount > 1) {
89 }
90 } else {
91 printf("Error: Unable to open database directory \"%s\": %s\n", cfg.dbdir, strerror(errno));
92 if (errno == ENOENT) {
93 printf("The vnStat daemon should have created this directory when started.\n");
94 printf("Check that it is configured and running. See also \"man vnstatd\".\n");
95 } else {
96 printf("Make sure it is at least read enabled for current user.\n");
97 printf("Use --help for help.\n");
98 }
99 return 1;
100 }
101 }
102
103 /* set used interface if none specified */
105
106 /* parameter handlers */
107 handleshowalert(&p);
111 handlesetalias(&p);
112 handleshowdata(&p);
114
115 /* show something if nothing was shown previously */
116 if (!p.query && !p.traffic && !p.livetraffic) {
117
118 /* give more help if there's no database */
119 if (p.dbifcount == 0) {
121 printf("No interfaces found in the database, nothing to do. Use --help for help.\n\n");
122 printf("Interfaces can be added to the database with the following command:\n");
123 printf(" %s --add -i eth0\n\n", argv[0]);
124 printf("Replace 'eth0' with the interface that should be monitored.\n\n");
125 if (strlen(cfg.cfgfile)) {
126 printf("The default interface can be changed by updating the \"Interface\" keyword\n");
127 printf("value in the configuration file \"%s\".\n\n", cfg.cfgfile);
128 }
129 printf("The following interfaces are currently available:\n %s\n", p.ifacelist);
130 free(p.ifacelist);
131 } else {
132 printf("Nothing to do. Use --help for help.\n");
133 }
134 }
135
136 /* cleanup */
137 db_close();
138 ibwflush();
139
140 return 0;
141}
void configlocale(void)
Definition: cfg.c:549
int loadcfg(const char *cfgfile, const ConfigType type)
Definition: cfg.c:4
@ CT_CLI
Definition: cfg.h:14
int debug
Definition: common.c:8
CFG cfg
Definition: common.c:4
char * strncpy_nt(char *dest, const char *src, size_t n)
Definition: common.c:253
#define DATABASEFILE
Definition: common.h:72
int db_close(void)
Definition: dbsql.c:248
int db_open_ro(void)
Definition: dbsql.c:11
uint64_t db_getinterfacecount(void)
Definition: dbsql.c:414
void ibwflush(void)
Definition: ibw.c:182
int ibwloadcfg(const char *cfgfile)
Definition: ibw.c:6
int getifliststring(char **ifacelist, int showspeed)
Definition: ifinfo.c:64
char iface[32]
Definition: common.h:308
char dbdir[512]
Definition: common.h:310
char cfgfile[512]
Definition: common.h:319
int livetraffic
Definition: vnstat_func.h:7
char alias[32]
Definition: vnstat_func.h:10
int traffic
Definition: vnstat_func.h:6
char cfgfile[512]
Definition: vnstat_func.h:11
char definterface[32]
Definition: vnstat_func.h:11
int query
Definition: vnstat_func.h:5
char * ifacelist
Definition: vnstat_func.h:11
char interface[32]
Definition: vnstat_func.h:10
uint64_t dbifcount
Definition: vnstat_func.h:9
int main(int argc, char *argv[])
Definition: vnstat.c:26
void handleshowalert(PARAMS *p)
Definition: vnstat_func.c:760
void handleifselection(PARAMS *p)
Definition: vnstat_func.c:1111
void handlesetalias(PARAMS *p)
Definition: vnstat_func.c:935
void handleaddinterface(PARAMS *p)
Definition: vnstat_func.c:877
void initparams(PARAMS *p)
Definition: vnstat_func.c:14
void handletrafficmeters(PARAMS *p)
Definition: vnstat_func.c:1060
void handleshowdata(PARAMS *p)
Definition: vnstat_func.c:970
void handlerenameinterface(PARAMS *p)
Definition: vnstat_func.c:826
void handleremoveinterface(PARAMS *p)
Definition: vnstat_func.c:784
void parseargs(PARAMS *p, const int argc, char **argv)
Definition: vnstat_func.c:141