4 Copyright (C) 2006-2009 Jonathan Zarate
11 #include <sys/ioctl.h>
20 static const char *hfn
= "/var/lib/misc/rstats-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 wi_bwmrestore(char *url
, int len
, char *boundary
)
60 error
= "Error reading file";
63 if (!skip_header(&len
)) {
67 if ((len
< 64) || (len
> 10240)) {
71 if ((buf
= malloc(len
)) == NULL
) {
72 error
= "Not enough memory";
76 n
= web_read(buf
, len
);
79 sprintf(tmp
, "%s.new", hfn
);
80 if (f_write(tmp
, buf
, n
, 0, 0600) != n
) {
82 error
= "Error writing temporary file";
85 f_write("/var/tmp/rstats-load", NULL
, 0, 0, 0600);
86 killall("rstats", SIGHUP
);
90 rboot
= 1; // used as "ok"
95 if (error
!= NULL
) resmsg_set(error
);
98 void wo_bwmrestore(char *url
)
101 redirect("/bwm-daily.asp");
104 parse_asp("error.asp");
108 void asp_netdev(int argc
, char **argv
)
112 unsigned long rx
, tx
;
120 exclude
= nvram_safe_get("rstats_exclude");
121 web_puts("\n\nnetdev={");
122 if ((f
= fopen("/proc/net/dev", "r")) != NULL
) {
123 fgets(buf
, sizeof(buf
), f
); // header
124 fgets(buf
, sizeof(buf
), f
); // "
127 if ((sfd
= socket(AF_INET
, SOCK_RAW
, IPPROTO_RAW
)) < 0) {
128 _dprintf("[%s %d]: error opening socket %m\n", __FUNCTION__
, __LINE__
);
131 while (fgets(buf
, sizeof(buf
), f
)) {
132 if ((p
= strchr(buf
, ':')) == NULL
) continue;
134 if ((ifname
= strrchr(buf
, ' ')) == NULL
) ifname
= buf
;
136 // if (strncmp(ifname, "ppp", 3) == 0) ifname = "ppp";
137 if ((strcmp(ifname
, "lo") == 0) || (find_word(exclude
, ifname
))) continue;
139 // skip down interfaces
141 strcpy(ifr
.ifr_name
, ifname
);
142 if (ioctl(sfd
, SIOCGIFFLAGS
, &ifr
) != 0) continue;
143 if ((ifr
.ifr_flags
& IFF_UP
) == 0) continue;
146 // <rx bytes, packets, errors, dropped, fifo errors, frame errors, compressed, multicast><tx ...>
147 if (sscanf(p
+ 1, "%lu%*u%*u%*u%*u%*u%*u%*u%lu", &rx
, &tx
) != 2) continue;
148 if (!strcmp(ifname
, "imq0"))
149 web_printf("%c'%s':{rx:0x0,tx:0x%lx}", comma
, ifname
, rx
, tx
);
150 else if (!strcmp(ifname
, "imq1"))
151 web_printf("%c'%s':{rx:0x%lx,tx:0x0}", comma
, ifname
, rx
, tx
);
153 web_printf("%c'%s':{rx:0x%lx,tx:0x%lx}", comma
, ifname
, rx
, tx
);
158 if (sfd
>= 0) close(sfd
);
164 void asp_bandwidth(int argc
, char **argv
)
169 if ((nvram_get_int("rstats_enable") == 1) && (argc
== 1)) {
170 if (strcmp(argv
[0], "speed") == 0) {
172 name
= "/var/spool/rstats-speed.js";
176 name
= "/var/spool/rstats-history.js";
179 killall("rstats", sig
);
180 f_wait_exists(name
, 5);