luci-mod-admin-full: status page formatting fix
[project/luci.git] / modules / luci-mod-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 <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 #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 #include <signal.h>
29
30 #include <sys/stat.h>
31 #include <sys/mman.h>
32 #include <arpa/inet.h>
33
34 #include <dlfcn.h>
35 #include <iwinfo.h>
36
37 #define STEP_COUNT      60
38 #define STEP_TIME       1
39 #define TIMEOUT         10
40
41 #define PID_PATH        "/var/run/luci-bwc.pid"
42
43 #define DB_PATH         "/var/lib/luci-bwc"
44 #define DB_IF_FILE      DB_PATH "/if/%s"
45 #define DB_RD_FILE      DB_PATH "/radio/%s"
46 #define DB_CN_FILE      DB_PATH "/connections"
47 #define DB_LD_FILE      DB_PATH "/load"
48
49 #define IF_SCAN_PATTERN \
50         " %[^ :]:%u %u" \
51         " %*d %*d %*d %*d %*d %*d" \
52         " %u %u"
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         uint32_t rxb;
67         uint32_t rxp;
68         uint32_t txb;
69         uint32_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 ntohl(((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, uint32_t rxb, uint32_t rxp, uint32_t txb, uint32_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 = htonl(time(NULL));
320         e.rxb  = htonl(rxb);
321         e.rxp  = htonl(rxp);
322         e.txb  = htonl(txb);
323         e.txp  = htonl(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  = htonl(time(NULL));
350         e.rate  = htons(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  = htonl(time(NULL));
378         e.udp   = htonl(udp);
379         e.tcp   = htonl(tcp);
380         e.other = htonl(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   = htonl(time(NULL));
406         e.load1  = htons(load1);
407         e.load5  = htons(load5);
408         e.load15 = htons(load15);
409
410         return update_file(path, &e, sizeof(struct load_entry));
411 }
412
413 static int run_daemon(void)
414 {
415         FILE *info;
416         uint32_t rxb, txb, rxp, txp;
417         uint32_t udp, tcp, other;
418         uint16_t rate;
419         uint8_t rssi, noise;
420         float lf1, lf5, lf15;
421         char line[1024];
422         char ifname[16];
423         int i;
424         void *iw;
425         struct sigaction sa;
426
427         struct stat s;
428         const char *ipc = stat("/proc/net/nf_conntrack", &s)
429                 ? "/proc/net/ip_conntrack" : "/proc/net/nf_conntrack";
430
431         switch (fork())
432         {
433                 case -1:
434                         perror("fork()");
435                         return -1;
436
437                 case 0:
438                         if (chdir("/") < 0)
439                         {
440                                 perror("chdir()");
441                                 exit(1);
442                         }
443
444                         close(0);
445                         close(1);
446                         close(2);
447                         break;
448
449                 default:
450                         return 0;
451         }
452
453         /* setup USR1 signal handler to reset timer */
454         sa.sa_handler = reset_countdown;
455         sa.sa_flags   = SA_RESTART;
456         sigemptyset(&sa.sa_mask);
457         sigaction(SIGUSR1, &sa, NULL);
458
459         /* write pid */
460         if (writepid())
461         {
462                 fprintf(stderr, "Failed to write pid file: %s\n", strerror(errno));
463                 return 1;
464         }
465
466         /* initialize iwinfo */
467         iw = iw_open();
468
469         /* go */
470         for (reset_countdown(0); countdown >= 0; countdown--)
471         {
472                 /* alter progname for ps, top */
473                 memset(progname, 0, prognamelen);
474                 snprintf(progname, prognamelen, "luci-bwc %d", countdown);
475
476                 if ((info = fopen("/proc/net/dev", "r")) != NULL)
477                 {
478                         while (fgets(line, sizeof(line), info))
479                         {
480                                 if (strchr(line, '|'))
481                                         continue;
482
483                                 if (sscanf(line, IF_SCAN_PATTERN, ifname, &rxb, &rxp, &txb, &txp))
484                                 {
485                                         if (strncmp(ifname, "lo", sizeof(ifname)))
486                                                 update_ifstat(ifname, rxb, rxp, txb, txp);
487                                 }
488                         }
489
490                         fclose(info);
491                 }
492
493                 if (iw)
494                 {
495                         for (i = 0; i < 5; i++)
496                         {
497 #define iw_checkif(pattern) \
498                                 do {                                                      \
499                                         snprintf(ifname, sizeof(ifname), pattern, i);         \
500                                         if (iw_update(iw, ifname, &rate, &rssi, &noise))  \
501                                         {                                                     \
502                                                 update_radiostat(ifname, rate, rssi, noise);      \
503                                                 continue;                                         \
504                                         }                                                     \
505                                 } while(0)
506
507                                 iw_checkif("wlan%d");
508                                 iw_checkif("ath%d");
509                                 iw_checkif("wl%d");
510                         }
511                 }
512
513                 if ((info = fopen(ipc, "r")) != NULL)
514                 {
515                         udp   = 0;
516                         tcp   = 0;
517                         other = 0;
518
519                         while (fgets(line, sizeof(line), info))
520                         {
521                                 if (strstr(line, "TIME_WAIT"))
522                                         continue;
523
524                                 if (strstr(line, "src=127.0.0.1 ") &&
525                                     strstr(line, "dst=127.0.0.1 "))
526                                         continue;
527
528                                 if (sscanf(line, "%*s %*d %s", ifname) || sscanf(line, "%s %*d", ifname))
529                                 {
530                                         if (!strcmp(ifname, "tcp"))
531                                                 tcp++;
532                                         else if (!strcmp(ifname, "udp"))
533                                                 udp++;
534                                         else
535                                                 other++;
536                                 }
537                         }
538
539                         update_cnstat(udp, tcp, other);
540
541                         fclose(info);
542                 }
543
544                 if ((info = fopen("/proc/loadavg", "r")) != NULL)
545                 {
546                         if (fscanf(info, LD_SCAN_PATTERN, &lf1, &lf5, &lf15))
547                         {
548                                 update_ldstat((uint16_t)(lf1  * 100),
549                                                           (uint16_t)(lf5  * 100),
550                                                           (uint16_t)(lf15 * 100));
551                         }
552
553                         fclose(info);
554                 }
555
556                 sleep(STEP_TIME);
557         }
558
559         unlink(PID_PATH);
560
561         if (iw)
562                 iw_close(iw);
563
564         return 0;
565 }
566
567 static void check_daemon(void)
568 {
569         int pid;
570
571         if ((pid = readpid()) < 0 || kill(pid, 0) < 0)
572         {
573                 /* daemon ping failed, try to start it up */
574                 if (run_daemon())
575                 {
576                         fprintf(stderr,
577                                 "Failed to ping daemon and unable to start it up: %s\n",
578                                 strerror(errno));
579
580                         exit(1);
581                 }
582         }
583         else if (kill(pid, SIGUSR1))
584         {
585                 fprintf(stderr, "Failed to send signal: %s\n", strerror(errno));
586                 exit(2);
587         }
588 }
589
590 static int run_dump_ifname(const char *ifname)
591 {
592         int i;
593         char path[1024];
594         struct file_map m;
595         struct traffic_entry *e;
596
597         check_daemon();
598         snprintf(path, sizeof(path), DB_IF_FILE, ifname);
599
600         if (mmap_file(path, sizeof(struct traffic_entry), &m))
601         {
602                 fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
603                 return 1;
604         }
605
606         for (i = 0; i < m.size; i += sizeof(struct traffic_entry))
607         {
608                 e = (struct traffic_entry *) &m.mmap[i];
609
610                 if (!e->time)
611                         continue;
612
613                 printf("[ %u, %u, %" PRIu32
614                            ", %u, %u ]%s\n",
615                         ntohl(e->time),
616                         ntohl(e->rxb), ntohl(e->rxp),
617                         ntohl(e->txb), ntohl(e->txp),
618                         ((i + sizeof(struct traffic_entry)) < m.size) ? "," : "");
619         }
620
621         umap_file(&m);
622
623         return 0;
624 }
625
626 static int run_dump_radio(const char *ifname)
627 {
628         int i;
629         char path[1024];
630         struct file_map m;
631         struct radio_entry *e;
632
633         check_daemon();
634         snprintf(path, sizeof(path), DB_RD_FILE, ifname);
635
636         if (mmap_file(path, sizeof(struct radio_entry), &m))
637         {
638                 fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
639                 return 1;
640         }
641
642         for (i = 0; i < m.size; i += sizeof(struct radio_entry))
643         {
644                 e = (struct radio_entry *) &m.mmap[i];
645
646                 if (!e->time)
647                         continue;
648
649                 printf("[ %u, %d, %d, %d ]%s\n",
650                         ntohl(e->time),
651                         e->rate, e->rssi, e->noise,
652                         ((i + sizeof(struct radio_entry)) < m.size) ? "," : "");
653         }
654
655         umap_file(&m);
656
657         return 0;
658 }
659
660 static int run_dump_conns(void)
661 {
662         int i;
663         char path[1024];
664         struct file_map m;
665         struct conn_entry *e;
666
667         check_daemon();
668         snprintf(path, sizeof(path), DB_CN_FILE);
669
670         if (mmap_file(path, sizeof(struct conn_entry), &m))
671         {
672                 fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
673                 return 1;
674         }
675
676         for (i = 0; i < m.size; i += sizeof(struct conn_entry))
677         {
678                 e = (struct conn_entry *) &m.mmap[i];
679
680                 if (!e->time)
681                         continue;
682
683                 printf("[ %u, %u, %u, %u ]%s\n",
684                         ntohl(e->time), ntohl(e->udp),
685                         ntohl(e->tcp), ntohl(e->other),
686                         ((i + sizeof(struct conn_entry)) < m.size) ? "," : "");
687         }
688
689         umap_file(&m);
690
691         return 0;
692 }
693
694 static int run_dump_load(void)
695 {
696         int i;
697         char path[1024];
698         struct file_map m;
699         struct load_entry *e;
700
701         check_daemon();
702         snprintf(path, sizeof(path), DB_LD_FILE);
703
704         if (mmap_file(path, sizeof(struct load_entry), &m))
705         {
706                 fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
707                 return 1;
708         }
709
710         for (i = 0; i < m.size; i += sizeof(struct load_entry))
711         {
712                 e = (struct load_entry *) &m.mmap[i];
713
714                 if (!e->time)
715                         continue;
716
717                 printf("[ %u, %u, %u, %u ]%s\n",
718                         ntohl(e->time),
719                         ntohs(e->load1), ntohs(e->load5), ntohs(e->load15),
720                         ((i + sizeof(struct load_entry)) < m.size) ? "," : "");
721         }
722
723         umap_file(&m);
724
725         return 0;
726 }
727
728
729 int main(int argc, char *argv[])
730 {
731         int opt;
732
733         progname = argv[0];
734         prognamelen = -1;
735
736         for (opt = 0; opt < argc; opt++)
737                 prognamelen += 1 + strlen(argv[opt]);
738
739         while ((opt = getopt(argc, argv, "t:i:r:cl")) > -1)
740         {
741                 switch (opt)
742                 {
743                         case 't':
744                                 timeout = atoi(optarg);
745                                 break;
746
747                         case 'i':
748                                 if (optarg)
749                                         return run_dump_ifname(optarg);
750                                 break;
751
752                         case 'r':
753                                 if (optarg)
754                                         return run_dump_radio(optarg);
755                                 break;
756
757                         case 'c':
758                                 return run_dump_conns();
759
760                         case 'l':
761                                 return run_dump_load();
762
763                         default:
764                                 break;
765                 }
766         }
767
768         fprintf(stderr,
769                 "Usage:\n"
770                 "       %s [-t timeout] -i ifname\n"
771                 "       %s [-t timeout] -r radiodev\n"
772                 "       %s [-t timeout] -c\n"
773                 "       %s [-t timeout] -l\n",
774                         argv[0], argv[0], argv[0], argv[0]
775         );
776
777         return 1;
778 }