modules/admin-full: add SVG based realtime bandwidth status
[project/luci.git] / modules / admin-full / src / luci-bwc.c
1 /*
2  * luci-bwc - Very simple bandwidth collector cache for LuCI realtime graphs
3  *
4  *   Copyright (C) 2010 Jo-Philipp Wich <xm@subsignal.org>
5  *
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdint.h>
23 #include <inttypes.h>
24 #include <fcntl.h>
25 #include <time.h>
26 #include <errno.h>
27 #include <unistd.h>
28
29 #include <sys/stat.h>
30 #include <sys/mman.h>
31 #include <arpa/inet.h>
32
33
34 #define STEP_COUNT      60
35 #define STEP_TIME       1
36
37 #define DB_PATH         "/var/lib/luci-bwc"
38 #define DB_FILE         DB_PATH "/%s"
39
40 #define SCAN_PATTERN \
41         " %[^ :]:%" SCNu64 " %" SCNu64 \
42         " %*d %*d %*d %*d %*d %*d" \
43         " %" SCNu64 " %" SCNu64
44
45
46 struct traffic_entry {
47         uint64_t time;
48         uint64_t rxb;
49         uint64_t rxp;
50         uint64_t txb;
51         uint64_t txp;
52 };
53
54 static uint64_t htonll(uint64_t value)
55 {
56         int num = 1;
57
58         if (*(char *)&num == 1)
59                 return htonl((uint32_t)(value & 0xFFFFFFFF)) |
60                        htonl((uint32_t)(value >> 32));
61
62         return value;
63 }
64
65 #define ntohll htonll
66
67
68 static int init_minutely(const char *ifname)
69 {
70         int i, file;
71         char path[1024];
72         char *p;
73         struct traffic_entry e = { 0 };
74
75         snprintf(path, sizeof(path), DB_FILE, ifname);
76
77         for (p = &path[1]; *p; p++)
78         {
79                 if (*p == '/')
80                 {
81                         *p = 0;
82
83                         if (mkdir(path, 0700) && (errno != EEXIST))
84                                 return -1;
85
86                         *p = '/';
87                 }
88         }
89
90         if ((file = open(path, O_WRONLY | O_CREAT, 0600)) >= 0)
91         {
92                 for (i = 0; i < STEP_COUNT; i++)
93                 {
94                         if (write(file, &e, sizeof(struct traffic_entry)) < 0)
95                                 break;
96                 }
97
98                 close(file);
99
100                 return 0;
101         }
102
103         return -1;
104 }
105
106 static int update_minutely(
107         const char *ifname, uint64_t rxb, uint64_t rxp, uint64_t txb, uint64_t txp
108 ) {
109         int rv = -1;
110
111         int file;
112         int entrysize = sizeof(struct traffic_entry);
113         int mapsize = STEP_COUNT * entrysize;
114
115         char path[1024];
116         char *map;
117
118         struct stat s;
119         struct traffic_entry e;
120
121         snprintf(path, sizeof(path), DB_FILE, ifname);
122
123         if (stat(path, &s))
124         {
125                 if (init_minutely(ifname))
126                 {
127                         fprintf(stderr, "Failed to init %s: %s\n",
128                                         path, strerror(errno));
129
130                         return rv;
131                 }
132         }
133
134         if ((file = open(path, O_RDWR)) >= 0)
135         {
136                 map = mmap(NULL, mapsize, PROT_READ | PROT_WRITE,
137                                    MAP_SHARED | MAP_LOCKED, file, 0);
138
139                 if ((map != NULL) && (map != MAP_FAILED))
140                 {
141                         e.time = htonll(time(NULL));
142                         e.rxb  = htonll(rxb);
143                         e.rxp  = htonll(rxp);
144                         e.txb  = htonll(txb);
145                         e.txp  = htonll(txp);
146
147                         memmove(map, map + entrysize, mapsize - entrysize);
148                         memcpy(map + mapsize - entrysize, &e, entrysize);
149
150                         munmap(map, mapsize);
151
152                         rv = 0;
153                 }
154
155                 close(file);
156         }
157
158         return rv;
159 }
160
161 static int run_daemon(int nofork)
162 {
163         FILE *info;
164         uint64_t rxb, txb, rxp, txp;
165         char line[1024];
166         char ifname[16];
167
168
169         if (!nofork)
170         {
171                 switch (fork())
172                 {
173                         case -1:
174                                 perror("fork()");
175                                 return -1;
176
177                         case 0:
178                                 if (chdir("/") < 0)
179                                 {
180                                         perror("chdir()");
181                                         exit(1);
182                                 }
183
184                                 close(0);
185                                 close(1);
186                                 close(2);
187                                 break;
188
189                         default:
190                                 exit(0);
191                 }
192         }
193
194
195         /* go */
196         while (1)
197         {
198                 if ((info = fopen("/proc/net/dev", "r")) != NULL)
199                 {
200                         while (fgets(line, sizeof(line), info))
201                         {
202                                 if (strchr(line, '|'))
203                                         continue;
204
205                                 if (sscanf(line, SCAN_PATTERN, ifname, &rxb, &rxp, &txb, &txp))
206                                 {
207                                         if (strncmp(ifname, "lo", sizeof(ifname)))
208                                                 update_minutely(ifname, rxb, rxp, txb, txp);
209                                 }
210                         }
211
212                         fclose(info);
213                 }
214
215                 sleep(STEP_TIME);
216         }
217 }
218
219 static int run_dump(const char *ifname)
220 {
221         int rv = 1;
222
223         int i, file;
224         int entrysize = sizeof(struct traffic_entry);
225         int mapsize = STEP_COUNT * entrysize;
226
227         char path[1024];
228         char *map;
229
230         struct traffic_entry *e;
231
232         snprintf(path, sizeof(path), DB_FILE, ifname);
233
234         if ((file = open(path, O_RDONLY)) >= 0)
235         {
236                 map = mmap(NULL, mapsize, PROT_READ, MAP_SHARED | MAP_LOCKED, file, 0);
237
238                 if ((map != NULL) && (map != MAP_FAILED))
239                 {
240                         for (i = 0; i < mapsize; i += entrysize)
241                         {
242                                 e = (struct traffic_entry *) &map[i];
243
244                                 if (!e->time)
245                                         continue;
246
247                                 printf("[ %" PRIu64 ", %" PRIu64 ", %" PRIu64
248                                            ", %" PRIu64 ", %" PRIu64 " ]%s\n",
249                                         ntohll(e->time),
250                                         ntohll(e->rxb), ntohll(e->rxp),
251                                         ntohll(e->txb), ntohll(e->txp),
252                                         ((i + entrysize) < mapsize) ? "," : "");
253                         }
254
255                         munmap(map, mapsize);
256                         rv = 0;
257                 }
258
259                 close(file);
260         }
261         else
262         {
263                 fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
264         }
265
266         return rv;
267 }
268
269
270 int main(int argc, char *argv[])
271 {
272         int opt;
273         int daemon = 0;
274         int nofork = 0;
275         int dprint = 0;
276         char *ifname = NULL;
277
278         while ((opt = getopt(argc, argv, "dfp:")) > -1)
279         {
280                 switch (opt)
281                 {
282                         case 'd':
283                                 daemon = 1;
284                                 break;
285
286                         case 'f':
287                                 nofork = 1;
288                                 break;
289
290                         case 'p':
291                                 dprint = 1;
292                                 ifname = optarg;
293                                 break;
294
295                         default:
296                                 break;
297                 }
298         }
299
300         if (daemon)
301                 return run_daemon(nofork);
302
303         else if (dprint && ifname)
304                 return run_dump(ifname);
305
306         else
307                 fprintf(stderr,
308                         "Usage:\n"
309                         "       %s -d [-f]\n"
310                         "       %s -p ifname\n",
311                                 argv[0], argv[0]
312                 );
313
314         return 1;
315 }