cstats fixes, improvements on IP Traffic and QoS pages
[tomato.git] / release / src / router / httpd / bwm.c
blob4a3128d2f44f7a506f2406753145a2e7124788bd
1 /*
3 Tomato Firmware
4 Copyright (C) 2006-2009 Jonathan Zarate
6 */
8 #include "tomato.h"
10 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <errno.h>
15 #include <sys/wait.h>
16 #include <typedefs.h>
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)
24 struct stat st;
25 time_t t;
26 int i;
28 if (stat(hfn, &st) == 0) {
29 t = st.st_mtime;
30 sleep(1);
32 else {
33 t = 0;
35 killall("rstats", SIGHUP);
36 for (i = 10; i > 0; --i) {
37 if ((stat(hfn, &st) == 0) && (st.st_mtime != t)) break;
38 sleep(1);
40 if (i == 0) {
41 send_error(500, NULL, NULL);
42 return;
44 send_header(200, NULL, mime_binary, 0);
45 do_file((char *)hfn);
48 void wo_iptbackup(char *url)
50 struct stat st;
51 time_t t;
52 int i;
54 if (stat(ifn, &st) == 0) {
55 t = st.st_mtime;
56 sleep(1);
58 else {
59 t = 0;
61 killall("cstats", SIGHUP);
62 for (i = 10; i > 0; --i) {
63 if ((stat(ifn, &st) == 0) && (st.st_mtime != t)) break;
64 sleep(1);
66 if (i == 0) {
67 send_error(500, NULL, NULL);
68 return;
70 send_header(200, NULL, mime_binary, 0);
71 do_file((char *)ifn);
74 void wi_bwmrestore(char *url, int len, char *boundary)
76 char *buf;
77 const char *error;
78 int ok;
79 int n;
80 char tmp[64];
82 check_id(url);
84 tmp[0] = 0;
85 buf = NULL;
86 error = "Error reading file";
87 ok = 0;
89 if (!skip_header(&len)) {
90 goto ERROR;
93 if ((len < 64) || (len > 10240)) {
94 goto ERROR;
97 if ((buf = malloc(len)) == NULL) {
98 error = "Not enough memory";
99 goto ERROR;
102 n = web_read(buf, len);
103 len -= n;
105 sprintf(tmp, "%s.new", hfn);
106 if (f_write(tmp, buf, n, 0, 0600) != n) {
107 unlink(tmp);
108 error = "Error writing temporary file";
109 goto ERROR;
111 f_write("/var/tmp/rstats-load", NULL, 0, 0, 0600);
112 killall("rstats", SIGHUP);
113 sleep(1);
115 error = NULL;
116 rboot = 1; // used as "ok"
118 ERROR:
119 free(buf);
120 web_eat(len);
121 if (error != NULL) resmsg_set(error);
124 void wi_iptrestore(char *url, int len, char *boundary)
126 char *buf;
127 const char *error;
128 int ok;
129 int n;
130 char tmp[64];
132 check_id(url);
134 tmp[0] = 0;
135 buf = NULL;
136 error = "Error reading file";
137 ok = 0;
139 if (!skip_header(&len)) {
140 goto ERROR;
143 if ((len < 64) || (len > 10240)) {
144 goto ERROR;
147 if ((buf = malloc(len)) == NULL) {
148 error = "Not enough memory";
149 goto ERROR;
152 n = web_read(buf, len);
153 len -= n;
155 sprintf(tmp, "%s.new", ifn);
156 if (f_write(tmp, buf, n, 0, 0600) != n) {
157 unlink(tmp);
158 error = "Error writing temporary file";
159 goto ERROR;
161 f_write("/var/tmp/cstats-load", NULL, 0, 0, 0600);
162 killall("cstats", SIGHUP);
163 sleep(1);
165 error = NULL;
166 rboot = 1; // used as "ok"
168 ERROR:
169 free(buf);
170 web_eat(len);
171 if (error != NULL) resmsg_set(error);
174 void wo_bwmrestore(char *url)
176 if (rboot) {
177 redirect("/bwm-daily.asp");
179 else {
180 parse_asp("error.asp");
184 void wo_iptrestore(char *url)
186 if (rboot) {
187 redirect("/ipt-daily.asp");
189 else {
190 parse_asp("error.asp");
194 void asp_netdev(int argc, char **argv)
196 FILE *f;
197 char buf[256];
198 unsigned long rx, tx;
199 char *p;
200 char *ifname;
201 char comma;
202 char *exclude;
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); // "
209 comma = ' ';
210 while (fgets(buf, sizeof(buf), f)) {
211 if ((p = strchr(buf, ':')) == NULL) continue;
212 *p = 0;
213 if ((ifname = strrchr(buf, ' ')) == NULL) ifname = buf;
214 else ++ifname;
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);
221 comma = ',';
223 fclose(f);
225 web_puts("};\n");
228 void asp_iptmon(int argc, char **argv) {
230 char comma;
231 char sa[256];
232 FILE *a;
234 char ip[INET6_ADDRSTRLEN];
236 unsigned long tx, rx;
238 char br;
239 char name[] = "/proc/net/ipt_account/lanX";
241 web_puts("\n\niptmon={");
242 comma = ' ';
244 for(br=0 ; br<=3 ; br++) {
245 char bridge[2] = "0";
246 if (br!=0)
247 bridge[0]+=br;
248 else
249 strcpy(bridge, "");
251 sprintf(name, "/proc/net/ipt_account/lan%s", bridge);
253 if ((a = fopen(name, "r")) == NULL) continue;
255 // fgets(sa, sizeof(sa), a); // network
256 while (fgets(sa, sizeof(sa), a)) {
257 if(sscanf(sa,
258 "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",
259 ip, &tx, &rx) != 3 ) continue;
260 if ((tx > 0) || (rx > 0)) {
261 web_printf("%c'%s':{rx:0x%lx,tx:0x%lx}", comma, ip, rx, tx);
262 comma = ',';
265 fclose(a);
267 web_puts("};\n");
270 void asp_bandwidth(int argc, char **argv)
272 char *name;
273 int sig;
275 if ((nvram_get_int("rstats_enable") == 1) && (argc == 1)) {
276 if (strcmp(argv[0], "speed") == 0) {
277 sig = SIGUSR1;
278 name = "/var/spool/rstats-speed.js";
280 else {
281 sig = SIGUSR2;
282 name = "/var/spool/rstats-history.js";
284 unlink(name);
285 killall("rstats", sig);
286 f_wait_exists(name, 5);
287 do_file(name);
288 unlink(name);
292 void asp_ipt_bandwidth(int argc, char **argv)
294 char *name;
295 int sig;
297 if ((nvram_get_int("cstats_enable") == 1) && (argc == 1)) {
298 if (strcmp(argv[0], "speed") == 0) {
299 sig = SIGUSR1;
300 name = "/var/spool/cstats-speed.js";
302 else {
303 sig = SIGUSR2;
304 name = "/var/spool/cstats-history.js";
306 unlink(name);
307 killall("cstats", sig);
308 f_wait_exists(name, 5);
309 do_file(name);
310 unlink(name);
314 void asp_iptraffic(int argc, char **argv) {
316 char comma;
317 char sa[256];
318 char sb[256];
319 FILE *a;
320 FILE *b;
321 char *p;
322 char ip[INET6_ADDRSTRLEN];
324 unsigned long tx_bytes, rx_bytes;
325 unsigned long tp_tcp, rp_tcp;
326 unsigned long tp_udp, rp_udp;
327 unsigned long tp_icmp, rp_icmp;
328 unsigned long ct_tcp, ct_udp;
331 // needs extra tweaks in the code before this works as it should
332 // so we'll stick to IPv4-only for now...
333 // #if defined(TCONFIG_IPV6) && defined(LINUX26)
334 // const char conntrack[] = "/proc/net/nf_conntrack";
335 // #else
336 const char conntrack[] = "/proc/net/ip_conntrack";
337 // #endif
339 if ((b = fopen(conntrack, "r")) == NULL) return;
341 char br;
342 char name[] = "/proc/net/ipt_account/lanX";
344 web_puts("\n\niptraffic=[");
345 comma = ' ';
347 for(br=0 ; br<=3 ; br++) {
348 char bridge[2] = "0";
349 if (br!=0)
350 bridge[0]+=br;
351 else
352 strcpy(bridge, "");
354 sprintf(name, "/proc/net/ipt_account/lan%s", bridge);
356 if ((a = fopen(name, "r")) == NULL) continue;
358 fgets(sa, sizeof(sa), a); // network
359 while (fgets(sa, sizeof(sa), a)) {
360 if(sscanf(sa,
361 "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",
362 ip, &tx_bytes, &tp_tcp, &tp_udp, &tp_icmp, &rx_bytes, &rp_tcp, &rp_udp, &rp_icmp) != 9 ) continue;
363 if ((tx_bytes > 0) || (rx_bytes > 0)) {
364 rewind(b);
365 ct_tcp = 0;
366 ct_udp = 0;
367 while (fgets(sb, sizeof(sb), b)) {
368 if ((p = strstr(sb, ip)) == NULL) continue;
369 if (strncmp(sb, "tcp", 3) == 0) ct_tcp++;
370 if (strncmp(sb, "udp", 3) == 0) ct_udp++;
372 web_printf("%c['%s', %lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu]",
373 comma, ip, tx_bytes, rx_bytes, tp_tcp, rp_tcp, tp_udp, rp_udp, tp_icmp, rp_icmp, ct_tcp, ct_udp);
374 comma = ',';
377 fclose(a);
379 web_puts("];\n");