1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
|
/*
* luci-bwc - Very simple bandwidth collector cache for LuCI realtime graphs
*
* Copyright (C) 2010 Jo-Philipp Wich <xm@subsignal.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <inttypes.h>
#include <fcntl.h>
#include <time.h>
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <arpa/inet.h>
#define STEP_COUNT 60
#define STEP_TIME 1
#define DB_PATH "/var/lib/luci-bwc"
#define DB_IF_FILE DB_PATH "/if/%s"
#define DB_LD_FILE DB_PATH "/load"
#define IF_SCAN_PATTERN \
" %[^ :]:%" SCNu64 " %" SCNu64 \
" %*d %*d %*d %*d %*d %*d" \
" %" SCNu64 " %" SCNu64
#define LD_SCAN_PATTERN \
"%f %f %f"
struct traffic_entry {
uint64_t time;
uint64_t rxb;
uint64_t rxp;
uint64_t txb;
uint64_t txp;
};
struct load_entry {
uint64_t time;
uint16_t load1;
uint16_t load5;
uint16_t load15;
};
static uint64_t htonll(uint64_t value)
{
int num = 1;
if (*(char *)&num == 1)
return htonl((uint32_t)(value & 0xFFFFFFFF)) |
htonl((uint32_t)(value >> 32));
return value;
}
#define ntohll htonll
static int init_directory(char *path)
{
char *p = path;
for (p = &path[1]; *p; p++)
{
if (*p == '/')
{
*p = 0;
if (mkdir(path, 0700) && (errno != EEXIST))
return -1;
*p = '/';
}
}
return 0;
}
static int update_file(const char *path, void *entry, int esize)
{
int rv = -1;
int file;
char *map;
if ((file = open(path, O_RDWR)) >= 0)
{
map = mmap(NULL, esize * STEP_COUNT, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_LOCKED, file, 0);
if ((map != NULL) && (map != MAP_FAILED))
{
memmove(map, map + esize, esize * (STEP_COUNT-1));
memcpy(map + esize * (STEP_COUNT-1), entry, esize);
munmap(map, esize * STEP_COUNT);
rv = 0;
}
close(file);
}
return rv;
}
static int init_ifstat(const char *ifname)
{
int i, file;
char path[1024];
struct traffic_entry e = { 0 };
snprintf(path, sizeof(path), DB_IF_FILE, ifname);
if (init_directory(path))
return -1;
if ((file = open(path, O_WRONLY | O_CREAT, 0600)) >= 0)
{
for (i = 0; i < STEP_COUNT; i++)
{
if (write(file, &e, sizeof(struct traffic_entry)) < 0)
break;
}
close(file);
return 0;
}
return -1;
}
static int update_ifstat(
const char *ifname, uint64_t rxb, uint64_t rxp, uint64_t txb, uint64_t txp
) {
char path[1024];
struct stat s;
struct traffic_entry e;
snprintf(path, sizeof(path), DB_IF_FILE, ifname);
if (stat(path, &s))
{
if (init_ifstat(ifname))
{
fprintf(stderr, "Failed to init %s: %s\n",
path, strerror(errno));
return -1;
}
}
e.time = htonll(time(NULL));
e.rxb = htonll(rxb);
e.rxp = htonll(rxp);
e.txb = htonll(txb);
e.txp = htonll(txp);
return update_file(path, &e, sizeof(struct traffic_entry));
}
static int init_ldstat(void)
{
int i, file;
char path[1024];
struct load_entry e = { 0 };
snprintf(path, sizeof(path), DB_LD_FILE);
if (init_directory(path))
return -1;
if ((file = open(path, O_WRONLY | O_CREAT, 0600)) >= 0)
{
for (i = 0; i < STEP_COUNT; i++)
{
if (write(file, &e, sizeof(struct load_entry)) < 0)
break;
}
close(file);
return 0;
}
return -1;
}
static int update_ldstat(uint16_t load1, uint16_t load5, uint16_t load15)
{
char path[1024];
struct stat s;
struct load_entry e;
snprintf(path, sizeof(path), DB_LD_FILE);
if (stat(path, &s))
{
if (init_ldstat())
{
fprintf(stderr, "Failed to init %s: %s\n",
path, strerror(errno));
return -1;
}
}
e.time = htonll(time(NULL));
e.load1 = htons(load1);
e.load5 = htons(load5);
e.load15 = htons(load15);
return update_file(path, &e, sizeof(struct load_entry));
}
static int run_daemon(int nofork)
{
FILE *info;
uint64_t rxb, txb, rxp, txp;
float lf1, lf5, lf15;
char line[1024];
char ifname[16];
if (!nofork)
{
switch (fork())
{
case -1:
perror("fork()");
return -1;
case 0:
if (chdir("/") < 0)
{
perror("chdir()");
exit(1);
}
close(0);
close(1);
close(2);
break;
default:
exit(0);
}
}
/* go */
while (1)
{
if ((info = fopen("/proc/net/dev", "r")) != NULL)
{
while (fgets(line, sizeof(line), info))
{
if (strchr(line, '|'))
continue;
if (sscanf(line, IF_SCAN_PATTERN, ifname, &rxb, &rxp, &txb, &txp))
{
if (strncmp(ifname, "lo", sizeof(ifname)))
update_ifstat(ifname, rxb, rxp, txb, txp);
}
}
fclose(info);
}
if ((info = fopen("/proc/loadavg", "r")) != NULL)
{
if (fscanf(info, LD_SCAN_PATTERN, &lf1, &lf5, &lf15))
{
update_ldstat((uint16_t)(lf1 * 100),
(uint16_t)(lf5 * 100),
(uint16_t)(lf15 * 100));
}
fclose(info);
}
sleep(STEP_TIME);
}
}
static int run_dump_ifname(const char *ifname)
{
int rv = 1;
int i, file;
int entrysize = sizeof(struct traffic_entry);
int mapsize = STEP_COUNT * entrysize;
char path[1024];
char *map;
struct traffic_entry *e;
snprintf(path, sizeof(path), DB_IF_FILE, ifname);
if ((file = open(path, O_RDONLY)) >= 0)
{
map = mmap(NULL, mapsize, PROT_READ, MAP_SHARED | MAP_LOCKED, file, 0);
if ((map != NULL) && (map != MAP_FAILED))
{
for (i = 0; i < mapsize; i += entrysize)
{
e = (struct traffic_entry *) &map[i];
if (!e->time)
continue;
printf("[ %" PRIu64 ", %" PRIu64 ", %" PRIu64
", %" PRIu64 ", %" PRIu64 " ]%s\n",
ntohll(e->time),
ntohll(e->rxb), ntohll(e->rxp),
ntohll(e->txb), ntohll(e->txp),
((i + entrysize) < mapsize) ? "," : "");
}
munmap(map, mapsize);
rv = 0;
}
close(file);
}
else
{
fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
}
return rv;
}
static int run_dump_load(void)
{
int rv = 1;
int i, file;
int entrysize = sizeof(struct load_entry);
int mapsize = STEP_COUNT * entrysize;
char path[1024];
char *map;
struct load_entry *e;
snprintf(path, sizeof(path), DB_LD_FILE);
if ((file = open(path, O_RDONLY)) >= 0)
{
map = mmap(NULL, mapsize, PROT_READ, MAP_SHARED | MAP_LOCKED, file, 0);
if ((map != NULL) && (map != MAP_FAILED))
{
for (i = 0; i < mapsize; i += entrysize)
{
e = (struct load_entry *) &map[i];
if (!e->time)
continue;
printf("[ %" PRIu64 ", %u, %u, %u ]%s\n",
ntohll(e->time),
ntohs(e->load1), ntohs(e->load5), ntohs(e->load15),
((i + entrysize) < mapsize) ? "," : "");
}
munmap(map, mapsize);
rv = 0;
}
close(file);
}
else
{
fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
}
return rv;
}
int main(int argc, char *argv[])
{
int opt;
int daemon = 0;
int nofork = 0;
int iprint = 0;
int lprint = 0;
char *ifname = NULL;
while ((opt = getopt(argc, argv, "dfi:l")) > -1)
{
switch (opt)
{
case 'd':
daemon = 1;
break;
case 'f':
nofork = 1;
break;
case 'i':
iprint = 1;
ifname = optarg;
break;
case 'l':
lprint = 1;
break;
default:
break;
}
}
if (daemon)
return run_daemon(nofork);
else if (iprint && ifname)
return run_dump_ifname(ifname);
else if (lprint)
return run_dump_load();
else
fprintf(stderr,
"Usage:\n"
" %s -d [-f]\n"
" %s -i ifname\n"
" %s -l\n",
argv[0], argv[0], argv[0]
);
return 1;
}
|