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.
31 #include <arpa/inet.h>
37 #define DB_PATH "/var/lib/luci-bwc"
38 #define DB_IF_FILE DB_PATH "/if/%s"
39 #define DB_CN_FILE DB_PATH "/connections"
40 #define DB_LD_FILE DB_PATH "/load"
42 #define IF_SCAN_PATTERN \
43 " %[^ :]:%" SCNu64 " %" SCNu64 \
44 " %*d %*d %*d %*d %*d %*d" \
45 " %" SCNu64 " %" SCNu64
47 #define LD_SCAN_PATTERN \
57 struct traffic_entry {
80 static uint64_t htonll(uint64_t value)
84 if (*(char *)&num == 1)
85 return htonl((uint32_t)(value & 0xFFFFFFFF)) |
86 htonl((uint32_t)(value >> 32));
94 static int init_directory(char *path)
98 for (p = &path[1]; *p; p++)
104 if (mkdir(path, 0700) && (errno != EEXIST))
114 static int init_file(char *path, int esize)
117 char buf[sizeof(struct traffic_entry)] = { 0 };
119 if (init_directory(path))
122 if ((file = open(path, O_WRONLY | O_CREAT, 0600)) >= 0)
124 for (i = 0; i < STEP_COUNT; i++)
126 if (write(file, buf, esize) < 0)
138 static int update_file(const char *path, void *entry, int esize)
144 if ((file = open(path, O_RDWR)) >= 0)
146 map = mmap(NULL, esize * STEP_COUNT, PROT_READ | PROT_WRITE,
147 MAP_SHARED | MAP_LOCKED, file, 0);
149 if ((map != NULL) && (map != MAP_FAILED))
151 memmove(map, map + esize, esize * (STEP_COUNT-1));
152 memcpy(map + esize * (STEP_COUNT-1), entry, esize);
154 munmap(map, esize * STEP_COUNT);
165 static int mmap_file(const char *path, int esize, struct file_map *m)
171 if ((m->fd = open(path, O_RDONLY)) >= 0)
173 m->size = STEP_COUNT * esize;
174 m->mmap = mmap(NULL, m->size, PROT_READ,
175 MAP_SHARED | MAP_LOCKED, m->fd, 0);
177 if ((m->mmap != NULL) && (m->mmap != MAP_FAILED))
184 static void umap_file(struct file_map *m)
186 if ((m->mmap != NULL) && (m->mmap != MAP_FAILED))
187 munmap(m->mmap, m->size);
194 static int update_ifstat(
195 const char *ifname, uint64_t rxb, uint64_t rxp, uint64_t txb, uint64_t txp
200 struct traffic_entry e;
202 snprintf(path, sizeof(path), DB_IF_FILE, ifname);
206 if (init_file(path, sizeof(struct traffic_entry)))
208 fprintf(stderr, "Failed to init %s: %s\n",
209 path, strerror(errno));
215 e.time = htonll(time(NULL));
221 return update_file(path, &e, sizeof(struct traffic_entry));
224 static int update_cnstat(uint32_t udp, uint32_t tcp, uint32_t other)
231 snprintf(path, sizeof(path), DB_CN_FILE);
235 if (init_file(path, sizeof(struct conn_entry)))
237 fprintf(stderr, "Failed to init %s: %s\n",
238 path, strerror(errno));
244 e.time = htonll(time(NULL));
247 e.other = htonl(other);
249 return update_file(path, &e, sizeof(struct conn_entry));
252 static int update_ldstat(uint16_t load1, uint16_t load5, uint16_t load15)
259 snprintf(path, sizeof(path), DB_LD_FILE);
263 if (init_file(path, sizeof(struct load_entry)))
265 fprintf(stderr, "Failed to init %s: %s\n",
266 path, strerror(errno));
272 e.time = htonll(time(NULL));
273 e.load1 = htons(load1);
274 e.load5 = htons(load5);
275 e.load15 = htons(load15);
277 return update_file(path, &e, sizeof(struct load_entry));
280 static int run_daemon(int nofork)
283 uint64_t rxb, txb, rxp, txp;
284 uint32_t udp, tcp, other;
285 float lf1, lf5, lf15;
290 const char *ipc = stat("/proc/net/nf_conntrack", &s)
291 ? "/proc/net/ip_conntrack" : "/proc/net/nf_conntrack";
322 if ((info = fopen("/proc/net/dev", "r")) != NULL)
324 while (fgets(line, sizeof(line), info))
326 if (strchr(line, '|'))
329 if (sscanf(line, IF_SCAN_PATTERN, ifname, &rxb, &rxp, &txb, &txp))
331 if (strncmp(ifname, "lo", sizeof(ifname)))
332 update_ifstat(ifname, rxb, rxp, txb, txp);
339 if ((info = fopen(ipc, "r")) != NULL)
345 while (fgets(line, sizeof(line), info))
347 if (sscanf(line, "%*s %*d %s", ifname) || sscanf(line, "%s %*d", ifname))
349 if (!strcmp(ifname, "tcp"))
351 else if (!strcmp(ifname, "udp"))
358 update_cnstat(udp, tcp, other);
363 if ((info = fopen("/proc/loadavg", "r")) != NULL)
365 if (fscanf(info, LD_SCAN_PATTERN, &lf1, &lf5, &lf15))
367 update_ldstat((uint16_t)(lf1 * 100),
368 (uint16_t)(lf5 * 100),
369 (uint16_t)(lf15 * 100));
379 static int run_dump_ifname(const char *ifname)
384 struct traffic_entry *e;
386 snprintf(path, sizeof(path), DB_IF_FILE, ifname);
388 if (mmap_file(path, sizeof(struct traffic_entry), &m))
390 fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
394 for (i = 0; i < m.size; i += sizeof(struct traffic_entry))
396 e = (struct traffic_entry *) &m.mmap[i];
401 printf("[ %" PRIu64 ", %" PRIu64 ", %" PRIu64
402 ", %" PRIu64 ", %" PRIu64 " ]%s\n",
404 ntohll(e->rxb), ntohll(e->rxp),
405 ntohll(e->txb), ntohll(e->txp),
406 ((i + sizeof(struct traffic_entry)) < m.size) ? "," : "");
414 static int run_dump_conns(void)
419 struct conn_entry *e;
421 snprintf(path, sizeof(path), DB_CN_FILE);
423 if (mmap_file(path, sizeof(struct conn_entry), &m))
425 fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
429 for (i = 0; i < m.size; i += sizeof(struct conn_entry))
431 e = (struct conn_entry *) &m.mmap[i];
436 printf("[ %" PRIu64 ", %u, %u, %u ]%s\n",
437 ntohll(e->time), ntohl(e->udp),
438 ntohl(e->tcp), ntohl(e->other),
439 ((i + sizeof(struct conn_entry)) < m.size) ? "," : "");
447 static int run_dump_load(void)
452 struct load_entry *e;
454 snprintf(path, sizeof(path), DB_LD_FILE);
456 if (mmap_file(path, sizeof(struct load_entry), &m))
458 fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
462 for (i = 0; i < m.size; i += sizeof(struct load_entry))
464 e = (struct load_entry *) &m.mmap[i];
469 printf("[ %" PRIu64 ", %u, %u, %u ]%s\n",
471 ntohs(e->load1), ntohs(e->load5), ntohs(e->load15),
472 ((i + sizeof(struct load_entry)) < m.size) ? "," : "");
481 int main(int argc, char *argv[])
487 while ((opt = getopt(argc, argv, "dfi:cl")) > -1)
501 return run_dump_ifname(optarg);
505 return run_dump_conns();
508 return run_dump_load();
516 return run_daemon(nofork);
525 argv[0], argv[0], argv[0], argv[0]