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.
35 #include <arpa/inet.h>
44 #define PID_PATH "/var/run/luci-bwc.pid"
46 #define DB_PATH "/var/lib/luci-bwc"
47 #define DB_IF_FILE DB_PATH "/if/%s"
48 #define DB_RD_FILE DB_PATH "/radio/%s"
49 #define DB_CN_FILE DB_PATH "/connections"
50 #define DB_LD_FILE DB_PATH "/load"
52 #define IF_SCAN_PATTERN \
53 " %[^ :]:%" SCNu64 " %" SCNu64 \
54 " %*u %*u %*u %*u %*u %*u" \
55 " %" SCNu64 " %" SCNu64
57 #define LD_SCAN_PATTERN \
67 struct traffic_entry {
96 static int readpid(void)
102 if ((fd = open(PID_PATH, O_RDONLY)) > -1)
104 if (read(fd, buf, sizeof(buf)))
116 static int writepid(void)
122 if ((fd = open(PID_PATH, O_WRONLY | O_CREAT | O_TRUNC, 0600)) > -1)
124 wlen = snprintf(buf, sizeof(buf), "%i", getpid());
125 write(fd, buf, wlen);
134 static int timeout = TIMEOUT;
135 static int countdown = -1;
137 static void reset_countdown(int sig)
144 static char *progname;
145 static int prognamelen;
147 static struct iwinfo_ops *backend = NULL;
150 static int init_directory(char *path)
154 for (p = &path[1]; *p; p++)
160 if (mkdir(path, 0700) && (errno != EEXIST))
170 static int init_file(char *path, int esize)
173 char buf[sizeof(struct traffic_entry)] = { 0 };
175 if (init_directory(path))
178 if ((file = open(path, O_WRONLY | O_CREAT, 0600)) >= 0)
180 for (i = 0; i < STEP_COUNT; i++)
182 if (write(file, buf, esize) < 0)
194 static inline uint32_t timeof(void *entry)
196 return be32toh(((struct traffic_entry *)entry)->time);
199 static int update_file(const char *path, void *entry, int esize)
205 if ((file = open(path, O_RDWR)) >= 0)
207 map = mmap(NULL, esize * STEP_COUNT, PROT_READ | PROT_WRITE,
208 MAP_SHARED | MAP_LOCKED, file, 0);
210 if ((map != NULL) && (map != MAP_FAILED))
212 if (timeof(entry) > timeof(map + esize * (STEP_COUNT-1)))
214 memmove(map, map + esize, esize * (STEP_COUNT-1));
215 memcpy(map + esize * (STEP_COUNT-1), entry, esize);
218 munmap(map, esize * STEP_COUNT);
229 static int mmap_file(const char *path, int esize, struct file_map *m)
235 if ((m->fd = open(path, O_RDONLY)) >= 0)
237 m->size = STEP_COUNT * esize;
238 m->mmap = mmap(NULL, m->size, PROT_READ,
239 MAP_SHARED | MAP_LOCKED, m->fd, 0);
241 if ((m->mmap != NULL) && (m->mmap != MAP_FAILED))
248 static void umap_file(struct file_map *m)
250 if ((m->mmap != NULL) && (m->mmap != MAP_FAILED))
251 munmap(m->mmap, m->size);
257 static void * iw_open(void)
259 return dlopen("/usr/lib/libiwinfo.so", RTLD_LAZY);
262 static int iw_update(
263 void *iw, const char *ifname, uint16_t *rate, uint8_t *rssi, uint8_t *noise
265 struct iwinfo_ops *(*probe)(const char *);
270 probe = dlsym(iw, "iwinfo_backend");
275 backend = probe(ifname);
281 *rate = (backend->bitrate && !backend->bitrate(ifname, &val)) ? val : 0;
282 *rssi = (backend->signal && !backend->signal(ifname, &val)) ? val : 0;
283 *noise = (backend->noise && !backend->noise(ifname, &val)) ? val : 0;
288 static void iw_close(void *iw)
290 void (*finish)(void);
292 finish = dlsym(iw, "iwinfo_finish");
301 static int update_ifstat(
302 const char *ifname, uint64_t rxb, uint64_t rxp, uint64_t txb, uint64_t txp
307 struct traffic_entry e;
309 snprintf(path, sizeof(path), DB_IF_FILE, ifname);
313 if (init_file(path, sizeof(struct traffic_entry)))
315 fprintf(stderr, "Failed to init %s: %s\n",
316 path, strerror(errno));
322 e.time = htobe32(time(NULL));
323 e.rxb = htobe64(rxb);
324 e.rxp = htobe64(rxp);
325 e.txb = htobe64(txb);
326 e.txp = htobe64(txp);
328 return update_file(path, &e, sizeof(struct traffic_entry));
331 static int update_radiostat(
332 const char *ifname, uint16_t rate, uint8_t rssi, uint8_t noise
337 struct radio_entry e;
339 snprintf(path, sizeof(path), DB_RD_FILE, ifname);
343 if (init_file(path, sizeof(struct radio_entry)))
345 fprintf(stderr, "Failed to init %s: %s\n",
346 path, strerror(errno));
352 e.time = htobe32(time(NULL));
353 e.rate = htobe16(rate);
357 return update_file(path, &e, sizeof(struct radio_entry));
360 static int update_cnstat(uint32_t udp, uint32_t tcp, uint32_t other)
367 snprintf(path, sizeof(path), DB_CN_FILE);
371 if (init_file(path, sizeof(struct conn_entry)))
373 fprintf(stderr, "Failed to init %s: %s\n",
374 path, strerror(errno));
380 e.time = htobe32(time(NULL));
381 e.udp = htobe32(udp);
382 e.tcp = htobe32(tcp);
383 e.other = htobe32(other);
385 return update_file(path, &e, sizeof(struct conn_entry));
388 static int update_ldstat(uint16_t load1, uint16_t load5, uint16_t load15)
395 snprintf(path, sizeof(path), DB_LD_FILE);
399 if (init_file(path, sizeof(struct load_entry)))
401 fprintf(stderr, "Failed to init %s: %s\n",
402 path, strerror(errno));
408 e.time = htobe32(time(NULL));
409 e.load1 = htobe16(load1);
410 e.load5 = htobe16(load5);
411 e.load15 = htobe16(load15);
413 return update_file(path, &e, sizeof(struct load_entry));
416 static int run_daemon(void)
419 uint64_t rxb, txb, rxp, txp;
420 uint32_t udp, tcp, other;
423 float lf1, lf5, lf15;
431 const char *ipc = stat("/proc/net/nf_conntrack", &s)
432 ? "/proc/net/ip_conntrack" : "/proc/net/nf_conntrack";
456 /* setup USR1 signal handler to reset timer */
457 sa.sa_handler = reset_countdown;
458 sa.sa_flags = SA_RESTART;
459 sigemptyset(&sa.sa_mask);
460 sigaction(SIGUSR1, &sa, NULL);
465 fprintf(stderr, "Failed to write pid file: %s\n", strerror(errno));
469 /* initialize iwinfo */
473 for (reset_countdown(0); countdown >= 0; countdown--)
475 /* alter progname for ps, top */
476 memset(progname, 0, prognamelen);
477 snprintf(progname, prognamelen, "luci-bwc %d", countdown);
479 if ((info = fopen("/proc/net/dev", "r")) != NULL)
481 while (fgets(line, sizeof(line), info))
483 if (strchr(line, '|'))
486 if (sscanf(line, IF_SCAN_PATTERN, ifname, &rxb, &rxp, &txb, &txp))
488 if (strncmp(ifname, "lo", sizeof(ifname)))
489 update_ifstat(ifname, rxb, rxp, txb, txp);
498 for (i = 0; i < 5; i++)
500 #define iw_checkif(pattern) \
502 snprintf(ifname, sizeof(ifname), pattern, i); \
503 if (iw_update(iw, ifname, &rate, &rssi, &noise)) \
505 update_radiostat(ifname, rate, rssi, noise); \
510 iw_checkif("wlan%d");
516 if ((info = fopen(ipc, "r")) != NULL)
522 while (fgets(line, sizeof(line), info))
524 if (strstr(line, "TIME_WAIT"))
527 if ((strstr(line, "src=127.0.0.1 ") && strstr(line, "dst=127.0.0.1 "))
528 || (strstr(line, "src=::1 ") && strstr(line, "dst=::1 ")))
531 if (sscanf(line, "%*s %*d %s", ifname) || sscanf(line, "%s %*d", ifname))
533 if (!strcmp(ifname, "tcp"))
535 else if (!strcmp(ifname, "udp"))
542 update_cnstat(udp, tcp, other);
547 if ((info = fopen("/proc/loadavg", "r")) != NULL)
549 if (fscanf(info, LD_SCAN_PATTERN, &lf1, &lf5, &lf15))
551 update_ldstat((uint16_t)(lf1 * 100),
552 (uint16_t)(lf5 * 100),
553 (uint16_t)(lf15 * 100));
570 static void check_daemon(void)
574 if ((pid = readpid()) < 0 || kill(pid, 0) < 0)
576 /* daemon ping failed, try to start it up */
580 "Failed to ping daemon and unable to start it up: %s\n",
586 else if (kill(pid, SIGUSR1))
588 fprintf(stderr, "Failed to send signal: %s\n", strerror(errno));
593 static int run_dump_ifname(const char *ifname)
598 struct traffic_entry *e;
601 snprintf(path, sizeof(path), DB_IF_FILE, ifname);
603 if (mmap_file(path, sizeof(struct traffic_entry), &m))
605 fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
609 for (i = 0; i < m.size; i += sizeof(struct traffic_entry))
611 e = (struct traffic_entry *) &m.mmap[i];
616 printf("[ %" PRIu32 ", %" PRIu64 ", %" PRIu64
617 ", %" PRIu64 ", %" PRIu64 " ]%s\n",
619 be64toh(e->rxb), be64toh(e->rxp),
620 be64toh(e->txb), be64toh(e->txp),
621 ((i + sizeof(struct traffic_entry)) < m.size) ? "," : "");
629 static int run_dump_radio(const char *ifname)
634 struct radio_entry *e;
637 snprintf(path, sizeof(path), DB_RD_FILE, ifname);
639 if (mmap_file(path, sizeof(struct radio_entry), &m))
641 fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
645 for (i = 0; i < m.size; i += sizeof(struct radio_entry))
647 e = (struct radio_entry *) &m.mmap[i];
652 printf("[ %" PRIu32 ", %" PRIu16 ", %" PRIu8 ", %" PRIu8 " ]%s\n",
654 be16toh(e->rate), e->rssi, e->noise,
655 ((i + sizeof(struct radio_entry)) < m.size) ? "," : "");
663 static int run_dump_conns(void)
668 struct conn_entry *e;
671 snprintf(path, sizeof(path), DB_CN_FILE);
673 if (mmap_file(path, sizeof(struct conn_entry), &m))
675 fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
679 for (i = 0; i < m.size; i += sizeof(struct conn_entry))
681 e = (struct conn_entry *) &m.mmap[i];
686 printf("[ %" PRIu32 ", %" PRIu32 ", %" PRIu32 ", %" PRIu32 " ]%s\n",
687 be32toh(e->time), be32toh(e->udp),
688 be32toh(e->tcp), be32toh(e->other),
689 ((i + sizeof(struct conn_entry)) < m.size) ? "," : "");
697 static int run_dump_load(void)
702 struct load_entry *e;
705 snprintf(path, sizeof(path), DB_LD_FILE);
707 if (mmap_file(path, sizeof(struct load_entry), &m))
709 fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
713 for (i = 0; i < m.size; i += sizeof(struct load_entry))
715 e = (struct load_entry *) &m.mmap[i];
720 printf("[ %" PRIu32 ", %" PRIu16 ", %" PRIu16 ", %" PRIu16 " ]%s\n",
722 be16toh(e->load1), be16toh(e->load5), be16toh(e->load15),
723 ((i + sizeof(struct load_entry)) < m.size) ? "," : "");
732 int main(int argc, char *argv[])
739 for (opt = 0; opt < argc; opt++)
740 prognamelen += 1 + strlen(argv[opt]);
742 while ((opt = getopt(argc, argv, "t:i:r:cl")) > -1)
747 timeout = atoi(optarg);
752 return run_dump_ifname(optarg);
757 return run_dump_radio(optarg);
761 return run_dump_conns();
764 return run_dump_load();
773 " %s [-t timeout] -i ifname\n"
774 " %s [-t timeout] -r radiodev\n"
775 " %s [-t timeout] -c\n"
776 " %s [-t timeout] -l\n",
777 argv[0], argv[0], argv[0], argv[0]