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>
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 \
50 " %[^ :]:%" SCNu64 " %" SCNu64 \
51 " %*d %*d %*d %*d %*d %*d" \
52 " %" SCNu64 " %" SCNu64
54 #define LD_SCAN_PATTERN \
64 struct traffic_entry {
94 static uint64_t htonll(uint64_t value)
98 if (*(char *)&num == 1)
99 return htonl((uint32_t)(value & 0xFFFFFFFF)) |
100 htonl((uint32_t)(value >> 32));
105 #define ntohll htonll
107 static int readpid(void)
113 if ((fd = open(PID_PATH, O_RDONLY)) > -1)
115 if (read(fd, buf, sizeof(buf)))
127 static int writepid(void)
133 if ((fd = open(PID_PATH, O_WRONLY | O_CREAT | O_TRUNC, 0600)) > -1)
135 wlen = snprintf(buf, sizeof(buf), "%i", getpid());
136 write(fd, buf, wlen);
145 static int timeout = TIMEOUT;
146 static int countdown = -1;
148 static void reset_countdown(int sig)
155 static char *progname;
156 static int prognamelen;
158 static int (*iw_get_rate)(const char *, int *) = NULL;
159 static int (*iw_get_rssi)(const char *, int *) = NULL;
160 static int (*iw_get_noise)(const char *, int *) = NULL;
163 static int init_directory(char *path)
167 for (p = &path[1]; *p; p++)
173 if (mkdir(path, 0700) && (errno != EEXIST))
183 static int init_file(char *path, int esize)
186 char buf[sizeof(struct traffic_entry)] = { 0 };
188 if (init_directory(path))
191 if ((file = open(path, O_WRONLY | O_CREAT, 0600)) >= 0)
193 for (i = 0; i < STEP_COUNT; i++)
195 if (write(file, buf, esize) < 0)
207 static inline uint64_t timeof(void *entry)
209 return ((struct traffic_entry *)entry)->time;
212 static int update_file(const char *path, void *entry, int esize)
218 if ((file = open(path, O_RDWR)) >= 0)
220 map = mmap(NULL, esize * STEP_COUNT, PROT_READ | PROT_WRITE,
221 MAP_SHARED | MAP_LOCKED, file, 0);
223 if ((map != NULL) && (map != MAP_FAILED))
225 if (timeof(entry) > timeof(map + esize * (STEP_COUNT-1)))
227 memmove(map, map + esize, esize * (STEP_COUNT-1));
228 memcpy(map + esize * (STEP_COUNT-1), entry, esize);
231 munmap(map, esize * STEP_COUNT);
242 static int mmap_file(const char *path, int esize, struct file_map *m)
248 if ((m->fd = open(path, O_RDONLY)) >= 0)
250 m->size = STEP_COUNT * esize;
251 m->mmap = mmap(NULL, m->size, PROT_READ,
252 MAP_SHARED | MAP_LOCKED, m->fd, 0);
254 if ((m->mmap != NULL) && (m->mmap != MAP_FAILED))
261 static void umap_file(struct file_map *m)
263 if ((m->mmap != NULL) && (m->mmap != MAP_FAILED))
264 munmap(m->mmap, m->size);
270 static void * iwinfo_open(void)
272 return dlopen("/usr/lib/lua/iwinfo.so", RTLD_LAZY);
275 static int iwinfo_update(
276 void *iw, const char *ifname, uint16_t *rate, uint8_t *rssi, uint8_t *noise
278 int (*probe)(const char *);
283 if ((probe = dlsym(iw, "nl80211_probe")) != NULL && probe(ifname))
285 iw_get_rate = dlsym(iw, "nl80211_get_bitrate");
286 iw_get_rssi = dlsym(iw, "nl80211_get_signal");
287 iw_get_noise = dlsym(iw, "nl80211_get_noise");
289 else if ((probe = dlsym(iw, "madwifi_probe")) != NULL && probe(ifname))
291 iw_get_rate = dlsym(iw, "madwifi_get_bitrate");
292 iw_get_rssi = dlsym(iw, "madwifi_get_signal");
293 iw_get_noise = dlsym(iw, "madwifi_get_noise");
295 else if ((probe = dlsym(iw, "wl_probe")) != NULL && probe(ifname))
297 iw_get_rate = dlsym(iw, "wl_get_bitrate");
298 iw_get_rssi = dlsym(iw, "wl_get_signal");
299 iw_get_noise = dlsym(iw, "wl_get_noise");
307 *rate = (iw_get_rate && !iw_get_rate(ifname, &val)) ? val : 0;
308 *rssi = (iw_get_rssi && !iw_get_rssi(ifname, &val)) ? val : 0;
309 *noise = (iw_get_noise && !iw_get_noise(ifname, &val)) ? val : 0;
314 static void iwinfo_close(void *iw)
316 void (*do_close)(void);
318 if ((do_close = dlsym(iw, "nl80211_close")) != NULL) do_close();
319 if ((do_close = dlsym(iw, "madwifi_close")) != NULL) do_close();
320 if ((do_close = dlsym(iw, "wl_close")) != NULL) do_close();
321 if ((do_close = dlsym(iw, "wext_close")) != NULL) do_close();
322 if ((do_close = dlsym(iw, "iwinfo_close")) != NULL) do_close();
328 static int update_ifstat(
329 const char *ifname, uint64_t rxb, uint64_t rxp, uint64_t txb, uint64_t txp
334 struct traffic_entry e;
336 snprintf(path, sizeof(path), DB_IF_FILE, ifname);
340 if (init_file(path, sizeof(struct traffic_entry)))
342 fprintf(stderr, "Failed to init %s: %s\n",
343 path, strerror(errno));
349 e.time = htonll(time(NULL));
355 return update_file(path, &e, sizeof(struct traffic_entry));
358 static int update_radiostat(
359 const char *ifname, uint16_t rate, uint8_t rssi, uint8_t noise
364 struct radio_entry e;
366 snprintf(path, sizeof(path), DB_RD_FILE, ifname);
370 if (init_file(path, sizeof(struct radio_entry)))
372 fprintf(stderr, "Failed to init %s: %s\n",
373 path, strerror(errno));
379 e.time = htonll(time(NULL));
380 e.rate = htons(rate);
384 return update_file(path, &e, sizeof(struct radio_entry));
387 static int update_cnstat(uint32_t udp, uint32_t tcp, uint32_t other)
394 snprintf(path, sizeof(path), DB_CN_FILE);
398 if (init_file(path, sizeof(struct conn_entry)))
400 fprintf(stderr, "Failed to init %s: %s\n",
401 path, strerror(errno));
407 e.time = htonll(time(NULL));
410 e.other = htonl(other);
412 return update_file(path, &e, sizeof(struct conn_entry));
415 static int update_ldstat(uint16_t load1, uint16_t load5, uint16_t load15)
422 snprintf(path, sizeof(path), DB_LD_FILE);
426 if (init_file(path, sizeof(struct load_entry)))
428 fprintf(stderr, "Failed to init %s: %s\n",
429 path, strerror(errno));
435 e.time = htonll(time(NULL));
436 e.load1 = htons(load1);
437 e.load5 = htons(load5);
438 e.load15 = htons(load15);
440 return update_file(path, &e, sizeof(struct load_entry));
443 static int run_daemon(void)
446 uint64_t rxb, txb, rxp, txp;
447 uint32_t udp, tcp, other;
450 float lf1, lf5, lf15;
458 const char *ipc = stat("/proc/net/nf_conntrack", &s)
459 ? "/proc/net/ip_conntrack" : "/proc/net/nf_conntrack";
483 /* setup USR1 signal handler to reset timer */
484 sa.sa_handler = reset_countdown;
485 sa.sa_flags = SA_RESTART;
486 sigemptyset(&sa.sa_mask);
487 sigaction(SIGUSR1, &sa, NULL);
492 fprintf(stderr, "Failed to write pid file: %s\n", strerror(errno));
496 /* initialize iwinfo */
500 for (reset_countdown(0); countdown >= 0; countdown--)
502 /* alter progname for ps, top */
503 memset(progname, 0, prognamelen);
504 snprintf(progname, prognamelen, "luci-bwc %d", countdown);
506 if ((info = fopen("/proc/net/dev", "r")) != NULL)
508 while (fgets(line, sizeof(line), info))
510 if (strchr(line, '|'))
513 if (sscanf(line, IF_SCAN_PATTERN, ifname, &rxb, &rxp, &txb, &txp))
515 if (strncmp(ifname, "lo", sizeof(ifname)))
516 update_ifstat(ifname, rxb, rxp, txb, txp);
525 for (i = 0; i < 5; i++)
527 #define iwinfo_checkif(pattern) \
529 snprintf(ifname, sizeof(ifname), pattern, i); \
530 if (iwinfo_update(iw, ifname, &rate, &rssi, &noise)) \
532 update_radiostat(ifname, rate, rssi, noise); \
537 iwinfo_checkif("wlan%d");
538 iwinfo_checkif("ath%d");
539 iwinfo_checkif("wl%d");
543 if ((info = fopen(ipc, "r")) != NULL)
549 while (fgets(line, sizeof(line), info))
551 if (strstr(line, "TIME_WAIT"))
554 if (sscanf(line, "%*s %*d %s", ifname) || sscanf(line, "%s %*d", ifname))
556 if (!strcmp(ifname, "tcp"))
558 else if (!strcmp(ifname, "udp"))
565 update_cnstat(udp, tcp, other);
570 if ((info = fopen("/proc/loadavg", "r")) != NULL)
572 if (fscanf(info, LD_SCAN_PATTERN, &lf1, &lf5, &lf15))
574 update_ldstat((uint16_t)(lf1 * 100),
575 (uint16_t)(lf5 * 100),
576 (uint16_t)(lf15 * 100));
593 static void check_daemon(void)
597 if ((pid = readpid()) < 0 || kill(pid, 0) < 0)
599 /* daemon ping failed, try to start it up */
603 "Failed to ping daemon and unable to start it up: %s\n",
609 else if (kill(pid, SIGUSR1))
611 fprintf(stderr, "Failed to send signal: %s\n", strerror(errno));
616 static int run_dump_ifname(const char *ifname)
621 struct traffic_entry *e;
624 snprintf(path, sizeof(path), DB_IF_FILE, ifname);
626 if (mmap_file(path, sizeof(struct traffic_entry), &m))
628 fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
632 for (i = 0; i < m.size; i += sizeof(struct traffic_entry))
634 e = (struct traffic_entry *) &m.mmap[i];
639 printf("[ %" PRIu64 ", %" PRIu64 ", %" PRIu64
640 ", %" PRIu64 ", %" PRIu64 " ]%s\n",
642 ntohll(e->rxb), ntohll(e->rxp),
643 ntohll(e->txb), ntohll(e->txp),
644 ((i + sizeof(struct traffic_entry)) < m.size) ? "," : "");
652 static int run_dump_radio(const char *ifname)
657 struct radio_entry *e;
660 snprintf(path, sizeof(path), DB_RD_FILE, ifname);
662 if (mmap_file(path, sizeof(struct radio_entry), &m))
664 fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
668 for (i = 0; i < m.size; i += sizeof(struct radio_entry))
670 e = (struct radio_entry *) &m.mmap[i];
675 printf("[ %" PRIu64 ", %d, %d, %d ]%s\n",
677 e->rate, e->rssi, e->noise,
678 ((i + sizeof(struct radio_entry)) < m.size) ? "," : "");
686 static int run_dump_conns(void)
691 struct conn_entry *e;
694 snprintf(path, sizeof(path), DB_CN_FILE);
696 if (mmap_file(path, sizeof(struct conn_entry), &m))
698 fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
702 for (i = 0; i < m.size; i += sizeof(struct conn_entry))
704 e = (struct conn_entry *) &m.mmap[i];
709 printf("[ %" PRIu64 ", %u, %u, %u ]%s\n",
710 ntohll(e->time), ntohl(e->udp),
711 ntohl(e->tcp), ntohl(e->other),
712 ((i + sizeof(struct conn_entry)) < m.size) ? "," : "");
720 static int run_dump_load(void)
725 struct load_entry *e;
728 snprintf(path, sizeof(path), DB_LD_FILE);
730 if (mmap_file(path, sizeof(struct load_entry), &m))
732 fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
736 for (i = 0; i < m.size; i += sizeof(struct load_entry))
738 e = (struct load_entry *) &m.mmap[i];
743 printf("[ %" PRIu64 ", %u, %u, %u ]%s\n",
745 ntohs(e->load1), ntohs(e->load5), ntohs(e->load15),
746 ((i + sizeof(struct load_entry)) < m.size) ? "," : "");
755 int main(int argc, char *argv[])
762 for (opt = 0; opt < argc; opt++)
763 prognamelen += 1 + strlen(argv[opt]);
765 while ((opt = getopt(argc, argv, "t:i:r:cl")) > -1)
770 timeout = atoi(optarg);
775 return run_dump_ifname(optarg);
780 return run_dump_radio(optarg);
784 return run_dump_conns();
787 return run_dump_load();
796 " %s [-t timeout] -i ifname\n"
797 " %s [-t timeout] -r radiodev\n"
798 " %s [-t timeout] -c\n"
799 " %s [-t timeout] -l\n",
800 argv[0], argv[0], argv[0], argv[0]