2 * luci-bwc - Very simple bandwidth collector cache for LuCI realtime graphs
4 * Copyright (C) 2010 Jo-Philipp Wich <xm@subsignal.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>
40 #define PID_PATH "/var/run/luci-bwc.pid"
42 #define DB_PATH "/var/lib/luci-bwc"
43 #define DB_IF_FILE DB_PATH "/if/%s"
44 #define DB_RD_FILE DB_PATH "/radio/%s"
45 #define DB_CN_FILE DB_PATH "/connections"
46 #define DB_LD_FILE DB_PATH "/load"
48 #define IF_SCAN_PATTERN \
50 " %*d %*d %*d %*d %*d %*d" \
53 #define LD_SCAN_PATTERN \
63 struct traffic_entry {
92 static int readpid(void)
98 if ((fd = open(PID_PATH, O_RDONLY)) > -1)
100 if (read(fd, buf, sizeof(buf)))
112 static int writepid(void)
118 if ((fd = open(PID_PATH, O_WRONLY | O_CREAT | O_TRUNC, 0600)) > -1)
120 wlen = snprintf(buf, sizeof(buf), "%i", getpid());
121 write(fd, buf, wlen);
130 static int timeout = TIMEOUT;
131 static int countdown = -1;
133 static void reset_countdown(int sig)
140 static char *progname;
141 static int prognamelen;
143 static int (*iw_get_rate)(const char *, int *) = NULL;
144 static int (*iw_get_rssi)(const char *, int *) = NULL;
145 static int (*iw_get_noise)(const char *, int *) = NULL;
148 static int init_directory(char *path)
152 for (p = &path[1]; *p; p++)
158 if (mkdir(path, 0700) && (errno != EEXIST))
168 static int init_file(char *path, int esize)
171 char buf[sizeof(struct traffic_entry)] = { 0 };
173 if (init_directory(path))
176 if ((file = open(path, O_WRONLY | O_CREAT, 0600)) >= 0)
178 for (i = 0; i < STEP_COUNT; i++)
180 if (write(file, buf, esize) < 0)
192 static inline uint32_t timeof(void *entry)
194 return ntohl(((struct traffic_entry *)entry)->time);
197 static int update_file(const char *path, void *entry, int esize)
203 if ((file = open(path, O_RDWR)) >= 0)
205 map = mmap(NULL, esize * STEP_COUNT, PROT_READ | PROT_WRITE,
206 MAP_SHARED | MAP_LOCKED, file, 0);
208 if ((map != NULL) && (map != MAP_FAILED))
210 if (timeof(entry) > timeof(map + esize * (STEP_COUNT-1)))
212 memmove(map, map + esize, esize * (STEP_COUNT-1));
213 memcpy(map + esize * (STEP_COUNT-1), entry, esize);
216 munmap(map, esize * STEP_COUNT);
227 static int mmap_file(const char *path, int esize, struct file_map *m)
233 if ((m->fd = open(path, O_RDONLY)) >= 0)
235 m->size = STEP_COUNT * esize;
236 m->mmap = mmap(NULL, m->size, PROT_READ,
237 MAP_SHARED | MAP_LOCKED, m->fd, 0);
239 if ((m->mmap != NULL) && (m->mmap != MAP_FAILED))
246 static void umap_file(struct file_map *m)
248 if ((m->mmap != NULL) && (m->mmap != MAP_FAILED))
249 munmap(m->mmap, m->size);
255 static void * iwinfo_open(void)
257 return dlopen("/usr/lib/libiwinfo.so", RTLD_LAZY);
260 static int iwinfo_update(
261 void *iw, const char *ifname, uint16_t *rate, uint8_t *rssi, uint8_t *noise
263 int (*probe)(const char *);
268 if ((probe = dlsym(iw, "nl80211_probe")) != NULL && probe(ifname))
270 iw_get_rate = dlsym(iw, "nl80211_get_bitrate");
271 iw_get_rssi = dlsym(iw, "nl80211_get_signal");
272 iw_get_noise = dlsym(iw, "nl80211_get_noise");
274 else if ((probe = dlsym(iw, "madwifi_probe")) != NULL && probe(ifname))
276 iw_get_rate = dlsym(iw, "madwifi_get_bitrate");
277 iw_get_rssi = dlsym(iw, "madwifi_get_signal");
278 iw_get_noise = dlsym(iw, "madwifi_get_noise");
280 else if ((probe = dlsym(iw, "wl_probe")) != NULL && probe(ifname))
282 iw_get_rate = dlsym(iw, "wl_get_bitrate");
283 iw_get_rssi = dlsym(iw, "wl_get_signal");
284 iw_get_noise = dlsym(iw, "wl_get_noise");
292 *rate = (iw_get_rate && !iw_get_rate(ifname, &val)) ? val : 0;
293 *rssi = (iw_get_rssi && !iw_get_rssi(ifname, &val)) ? val : 0;
294 *noise = (iw_get_noise && !iw_get_noise(ifname, &val)) ? val : 0;
299 static void iwinfo_close(void *iw)
301 void (*do_close)(void);
303 if ((do_close = dlsym(iw, "nl80211_close")) != NULL) do_close();
304 if ((do_close = dlsym(iw, "madwifi_close")) != NULL) do_close();
305 if ((do_close = dlsym(iw, "wl_close")) != NULL) do_close();
306 if ((do_close = dlsym(iw, "wext_close")) != NULL) do_close();
307 if ((do_close = dlsym(iw, "iwinfo_close")) != NULL) do_close();
313 static int update_ifstat(
314 const char *ifname, uint32_t rxb, uint32_t rxp, uint32_t txb, uint32_t txp
319 struct traffic_entry e;
321 snprintf(path, sizeof(path), DB_IF_FILE, ifname);
325 if (init_file(path, sizeof(struct traffic_entry)))
327 fprintf(stderr, "Failed to init %s: %s\n",
328 path, strerror(errno));
334 e.time = htonl(time(NULL));
340 return update_file(path, &e, sizeof(struct traffic_entry));
343 static int update_radiostat(
344 const char *ifname, uint16_t rate, uint8_t rssi, uint8_t noise
349 struct radio_entry e;
351 snprintf(path, sizeof(path), DB_RD_FILE, ifname);
355 if (init_file(path, sizeof(struct radio_entry)))
357 fprintf(stderr, "Failed to init %s: %s\n",
358 path, strerror(errno));
364 e.time = htonl(time(NULL));
365 e.rate = htons(rate);
369 return update_file(path, &e, sizeof(struct radio_entry));
372 static int update_cnstat(uint32_t udp, uint32_t tcp, uint32_t other)
379 snprintf(path, sizeof(path), DB_CN_FILE);
383 if (init_file(path, sizeof(struct conn_entry)))
385 fprintf(stderr, "Failed to init %s: %s\n",
386 path, strerror(errno));
392 e.time = htonl(time(NULL));
395 e.other = htonl(other);
397 return update_file(path, &e, sizeof(struct conn_entry));
400 static int update_ldstat(uint16_t load1, uint16_t load5, uint16_t load15)
407 snprintf(path, sizeof(path), DB_LD_FILE);
411 if (init_file(path, sizeof(struct load_entry)))
413 fprintf(stderr, "Failed to init %s: %s\n",
414 path, strerror(errno));
420 e.time = htonl(time(NULL));
421 e.load1 = htons(load1);
422 e.load5 = htons(load5);
423 e.load15 = htons(load15);
425 return update_file(path, &e, sizeof(struct load_entry));
428 static int run_daemon(void)
431 uint32_t rxb, txb, rxp, txp;
432 uint32_t udp, tcp, other;
435 float lf1, lf5, lf15;
443 const char *ipc = stat("/proc/net/nf_conntrack", &s)
444 ? "/proc/net/ip_conntrack" : "/proc/net/nf_conntrack";
468 /* setup USR1 signal handler to reset timer */
469 sa.sa_handler = reset_countdown;
470 sa.sa_flags = SA_RESTART;
471 sigemptyset(&sa.sa_mask);
472 sigaction(SIGUSR1, &sa, NULL);
477 fprintf(stderr, "Failed to write pid file: %s\n", strerror(errno));
481 /* initialize iwinfo */
485 for (reset_countdown(0); countdown >= 0; countdown--)
487 /* alter progname for ps, top */
488 memset(progname, 0, prognamelen);
489 snprintf(progname, prognamelen, "luci-bwc %d", countdown);
491 if ((info = fopen("/proc/net/dev", "r")) != NULL)
493 while (fgets(line, sizeof(line), info))
495 if (strchr(line, '|'))
498 if (sscanf(line, IF_SCAN_PATTERN, ifname, &rxb, &rxp, &txb, &txp))
500 if (strncmp(ifname, "lo", sizeof(ifname)))
501 update_ifstat(ifname, rxb, rxp, txb, txp);
510 for (i = 0; i < 5; i++)
512 #define iwinfo_checkif(pattern) \
514 snprintf(ifname, sizeof(ifname), pattern, i); \
515 if (iwinfo_update(iw, ifname, &rate, &rssi, &noise)) \
517 update_radiostat(ifname, rate, rssi, noise); \
522 iwinfo_checkif("wlan%d");
523 iwinfo_checkif("ath%d");
524 iwinfo_checkif("wl%d");
528 if ((info = fopen(ipc, "r")) != NULL)
534 while (fgets(line, sizeof(line), info))
536 if (strstr(line, "TIME_WAIT"))
539 if (strstr(line, "src=127.0.0.1 ") &&
540 strstr(line, "dst=127.0.0.1 "))
543 if (sscanf(line, "%*s %*d %s", ifname) || sscanf(line, "%s %*d", ifname))
545 if (!strcmp(ifname, "tcp"))
547 else if (!strcmp(ifname, "udp"))
554 update_cnstat(udp, tcp, other);
559 if ((info = fopen("/proc/loadavg", "r")) != NULL)
561 if (fscanf(info, LD_SCAN_PATTERN, &lf1, &lf5, &lf15))
563 update_ldstat((uint16_t)(lf1 * 100),
564 (uint16_t)(lf5 * 100),
565 (uint16_t)(lf15 * 100));
582 static void check_daemon(void)
586 if ((pid = readpid()) < 0 || kill(pid, 0) < 0)
588 /* daemon ping failed, try to start it up */
592 "Failed to ping daemon and unable to start it up: %s\n",
598 else if (kill(pid, SIGUSR1))
600 fprintf(stderr, "Failed to send signal: %s\n", strerror(errno));
605 static int run_dump_ifname(const char *ifname)
610 struct traffic_entry *e;
613 snprintf(path, sizeof(path), DB_IF_FILE, ifname);
615 if (mmap_file(path, sizeof(struct traffic_entry), &m))
617 fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
621 for (i = 0; i < m.size; i += sizeof(struct traffic_entry))
623 e = (struct traffic_entry *) &m.mmap[i];
628 printf("[ %u, %u, %" PRIu32
631 ntohl(e->rxb), ntohl(e->rxp),
632 ntohl(e->txb), ntohl(e->txp),
633 ((i + sizeof(struct traffic_entry)) < m.size) ? "," : "");
641 static int run_dump_radio(const char *ifname)
646 struct radio_entry *e;
649 snprintf(path, sizeof(path), DB_RD_FILE, ifname);
651 if (mmap_file(path, sizeof(struct radio_entry), &m))
653 fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
657 for (i = 0; i < m.size; i += sizeof(struct radio_entry))
659 e = (struct radio_entry *) &m.mmap[i];
664 printf("[ %u, %d, %d, %d ]%s\n",
666 e->rate, e->rssi, e->noise,
667 ((i + sizeof(struct radio_entry)) < m.size) ? "," : "");
675 static int run_dump_conns(void)
680 struct conn_entry *e;
683 snprintf(path, sizeof(path), DB_CN_FILE);
685 if (mmap_file(path, sizeof(struct conn_entry), &m))
687 fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
691 for (i = 0; i < m.size; i += sizeof(struct conn_entry))
693 e = (struct conn_entry *) &m.mmap[i];
698 printf("[ %u, %u, %u, %u ]%s\n",
699 ntohl(e->time), ntohl(e->udp),
700 ntohl(e->tcp), ntohl(e->other),
701 ((i + sizeof(struct conn_entry)) < m.size) ? "," : "");
709 static int run_dump_load(void)
714 struct load_entry *e;
717 snprintf(path, sizeof(path), DB_LD_FILE);
719 if (mmap_file(path, sizeof(struct load_entry), &m))
721 fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
725 for (i = 0; i < m.size; i += sizeof(struct load_entry))
727 e = (struct load_entry *) &m.mmap[i];
732 printf("[ %u, %u, %u, %u ]%s\n",
734 ntohs(e->load1), ntohs(e->load5), ntohs(e->load15),
735 ((i + sizeof(struct load_entry)) < m.size) ? "," : "");
744 int main(int argc, char *argv[])
751 for (opt = 0; opt < argc; opt++)
752 prognamelen += 1 + strlen(argv[opt]);
754 while ((opt = getopt(argc, argv, "t:i:r:cl")) > -1)
759 timeout = atoi(optarg);
764 return run_dump_ifname(optarg);
769 return run_dump_radio(optarg);
773 return run_dump_conns();
776 return run_dump_load();
785 " %s [-t timeout] -i ifname\n"
786 " %s [-t timeout] -r radiodev\n"
787 " %s [-t timeout] -c\n"
788 " %s [-t timeout] -l\n",
789 argv[0], argv[0], argv[0], argv[0]