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>
39 #define PID_PATH "/var/run/luci-bwc.pid"
41 #define DB_PATH "/var/lib/luci-bwc"
42 #define DB_IF_FILE DB_PATH "/if/%s"
43 #define DB_CN_FILE DB_PATH "/connections"
44 #define DB_LD_FILE DB_PATH "/load"
46 #define IF_SCAN_PATTERN \
47 " %[^ :]:%" SCNu64 " %" SCNu64 \
48 " %*d %*d %*d %*d %*d %*d" \
49 " %" SCNu64 " %" SCNu64
51 #define LD_SCAN_PATTERN \
61 struct traffic_entry {
84 static uint64_t htonll(uint64_t value)
88 if (*(char *)&num == 1)
89 return htonl((uint32_t)(value & 0xFFFFFFFF)) |
90 htonl((uint32_t)(value >> 32));
97 static int readpid(void)
103 if ((fd = open(PID_PATH, O_RDONLY)) > -1)
105 if (read(fd, buf, sizeof(buf)))
117 static int writepid(void)
123 if ((fd = open(PID_PATH, O_WRONLY | O_CREAT | O_TRUNC, 0600)) > -1)
125 wlen = snprintf(buf, sizeof(buf), "%i", getpid());
126 write(fd, buf, wlen);
135 static int timeout = TIMEOUT;
136 static int countdown = -1;
138 static void reset_countdown(int sig)
145 static int init_directory(char *path)
149 for (p = &path[1]; *p; p++)
155 if (mkdir(path, 0700) && (errno != EEXIST))
165 static int init_file(char *path, int esize)
168 char buf[sizeof(struct traffic_entry)] = { 0 };
170 if (init_directory(path))
173 if ((file = open(path, O_WRONLY | O_CREAT, 0600)) >= 0)
175 for (i = 0; i < STEP_COUNT; i++)
177 if (write(file, buf, esize) < 0)
189 static inline uint64_t timeof(void *entry)
191 return ((struct traffic_entry *)entry)->time;
194 static int update_file(const char *path, void *entry, int esize)
200 if ((file = open(path, O_RDWR)) >= 0)
202 map = mmap(NULL, esize * STEP_COUNT, PROT_READ | PROT_WRITE,
203 MAP_SHARED | MAP_LOCKED, file, 0);
205 if ((map != NULL) && (map != MAP_FAILED))
207 if (timeof(entry) > timeof(map + esize * (STEP_COUNT-1)))
209 memmove(map, map + esize, esize * (STEP_COUNT-1));
210 memcpy(map + esize * (STEP_COUNT-1), entry, esize);
213 munmap(map, esize * STEP_COUNT);
224 static int mmap_file(const char *path, int esize, struct file_map *m)
230 if ((m->fd = open(path, O_RDONLY)) >= 0)
232 m->size = STEP_COUNT * esize;
233 m->mmap = mmap(NULL, m->size, PROT_READ,
234 MAP_SHARED | MAP_LOCKED, m->fd, 0);
236 if ((m->mmap != NULL) && (m->mmap != MAP_FAILED))
243 static void umap_file(struct file_map *m)
245 if ((m->mmap != NULL) && (m->mmap != MAP_FAILED))
246 munmap(m->mmap, m->size);
253 static int update_ifstat(
254 const char *ifname, uint64_t rxb, uint64_t rxp, uint64_t txb, uint64_t txp
259 struct traffic_entry e;
261 snprintf(path, sizeof(path), DB_IF_FILE, ifname);
265 if (init_file(path, sizeof(struct traffic_entry)))
267 fprintf(stderr, "Failed to init %s: %s\n",
268 path, strerror(errno));
274 e.time = htonll(time(NULL));
280 return update_file(path, &e, sizeof(struct traffic_entry));
283 static int update_cnstat(uint32_t udp, uint32_t tcp, uint32_t other)
290 snprintf(path, sizeof(path), DB_CN_FILE);
294 if (init_file(path, sizeof(struct conn_entry)))
296 fprintf(stderr, "Failed to init %s: %s\n",
297 path, strerror(errno));
303 e.time = htonll(time(NULL));
306 e.other = htonl(other);
308 return update_file(path, &e, sizeof(struct conn_entry));
311 static int update_ldstat(uint16_t load1, uint16_t load5, uint16_t load15)
318 snprintf(path, sizeof(path), DB_LD_FILE);
322 if (init_file(path, sizeof(struct load_entry)))
324 fprintf(stderr, "Failed to init %s: %s\n",
325 path, strerror(errno));
331 e.time = htonll(time(NULL));
332 e.load1 = htons(load1);
333 e.load5 = htons(load5);
334 e.load15 = htons(load15);
336 return update_file(path, &e, sizeof(struct load_entry));
339 static int run_daemon(char *progname)
342 uint64_t rxb, txb, rxp, txp;
343 uint32_t udp, tcp, other;
344 float lf1, lf5, lf15;
351 const char *ipc = stat("/proc/net/nf_conntrack", &s)
352 ? "/proc/net/ip_conntrack" : "/proc/net/nf_conntrack";
376 /* setup USR1 signal handler to reset timer */
377 sa.sa_handler = reset_countdown;
378 sa.sa_flags = SA_RESTART;
379 sigemptyset(&sa.sa_mask);
380 sigaction(SIGUSR1, &sa, NULL);
385 fprintf(stderr, "Failed to write pid file: %s\n", strerror(errno));
390 for (reset_countdown(0); countdown >= 0; countdown--)
392 /* alter progname for ps, top */
393 sprintf(progname, "luci-bwc %d", countdown);
395 if ((info = fopen("/proc/net/dev", "r")) != NULL)
397 while (fgets(line, sizeof(line), info))
399 if (strchr(line, '|'))
402 if (sscanf(line, IF_SCAN_PATTERN, ifname, &rxb, &rxp, &txb, &txp))
404 if (strncmp(ifname, "lo", sizeof(ifname)))
405 update_ifstat(ifname, rxb, rxp, txb, txp);
412 if ((info = fopen(ipc, "r")) != NULL)
418 while (fgets(line, sizeof(line), info))
420 if (strstr(line, "TIME_WAIT"))
423 if (sscanf(line, "%*s %*d %s", ifname) || sscanf(line, "%s %*d", ifname))
425 if (!strcmp(ifname, "tcp"))
427 else if (!strcmp(ifname, "udp"))
434 update_cnstat(udp, tcp, other);
439 if ((info = fopen("/proc/loadavg", "r")) != NULL)
441 if (fscanf(info, LD_SCAN_PATTERN, &lf1, &lf5, &lf15))
443 update_ldstat((uint16_t)(lf1 * 100),
444 (uint16_t)(lf5 * 100),
445 (uint16_t)(lf15 * 100));
459 static int check_daemon(char *progname)
463 if ((pid = readpid()) < 0 || kill(pid, 0) < 0)
465 /* daemon ping failed, try to start it up */
466 if (run_daemon(progname))
469 "Failed to ping daemon and unable to start it up: %s\n",
475 else if (kill(pid, SIGUSR1))
477 fprintf(stderr, "Failed to send signal: %s\n", strerror(errno));
484 static int run_dump_ifname(char *progname, const char *ifname)
489 struct traffic_entry *e;
491 snprintf(path, sizeof(path), DB_IF_FILE, ifname);
493 if (check_daemon(progname))
498 if (mmap_file(path, sizeof(struct traffic_entry), &m))
500 fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
504 for (i = 0; i < m.size; i += sizeof(struct traffic_entry))
506 e = (struct traffic_entry *) &m.mmap[i];
511 printf("[ %" PRIu64 ", %" PRIu64 ", %" PRIu64
512 ", %" PRIu64 ", %" PRIu64 " ]%s\n",
514 ntohll(e->rxb), ntohll(e->rxp),
515 ntohll(e->txb), ntohll(e->txp),
516 ((i + sizeof(struct traffic_entry)) < m.size) ? "," : "");
524 static int run_dump_conns(char *progname)
529 struct conn_entry *e;
531 snprintf(path, sizeof(path), DB_CN_FILE);
533 if (check_daemon(progname))
538 if (mmap_file(path, sizeof(struct conn_entry), &m))
540 fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
544 for (i = 0; i < m.size; i += sizeof(struct conn_entry))
546 e = (struct conn_entry *) &m.mmap[i];
551 printf("[ %" PRIu64 ", %u, %u, %u ]%s\n",
552 ntohll(e->time), ntohl(e->udp),
553 ntohl(e->tcp), ntohl(e->other),
554 ((i + sizeof(struct conn_entry)) < m.size) ? "," : "");
562 static int run_dump_load(char *progname)
567 struct load_entry *e;
569 snprintf(path, sizeof(path), DB_LD_FILE);
571 if (check_daemon(progname))
576 if (mmap_file(path, sizeof(struct load_entry), &m))
578 fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
582 for (i = 0; i < m.size; i += sizeof(struct load_entry))
584 e = (struct load_entry *) &m.mmap[i];
589 printf("[ %" PRIu64 ", %u, %u, %u ]%s\n",
591 ntohs(e->load1), ntohs(e->load5), ntohs(e->load15),
592 ((i + sizeof(struct load_entry)) < m.size) ? "," : "");
601 int main(int argc, char *argv[])
605 while ((opt = getopt(argc, argv, "t:i:cl")) > -1)
610 timeout = atoi(optarg);
615 return run_dump_ifname(argv[0], optarg);
619 return run_dump_conns(argv[0]);
622 return run_dump_load(argv[0]);
631 " %s [-t timeout] -i ifname\n"
632 " %s [-t timeout] -c\n"
633 " %s [-t timeout] -l\n",
634 argv[0], argv[0], argv[0]