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 char *progname;
146 static int prognamelen;
149 static int init_directory(char *path)
153 for (p = &path[1]; *p; p++)
159 if (mkdir(path, 0700) && (errno != EEXIST))
169 static int init_file(char *path, int esize)
172 char buf[sizeof(struct traffic_entry)] = { 0 };
174 if (init_directory(path))
177 if ((file = open(path, O_WRONLY | O_CREAT, 0600)) >= 0)
179 for (i = 0; i < STEP_COUNT; i++)
181 if (write(file, buf, esize) < 0)
193 static inline uint64_t timeof(void *entry)
195 return ((struct traffic_entry *)entry)->time;
198 static int update_file(const char *path, void *entry, int esize)
204 if ((file = open(path, O_RDWR)) >= 0)
206 map = mmap(NULL, esize * STEP_COUNT, PROT_READ | PROT_WRITE,
207 MAP_SHARED | MAP_LOCKED, file, 0);
209 if ((map != NULL) && (map != MAP_FAILED))
211 if (timeof(entry) > timeof(map + esize * (STEP_COUNT-1)))
213 memmove(map, map + esize, esize * (STEP_COUNT-1));
214 memcpy(map + esize * (STEP_COUNT-1), entry, esize);
217 munmap(map, esize * STEP_COUNT);
228 static int mmap_file(const char *path, int esize, struct file_map *m)
234 if ((m->fd = open(path, O_RDONLY)) >= 0)
236 m->size = STEP_COUNT * esize;
237 m->mmap = mmap(NULL, m->size, PROT_READ,
238 MAP_SHARED | MAP_LOCKED, m->fd, 0);
240 if ((m->mmap != NULL) && (m->mmap != MAP_FAILED))
247 static void umap_file(struct file_map *m)
249 if ((m->mmap != NULL) && (m->mmap != MAP_FAILED))
250 munmap(m->mmap, m->size);
257 static int update_ifstat(
258 const char *ifname, uint64_t rxb, uint64_t rxp, uint64_t txb, uint64_t txp
263 struct traffic_entry e;
265 snprintf(path, sizeof(path), DB_IF_FILE, ifname);
269 if (init_file(path, sizeof(struct traffic_entry)))
271 fprintf(stderr, "Failed to init %s: %s\n",
272 path, strerror(errno));
278 e.time = htonll(time(NULL));
284 return update_file(path, &e, sizeof(struct traffic_entry));
287 static int update_cnstat(uint32_t udp, uint32_t tcp, uint32_t other)
294 snprintf(path, sizeof(path), DB_CN_FILE);
298 if (init_file(path, sizeof(struct conn_entry)))
300 fprintf(stderr, "Failed to init %s: %s\n",
301 path, strerror(errno));
307 e.time = htonll(time(NULL));
310 e.other = htonl(other);
312 return update_file(path, &e, sizeof(struct conn_entry));
315 static int update_ldstat(uint16_t load1, uint16_t load5, uint16_t load15)
322 snprintf(path, sizeof(path), DB_LD_FILE);
326 if (init_file(path, sizeof(struct load_entry)))
328 fprintf(stderr, "Failed to init %s: %s\n",
329 path, strerror(errno));
335 e.time = htonll(time(NULL));
336 e.load1 = htons(load1);
337 e.load5 = htons(load5);
338 e.load15 = htons(load15);
340 return update_file(path, &e, sizeof(struct load_entry));
343 static int run_daemon(void)
346 uint64_t rxb, txb, rxp, txp;
347 uint32_t udp, tcp, other;
348 float lf1, lf5, lf15;
355 const char *ipc = stat("/proc/net/nf_conntrack", &s)
356 ? "/proc/net/ip_conntrack" : "/proc/net/nf_conntrack";
380 /* setup USR1 signal handler to reset timer */
381 sa.sa_handler = reset_countdown;
382 sa.sa_flags = SA_RESTART;
383 sigemptyset(&sa.sa_mask);
384 sigaction(SIGUSR1, &sa, NULL);
389 fprintf(stderr, "Failed to write pid file: %s\n", strerror(errno));
394 for (reset_countdown(0); countdown >= 0; countdown--)
396 /* alter progname for ps, top */
397 memset(progname, 0, prognamelen);
398 snprintf(progname, prognamelen, "luci-bwc %d", countdown);
400 if ((info = fopen("/proc/net/dev", "r")) != NULL)
402 while (fgets(line, sizeof(line), info))
404 if (strchr(line, '|'))
407 if (sscanf(line, IF_SCAN_PATTERN, ifname, &rxb, &rxp, &txb, &txp))
409 if (strncmp(ifname, "lo", sizeof(ifname)))
410 update_ifstat(ifname, rxb, rxp, txb, txp);
417 if ((info = fopen(ipc, "r")) != NULL)
423 while (fgets(line, sizeof(line), info))
425 if (strstr(line, "TIME_WAIT"))
428 if (sscanf(line, "%*s %*d %s", ifname) || sscanf(line, "%s %*d", ifname))
430 if (!strcmp(ifname, "tcp"))
432 else if (!strcmp(ifname, "udp"))
439 update_cnstat(udp, tcp, other);
444 if ((info = fopen("/proc/loadavg", "r")) != NULL)
446 if (fscanf(info, LD_SCAN_PATTERN, &lf1, &lf5, &lf15))
448 update_ldstat((uint16_t)(lf1 * 100),
449 (uint16_t)(lf5 * 100),
450 (uint16_t)(lf15 * 100));
464 static void check_daemon(void)
468 if ((pid = readpid()) < 0 || kill(pid, 0) < 0)
470 /* daemon ping failed, try to start it up */
474 "Failed to ping daemon and unable to start it up: %s\n",
480 else if (kill(pid, SIGUSR1))
482 fprintf(stderr, "Failed to send signal: %s\n", strerror(errno));
487 static int run_dump_ifname(const char *ifname)
492 struct traffic_entry *e;
495 snprintf(path, sizeof(path), DB_IF_FILE, ifname);
497 if (mmap_file(path, sizeof(struct traffic_entry), &m))
499 fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
503 for (i = 0; i < m.size; i += sizeof(struct traffic_entry))
505 e = (struct traffic_entry *) &m.mmap[i];
510 printf("[ %" PRIu64 ", %" PRIu64 ", %" PRIu64
511 ", %" PRIu64 ", %" PRIu64 " ]%s\n",
513 ntohll(e->rxb), ntohll(e->rxp),
514 ntohll(e->txb), ntohll(e->txp),
515 ((i + sizeof(struct traffic_entry)) < m.size) ? "," : "");
523 static int run_dump_conns(void)
528 struct conn_entry *e;
531 snprintf(path, sizeof(path), DB_CN_FILE);
533 if (mmap_file(path, sizeof(struct conn_entry), &m))
535 fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
539 for (i = 0; i < m.size; i += sizeof(struct conn_entry))
541 e = (struct conn_entry *) &m.mmap[i];
546 printf("[ %" PRIu64 ", %u, %u, %u ]%s\n",
547 ntohll(e->time), ntohl(e->udp),
548 ntohl(e->tcp), ntohl(e->other),
549 ((i + sizeof(struct conn_entry)) < m.size) ? "," : "");
557 static int run_dump_load(void)
562 struct load_entry *e;
565 snprintf(path, sizeof(path), DB_LD_FILE);
567 if (mmap_file(path, sizeof(struct load_entry), &m))
569 fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
573 for (i = 0; i < m.size; i += sizeof(struct load_entry))
575 e = (struct load_entry *) &m.mmap[i];
580 printf("[ %" PRIu64 ", %u, %u, %u ]%s\n",
582 ntohs(e->load1), ntohs(e->load5), ntohs(e->load15),
583 ((i + sizeof(struct load_entry)) < m.size) ? "," : "");
592 int main(int argc, char *argv[])
599 for (opt = 0; opt < argc; opt++)
600 prognamelen += 1 + strlen(argv[opt]);
602 while ((opt = getopt(argc, argv, "t:i:cl")) > -1)
607 timeout = atoi(optarg);
612 return run_dump_ifname(optarg);
616 return run_dump_conns();
619 return run_dump_load();
628 " %s [-t timeout] -i ifname\n"
629 " %s [-t timeout] -c\n"
630 " %s [-t timeout] -l\n",
631 argv[0], argv[0], argv[0]