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
{
79 static uint64_t htonll(uint64_t value
)
83 if (*(char *)&num
== 1)
84 return htonl((uint32_t)(value
& 0xFFFFFFFF)) |
85 htonl((uint32_t)(value
>> 32));
93 static int init_directory(char *path
)
97 for (p
= &path
[1]; *p
; p
++)
103 if (mkdir(path
, 0700) && (errno
!= EEXIST
))
113 static int init_file(char *path
, int esize
)
116 char buf
[sizeof(struct traffic_entry
)] = { 0 };
118 if (init_directory(path
))
121 if ((file
= open(path
, O_WRONLY
| O_CREAT
, 0600)) >= 0)
123 for (i
= 0; i
< STEP_COUNT
; i
++)
125 if (write(file
, buf
, esize
) < 0)
137 static int update_file(const char *path
, void *entry
, int esize
)
143 if ((file
= open(path
, O_RDWR
)) >= 0)
145 map
= mmap(NULL
, esize
* STEP_COUNT
, PROT_READ
| PROT_WRITE
,
146 MAP_SHARED
| MAP_LOCKED
, file
, 0);
148 if ((map
!= NULL
) && (map
!= MAP_FAILED
))
150 memmove(map
, map
+ esize
, esize
* (STEP_COUNT
-1));
151 memcpy(map
+ esize
* (STEP_COUNT
-1), entry
, esize
);
153 munmap(map
, esize
* STEP_COUNT
);
164 static int mmap_file(const char *path
, int esize
, struct file_map
*m
)
170 if ((m
->fd
= open(path
, O_RDONLY
)) >= 0)
172 m
->size
= STEP_COUNT
* esize
;
173 m
->mmap
= mmap(NULL
, m
->size
, PROT_READ
,
174 MAP_SHARED
| MAP_LOCKED
, m
->fd
, 0);
176 if ((m
->mmap
!= NULL
) && (m
->mmap
!= MAP_FAILED
))
183 static void umap_file(struct file_map
*m
)
185 if ((m
->mmap
!= NULL
) && (m
->mmap
!= MAP_FAILED
))
186 munmap(m
->mmap
, m
->size
);
193 static int update_ifstat(
194 const char *ifname
, uint64_t rxb
, uint64_t rxp
, uint64_t txb
, uint64_t txp
199 struct traffic_entry e
;
201 snprintf(path
, sizeof(path
), DB_IF_FILE
, ifname
);
205 if (init_file(path
, sizeof(struct traffic_entry
)))
207 fprintf(stderr
, "Failed to init %s: %s\n",
208 path
, strerror(errno
));
214 e
.time
= htonll(time(NULL
));
220 return update_file(path
, &e
, sizeof(struct traffic_entry
));
223 static int update_cnstat(uint32_t udp
, uint32_t tcp
)
230 snprintf(path
, sizeof(path
), DB_CN_FILE
);
234 if (init_file(path
, sizeof(struct conn_entry
)))
236 fprintf(stderr
, "Failed to init %s: %s\n",
237 path
, strerror(errno
));
243 e
.time
= htonll(time(NULL
));
247 return update_file(path
, &e
, sizeof(struct conn_entry
));
250 static int update_ldstat(uint16_t load1
, uint16_t load5
, uint16_t load15
)
257 snprintf(path
, sizeof(path
), DB_LD_FILE
);
261 if (init_file(path
, sizeof(struct load_entry
)))
263 fprintf(stderr
, "Failed to init %s: %s\n",
264 path
, strerror(errno
));
270 e
.time
= htonll(time(NULL
));
271 e
.load1
= htons(load1
);
272 e
.load5
= htons(load5
);
273 e
.load15
= htons(load15
);
275 return update_file(path
, &e
, sizeof(struct load_entry
));
278 static int run_daemon(int nofork
)
281 uint64_t rxb
, txb
, rxp
, txp
;
283 float lf1
, lf5
, lf15
;
317 if ((info
= fopen("/proc/net/dev", "r")) != NULL
)
319 while (fgets(line
, sizeof(line
), info
))
321 if (strchr(line
, '|'))
324 if (sscanf(line
, IF_SCAN_PATTERN
, ifname
, &rxb
, &rxp
, &txb
, &txp
))
326 if (strncmp(ifname
, "lo", sizeof(ifname
)))
327 update_ifstat(ifname
, rxb
, rxp
, txb
, txp
);
334 if ((info
= fopen("/proc/net/ip_conntrack", "r")) != NULL
)
339 while (fgets(line
, sizeof(line
), info
))
356 update_cnstat(udp
, tcp
);
361 if ((info
= fopen("/proc/loadavg", "r")) != NULL
)
363 if (fscanf(info
, LD_SCAN_PATTERN
, &lf1
, &lf5
, &lf15
))
365 update_ldstat((uint16_t)(lf1
* 100),
366 (uint16_t)(lf5
* 100),
367 (uint16_t)(lf15
* 100));
377 static int run_dump_ifname(const char *ifname
)
382 struct traffic_entry
*e
;
384 snprintf(path
, sizeof(path
), DB_IF_FILE
, ifname
);
386 if (mmap_file(path
, sizeof(struct traffic_entry
), &m
))
388 fprintf(stderr
, "Failed to open %s: %s\n", path
, strerror(errno
));
392 for (i
= 0; i
< m
.size
; i
+= sizeof(struct traffic_entry
))
394 e
= (struct traffic_entry
*) &m
.mmap
[i
];
399 printf("[ %" PRIu64
", %" PRIu64
", %" PRIu64
400 ", %" PRIu64
", %" PRIu64
" ]%s\n",
402 ntohll(e
->rxb
), ntohll(e
->rxp
),
403 ntohll(e
->txb
), ntohll(e
->txp
),
404 ((i
+ sizeof(struct traffic_entry
)) < m
.size
) ? "," : "");
412 static int run_dump_conns(void)
417 struct conn_entry
*e
;
419 snprintf(path
, sizeof(path
), DB_CN_FILE
);
421 if (mmap_file(path
, sizeof(struct conn_entry
), &m
))
423 fprintf(stderr
, "Failed to open %s: %s\n", path
, strerror(errno
));
427 for (i
= 0; i
< m
.size
; i
+= sizeof(struct conn_entry
))
429 e
= (struct conn_entry
*) &m
.mmap
[i
];
434 printf("[ %" PRIu64
", %u, %u ]%s\n",
435 ntohll(e
->time
), ntohl(e
->udp
), ntohl(e
->tcp
),
436 ((i
+ sizeof(struct conn_entry
)) < m
.size
) ? "," : "");
444 static int run_dump_load(void)
449 struct load_entry
*e
;
451 snprintf(path
, sizeof(path
), DB_LD_FILE
);
453 if (mmap_file(path
, sizeof(struct load_entry
), &m
))
455 fprintf(stderr
, "Failed to open %s: %s\n", path
, strerror(errno
));
459 for (i
= 0; i
< m
.size
; i
+= sizeof(struct load_entry
))
461 e
= (struct load_entry
*) &m
.mmap
[i
];
466 printf("[ %" PRIu64
", %u, %u, %u ]%s\n",
468 ntohs(e
->load1
), ntohs(e
->load5
), ntohs(e
->load15
),
469 ((i
+ sizeof(struct load_entry
)) < m
.size
) ? "," : "");
478 int main(int argc
, char *argv
[])
484 while ((opt
= getopt(argc
, argv
, "dfi:cl")) > -1)
498 return run_dump_ifname(optarg
);
502 return run_dump_conns();
505 return run_dump_load();
513 return run_daemon(nofork
);
522 argv
[0], argv
[0], argv
[0], argv
[0]