4 Copyright (C) 2006-2009 Jonathan Zarate
19 static const char *hfn
= "/var/lib/misc/rstats-history.gz";
20 static const char *ifn
= "/var/lib/misc/cstats-history.gz";
22 void wo_bwmbackup(char *url
)
28 if (stat(hfn
, &st
) == 0) {
35 killall("rstats", SIGHUP
);
36 for (i
= 10; i
> 0; --i
) {
37 if ((stat(hfn
, &st
) == 0) && (st
.st_mtime
!= t
)) break;
41 send_error(500, NULL
, NULL
);
44 send_header(200, NULL
, mime_binary
, 0);
48 void wo_iptbackup(char *url
)
54 if (stat(ifn
, &st
) == 0) {
61 killall("cstats", SIGHUP
);
62 for (i
= 10; i
> 0; --i
) {
63 if ((stat(ifn
, &st
) == 0) && (st
.st_mtime
!= t
)) break;
67 send_error(500, NULL
, NULL
);
70 send_header(200, NULL
, mime_binary
, 0);
74 void wi_bwmrestore(char *url
, int len
, char *boundary
)
86 error
= "Error reading file";
89 if (!skip_header(&len
)) {
93 if ((len
< 64) || (len
> 10240)) {
97 if ((buf
= malloc(len
)) == NULL
) {
98 error
= "Not enough memory";
102 n
= web_read(buf
, len
);
105 sprintf(tmp
, "%s.new", hfn
);
106 if (f_write(tmp
, buf
, n
, 0, 0600) != n
) {
108 error
= "Error writing temporary file";
111 f_write("/var/tmp/rstats-load", NULL
, 0, 0, 0600);
112 killall("rstats", SIGHUP
);
116 rboot
= 1; // used as "ok"
121 if (error
!= NULL
) resmsg_set(error
);
124 void wi_iptrestore(char *url
, int len
, char *boundary
)
136 error
= "Error reading file";
139 if (!skip_header(&len
)) {
143 if ((len
< 64) || (len
> 10240)) {
147 if ((buf
= malloc(len
)) == NULL
) {
148 error
= "Not enough memory";
152 n
= web_read(buf
, len
);
155 sprintf(tmp
, "%s.new", ifn
);
156 if (f_write(tmp
, buf
, n
, 0, 0600) != n
) {
158 error
= "Error writing temporary file";
161 f_write("/var/tmp/cstats-load", NULL
, 0, 0, 0600);
162 killall("cstats", SIGHUP
);
166 rboot
= 1; // used as "ok"
171 if (error
!= NULL
) resmsg_set(error
);
174 void wo_bwmrestore(char *url
)
177 redirect("/bwm-daily.asp");
180 parse_asp("error.asp");
184 void wo_iptrestore(char *url
)
187 redirect("/ipt-daily.asp");
190 parse_asp("error.asp");
194 void asp_netdev(int argc
, char **argv
)
198 unsigned long rx
, tx
;
204 exclude
= nvram_safe_get("rstats_exclude");
205 web_puts("\n\nnetdev={");
206 if ((f
= fopen("/proc/net/dev", "r")) != NULL
) {
207 fgets(buf
, sizeof(buf
), f
); // header
208 fgets(buf
, sizeof(buf
), f
); // "
210 while (fgets(buf
, sizeof(buf
), f
)) {
211 if ((p
= strchr(buf
, ':')) == NULL
) continue;
213 if ((ifname
= strrchr(buf
, ' ')) == NULL
) ifname
= buf
;
215 // if (strncmp(ifname, "ppp", 3) == 0) ifname = "ppp";
216 if ((strcmp(ifname
, "lo") == 0) || (find_word(exclude
, ifname
))) continue;
218 // <rx bytes, packets, errors, dropped, fifo errors, frame errors, compressed, multicast><tx ...>
219 if (sscanf(p
+ 1, "%lu%*u%*u%*u%*u%*u%*u%*u%lu", &rx
, &tx
) != 2) continue;
220 web_printf("%c'%s':{rx:0x%lx,tx:0x%lx}", comma
, ifname
, rx
, tx
);
228 void asp_climon(int argc
, char **argv
) {
236 unsigned long rx
, tx
;
240 web_puts("\n\nclimon={");
241 if ((to
= popen("iptables -vnxL traffic_out", "r")) != NULL
) {
242 if ((ti
= popen("iptables -vnxL traffic_in", "r")) != NULL
) {
244 while ((fgets(bi
, sizeof(bi
), ti
)) && (fgets(bo
, sizeof(bo
), to
))) {
245 if( (sscanf(bi
, "%*s %lu %*s %*s %*s %*s %*s %s", &rx
, ip
)!=2) ||
246 (sscanf(bo
, "%*s %lu %*s %*s %*s %*s %*s %*s", &tx
)!=1) ) continue;
247 web_printf("%c'%s':{rx:0x%lx,tx:0x%lx}", comma
, ip
, rx
, tx
);
257 void asp_iptmon(int argc
, char **argv
) {
263 char ip
[INET6_ADDRSTRLEN
];
265 unsigned long tx
, rx
;
268 char name
[] = "/proc/net/ipt_account/lanX";
270 web_puts("\n\niptmon={");
273 for(br
=0 ; br
<=3 ; br
++) {
274 char bridge
[2] = "0";
280 sprintf(name
, "/proc/net/ipt_account/lan%s", bridge
);
282 if ((a
= fopen(name
, "r")) == NULL
) continue;
284 // fgets(sa, sizeof(sa), a); // network
285 while (fgets(sa
, sizeof(sa
), a
)) {
287 "ip = %s bytes_src = %lu %*u %*u %*u %*u packets_src = %*u %*u %*u %*u %*u bytes_dest = %lu %*u %*u %*u %*u packets_dest = %*u %*u %*u %*u %*u time = %*u",
288 ip
, &tx
, &rx
) != 3 ) continue;
289 if ((tx
> 0) || (rx
> 0)) {
290 web_printf("%c'%s':{rx:0x%lx,tx:0x%lx}", comma
, ip
, rx
, tx
);
299 void asp_bandwidth(int argc
, char **argv
)
304 if ((nvram_get_int("rstats_enable") == 1) && (argc
== 1)) {
305 if (strcmp(argv
[0], "speed") == 0) {
307 name
= "/var/spool/rstats-speed.js";
311 name
= "/var/spool/rstats-history.js";
314 killall("rstats", sig
);
315 f_wait_exists(name
, 5);
321 void asp_ipt_bandwidth(int argc
, char **argv
)
326 if ((nvram_get_int("cstats_enable") == 1) && (argc
== 1)) {
327 if (strcmp(argv
[0], "speed") == 0) {
329 name
= "/var/spool/cstats-speed.js";
333 name
= "/var/spool/cstats-history.js";
336 killall("cstats", sig
);
337 f_wait_exists(name
, 5);
343 void asp_iptraffic(int argc
, char **argv
) {
351 char ip
[INET6_ADDRSTRLEN
];
353 unsigned long tx_bytes
, rx_bytes
;
354 unsigned long tp_tcp
, rp_tcp
;
355 unsigned long tp_udp
, rp_udp
;
356 unsigned long tp_icmp
, rp_icmp
;
357 unsigned long ct_tcp
, ct_udp
;
360 // needs extra tweaks in the code before this works as it should
361 // so we'll stick to IPv4-only for now...
362 // #if defined(TCONFIG_IPV6) && defined(LINUX26)
363 // const char conntrack[] = "/proc/net/nf_conntrack";
365 const char conntrack
[] = "/proc/net/ip_conntrack";
368 if ((b
= fopen(conntrack
, "r")) == NULL
) return;
371 char name
[] = "/proc/net/ipt_account/lanX";
373 web_puts("\n\niptraffic=[");
376 for(br
=0 ; br
<=3 ; br
++) {
377 char bridge
[2] = "0";
383 sprintf(name
, "/proc/net/ipt_account/lan%s", bridge
);
385 if ((a
= fopen(name
, "r")) == NULL
) continue;
387 fgets(sa
, sizeof(sa
), a
); // network
388 while (fgets(sa
, sizeof(sa
), a
)) {
390 "ip = %s bytes_src = %lu %*u %*u %*u %*u packets_src = %*u %lu %lu %lu %*u bytes_dest = %lu %*u %*u %*u %*u packets_dest = %*u %lu %lu %lu %*u time = %*u",
391 ip
, &tx_bytes
, &tp_tcp
, &tp_udp
, &tp_icmp
, &rx_bytes
, &rp_tcp
, &rp_udp
, &rp_icmp
) != 9 ) continue;
392 if ((tx_bytes
> 0) || (rx_bytes
> 0)) {
396 while (fgets(sb
, sizeof(sb
), b
)) {
397 if ((p
= strstr(sb
, ip
)) == NULL
) continue;
398 if (strncmp(sb
, "tcp", 3) == 0) ct_tcp
++;
399 if (strncmp(sb
, "udp", 3) == 0) ct_udp
++;
401 web_printf("%c['%s', %lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu]",
402 comma
, ip
, tx_bytes
, rx_bytes
, tp_tcp
, rp_tcp
, tp_udp
, rp_udp
, tp_icmp
, rp_icmp
, ct_tcp
, ct_udp
);