Merge pull request #3591 from jow-/vnstat2-client-side
[oweals/luci.git] / modules / luci-mod-status / 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 <jow@openwrt.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 #define _BSD_SOURCE
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdint.h>
25 #include <inttypes.h>
26 #include <fcntl.h>
27 #include <time.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <signal.h>
31 #include <endian.h>
32 #include <dirent.h>
33
34 #include <sys/stat.h>
35 #include <sys/mman.h>
36 #include <sys/types.h>
37 #include <arpa/inet.h>
38
39 #include <dlfcn.h>
40 #include <iwinfo.h>
41
42 #define STEP_COUNT      60
43 #define STEP_TIME       1
44 #define TIMEOUT         10
45
46 #define PID_PATH        "/var/run/luci-bwc.pid"
47
48 #define DB_PATH         "/var/lib/luci-bwc"
49 #define DB_IF_FILE      DB_PATH "/if/%s"
50 #define DB_RD_FILE      DB_PATH "/radio/%s"
51 #define DB_CN_FILE      DB_PATH "/connections"
52 #define DB_LD_FILE      DB_PATH "/load"
53
54 #define LD_SCAN_PATTERN \
55         "%f %f %f"
56
57
58 struct file_map {
59         int fd;
60         int size;
61         char *mmap;
62 };
63
64 struct traffic_entry {
65         uint32_t time;
66         uint64_t rxb;
67         uint64_t rxp;
68         uint64_t txb;
69         uint64_t txp;
70 };
71
72 struct conn_entry {
73         uint32_t time;
74         uint32_t udp;
75         uint32_t tcp;
76         uint32_t other;
77 };
78
79 struct load_entry {
80         uint32_t time;
81         uint16_t load1;
82         uint16_t load5;
83         uint16_t load15;
84 };
85
86 struct radio_entry {
87         uint32_t time;
88         uint16_t rate;
89         uint8_t  rssi;
90         uint8_t  noise;
91 };
92
93 static int readpid(void)
94 {
95         int fd;
96         int pid = -1;
97         char buf[9] = { 0 };
98
99         if ((fd = open(PID_PATH, O_RDONLY)) > -1)
100         {
101                 if (read(fd, buf, sizeof(buf)))
102                 {
103                         buf[8] = 0;
104                         pid = atoi(buf);
105                 }
106
107                 close(fd);
108         }
109
110         return pid;
111 }
112
113 static int writepid(void)
114 {
115         int fd;
116         int wlen;
117         char buf[9] = { 0 };
118
119         if ((fd = open(PID_PATH, O_WRONLY | O_CREAT | O_TRUNC, 0600)) > -1)
120         {
121                 wlen = snprintf(buf, sizeof(buf), "%i", getpid());
122                 write(fd, buf, wlen);
123                 close(fd);
124
125                 return 0;
126         }
127
128         return -1;
129 }
130
131 static int timeout = TIMEOUT;
132 static int countdown = -1;
133
134 static void reset_countdown(int sig)
135 {
136         countdown = timeout;
137
138 }
139
140
141 static char *progname;
142 static int prognamelen;
143
144 static struct iwinfo_ops *backend = NULL;
145
146
147 static int init_directory(char *path)
148 {
149         char *p = path;
150
151         for (p = &path[1]; *p; p++)
152         {
153                 if (*p == '/')
154                 {
155                         *p = 0;
156
157                         if (mkdir(path, 0700) && (errno != EEXIST))
158                                 return -1;
159
160                         *p = '/';
161                 }
162         }
163
164         return 0;
165 }
166
167 static int init_file(char *path, int esize)
168 {
169         int i, file;
170         char buf[sizeof(struct traffic_entry)] = { 0 };
171
172         if (init_directory(path))
173                 return -1;
174
175         if ((file = open(path, O_WRONLY | O_CREAT, 0600)) >= 0)
176         {
177                 for (i = 0; i < STEP_COUNT; i++)
178                 {
179                         if (write(file, buf, esize) < 0)
180                                 break;
181                 }
182
183                 close(file);
184
185                 return 0;
186         }
187
188         return -1;
189 }
190
191 static inline uint32_t timeof(void *entry)
192 {
193         return be32toh(((struct traffic_entry *)entry)->time);
194 }
195
196 static int update_file(const char *path, void *entry, int esize)
197 {
198         int rv = -1;
199         int file;
200         char *map;
201
202         if ((file = open(path, O_RDWR)) >= 0)
203         {
204                 map = mmap(NULL, esize * STEP_COUNT, PROT_READ | PROT_WRITE,
205                                    MAP_SHARED | MAP_LOCKED, file, 0);
206
207                 if ((map != NULL) && (map != MAP_FAILED))
208                 {
209                         if (timeof(entry) > timeof(map + esize * (STEP_COUNT-1)))
210                         {
211                                 memmove(map, map + esize, esize * (STEP_COUNT-1));
212                                 memcpy(map + esize * (STEP_COUNT-1), entry, esize);
213                         }
214
215                         munmap(map, esize * STEP_COUNT);
216
217                         rv = 0;
218                 }
219
220                 close(file);
221         }
222
223         return rv;
224 }
225
226 static int mmap_file(const char *path, int esize, struct file_map *m)
227 {
228         m->fd   = -1;
229         m->size = -1;
230         m->mmap = NULL;
231
232         if ((m->fd = open(path, O_RDONLY)) >= 0)
233         {
234                 m->size = STEP_COUNT * esize;
235                 m->mmap = mmap(NULL, m->size, PROT_READ,
236                                            MAP_SHARED | MAP_LOCKED, m->fd, 0);
237
238                 if ((m->mmap != NULL) && (m->mmap != MAP_FAILED))
239                         return 0;
240         }
241
242         return -1;
243 }
244
245 static void umap_file(struct file_map *m)
246 {
247         if ((m->mmap != NULL) && (m->mmap != MAP_FAILED))
248                 munmap(m->mmap, m->size);
249
250         if (m->fd > -1)
251                 close(m->fd);
252 }
253
254 static void * iw_open(void)
255 {
256         return dlopen("/usr/lib/libiwinfo.so", RTLD_LAZY);
257 }
258
259 static int iw_update(
260         void *iw, const char *ifname, uint16_t *rate, uint8_t *rssi, uint8_t *noise
261 ) {
262         struct iwinfo_ops *(*probe)(const char *);
263         int val;
264
265         if (!backend)
266         {
267                 probe = dlsym(iw, "iwinfo_backend");
268
269                 if (!probe)
270                         return 0;
271
272                 backend = probe(ifname);
273
274                 if (!backend)
275                         return 0;
276         }
277
278         *rate = (backend->bitrate && !backend->bitrate(ifname, &val)) ? val : 0;
279         *rssi = (backend->signal && !backend->signal(ifname, &val)) ? val : 0;
280         *noise = (backend->noise && !backend->noise(ifname, &val)) ? val : 0;
281
282         return 1;
283 }
284
285 static void iw_close(void *iw)
286 {
287         void (*finish)(void);
288
289         finish = dlsym(iw, "iwinfo_finish");
290
291         if (finish)
292                 finish();
293
294         dlclose(iw);
295 }
296
297
298 static int update_ifstat(
299         const char *ifname, uint64_t rxb, uint64_t rxp, uint64_t txb, uint64_t txp
300 ) {
301         char path[1024];
302
303         struct stat s;
304         struct traffic_entry e;
305
306         snprintf(path, sizeof(path), DB_IF_FILE, ifname);
307
308         if (stat(path, &s))
309         {
310                 if (init_file(path, sizeof(struct traffic_entry)))
311                 {
312                         fprintf(stderr, "Failed to init %s: %s\n",
313                                         path, strerror(errno));
314
315                         return -1;
316                 }
317         }
318
319         e.time = htobe32(time(NULL));
320         e.rxb  = htobe64(rxb);
321         e.rxp  = htobe64(rxp);
322         e.txb  = htobe64(txb);
323         e.txp  = htobe64(txp);
324
325         return update_file(path, &e, sizeof(struct traffic_entry));
326 }
327
328 static int update_radiostat(
329         const char *ifname, uint16_t rate, uint8_t rssi, uint8_t noise
330 ) {
331         char path[1024];
332
333         struct stat s;
334         struct radio_entry e;
335
336         snprintf(path, sizeof(path), DB_RD_FILE, ifname);
337
338         if (stat(path, &s))
339         {
340                 if (init_file(path, sizeof(struct radio_entry)))
341                 {
342                         fprintf(stderr, "Failed to init %s: %s\n",
343                                         path, strerror(errno));
344
345                         return -1;
346                 }
347         }
348
349         e.time  = htobe32(time(NULL));
350         e.rate  = htobe16(rate);
351         e.rssi  = rssi;
352         e.noise = noise;
353
354         return update_file(path, &e, sizeof(struct radio_entry));
355 }
356
357 static int update_cnstat(uint32_t udp, uint32_t tcp, uint32_t other)
358 {
359         char path[1024];
360
361         struct stat s;
362         struct conn_entry e;
363
364         snprintf(path, sizeof(path), DB_CN_FILE);
365
366         if (stat(path, &s))
367         {
368                 if (init_file(path, sizeof(struct conn_entry)))
369                 {
370                         fprintf(stderr, "Failed to init %s: %s\n",
371                                         path, strerror(errno));
372
373                         return -1;
374                 }
375         }
376
377         e.time  = htobe32(time(NULL));
378         e.udp   = htobe32(udp);
379         e.tcp   = htobe32(tcp);
380         e.other = htobe32(other);
381
382         return update_file(path, &e, sizeof(struct conn_entry));
383 }
384
385 static int update_ldstat(uint16_t load1, uint16_t load5, uint16_t load15)
386 {
387         char path[1024];
388
389         struct stat s;
390         struct load_entry e;
391
392         snprintf(path, sizeof(path), DB_LD_FILE);
393
394         if (stat(path, &s))
395         {
396                 if (init_file(path, sizeof(struct load_entry)))
397                 {
398                         fprintf(stderr, "Failed to init %s: %s\n",
399                                         path, strerror(errno));
400
401                         return -1;
402                 }
403         }
404
405         e.time   = htobe32(time(NULL));
406         e.load1  = htobe16(load1);
407         e.load5  = htobe16(load5);
408         e.load15 = htobe16(load15);
409
410         return update_file(path, &e, sizeof(struct load_entry));
411 }
412
413 static int run_daemon(void)
414 {
415         DIR *dir;
416         FILE *info;
417         uint64_t rxb, txb, rxp, txp;
418         uint32_t udp, tcp, other;
419         uint16_t rate;
420         uint8_t rssi, noise;
421         float lf1, lf5, lf15;
422         char line[1024];
423         char path[64];
424         char buf[32];
425         int i;
426         void *iw;
427         struct sigaction sa;
428         struct dirent *e;
429
430         struct stat s;
431         const char *ipc = stat("/proc/net/nf_conntrack", &s)
432                 ? "/proc/net/ip_conntrack" : "/proc/net/nf_conntrack";
433
434         const struct {
435                 const char *file;
436                 uint64_t *value;
437         } sysfs_stats[] = {
438                 { "rx_packets", &rxp },
439                 { "tx_packets", &txp },
440                 { "rx_bytes",   &rxb },
441                 { "tx_bytes",   &txb }
442         };
443
444         switch (fork())
445         {
446                 case -1:
447                         perror("fork()");
448                         return -1;
449
450                 case 0:
451                         if (chdir("/") < 0)
452                         {
453                                 perror("chdir()");
454                                 exit(1);
455                         }
456
457                         close(0);
458                         close(1);
459                         close(2);
460                         break;
461
462                 default:
463                         return 0;
464         }
465
466         /* setup USR1 signal handler to reset timer */
467         sa.sa_handler = reset_countdown;
468         sa.sa_flags   = SA_RESTART;
469         sigemptyset(&sa.sa_mask);
470         sigaction(SIGUSR1, &sa, NULL);
471
472         /* write pid */
473         if (writepid())
474         {
475                 fprintf(stderr, "Failed to write pid file: %s\n", strerror(errno));
476                 return 1;
477         }
478
479         /* initialize iwinfo */
480         iw = iw_open();
481
482         /* go */
483         for (reset_countdown(0); countdown >= 0; countdown--)
484         {
485                 /* alter progname for ps, top */
486                 memset(progname, 0, prognamelen);
487                 snprintf(progname, prognamelen, "luci-bwc %d", countdown);
488
489                 dir = opendir("/sys/class/net");
490
491                 if (dir)
492                 {
493                         while ((e = readdir(dir)) != NULL)
494                         {
495                                 if (iw && iw_update(iw, e->d_name, &rate, &rssi, &noise))
496                                         update_radiostat(e->d_name, rate, rssi, noise);
497
498                                 if (!strcmp(e->d_name, "lo"))
499                                         continue;
500
501                                 for (i = 0; i < sizeof(sysfs_stats)/sizeof(sysfs_stats[0]); i++)
502                                 {
503                                         *sysfs_stats[i].value = 0;
504
505                                         snprintf(path, sizeof(path), "/sys/class/net/%s/statistics/%s",
506                                                 e->d_name, sysfs_stats[i].file);
507
508                                         if ((info = fopen(path, "r")) != NULL)
509                                         {
510                                                 memset(buf, 0, sizeof(buf));
511                                                 fread(buf, 1, sizeof(buf) - 1, info);
512                                                 fclose(info);
513
514                                                 *sysfs_stats[i].value = (uint64_t)strtoull(buf, NULL, 10);
515                                         }
516                                 }
517
518                                 update_ifstat(e->d_name, rxb, rxp, txb, txp);
519                         }
520
521                         closedir(dir);
522                 }
523
524                 if ((info = fopen(ipc, "r")) != NULL)
525                 {
526                         udp   = 0;
527                         tcp   = 0;
528                         other = 0;
529
530                         while (fgets(line, sizeof(line), info))
531                         {
532                                 if (strstr(line, "TIME_WAIT"))
533                                         continue;
534
535                                 if ((strstr(line, "src=127.0.0.1 ") && strstr(line, "dst=127.0.0.1 "))
536                                 || (strstr(line, "src=::1 ") && strstr(line, "dst=::1 ")))
537                                         continue;
538
539                                 if (sscanf(line, "%*s %*d %s", buf) || sscanf(line, "%s %*d", buf))
540                                 {
541                                         if (!strcmp(buf, "tcp"))
542                                                 tcp++;
543                                         else if (!strcmp(buf, "udp"))
544                                                 udp++;
545                                         else
546                                                 other++;
547                                 }
548                         }
549
550                         update_cnstat(udp, tcp, other);
551
552                         fclose(info);
553                 }
554
555                 if ((info = fopen("/proc/loadavg", "r")) != NULL)
556                 {
557                         if (fscanf(info, LD_SCAN_PATTERN, &lf1, &lf5, &lf15))
558                         {
559                                 update_ldstat((uint16_t)(lf1  * 100),
560                                                           (uint16_t)(lf5  * 100),
561                                                           (uint16_t)(lf15 * 100));
562                         }
563
564                         fclose(info);
565                 }
566
567                 sleep(STEP_TIME);
568         }
569
570         unlink(PID_PATH);
571
572         if (iw)
573                 iw_close(iw);
574
575         return 0;
576 }
577
578 static void check_daemon(void)
579 {
580         int pid;
581
582         if ((pid = readpid()) < 0 || kill(pid, 0) < 0)
583         {
584                 /* daemon ping failed, try to start it up */
585                 if (run_daemon())
586                 {
587                         fprintf(stderr,
588                                 "Failed to ping daemon and unable to start it up: %s\n",
589                                 strerror(errno));
590
591                         exit(1);
592                 }
593         }
594         else if (kill(pid, SIGUSR1))
595         {
596                 fprintf(stderr, "Failed to send signal: %s\n", strerror(errno));
597                 exit(2);
598         }
599 }
600
601 static int run_dump_ifname(const char *ifname)
602 {
603         int i;
604         char path[1024];
605         struct file_map m;
606         struct traffic_entry *e;
607
608         check_daemon();
609         snprintf(path, sizeof(path), DB_IF_FILE, ifname);
610
611         if (mmap_file(path, sizeof(struct traffic_entry), &m))
612         {
613                 fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
614                 return 1;
615         }
616
617         for (i = 0; i < m.size; i += sizeof(struct traffic_entry))
618         {
619                 e = (struct traffic_entry *) &m.mmap[i];
620
621                 if (!e->time)
622                         continue;
623
624                 printf("[ %" PRIu32 ", %" PRIu64 ", %" PRIu64
625                            ", %" PRIu64 ", %" PRIu64 " ]%s\n",
626                         be32toh(e->time),
627                         be64toh(e->rxb), be64toh(e->rxp),
628                         be64toh(e->txb), be64toh(e->txp),
629                         ((i + sizeof(struct traffic_entry)) < m.size) ? "," : "");
630         }
631
632         umap_file(&m);
633
634         return 0;
635 }
636
637 static int run_dump_radio(const char *ifname)
638 {
639         int i;
640         char path[1024];
641         struct file_map m;
642         struct radio_entry *e;
643
644         check_daemon();
645         snprintf(path, sizeof(path), DB_RD_FILE, ifname);
646
647         if (mmap_file(path, sizeof(struct radio_entry), &m))
648         {
649                 fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
650                 return 1;
651         }
652
653         for (i = 0; i < m.size; i += sizeof(struct radio_entry))
654         {
655                 e = (struct radio_entry *) &m.mmap[i];
656
657                 if (!e->time)
658                         continue;
659
660                 printf("[ %" PRIu32 ", %" PRIu16 ", %" PRIu8 ", %" PRIu8 " ]%s\n",
661                         be32toh(e->time),
662                         be16toh(e->rate), e->rssi, e->noise,
663                         ((i + sizeof(struct radio_entry)) < m.size) ? "," : "");
664         }
665
666         umap_file(&m);
667
668         return 0;
669 }
670
671 static int run_dump_conns(void)
672 {
673         int i;
674         char path[1024];
675         struct file_map m;
676         struct conn_entry *e;
677
678         check_daemon();
679         snprintf(path, sizeof(path), DB_CN_FILE);
680
681         if (mmap_file(path, sizeof(struct conn_entry), &m))
682         {
683                 fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
684                 return 1;
685         }
686
687         for (i = 0; i < m.size; i += sizeof(struct conn_entry))
688         {
689                 e = (struct conn_entry *) &m.mmap[i];
690
691                 if (!e->time)
692                         continue;
693
694                 printf("[ %" PRIu32 ", %" PRIu32 ", %" PRIu32 ", %" PRIu32 " ]%s\n",
695                         be32toh(e->time), be32toh(e->udp),
696                         be32toh(e->tcp), be32toh(e->other),
697                         ((i + sizeof(struct conn_entry)) < m.size) ? "," : "");
698         }
699
700         umap_file(&m);
701
702         return 0;
703 }
704
705 static int run_dump_load(void)
706 {
707         int i;
708         char path[1024];
709         struct file_map m;
710         struct load_entry *e;
711
712         check_daemon();
713         snprintf(path, sizeof(path), DB_LD_FILE);
714
715         if (mmap_file(path, sizeof(struct load_entry), &m))
716         {
717                 fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
718                 return 1;
719         }
720
721         for (i = 0; i < m.size; i += sizeof(struct load_entry))
722         {
723                 e = (struct load_entry *) &m.mmap[i];
724
725                 if (!e->time)
726                         continue;
727
728                 printf("[ %" PRIu32 ", %" PRIu16 ", %" PRIu16 ", %" PRIu16 " ]%s\n",
729                         be32toh(e->time),
730                         be16toh(e->load1), be16toh(e->load5), be16toh(e->load15),
731                         ((i + sizeof(struct load_entry)) < m.size) ? "," : "");
732         }
733
734         umap_file(&m);
735
736         return 0;
737 }
738
739
740 int main(int argc, char *argv[])
741 {
742         int opt;
743
744         progname = argv[0];
745         prognamelen = -1;
746
747         for (opt = 0; opt < argc; opt++)
748                 prognamelen += 1 + strlen(argv[opt]);
749
750         while ((opt = getopt(argc, argv, "t:i:r:cl")) > -1)
751         {
752                 switch (opt)
753                 {
754                         case 't':
755                                 timeout = atoi(optarg);
756                                 break;
757
758                         case 'i':
759                                 if (optarg)
760                                         return run_dump_ifname(optarg);
761                                 break;
762
763                         case 'r':
764                                 if (optarg)
765                                         return run_dump_radio(optarg);
766                                 break;
767
768                         case 'c':
769                                 return run_dump_conns();
770
771                         case 'l':
772                                 return run_dump_load();
773
774                         default:
775                                 break;
776                 }
777         }
778
779         fprintf(stderr,
780                 "Usage:\n"
781                 "       %s [-t timeout] -i ifname\n"
782                 "       %s [-t timeout] -r radiodev\n"
783                 "       %s [-t timeout] -c\n"
784                 "       %s [-t timeout] -l\n",
785                         argv[0], argv[0], argv[0], argv[0]
786         );
787
788         return 1;
789 }