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>
40 #define PID_PATH "/var/run/luci-bwc.pid"
42 #define DB_PATH "/var/lib/luci-bwc"
43 #define DB_IF_FILE DB_PATH "/if/%s"
44 #define DB_RD_FILE DB_PATH "/radio/%s"
45 #define DB_CN_FILE DB_PATH "/connections"
46 #define DB_LD_FILE DB_PATH "/load"
48 #define IF_SCAN_PATTERN \
50 " %*d %*d %*d %*d %*d %*d" \
53 #define LD_SCAN_PATTERN \
63 struct traffic_entry
{
92 static int readpid(void)
98 if ((fd
= open(PID_PATH
, O_RDONLY
)) > -1)
100 if (read(fd
, buf
, sizeof(buf
)))
112 static int writepid(void)
118 if ((fd
= open(PID_PATH
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600)) > -1)
120 wlen
= snprintf(buf
, sizeof(buf
), "%i", getpid());
121 write(fd
, buf
, wlen
);
130 static int timeout
= TIMEOUT
;
131 static int countdown
= -1;
133 static void reset_countdown(int sig
)
140 static char *progname
;
141 static int prognamelen
;
143 static int (*iw_get_rate
)(const char *, int *) = NULL
;
144 static int (*iw_get_rssi
)(const char *, int *) = NULL
;
145 static int (*iw_get_noise
)(const char *, int *) = NULL
;
148 static int init_directory(char *path
)
152 for (p
= &path
[1]; *p
; p
++)
158 if (mkdir(path
, 0700) && (errno
!= EEXIST
))
168 static int init_file(char *path
, int esize
)
171 char buf
[sizeof(struct traffic_entry
)] = { 0 };
173 if (init_directory(path
))
176 if ((file
= open(path
, O_WRONLY
| O_CREAT
, 0600)) >= 0)
178 for (i
= 0; i
< STEP_COUNT
; i
++)
180 if (write(file
, buf
, esize
) < 0)
192 static inline uint32_t timeof(void *entry
)
194 return ntohl(((struct traffic_entry
*)entry
)->time
);
197 static int update_file(const char *path
, void *entry
, int esize
)
203 if ((file
= open(path
, O_RDWR
)) >= 0)
205 map
= mmap(NULL
, esize
* STEP_COUNT
, PROT_READ
| PROT_WRITE
,
206 MAP_SHARED
| MAP_LOCKED
, file
, 0);
208 if ((map
!= NULL
) && (map
!= MAP_FAILED
))
210 if (timeof(entry
) > timeof(map
+ esize
* (STEP_COUNT
-1)))
212 memmove(map
, map
+ esize
, esize
* (STEP_COUNT
-1));
213 memcpy(map
+ esize
* (STEP_COUNT
-1), entry
, esize
);
216 munmap(map
, esize
* STEP_COUNT
);
227 static int mmap_file(const char *path
, int esize
, struct file_map
*m
)
233 if ((m
->fd
= open(path
, O_RDONLY
)) >= 0)
235 m
->size
= STEP_COUNT
* esize
;
236 m
->mmap
= mmap(NULL
, m
->size
, PROT_READ
,
237 MAP_SHARED
| MAP_LOCKED
, m
->fd
, 0);
239 if ((m
->mmap
!= NULL
) && (m
->mmap
!= MAP_FAILED
))
246 static void umap_file(struct file_map
*m
)
248 if ((m
->mmap
!= NULL
) && (m
->mmap
!= MAP_FAILED
))
249 munmap(m
->mmap
, m
->size
);
255 static void * iwinfo_open(void)
257 return dlopen("/usr/lib/lua/iwinfo.so", RTLD_LAZY
);
260 static int iwinfo_update(
261 void *iw
, const char *ifname
, uint16_t *rate
, uint8_t *rssi
, uint8_t *noise
263 int (*probe
)(const char *);
268 if ((probe
= dlsym(iw
, "nl80211_probe")) != NULL
&& probe(ifname
))
270 iw_get_rate
= dlsym(iw
, "nl80211_get_bitrate");
271 iw_get_rssi
= dlsym(iw
, "nl80211_get_signal");
272 iw_get_noise
= dlsym(iw
, "nl80211_get_noise");
274 else if ((probe
= dlsym(iw
, "madwifi_probe")) != NULL
&& probe(ifname
))
276 iw_get_rate
= dlsym(iw
, "madwifi_get_bitrate");
277 iw_get_rssi
= dlsym(iw
, "madwifi_get_signal");
278 iw_get_noise
= dlsym(iw
, "madwifi_get_noise");
280 else if ((probe
= dlsym(iw
, "wl_probe")) != NULL
&& probe(ifname
))
282 iw_get_rate
= dlsym(iw
, "wl_get_bitrate");
283 iw_get_rssi
= dlsym(iw
, "wl_get_signal");
284 iw_get_noise
= dlsym(iw
, "wl_get_noise");
292 *rate
= (iw_get_rate
&& !iw_get_rate(ifname
, &val
)) ? val
: 0;
293 *rssi
= (iw_get_rssi
&& !iw_get_rssi(ifname
, &val
)) ? val
: 0;
294 *noise
= (iw_get_noise
&& !iw_get_noise(ifname
, &val
)) ? val
: 0;
299 static void iwinfo_close(void *iw
)
301 void (*do_close
)(void);
303 if ((do_close
= dlsym(iw
, "nl80211_close")) != NULL
) do_close();
304 if ((do_close
= dlsym(iw
, "madwifi_close")) != NULL
) do_close();
305 if ((do_close
= dlsym(iw
, "wl_close")) != NULL
) do_close();
306 if ((do_close
= dlsym(iw
, "wext_close")) != NULL
) do_close();
307 if ((do_close
= dlsym(iw
, "iwinfo_close")) != NULL
) do_close();
313 static int update_ifstat(
314 const char *ifname
, uint32_t rxb
, uint32_t rxp
, uint32_t txb
, uint32_t txp
319 struct traffic_entry e
;
321 snprintf(path
, sizeof(path
), DB_IF_FILE
, ifname
);
325 if (init_file(path
, sizeof(struct traffic_entry
)))
327 fprintf(stderr
, "Failed to init %s: %s\n",
328 path
, strerror(errno
));
334 e
.time
= htonl(time(NULL
));
340 return update_file(path
, &e
, sizeof(struct traffic_entry
));
343 static int update_radiostat(
344 const char *ifname
, uint16_t rate
, uint8_t rssi
, uint8_t noise
349 struct radio_entry e
;
351 snprintf(path
, sizeof(path
), DB_RD_FILE
, ifname
);
355 if (init_file(path
, sizeof(struct radio_entry
)))
357 fprintf(stderr
, "Failed to init %s: %s\n",
358 path
, strerror(errno
));
364 e
.time
= htonl(time(NULL
));
365 e
.rate
= htons(rate
);
369 return update_file(path
, &e
, sizeof(struct radio_entry
));
372 static int update_cnstat(uint32_t udp
, uint32_t tcp
, uint32_t other
)
379 snprintf(path
, sizeof(path
), DB_CN_FILE
);
383 if (init_file(path
, sizeof(struct conn_entry
)))
385 fprintf(stderr
, "Failed to init %s: %s\n",
386 path
, strerror(errno
));
392 e
.time
= htonl(time(NULL
));
395 e
.other
= htonl(other
);
397 return update_file(path
, &e
, sizeof(struct conn_entry
));
400 static int update_ldstat(uint16_t load1
, uint16_t load5
, uint16_t load15
)
407 snprintf(path
, sizeof(path
), DB_LD_FILE
);
411 if (init_file(path
, sizeof(struct load_entry
)))
413 fprintf(stderr
, "Failed to init %s: %s\n",
414 path
, strerror(errno
));
420 e
.time
= htonl(time(NULL
));
421 e
.load1
= htons(load1
);
422 e
.load5
= htons(load5
);
423 e
.load15
= htons(load15
);
425 return update_file(path
, &e
, sizeof(struct load_entry
));
428 static int run_daemon(void)
431 uint32_t rxb
, txb
, rxp
, txp
;
432 uint32_t udp
, tcp
, other
;
435 float lf1
, lf5
, lf15
;
443 const char *ipc
= stat("/proc/net/nf_conntrack", &s
)
444 ? "/proc/net/ip_conntrack" : "/proc/net/nf_conntrack";
468 /* setup USR1 signal handler to reset timer */
469 sa
.sa_handler
= reset_countdown
;
470 sa
.sa_flags
= SA_RESTART
;
471 sigemptyset(&sa
.sa_mask
);
472 sigaction(SIGUSR1
, &sa
, NULL
);
477 fprintf(stderr
, "Failed to write pid file: %s\n", strerror(errno
));
481 /* initialize iwinfo */
485 for (reset_countdown(0); countdown
>= 0; countdown
--)
487 /* alter progname for ps, top */
488 memset(progname
, 0, prognamelen
);
489 snprintf(progname
, prognamelen
, "luci-bwc %d", countdown
);
491 if ((info
= fopen("/proc/net/dev", "r")) != NULL
)
493 while (fgets(line
, sizeof(line
), info
))
495 if (strchr(line
, '|'))
498 if (sscanf(line
, IF_SCAN_PATTERN
, ifname
, &rxb
, &rxp
, &txb
, &txp
))
500 if (strncmp(ifname
, "lo", sizeof(ifname
)))
501 update_ifstat(ifname
, rxb
, rxp
, txb
, txp
);
510 for (i
= 0; i
< 5; i
++)
512 #define iwinfo_checkif(pattern) \
514 snprintf(ifname, sizeof(ifname), pattern, i); \
515 if (iwinfo_update(iw, ifname, &rate, &rssi, &noise)) \
517 update_radiostat(ifname, rate, rssi, noise); \
522 iwinfo_checkif("wlan%d");
523 iwinfo_checkif("ath%d");
524 iwinfo_checkif("wl%d");
528 if ((info
= fopen(ipc
, "r")) != NULL
)
534 while (fgets(line
, sizeof(line
), info
))
536 if (strstr(line
, "TIME_WAIT"))
539 if (sscanf(line
, "%*s %*d %s", ifname
) || sscanf(line
, "%s %*d", ifname
))
541 if (!strcmp(ifname
, "tcp"))
543 else if (!strcmp(ifname
, "udp"))
550 update_cnstat(udp
, tcp
, other
);
555 if ((info
= fopen("/proc/loadavg", "r")) != NULL
)
557 if (fscanf(info
, LD_SCAN_PATTERN
, &lf1
, &lf5
, &lf15
))
559 update_ldstat((uint16_t)(lf1
* 100),
560 (uint16_t)(lf5
* 100),
561 (uint16_t)(lf15
* 100));
578 static void check_daemon(void)
582 if ((pid
= readpid()) < 0 || kill(pid
, 0) < 0)
584 /* daemon ping failed, try to start it up */
588 "Failed to ping daemon and unable to start it up: %s\n",
594 else if (kill(pid
, SIGUSR1
))
596 fprintf(stderr
, "Failed to send signal: %s\n", strerror(errno
));
601 static int run_dump_ifname(const char *ifname
)
606 struct traffic_entry
*e
;
609 snprintf(path
, sizeof(path
), DB_IF_FILE
, ifname
);
611 if (mmap_file(path
, sizeof(struct traffic_entry
), &m
))
613 fprintf(stderr
, "Failed to open %s: %s\n", path
, strerror(errno
));
617 for (i
= 0; i
< m
.size
; i
+= sizeof(struct traffic_entry
))
619 e
= (struct traffic_entry
*) &m
.mmap
[i
];
624 printf("[ %u, %u, %" PRIu32
627 ntohl(e
->rxb
), ntohl(e
->rxp
),
628 ntohl(e
->txb
), ntohl(e
->txp
),
629 ((i
+ sizeof(struct traffic_entry
)) < m
.size
) ? "," : "");
637 static int run_dump_radio(const char *ifname
)
642 struct radio_entry
*e
;
645 snprintf(path
, sizeof(path
), DB_RD_FILE
, ifname
);
647 if (mmap_file(path
, sizeof(struct radio_entry
), &m
))
649 fprintf(stderr
, "Failed to open %s: %s\n", path
, strerror(errno
));
653 for (i
= 0; i
< m
.size
; i
+= sizeof(struct radio_entry
))
655 e
= (struct radio_entry
*) &m
.mmap
[i
];
660 printf("[ %u, %d, %d, %d ]%s\n",
662 e
->rate
, e
->rssi
, e
->noise
,
663 ((i
+ sizeof(struct radio_entry
)) < m
.size
) ? "," : "");
671 static int run_dump_conns(void)
676 struct conn_entry
*e
;
679 snprintf(path
, sizeof(path
), DB_CN_FILE
);
681 if (mmap_file(path
, sizeof(struct conn_entry
), &m
))
683 fprintf(stderr
, "Failed to open %s: %s\n", path
, strerror(errno
));
687 for (i
= 0; i
< m
.size
; i
+= sizeof(struct conn_entry
))
689 e
= (struct conn_entry
*) &m
.mmap
[i
];
694 printf("[ %u, %u, %u, %u ]%s\n",
695 ntohl(e
->time
), ntohl(e
->udp
),
696 ntohl(e
->tcp
), ntohl(e
->other
),
697 ((i
+ sizeof(struct conn_entry
)) < m
.size
) ? "," : "");
705 static int run_dump_load(void)
710 struct load_entry
*e
;
713 snprintf(path
, sizeof(path
), DB_LD_FILE
);
715 if (mmap_file(path
, sizeof(struct load_entry
), &m
))
717 fprintf(stderr
, "Failed to open %s: %s\n", path
, strerror(errno
));
721 for (i
= 0; i
< m
.size
; i
+= sizeof(struct load_entry
))
723 e
= (struct load_entry
*) &m
.mmap
[i
];
728 printf("[ %u, %u, %u, %u ]%s\n",
730 ntohs(e
->load1
), ntohs(e
->load5
), ntohs(e
->load15
),
731 ((i
+ sizeof(struct load_entry
)) < m
.size
) ? "," : "");
740 int main(int argc
, char *argv
[])
747 for (opt
= 0; opt
< argc
; opt
++)
748 prognamelen
+= 1 + strlen(argv
[opt
]);
750 while ((opt
= getopt(argc
, argv
, "t:i:r:cl")) > -1)
755 timeout
= atoi(optarg
);
760 return run_dump_ifname(optarg
);
765 return run_dump_radio(optarg
);
769 return run_dump_conns();
772 return run_dump_load();
781 " %s [-t timeout] -i ifname\n"
782 " %s [-t timeout] -r radiodev\n"
783 " %s [-t timeout] -c\n"
784 " %s [-t timeout] -l\n",
785 argv
[0], argv
[0], argv
[0], argv
[0]