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)  

dbxml.c
Go to the documentation of this file.
1#include "common.h"
2#include "dbsql.h"
3#include "dbxml.h"
4
5void showxml(const char *interface, const char mode, const char *databegin, const char *dataend)
6{
7 interfaceinfo info;
8
10
11 if (!db_getinterfacecountbyname(interface)) {
12 return;
13 }
14
15 if (!db_getinterfaceinfo(interface, &info)) {
16 return;
17 }
18
19 printf(" <interface name=\"%s\">\n", info.name);
20
21 printf(" <name>%s</name>\n", info.name);
22 printf(" <alias>%s</alias>\n", info.alias);
23
24 printf(" <created>");
25 xmldate(&info.created, 1);
26 printf("</created>\n");
27 printf(" <updated>");
28 xmldate(&info.updated, 2);
29 printf("</updated>\n");
30
31 printf(" <traffic>\n");
32 printf(" <total><rx>%" PRIu64 "</rx><tx>%" PRIu64 "</tx></total>\n", info.rxtotal, info.txtotal);
33
34 switch (mode) {
35 case 'd':
36 xmldump(&info, "day", 1, databegin, dataend);
37 break;
38 case 'm':
39 xmldump(&info, "month", 3, databegin, dataend);
40 break;
41 case 't':
42 xmldump(&info, "top", 1, databegin, dataend);
43 break;
44 case 'h':
45 xmldump(&info, "hour", 2, databegin, dataend);
46 break;
47 case 'y':
48 xmldump(&info, "year", 4, databegin, dataend);
49 break;
50 case 'f':
51 xmldump(&info, "fiveminute", 2, databegin, dataend);
52 break;
53 case 'a':
54 default:
55 xmldump(&info, "fiveminute", 2, databegin, dataend);
56 xmldump(&info, "hour", 2, databegin, dataend);
57 xmldump(&info, "day", 1, databegin, dataend);
58 xmldump(&info, "month", 3, databegin, dataend);
59 xmldump(&info, "year", 2, databegin, dataend);
60 xmldump(&info, "top", 1, databegin, dataend);
61 break;
62 }
63
64 printf(" </traffic>\n");
65 printf(" </interface>\n");
66
68}
69
70void xmldump(const interfaceinfo *interface, const char *tablename, const int datetype, const char *databegin, const char *dataend)
71{
72 dbdatalist *datalist = NULL, *datalist_i = NULL;
73 dbdatalistinfo datainfo;
74
75 if (!db_getdata_range(&datalist, &datainfo, interface->name, tablename, (uint32_t)cfg.listjsonxml, databegin, dataend)) {
76 printf("Error: Failed to fetch %s data.\n", tablename);
77 return;
78 }
79
80 printf(" <%ss>\n", tablename);
81 datalist_i = datalist;
82 while (datalist_i != NULL) {
83 printf(" <%s id=\"%" PRId64 "\">", tablename, datalist_i->rowid);
84 xmldate(&datalist_i->timestamp, datetype);
85 printf("<rx>%" PRIu64 "</rx><tx>%" PRIu64 "</tx></%s>\n", datalist_i->rx, datalist_i->tx, tablename);
86 datalist_i = datalist_i->next;
87 }
88 dbdatalistfree(&datalist);
89 printf(" </%ss>\n", tablename);
90}
91
92void xmldate(const time_t *date, const int type)
93{
94 struct tm *d;
95
96 d = localtime(date);
97
98 switch (type) {
99 case 1:
100 printf("<date><year>%d</year><month>%02d</month><day>%02d</day></date>",
101 1900 + d->tm_year, 1 + d->tm_mon, d->tm_mday);
102 break;
103 case 2:
104 printf("<date><year>%d</year><month>%02d</month><day>%02d</day></date><time><hour>%02d</hour><minute>%02d</minute></time>",
105 1900 + d->tm_year, 1 + d->tm_mon, d->tm_mday, d->tm_hour, d->tm_min);
106 break;
107 case 3:
108 printf("<date><year>%d</year><month>%02d</month></date>",
109 1900 + d->tm_year, 1 + d->tm_mon);
110 break;
111 case 4:
112 printf("<date><year>%d</year></date>",
113 1900 + d->tm_year);
114 break;
115 default:
116 break;
117 }
118}
119
120void xmlheader(void)
121{
122 printf("<vnstat version=\"%s\" xmlversion=\"%d\">\n", getversion(), XMLVERSION);
123}
124
125void xmlfooter(void)
126{
127 printf("</vnstat>\n");
128}
void timeused_debug(const char *func, const int reset)
Definition: common.c:322
char * getversion(void)
Definition: common.c:285
CFG cfg
Definition: common.c:4
#define __func__
Definition: common.h:51
#define XMLVERSION
Definition: common.h:196
uint64_t db_getinterfacecountbyname(const char *iface)
Definition: dbsql.c:419
int db_getdata_range(dbdatalist **dbdata, dbdatalistinfo *listinfo, const char *iface, const char *table, const uint32_t resultlimit, const char *databegin, const char *dataend)
Definition: dbsql.c:1217
void dbdatalistfree(dbdatalist **dbdata)
Definition: dbsql.c:1393
int db_getinterfaceinfo(const char *iface, interfaceinfo *info)
Definition: dbsql.c:651
void xmlfooter(void)
Definition: dbxml.c:125
void xmldate(const time_t *date, const int type)
Definition: dbxml.c:92
void xmlheader(void)
Definition: dbxml.c:120
void showxml(const char *interface, const char mode, const char *databegin, const char *dataend)
Definition: dbxml.c:5
void xmldump(const interfaceinfo *interface, const char *tablename, const int datetype, const char *databegin, const char *dataend)
Definition: dbxml.c:70
int32_t listjsonxml
Definition: common.h:324
char alias[32]
Definition: dbsql.h:24
time_t created
Definition: dbsql.h:26
time_t updated
Definition: dbsql.h:26
uint64_t txtotal
Definition: dbsql.h:28
char name[32]
Definition: dbsql.h:24
uint64_t rxtotal
Definition: dbsql.h:28