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.
32 #include <arpa/inet.h>
41 #define PID_PATH "/var/run/luci-bwc.pid"
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"
49 #define IF_SCAN_PATTERN \
51 " %*d %*d %*d %*d %*d %*d" \
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 ntohl(((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, uint32_t rxb, uint32_t rxp, uint32_t txb, uint32_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 = htonl(time(NULL));
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 = htonl(time(NULL));
350 e.rate = htons(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 = htonl(time(NULL));
380 e.other = htonl(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 = htonl(time(NULL));
406 e.load1 = htons(load1);
407 e.load5 = htons(load5);
408 e.load15 = htons(load15);
410 return update_file(path, &e, sizeof(struct load_entry));
413 static int run_daemon(void)
416 uint32_t rxb, txb, rxp, txp;
417 uint32_t udp, tcp, other;
420 float lf1, lf5, lf15;
428 const char *ipc = stat("/proc/net/nf_conntrack", &s)
429 ? "/proc/net/ip_conntrack" : "/proc/net/nf_conntrack";
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);
462 fprintf(stderr, "Failed to write pid file: %s\n", strerror(errno));
466 /* initialize iwinfo */
470 for (reset_countdown(0); countdown >= 0; countdown--)
472 /* alter progname for ps, top */
473 memset(progname, 0, prognamelen);
474 snprintf(progname, prognamelen, "luci-bwc %d", countdown);
476 if ((info = fopen("/proc/net/dev", "r")) != NULL)
478 while (fgets(line, sizeof(line), info))
480 if (strchr(line, '|'))
483 if (sscanf(line, IF_SCAN_PATTERN, ifname, &rxb, &rxp, &txb, &txp))
485 if (strncmp(ifname, "lo", sizeof(ifname)))
486 update_ifstat(ifname, rxb, rxp, txb, txp);
495 for (i = 0; i < 5; i++)
497 #define iw_checkif(pattern) \
499 snprintf(ifname, sizeof(ifname), pattern, i); \
500 if (iw_update(iw, ifname, &rate, &rssi, &noise)) \
502 update_radiostat(ifname, rate, rssi, noise); \
507 iw_checkif("wlan%d");
513 if ((info = fopen(ipc, "r")) != NULL)
519 while (fgets(line, sizeof(line), info))
521 if (strstr(line, "TIME_WAIT"))
524 if ((strstr(line, "src=127.0.0.1 ") && strstr(line, "dst=127.0.0.1 "))
525 || (strstr(line, "src=::1 ") && strstr(line, "dst=::1 ")))
528 if (sscanf(line, "%*s %*d %s", ifname) || sscanf(line, "%s %*d", ifname))
530 if (!strcmp(ifname, "tcp"))
532 else if (!strcmp(ifname, "udp"))
539 update_cnstat(udp, tcp, other);
544 if ((info = fopen("/proc/loadavg", "r")) != NULL)
546 if (fscanf(info, LD_SCAN_PATTERN, &lf1, &lf5, &lf15))
548 update_ldstat((uint16_t)(lf1 * 100),
549 (uint16_t)(lf5 * 100),
550 (uint16_t)(lf15 * 100));
567 static void check_daemon(void)
571 if ((pid = readpid()) < 0 || kill(pid, 0) < 0)
573 /* daemon ping failed, try to start it up */
577 "Failed to ping daemon and unable to start it up: %s\n",
583 else if (kill(pid, SIGUSR1))
585 fprintf(stderr, "Failed to send signal: %s\n", strerror(errno));
590 static int run_dump_ifname(const char *ifname)
595 struct traffic_entry *e;
598 snprintf(path, sizeof(path), DB_IF_FILE, ifname);
600 if (mmap_file(path, sizeof(struct traffic_entry), &m))
602 fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
606 for (i = 0; i < m.size; i += sizeof(struct traffic_entry))
608 e = (struct traffic_entry *) &m.mmap[i];
613 printf("[ %u, %u, %" PRIu32
616 ntohl(e->rxb), ntohl(e->rxp),
617 ntohl(e->txb), ntohl(e->txp),
618 ((i + sizeof(struct traffic_entry)) < m.size) ? "," : "");
626 static int run_dump_radio(const char *ifname)
631 struct radio_entry *e;
634 snprintf(path, sizeof(path), DB_RD_FILE, ifname);
636 if (mmap_file(path, sizeof(struct radio_entry), &m))
638 fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
642 for (i = 0; i < m.size; i += sizeof(struct radio_entry))
644 e = (struct radio_entry *) &m.mmap[i];
649 printf("[ %u, %d, %d, %d ]%s\n",
651 e->rate, e->rssi, e->noise,
652 ((i + sizeof(struct radio_entry)) < m.size) ? "," : "");
660 static int run_dump_conns(void)
665 struct conn_entry *e;
668 snprintf(path, sizeof(path), DB_CN_FILE);
670 if (mmap_file(path, sizeof(struct conn_entry), &m))
672 fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
676 for (i = 0; i < m.size; i += sizeof(struct conn_entry))
678 e = (struct conn_entry *) &m.mmap[i];
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) ? "," : "");
694 static int run_dump_load(void)
699 struct load_entry *e;
702 snprintf(path, sizeof(path), DB_LD_FILE);
704 if (mmap_file(path, sizeof(struct load_entry), &m))
706 fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
710 for (i = 0; i < m.size; i += sizeof(struct load_entry))
712 e = (struct load_entry *) &m.mmap[i];
717 printf("[ %u, %u, %u, %u ]%s\n",
719 ntohs(e->load1), ntohs(e->load5), ntohs(e->load15),
720 ((i + sizeof(struct load_entry)) < m.size) ? "," : "");
729 int main(int argc, char *argv[])
736 for (opt = 0; opt < argc; opt++)
737 prognamelen += 1 + strlen(argv[opt]);
739 while ((opt = getopt(argc, argv, "t:i:r:cl")) > -1)
744 timeout = atoi(optarg);
749 return run_dump_ifname(optarg);
754 return run_dump_radio(optarg);
758 return run_dump_conns();
761 return run_dump_load();
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]