2 * luci-bwc - Very simple bandwidth collector cache for LuCI realtime graphs
4 * Copyright (C) 2010 Jo-Philipp Wich <jow@openwrt.org>
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
36 #include <sys/types.h>
37 #include <arpa/inet.h>
46 #define PID_PATH "/var/run/luci-bwc.pid"
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"
54 #define LD_SCAN_PATTERN \
64 struct traffic_entry {
93 static int readpid(void)
99 if ((fd = open(PID_PATH, O_RDONLY)) > -1)
101 if (read(fd, buf, sizeof(buf)))
113 static int writepid(void)
119 if ((fd = open(PID_PATH, O_WRONLY | O_CREAT | O_TRUNC, 0600)) > -1)
121 wlen = snprintf(buf, sizeof(buf), "%i", getpid());
122 write(fd, buf, wlen);
131 static int timeout = TIMEOUT;
132 static int countdown = -1;
134 static void reset_countdown(int sig)
141 static char *progname;
142 static int prognamelen;
144 static struct iwinfo_ops *backend = NULL;
147 static int init_directory(char *path)
151 for (p = &path[1]; *p; p++)
157 if (mkdir(path, 0700) && (errno != EEXIST))
167 static int init_file(char *path, int esize)
170 char buf[sizeof(struct traffic_entry)] = { 0 };
172 if (init_directory(path))
175 if ((file = open(path, O_WRONLY | O_CREAT, 0600)) >= 0)
177 for (i = 0; i < STEP_COUNT; i++)
179 if (write(file, buf, esize) < 0)
191 static inline uint32_t timeof(void *entry)
193 return be32toh(((struct traffic_entry *)entry)->time);
196 static int update_file(const char *path, void *entry, int esize)
202 if ((file = open(path, O_RDWR)) >= 0)
204 map = mmap(NULL, esize * STEP_COUNT, PROT_READ | PROT_WRITE,
205 MAP_SHARED | MAP_LOCKED, file, 0);
207 if ((map != NULL) && (map != MAP_FAILED))
209 if (timeof(entry) > timeof(map + esize * (STEP_COUNT-1)))
211 memmove(map, map + esize, esize * (STEP_COUNT-1));
212 memcpy(map + esize * (STEP_COUNT-1), entry, esize);
215 munmap(map, esize * STEP_COUNT);
226 static int mmap_file(const char *path, int esize, struct file_map *m)
232 if ((m->fd = open(path, O_RDONLY)) >= 0)
234 m->size = STEP_COUNT * esize;
235 m->mmap = mmap(NULL, m->size, PROT_READ,
236 MAP_SHARED | MAP_LOCKED, m->fd, 0);
238 if ((m->mmap != NULL) && (m->mmap != MAP_FAILED))
245 static void umap_file(struct file_map *m)
247 if ((m->mmap != NULL) && (m->mmap != MAP_FAILED))
248 munmap(m->mmap, m->size);
254 static void * iw_open(void)
256 return dlopen("/usr/lib/libiwinfo.so", RTLD_LAZY);
259 static int iw_update(
260 void *iw, const char *ifname, uint16_t *rate, uint8_t *rssi, uint8_t *noise
262 struct iwinfo_ops *(*probe)(const char *);
267 probe = dlsym(iw, "iwinfo_backend");
272 backend = probe(ifname);
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;
285 static void iw_close(void *iw)
287 void (*finish)(void);
289 finish = dlsym(iw, "iwinfo_finish");
298 static int update_ifstat(
299 const char *ifname, uint64_t rxb, uint64_t rxp, uint64_t txb, uint64_t txp
304 struct traffic_entry e;
306 snprintf(path, sizeof(path), DB_IF_FILE, ifname);
310 if (init_file(path, sizeof(struct traffic_entry)))
312 fprintf(stderr, "Failed to init %s: %s\n",
313 path, strerror(errno));
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);
325 return update_file(path, &e, sizeof(struct traffic_entry));
328 static int update_radiostat(
329 const char *ifname, uint16_t rate, uint8_t rssi, uint8_t noise
334 struct radio_entry e;
336 snprintf(path, sizeof(path), DB_RD_FILE, ifname);
340 if (init_file(path, sizeof(struct radio_entry)))
342 fprintf(stderr, "Failed to init %s: %s\n",
343 path, strerror(errno));
349 e.time = htobe32(time(NULL));
350 e.rate = htobe16(rate);
354 return update_file(path, &e, sizeof(struct radio_entry));
357 static int update_cnstat(uint32_t udp, uint32_t tcp, uint32_t other)
364 snprintf(path, sizeof(path), DB_CN_FILE);
368 if (init_file(path, sizeof(struct conn_entry)))
370 fprintf(stderr, "Failed to init %s: %s\n",
371 path, strerror(errno));
377 e.time = htobe32(time(NULL));
378 e.udp = htobe32(udp);
379 e.tcp = htobe32(tcp);
380 e.other = htobe32(other);
382 return update_file(path, &e, sizeof(struct conn_entry));
385 static int update_ldstat(uint16_t load1, uint16_t load5, uint16_t load15)
392 snprintf(path, sizeof(path), DB_LD_FILE);
396 if (init_file(path, sizeof(struct load_entry)))
398 fprintf(stderr, "Failed to init %s: %s\n",
399 path, strerror(errno));
405 e.time = htobe32(time(NULL));
406 e.load1 = htobe16(load1);
407 e.load5 = htobe16(load5);
408 e.load15 = htobe16(load15);
410 return update_file(path, &e, sizeof(struct load_entry));
413 static int run_daemon(void)
417 uint64_t rxb, txb, rxp, txp;
418 uint32_t udp, tcp, other;
421 float lf1, lf5, lf15;
431 const char *ipc = stat("/proc/net/nf_conntrack", &s)
432 ? "/proc/net/ip_conntrack" : "/proc/net/nf_conntrack";
438 { "rx_packets", &rxp },
439 { "tx_packets", &txp },
440 { "rx_bytes", &rxb },
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);
475 fprintf(stderr, "Failed to write pid file: %s\n", strerror(errno));
479 /* initialize iwinfo */
483 for (reset_countdown(0); countdown >= 0; countdown--)
485 /* alter progname for ps, top */
486 memset(progname, 0, prognamelen);
487 snprintf(progname, prognamelen, "luci-bwc %d", countdown);
489 dir = opendir("/sys/class/net");
493 while ((e = readdir(dir)) != NULL)
495 if (iw && iw_update(iw, e->d_name, &rate, &rssi, &noise))
496 update_radiostat(e->d_name, rate, rssi, noise);
498 if (!strcmp(e->d_name, "lo"))
501 for (i = 0; i < sizeof(sysfs_stats)/sizeof(sysfs_stats[0]); i++)
503 *sysfs_stats[i].value = 0;
505 snprintf(path, sizeof(path), "/sys/class/net/%s/statistics/%s",
506 e->d_name, sysfs_stats[i].file);
508 if ((info = fopen(path, "r")) != NULL)
510 memset(buf, 0, sizeof(buf));
511 fread(buf, 1, sizeof(buf) - 1, info);
514 *sysfs_stats[i].value = (uint64_t)strtoull(buf, NULL, 10);
518 update_ifstat(e->d_name, rxb, rxp, txb, txp);
524 if ((info = fopen(ipc, "r")) != NULL)
530 while (fgets(line, sizeof(line), info))
532 if (strstr(line, "TIME_WAIT"))
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 ")))
539 if (sscanf(line, "%*s %*d %s", buf) || sscanf(line, "%s %*d", buf))
541 if (!strcmp(buf, "tcp"))
543 else if (!strcmp(buf, "udp"))
550 update_cnstat(udp, tcp, other);
555 if ((info = fopen("/proc/loadavg", "r")) != NULL)
557 if (fscanf(info, LD_SCAN_PATTERN, &lf1, &lf5, &lf15))
559 update_ldstat((uint16_t)(lf1 * 100),
560 (uint16_t)(lf5 * 100),
561 (uint16_t)(lf15 * 100));
578 static void check_daemon(void)
582 if ((pid = readpid()) < 0 || kill(pid, 0) < 0)
584 /* daemon ping failed, try to start it up */
588 "Failed to ping daemon and unable to start it up: %s\n",
594 else if (kill(pid, SIGUSR1))
596 fprintf(stderr, "Failed to send signal: %s\n", strerror(errno));
601 static int run_dump_ifname(const char *ifname)
606 struct traffic_entry *e;
609 snprintf(path, sizeof(path), DB_IF_FILE, ifname);
611 if (mmap_file(path, sizeof(struct traffic_entry), &m))
613 fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
617 for (i = 0; i < m.size; i += sizeof(struct traffic_entry))
619 e = (struct traffic_entry *) &m.mmap[i];
624 printf("[ %" PRIu32 ", %" PRIu64 ", %" PRIu64
625 ", %" PRIu64 ", %" PRIu64 " ]%s\n",
627 be64toh(e->rxb), be64toh(e->rxp),
628 be64toh(e->txb), be64toh(e->txp),
629 ((i + sizeof(struct traffic_entry)) < m.size) ? "," : "");
637 static int run_dump_radio(const char *ifname)
642 struct radio_entry *e;
645 snprintf(path, sizeof(path), DB_RD_FILE, ifname);
647 if (mmap_file(path, sizeof(struct radio_entry), &m))
649 fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
653 for (i = 0; i < m.size; i += sizeof(struct radio_entry))
655 e = (struct radio_entry *) &m.mmap[i];
660 printf("[ %" PRIu32 ", %" PRIu16 ", %" PRIu8 ", %" PRIu8 " ]%s\n",
662 be16toh(e->rate), e->rssi, e->noise,
663 ((i + sizeof(struct radio_entry)) < m.size) ? "," : "");
671 static int run_dump_conns(void)
676 struct conn_entry *e;
679 snprintf(path, sizeof(path), DB_CN_FILE);
681 if (mmap_file(path, sizeof(struct conn_entry), &m))
683 fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
687 for (i = 0; i < m.size; i += sizeof(struct conn_entry))
689 e = (struct conn_entry *) &m.mmap[i];
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) ? "," : "");
705 static int run_dump_load(void)
710 struct load_entry *e;
713 snprintf(path, sizeof(path), DB_LD_FILE);
715 if (mmap_file(path, sizeof(struct load_entry), &m))
717 fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
721 for (i = 0; i < m.size; i += sizeof(struct load_entry))
723 e = (struct load_entry *) &m.mmap[i];
728 printf("[ %" PRIu32 ", %" PRIu16 ", %" PRIu16 ", %" PRIu16 " ]%s\n",
730 be16toh(e->load1), be16toh(e->load5), be16toh(e->load15),
731 ((i + sizeof(struct load_entry)) < m.size) ? "," : "");
740 int main(int argc, char *argv[])
747 for (opt = 0; opt < argc; opt++)
748 prognamelen += 1 + strlen(argv[opt]);
750 while ((opt = getopt(argc, argv, "t:i:r:cl")) > -1)
755 timeout = atoi(optarg);
760 return run_dump_ifname(optarg);
765 return run_dump_radio(optarg);
769 return run_dump_conns();
772 return run_dump_load();
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]