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)  

dbjson.c
Go to the documentation of this file.
1#include "common.h"
2#include "dbsql.h"
3#include "dbjson.h"
4
5void showjson(const char *interface, const int ifcount, 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 if (ifcount) {
20 printf(",");
21 }
22 printf("{");
23 printf("\"name\":\"%s\",", info.name);
24 printf("\"alias\":\"%s\",", info.alias);
25
26 printf("\"created\":{");
27 jsondate(&info.created, 1);
28 printf("},");
29 printf("\"updated\":{");
30 jsondate(&info.updated, 2);
31 printf("},");
32
33 printf("\"traffic\":");
34 printf("{\"total\":{\"rx\":%" PRIu64 ",\"tx\":%" PRIu64 "},", info.rxtotal, info.txtotal);
35
36 switch (mode) {
37 case 'd':
38 jsondump(&info, "day", 1, databegin, dataend);
39 break;
40 case 'm':
41 jsondump(&info, "month", 3, databegin, dataend);
42 break;
43 case 't':
44 jsondump(&info, "top", 1, databegin, dataend);
45 break;
46 case 'h':
47 jsondump(&info, "hour", 2, databegin, dataend);
48 break;
49 case 'y':
50 jsondump(&info, "year", 4, databegin, dataend);
51 break;
52 case 'f':
53 jsondump(&info, "fiveminute", 2, databegin, dataend);
54 break;
55 case 'a':
56 default:
57 jsondump(&info, "fiveminute", 2, databegin, dataend);
58 printf(",");
59 jsondump(&info, "hour", 2, databegin, dataend);
60 printf(",");
61 jsondump(&info, "day", 1, databegin, dataend);
62 printf(",");
63 jsondump(&info, "month", 3, databegin, dataend);
64 printf(",");
65 jsondump(&info, "year", 4, databegin, dataend);
66 printf(",");
67 jsondump(&info, "top", 1, databegin, dataend);
68 break;
69 }
70
71 printf("}}");
72
74}
75
76void jsondump(const interfaceinfo *interface, const char *tablename, const int datetype, const char *databegin, const char *dataend)
77{
78 int first = 1;
79 dbdatalist *datalist = NULL, *datalist_i = NULL;
80 dbdatalistinfo datainfo;
81
82 if (!db_getdata_range(&datalist, &datainfo, interface->name, tablename, (uint32_t)cfg.listjsonxml, databegin, dataend)) {
83 printf("Error: Failed to fetch %s data.\n", tablename);
84 return;
85 }
86
87 printf("\"%s\":[", tablename);
88 datalist_i = datalist;
89 while (datalist_i != NULL) {
90 if (!first) {
91 printf(",");
92 } else {
93 first = 0;
94 }
95 printf("{\"id\":%" PRId64 ",", datalist_i->rowid);
96 jsondate(&datalist_i->timestamp, datetype);
97 printf(",\"rx\":%" PRIu64 ",\"tx\":%" PRIu64 "}", datalist_i->rx, datalist_i->tx);
98 datalist_i = datalist_i->next;
99 }
100 dbdatalistfree(&datalist);
101 printf("]");
102}
103
104void jsondate(const time_t *date, const int type)
105{
106 struct tm *d;
107
108 d = localtime(date);
109
110 switch (type) {
111 case 1:
112 printf("\"date\":{\"year\":%d,\"month\":%d,\"day\":%d}",
113 1900 + d->tm_year, 1 + d->tm_mon, d->tm_mday);
114 break;
115 case 2:
116 printf("\"date\":{\"year\":%d,\"month\":%d,\"day\":%d},\"time\":{\"hour\":%d,\"minute\":%d}",
117 1900 + d->tm_year, 1 + d->tm_mon, d->tm_mday, d->tm_hour, d->tm_min);
118 break;
119 case 3:
120 printf("\"date\":{\"year\":%d,\"month\":%d}",
121 1900 + d->tm_year, 1 + d->tm_mon);
122 break;
123 case 4:
124 printf("\"date\":{\"year\":%d}",
125 1900 + d->tm_year);
126 break;
127 default:
128 break;
129 }
130}
131
132void jsonheader(void)
133{
134 printf("{\"vnstatversion\":\"%s\",\"jsonversion\":\"%d\",\"interfaces\":[", getversion(), JSONVERSION);
135}
136
137void jsonfooter(void)
138{
139 printf("]}\n");
140}
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 JSONVERSION
Definition: common.h:200
#define __func__
Definition: common.h:51
void jsonfooter(void)
Definition: dbjson.c:137
void showjson(const char *interface, const int ifcount, const char mode, const char *databegin, const char *dataend)
Definition: dbjson.c:5
void jsondump(const interfaceinfo *interface, const char *tablename, const int datetype, const char *databegin, const char *dataend)
Definition: dbjson.c:76
void jsonheader(void)
Definition: dbjson.c:132
void jsondate(const time_t *date, const int type)
Definition: dbjson.c:104
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
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