4 Copyright (C) 2006-2009 Jonathan Zarate
19 static const char *hfn
= "/var/lib/misc/rstats-history.gz";
21 void wo_bwmbackup(char *url
)
27 if (stat(hfn
, &st
) == 0) {
34 killall("rstats", SIGHUP
);
35 for (i
= 10; i
> 0; --i
) {
36 if ((stat(hfn
, &st
) == 0) && (st
.st_mtime
!= t
)) break;
40 send_error(500, NULL
, NULL
);
43 send_header(200, NULL
, mime_binary
, 0);
47 void wi_bwmrestore(char *url
, int len
, char *boundary
)
59 error
= "Error reading file";
62 if (!skip_header(&len
)) {
66 if ((len
< 64) || (len
> 10240)) {
70 if ((buf
= malloc(len
)) == NULL
) {
71 error
= "Not enough memory";
75 n
= web_read(buf
, len
);
78 sprintf(tmp
, "%s.new", hfn
);
79 if (f_write(tmp
, buf
, n
, 0, 0600) != n
) {
81 error
= "Error writing temporary file";
84 f_write("/var/tmp/rstats-load", NULL
, 0, 0, 0600);
85 killall("rstats", SIGHUP
);
89 rboot
= 1; // used as "ok"
94 if (error
!= NULL
) resmsg_set(error
);
97 void wo_bwmrestore(char *url
)
100 redirect("/bwm-daily.asp");
103 parse_asp("error.asp");
107 void asp_netdev(int argc
, char **argv
)
111 unsigned long rx
, tx
;
117 exclude
= nvram_safe_get("rstats_exclude");
118 web_puts("\n\nnetdev={");
119 if ((f
= fopen("/proc/net/dev", "r")) != NULL
) {
120 fgets(buf
, sizeof(buf
), f
); // header
121 fgets(buf
, sizeof(buf
), f
); // "
123 while (fgets(buf
, sizeof(buf
), f
)) {
124 if ((p
= strchr(buf
, ':')) == NULL
) continue;
126 if ((ifname
= strrchr(buf
, ' ')) == NULL
) ifname
= buf
;
128 // if (strncmp(ifname, "ppp", 3) == 0) ifname = "ppp";
129 if ((strcmp(ifname
, "lo") == 0) || (find_word(exclude
, ifname
))) continue;
131 // <rx bytes, packets, errors, dropped, fifo errors, frame errors, compressed, multicast><tx ...>
132 if (sscanf(p
+ 1, "%lu%*u%*u%*u%*u%*u%*u%*u%lu", &rx
, &tx
) != 2) continue;
133 if (!strcmp(ifname
, "imq0"))
134 web_printf("%c'%s':{rx:0x0,tx:0x%lx}", comma
, ifname
, rx
, tx
);
135 else if (!strcmp(ifname
, "imq1"))
136 web_printf("%c'%s':{rx:0x%lx,tx:0x0}", comma
, ifname
, rx
, tx
);
138 web_printf("%c'%s':{rx:0x%lx,tx:0x%lx}", comma
, ifname
, rx
, tx
);
147 void asp_bandwidth(int argc
, char **argv
)
152 if ((nvram_get_int("rstats_enable") == 1) && (argc
== 1)) {
153 if (strcmp(argv
[0], "speed") == 0) {
155 name
= "/var/spool/rstats-speed.js";
159 name
= "/var/spool/rstats-history.js";
162 killall("rstats", sig
);
163 f_wait_exists(name
, 5);