]>
Commit | Line | Data |
---|---|---|
aba5acdf SH |
1 | /* |
2 | * nstat.c handy utility to read counters /proc/net/netstat and snmp | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public License | |
6 | * as published by the Free Software Foundation; either version | |
7 | * 2 of the License, or (at your option) any later version. | |
8 | * | |
9 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> | |
10 | */ | |
11 | ||
12 | #include <stdio.h> | |
13 | #include <stdlib.h> | |
14 | #include <unistd.h> | |
15 | #include <fcntl.h> | |
16 | #include <string.h> | |
17 | #include <errno.h> | |
18 | #include <time.h> | |
19 | #include <sys/time.h> | |
20 | #include <fnmatch.h> | |
21 | #include <sys/file.h> | |
22 | #include <sys/socket.h> | |
23 | #include <sys/un.h> | |
24 | #include <sys/poll.h> | |
25 | #include <sys/wait.h> | |
26 | #include <sys/stat.h> | |
27 | #include <signal.h> | |
28 | #include <math.h> | |
29 | ||
30 | #include <SNAPSHOT.h> | |
31 | ||
32 | int dump_zeros = 0; | |
33 | int reset_history = 0; | |
34 | int ignore_history = 0; | |
35 | int no_output = 0; | |
36 | int no_update = 0; | |
37 | int scan_interval = 0; | |
38 | int time_constant = 0; | |
39 | double W; | |
40 | char **patterns; | |
41 | int npatterns; | |
42 | ||
43 | char info_source[128]; | |
44 | int source_mismatch; | |
45 | ||
46 | int generic_proc_open(char *env, char *name) | |
47 | { | |
48 | char store[128]; | |
49 | char *p = getenv(env); | |
50 | if (!p) { | |
51 | p = getenv("PROC_ROOT") ? : "/proc"; | |
52 | snprintf(store, sizeof(store)-1, "%s/%s", p, name); | |
53 | p = store; | |
54 | } | |
55 | return open(store, O_RDONLY); | |
56 | } | |
57 | ||
58 | int net_netstat_open(void) | |
59 | { | |
60 | return generic_proc_open("PROC_NET_NETSTAT", "net/netstat"); | |
61 | } | |
62 | ||
63 | int net_snmp_open(void) | |
64 | { | |
65 | return generic_proc_open("PROC_NET_SNMP", "net/snmp"); | |
66 | } | |
67 | ||
68 | int net_snmp6_open(void) | |
69 | { | |
70 | return generic_proc_open("PROC_NET_SNMP6", "net/snmp6"); | |
71 | } | |
72 | ||
73 | struct nstat_ent | |
74 | { | |
75 | struct nstat_ent *next; | |
76 | char *id; | |
77 | unsigned long long val; | |
78 | unsigned long ival; | |
79 | double rate; | |
80 | }; | |
81 | ||
82 | struct nstat_ent *kern_db; | |
83 | struct nstat_ent *hist_db; | |
84 | ||
85 | char *useless_numbers[] = { | |
86 | "IpForwarding", "IpDefaultTTL", | |
87 | "TcpRtoAlgorithm", "TcpRtoMin", "TcpRtoMax", | |
88 | "TcpMaxConn", "TcpCurrEstab" | |
89 | }; | |
90 | ||
91 | int useless_number(char *id) | |
92 | { | |
93 | int i; | |
94 | for (i=0; i<sizeof(useless_numbers)/sizeof(*useless_numbers); i++) | |
95 | if (strcmp(id, useless_numbers[i]) == 0) | |
96 | return 1; | |
97 | return 0; | |
98 | } | |
99 | ||
100 | int match(char *id) | |
101 | { | |
102 | int i; | |
103 | ||
104 | if (npatterns == 0) | |
105 | return 1; | |
106 | ||
107 | for (i=0; i<npatterns; i++) { | |
108 | if (!fnmatch(patterns[i], id, 0)) | |
109 | return 1; | |
110 | } | |
111 | return 0; | |
112 | } | |
113 | ||
114 | void load_good_table(FILE *fp) | |
115 | { | |
116 | char buf[4096]; | |
117 | struct nstat_ent *db = NULL; | |
118 | struct nstat_ent *n; | |
119 | ||
120 | while (fgets(buf, sizeof(buf), fp) != NULL) { | |
121 | int nr; | |
122 | unsigned long long val; | |
123 | double rate; | |
b482ffa6 | 124 | char idbuf[sizeof(buf)]; |
aba5acdf SH |
125 | if (buf[0] == '#') { |
126 | buf[strlen(buf)-1] = 0; | |
127 | if (info_source[0] && strcmp(info_source, buf+1)) | |
128 | source_mismatch = 1; | |
b482ffa6 | 129 | info_source[0] = 0; |
130 | strncat(info_source, buf+1, sizeof(info_source)-1); | |
aba5acdf SH |
131 | continue; |
132 | } | |
b482ffa6 | 133 | /* idbuf is as big as buf, so this is safe */ |
aba5acdf SH |
134 | nr = sscanf(buf, "%s%llu%lg", idbuf, &val, &rate); |
135 | if (nr < 2) | |
136 | abort(); | |
137 | if (nr < 3) | |
138 | rate = 0; | |
139 | if (useless_number(idbuf)) | |
140 | continue; | |
141 | if ((n = malloc(sizeof(*n))) == NULL) | |
142 | abort(); | |
143 | n->id = strdup(idbuf); | |
144 | n->ival = (unsigned long)val; | |
145 | n->val = val; | |
146 | n->rate = rate; | |
147 | n->next = db; | |
148 | db = n; | |
149 | } | |
150 | ||
151 | while (db) { | |
152 | n = db; | |
153 | db = db->next; | |
154 | n->next = kern_db; | |
155 | kern_db = n; | |
156 | } | |
157 | } | |
158 | ||
159 | ||
160 | void load_ugly_table(FILE *fp) | |
161 | { | |
162 | char buf[4096]; | |
163 | struct nstat_ent *db = NULL; | |
164 | struct nstat_ent *n; | |
165 | ||
166 | while (fgets(buf, sizeof(buf), fp) != NULL) { | |
b482ffa6 | 167 | char idbuf[sizeof(buf)]; |
aba5acdf SH |
168 | int off; |
169 | char *p; | |
170 | ||
171 | p = strchr(buf, ':'); | |
172 | if (!p) | |
173 | abort(); | |
174 | *p = 0; | |
b482ffa6 | 175 | idbuf[0] = 0; |
176 | strncat(idbuf, buf, sizeof(idbuf) - 1); | |
177 | off = p - buf; | |
aba5acdf SH |
178 | p += 2; |
179 | ||
180 | while (*p) { | |
181 | char *next; | |
182 | if ((next = strchr(p, ' ')) != NULL) | |
183 | *next++ = 0; | |
184 | else if ((next = strchr(p, '\n')) != NULL) | |
185 | *next++ = 0; | |
b482ffa6 | 186 | if (off < sizeof(idbuf)) { |
187 | idbuf[off] = 0; | |
188 | strncat(idbuf, p, sizeof(idbuf) - off - 1); | |
189 | } | |
aba5acdf SH |
190 | n = malloc(sizeof(*n)); |
191 | if (!n) | |
192 | abort(); | |
193 | n->id = strdup(idbuf); | |
194 | n->rate = 0; | |
195 | n->next = db; | |
196 | db = n; | |
197 | p = next; | |
198 | } | |
199 | n = db; | |
200 | if (fgets(buf, sizeof(buf), fp) == NULL) | |
201 | abort(); | |
202 | do { | |
203 | p = strrchr(buf, ' '); | |
204 | if (!p) | |
205 | abort(); | |
206 | *p = 0; | |
207 | if (sscanf(p+1, "%lu", &n->ival) != 1) | |
208 | abort(); | |
209 | n->val = n->ival; | |
210 | /* Trick to skip "dummy" trailing ICMP MIB in 2.4 */ | |
211 | if (strcmp(idbuf, "IcmpOutAddrMaskReps") == 0) | |
212 | idbuf[5] = 0; | |
213 | else | |
214 | n = n->next; | |
215 | } while (p > buf + off + 2); | |
216 | } | |
217 | ||
218 | while (db) { | |
219 | n = db; | |
220 | db = db->next; | |
221 | if (useless_number(n->id)) { | |
222 | free(n->id); | |
223 | free(n); | |
224 | } else { | |
225 | n->next = kern_db; | |
226 | kern_db = n; | |
227 | } | |
228 | } | |
229 | } | |
230 | ||
231 | void load_snmp(void) | |
232 | { | |
233 | FILE *fp = fdopen(net_snmp_open(), "r"); | |
234 | if (fp) { | |
235 | load_ugly_table(fp); | |
236 | fclose(fp); | |
237 | } | |
238 | } | |
239 | ||
240 | void load_snmp6(void) | |
241 | { | |
242 | FILE *fp = fdopen(net_snmp6_open(), "r"); | |
243 | if (fp) { | |
244 | load_good_table(fp); | |
245 | fclose(fp); | |
246 | } | |
247 | } | |
248 | ||
249 | void load_netstat(void) | |
250 | { | |
251 | FILE *fp = fdopen(net_netstat_open(), "r"); | |
252 | if (fp) { | |
253 | load_ugly_table(fp); | |
254 | fclose(fp); | |
255 | } | |
256 | } | |
257 | ||
258 | void dump_kern_db(FILE *fp, int to_hist) | |
259 | { | |
260 | struct nstat_ent *n, *h; | |
261 | h = hist_db; | |
262 | fprintf(fp, "#%s\n", info_source); | |
263 | for (n=kern_db; n; n=n->next) { | |
264 | unsigned long long val = n->val; | |
265 | if (!dump_zeros && !val && !n->rate) | |
266 | continue; | |
267 | if (!match(n->id)) { | |
268 | struct nstat_ent *h1; | |
269 | if (!to_hist) | |
270 | continue; | |
271 | for (h1 = h; h1; h1 = h1->next) { | |
272 | if (strcmp(h1->id, n->id) == 0) { | |
273 | val = h1->val; | |
274 | h = h1->next; | |
275 | break; | |
276 | } | |
277 | } | |
278 | } | |
279 | fprintf(fp, "%-32s%-16llu%6.1f\n", n->id, val, n->rate); | |
280 | } | |
281 | } | |
282 | ||
283 | void dump_incr_db(FILE *fp) | |
284 | { | |
285 | struct nstat_ent *n, *h; | |
286 | h = hist_db; | |
287 | fprintf(fp, "#%s\n", info_source); | |
288 | for (n=kern_db; n; n=n->next) { | |
289 | int ovfl = 0; | |
290 | unsigned long long val = n->val; | |
291 | struct nstat_ent *h1; | |
292 | for (h1 = h; h1; h1 = h1->next) { | |
293 | if (strcmp(h1->id, n->id) == 0) { | |
294 | if (val < h1->val) { | |
295 | ovfl = 1; | |
296 | val = h1->val; | |
297 | } | |
298 | val -= h1->val; | |
299 | h = h1->next; | |
300 | break; | |
301 | } | |
302 | } | |
303 | if (!dump_zeros && !val && !n->rate) | |
304 | continue; | |
305 | if (!match(n->id)) | |
306 | continue; | |
307 | fprintf(fp, "%-32s%-16llu%6.1f%s\n", n->id, val, | |
308 | n->rate, ovfl?" (overflow)":""); | |
309 | } | |
310 | } | |
311 | ||
312 | static int children; | |
313 | ||
314 | void sigchild(int signo) | |
315 | { | |
316 | } | |
317 | ||
318 | void update_db(int interval) | |
319 | { | |
320 | struct nstat_ent *n, *h; | |
321 | ||
322 | n = kern_db; | |
323 | kern_db = NULL; | |
324 | ||
325 | load_netstat(); | |
326 | load_snmp6(); | |
327 | load_snmp(); | |
328 | ||
329 | h = kern_db; | |
330 | kern_db = n; | |
331 | ||
332 | for (n = kern_db; n; n = n->next) { | |
333 | struct nstat_ent *h1; | |
334 | for (h1 = h; h1; h1 = h1->next) { | |
335 | if (strcmp(h1->id, n->id) == 0) { | |
336 | double sample; | |
337 | unsigned long incr = h1->ival - n->ival; | |
338 | n->val += incr; | |
339 | n->ival = h1->ival; | |
340 | sample = (double)(incr*1000)/interval; | |
341 | if (interval >= scan_interval) { | |
342 | n->rate += W*(sample-n->rate); | |
343 | } else if (interval >= 1000) { | |
344 | if (interval >= time_constant) { | |
345 | n->rate = sample; | |
346 | } else { | |
347 | double w = W*(double)interval/scan_interval; | |
348 | n->rate += w*(sample-n->rate); | |
349 | } | |
350 | } | |
351 | ||
352 | while (h != h1) { | |
353 | struct nstat_ent *tmp = h; | |
354 | h = h->next; | |
355 | free(tmp->id); | |
356 | free(tmp); | |
357 | }; | |
358 | h = h1->next; | |
359 | free(h1->id); | |
360 | free(h1); | |
361 | break; | |
362 | } | |
363 | } | |
364 | } | |
365 | } | |
366 | ||
367 | #define T_DIFF(a,b) (((a).tv_sec-(b).tv_sec)*1000 + ((a).tv_usec-(b).tv_usec)/1000) | |
368 | ||
369 | ||
370 | void server_loop(int fd) | |
371 | { | |
372 | struct timeval snaptime; | |
373 | struct pollfd p; | |
374 | p.fd = fd; | |
375 | p.events = p.revents = POLLIN; | |
376 | ||
377 | sprintf(info_source, "%d.%lu sampling_interval=%d time_const=%d", | |
378 | getpid(), (unsigned long)random(), scan_interval/1000, time_constant/1000); | |
379 | ||
380 | load_netstat(); | |
381 | load_snmp6(); | |
382 | load_snmp(); | |
383 | ||
384 | for (;;) { | |
385 | int status; | |
386 | int tdiff; | |
387 | struct timeval now; | |
388 | gettimeofday(&now, NULL); | |
389 | tdiff = T_DIFF(now, snaptime); | |
390 | if (tdiff >= scan_interval) { | |
391 | update_db(tdiff); | |
392 | snaptime = now; | |
393 | tdiff = 0; | |
394 | } | |
395 | if (poll(&p, 1, tdiff + scan_interval) > 0 | |
396 | && (p.revents&POLLIN)) { | |
397 | int clnt = accept(fd, NULL, NULL); | |
398 | if (clnt >= 0) { | |
399 | pid_t pid; | |
400 | if (children >= 5) { | |
401 | close(clnt); | |
402 | } else if ((pid = fork()) != 0) { | |
403 | if (pid>0) | |
404 | children++; | |
405 | close(clnt); | |
406 | } else { | |
407 | FILE *fp = fdopen(clnt, "w"); | |
408 | if (fp) { | |
409 | if (tdiff > 0) | |
410 | update_db(tdiff); | |
411 | dump_kern_db(fp, 0); | |
412 | } | |
413 | exit(0); | |
414 | } | |
415 | } | |
416 | } | |
417 | while (children && waitpid(-1, &status, WNOHANG) > 0) | |
418 | children--; | |
419 | } | |
420 | } | |
421 | ||
422 | int verify_forging(int fd) | |
423 | { | |
424 | struct ucred cred; | |
425 | int olen = sizeof(cred); | |
426 | if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, (void*)&cred, &olen) || | |
427 | olen < sizeof(cred)) | |
428 | return -1; | |
429 | if (cred.uid == getuid() || cred.uid == 0) | |
430 | return 0; | |
431 | return -1; | |
432 | } | |
433 | ||
434 | static void usage(void) __attribute__((noreturn)); | |
435 | ||
436 | static void usage(void) | |
437 | { | |
438 | fprintf(stderr, | |
439 | "Usage: nstat [ -h?vVzrnasd:t: ] [ PATTERN [ PATTERN ] ]\n" | |
440 | ); | |
441 | exit(-1); | |
442 | } | |
443 | ||
444 | ||
445 | int main(int argc, char *argv[]) | |
446 | { | |
447 | char hist_name[128]; | |
448 | struct sockaddr_un sun; | |
449 | FILE *hist_fp = NULL; | |
450 | int ch; | |
451 | int fd; | |
452 | ||
453 | while ((ch = getopt(argc, argv, "h?vVzrnasd:t:")) != EOF) { | |
454 | switch(ch) { | |
455 | case 'z': | |
456 | dump_zeros = 1; | |
457 | break; | |
458 | case 'r': | |
459 | reset_history = 1; | |
460 | break; | |
461 | case 'a': | |
462 | ignore_history = 1; | |
463 | break; | |
464 | case 's': | |
465 | no_update = 1; | |
466 | break; | |
467 | case 'n': | |
468 | no_output = 1; | |
469 | break; | |
470 | case 'd': | |
471 | scan_interval = 1000*atoi(optarg); | |
472 | break; | |
473 | case 't': | |
474 | if (sscanf(optarg, "%d", &time_constant) != 1 || | |
475 | time_constant <= 0) { | |
476 | fprintf(stderr, "nstat: invalid time constant divisor\n"); | |
477 | exit(-1); | |
478 | } | |
479 | break; | |
480 | case 'v': | |
481 | case 'V': | |
482 | printf("nstat utility, iproute2-ss%s\n", SNAPSHOT); | |
483 | exit(0); | |
484 | case 'h': | |
485 | case '?': | |
486 | default: | |
487 | usage(); | |
488 | } | |
489 | } | |
490 | ||
491 | argc -= optind; | |
492 | argv += optind; | |
493 | ||
494 | sun.sun_family = AF_UNIX; | |
495 | sun.sun_path[0] = 0; | |
496 | sprintf(sun.sun_path+1, "nstat%d", getuid()); | |
497 | ||
498 | if (scan_interval > 0) { | |
499 | if (time_constant == 0) | |
500 | time_constant = 60; | |
501 | time_constant *= 1000; | |
502 | W = 1 - 1/exp(log(10)*(double)scan_interval/time_constant); | |
503 | if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { | |
504 | perror("nstat: socket"); | |
505 | exit(-1); | |
506 | } | |
507 | if (bind(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) < 0) { | |
508 | perror("nstat: bind"); | |
509 | exit(-1); | |
510 | } | |
511 | if (listen(fd, 5) < 0) { | |
512 | perror("nstat: listen"); | |
513 | exit(-1); | |
514 | } | |
515 | if (fork()) | |
516 | exit(0); | |
517 | chdir("/"); | |
518 | close(0); close(1); close(2); setsid(); | |
519 | signal(SIGPIPE, SIG_IGN); | |
520 | signal(SIGCHLD, sigchild); | |
521 | server_loop(fd); | |
522 | exit(0); | |
523 | } | |
524 | ||
525 | patterns = argv; | |
526 | npatterns = argc; | |
527 | ||
528 | if (getenv("NSTAT_HISTORY")) | |
529 | snprintf(hist_name, sizeof(hist_name), getenv("NSTAT_HISTORY")); | |
530 | else | |
531 | sprintf(hist_name, "/tmp/.nstat.u%d", getuid()); | |
532 | ||
533 | if (reset_history) | |
534 | unlink(hist_name); | |
535 | ||
536 | if (!ignore_history || !no_update) { | |
537 | struct stat stb; | |
538 | ||
539 | fd = open(hist_name, O_RDWR|O_CREAT|O_NOFOLLOW, 0600); | |
540 | if (fd < 0) { | |
541 | perror("nstat: open history file"); | |
542 | exit(-1); | |
543 | } | |
544 | if ((hist_fp = fdopen(fd, "r+")) == NULL) { | |
545 | perror("nstat: fdopen history file"); | |
546 | exit(-1); | |
547 | } | |
548 | if (flock(fileno(hist_fp), LOCK_EX)) { | |
549 | perror("nstat: flock history file"); | |
550 | exit(-1); | |
551 | } | |
552 | if (fstat(fileno(hist_fp), &stb) != 0) { | |
553 | perror("nstat: fstat history file"); | |
554 | exit(-1); | |
555 | } | |
556 | if (stb.st_nlink != 1 || stb.st_uid != getuid()) { | |
557 | fprintf(stderr, "nstat: something is so wrong with history file, that I prefer not to proceed.\n"); | |
558 | exit(-1); | |
559 | } | |
560 | if (!ignore_history) { | |
561 | FILE *tfp; | |
562 | long uptime; | |
563 | if ((tfp = fopen("/proc/uptime", "r")) != NULL) { | |
564 | if (fscanf(tfp, "%ld", &uptime) != 1) | |
565 | uptime = -1; | |
566 | fclose(tfp); | |
567 | } | |
568 | if (uptime >= 0 && time(NULL) >= stb.st_mtime+uptime) { | |
569 | fprintf(stderr, "nstat: history is aged out, resetting\n"); | |
570 | ftruncate(fileno(hist_fp), 0); | |
571 | } | |
572 | } | |
573 | ||
574 | load_good_table(hist_fp); | |
575 | ||
576 | hist_db = kern_db; | |
577 | kern_db = NULL; | |
578 | } | |
579 | ||
580 | if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) >= 0 && | |
581 | (connect(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) == 0 | |
582 | || (strcpy(sun.sun_path+1, "nstat0"), | |
583 | connect(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) == 0)) | |
584 | && verify_forging(fd) == 0) { | |
585 | FILE *sfp = fdopen(fd, "r"); | |
586 | load_good_table(sfp); | |
587 | if (hist_db && source_mismatch) { | |
588 | fprintf(stderr, "nstat: history is stale, ignoring it.\n"); | |
589 | hist_db = NULL; | |
590 | } | |
591 | fclose(sfp); | |
592 | } else { | |
593 | if (fd >= 0) | |
594 | close(fd); | |
595 | if (hist_db && info_source[0] && strcmp(info_source, "kernel")) { | |
596 | fprintf(stderr, "nstat: history is stale, ignoring it.\n"); | |
597 | hist_db = NULL; | |
598 | info_source[0] = 0; | |
599 | } | |
600 | load_netstat(); | |
601 | load_snmp6(); | |
602 | load_snmp(); | |
603 | if (info_source[0] == 0) | |
604 | strcpy(info_source, "kernel"); | |
605 | } | |
606 | ||
607 | if (!no_output) { | |
608 | if (ignore_history || hist_db == NULL) | |
609 | dump_kern_db(stdout, 0); | |
610 | else | |
611 | dump_incr_db(stdout); | |
612 | } | |
613 | if (!no_update) { | |
614 | ftruncate(fileno(hist_fp), 0); | |
615 | rewind(hist_fp); | |
616 | dump_kern_db(hist_fp, 1); | |
617 | fflush(hist_fp); | |
618 | } | |
619 | exit(0); | |
620 | } |