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