]>
git.proxmox.com Git - mirror_iproute2.git/blob - misc/nstat.c
2 * nstat.c handy utility to read counters /proc/net/netstat and snmp
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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
22 #include <sys/socket.h>
31 #include <json_writer.h>
47 char info_source
[128];
50 static int generic_proc_open(const char *env
, char *name
)
53 char *p
= getenv(env
);
56 p
= getenv("PROC_ROOT") ? : "/proc";
57 snprintf(store
, sizeof(store
)-1, "%s/%s", p
, name
);
60 return open(p
, O_RDONLY
);
63 static int net_netstat_open(void)
65 return generic_proc_open("PROC_NET_NETSTAT", "net/netstat");
68 static int net_snmp_open(void)
70 return generic_proc_open("PROC_NET_SNMP", "net/snmp");
73 static int net_snmp6_open(void)
75 return generic_proc_open("PROC_NET_SNMP6", "net/snmp6");
78 static int net_sctp_snmp_open(void)
80 return generic_proc_open("PROC_NET_SCTP_SNMP", "net/sctp/snmp");
84 struct nstat_ent
*next
;
86 unsigned long long val
;
90 struct nstat_ent
*kern_db
;
91 struct nstat_ent
*hist_db
;
93 static const char *useless_numbers
[] = {
94 "IpForwarding", "IpDefaultTTL",
95 "TcpRtoAlgorithm", "TcpRtoMin", "TcpRtoMax",
96 "TcpMaxConn", "TcpCurrEstab"
99 static int useless_number(const char *id
)
103 for (i
= 0; i
< ARRAY_SIZE(useless_numbers
); i
++)
104 if (strcmp(id
, useless_numbers
[i
]) == 0)
109 static int match(const char *id
)
116 for (i
= 0; i
< npatterns
; i
++) {
117 if (!fnmatch(patterns
[i
], id
, FNM_CASEFOLD
))
123 static void load_good_table(FILE *fp
)
126 struct nstat_ent
*db
= NULL
;
129 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
131 unsigned long long val
;
133 char idbuf
[sizeof(buf
)];
136 buf
[strlen(buf
)-1] = 0;
137 if (info_source
[0] && strcmp(info_source
, buf
+1))
140 strncat(info_source
, buf
+1, sizeof(info_source
)-1);
143 /* idbuf is as big as buf, so this is safe */
144 nr
= sscanf(buf
, "%s%llu%lg", idbuf
, &val
, &rate
);
146 fprintf(stderr
, "%s:%d: error parsing history file\n",
152 if (useless_number(idbuf
))
154 if ((n
= malloc(sizeof(*n
))) == NULL
) {
155 perror("nstat: malloc");
158 n
->id
= strdup(idbuf
);
173 static int count_spaces(const char *line
)
178 while ((c
= *line
++) != 0)
179 count
+= c
== ' ' || c
== '\n';
183 static void load_ugly_table(FILE *fp
)
188 struct nstat_ent
*db
= NULL
;
191 while ((nread
= getline(&buf
, &buflen
, fp
)) != -1) {
195 int count1
, count2
, skip
= 0;
197 p
= strchr(buf
, ':');
199 fprintf(stderr
, "%s:%d: error parsing history file\n",
203 count1
= count_spaces(buf
);
206 strncat(idbuf
, buf
, sizeof(idbuf
) - 1);
213 if ((next
= strchr(p
, ' ')) != NULL
)
215 else if ((next
= strchr(p
, '\n')) != NULL
)
217 if (off
< sizeof(idbuf
)) {
219 strncat(idbuf
, p
, sizeof(idbuf
) - off
- 1);
221 n
= malloc(sizeof(*n
));
223 perror("nstat: malloc");
226 n
->id
= strdup(idbuf
);
233 nread
= getline(&buf
, &buflen
, fp
);
235 fprintf(stderr
, "%s:%d: error parsing history file\n",
239 count2
= count_spaces(buf
);
241 skip
= count2
- count1
;
243 p
= strrchr(buf
, ' ');
245 fprintf(stderr
, "%s:%d: error parsing history file\n",
250 if (sscanf(p
+1, "%llu", &n
->val
) != 1) {
251 fprintf(stderr
, "%s:%d: error parsing history file\n",
255 /* Trick to skip "dummy" trailing ICMP MIB in 2.4 */
260 } while (p
> buf
+ off
+ 2);
267 if (useless_number(n
->id
)) {
277 static void load_sctp_snmp(void)
279 FILE *fp
= fdopen(net_sctp_snmp_open(), "r");
287 static void load_snmp(void)
289 FILE *fp
= fdopen(net_snmp_open(), "r");
297 static void load_snmp6(void)
299 FILE *fp
= fdopen(net_snmp6_open(), "r");
307 static void load_netstat(void)
309 FILE *fp
= fdopen(net_netstat_open(), "r");
318 static void dump_kern_db(FILE *fp
, int to_hist
)
320 json_writer_t
*jw
= json_output
? jsonw_new(fp
) : NULL
;
321 struct nstat_ent
*n
, *h
;
325 jsonw_start_object(jw
);
326 jsonw_pretty(jw
, pretty
);
327 jsonw_name(jw
, info_source
);
328 jsonw_start_object(jw
);
330 fprintf(fp
, "#%s\n", info_source
);
332 for (n
= kern_db
; n
; n
= n
->next
) {
333 unsigned long long val
= n
->val
;
335 if (!dump_zeros
&& !val
&& !n
->rate
)
338 struct nstat_ent
*h1
;
342 for (h1
= h
; h1
; h1
= h1
->next
) {
343 if (strcmp(h1
->id
, n
->id
) == 0) {
352 jsonw_uint_field(jw
, n
->id
, val
);
354 fprintf(fp
, "%-32s%-16llu%6.1f\n", n
->id
, val
, n
->rate
);
358 jsonw_end_object(jw
);
360 jsonw_end_object(jw
);
365 static void dump_incr_db(FILE *fp
)
367 json_writer_t
*jw
= json_output
? jsonw_new(fp
) : NULL
;
368 struct nstat_ent
*n
, *h
;
372 jsonw_start_object(jw
);
373 jsonw_pretty(jw
, pretty
);
374 jsonw_name(jw
, info_source
);
375 jsonw_start_object(jw
);
377 fprintf(fp
, "#%s\n", info_source
);
379 for (n
= kern_db
; n
; n
= n
->next
) {
381 unsigned long long val
= n
->val
;
382 struct nstat_ent
*h1
;
384 for (h1
= h
; h1
; h1
= h1
->next
) {
385 if (strcmp(h1
->id
, n
->id
) == 0) {
395 if (!dump_zeros
&& !val
&& !n
->rate
)
401 jsonw_uint_field(jw
, n
->id
, val
);
403 fprintf(fp
, "%-32s%-16llu%6.1f%s\n", n
->id
, val
,
404 n
->rate
, ovfl
?" (overflow)":"");
408 jsonw_end_object(jw
);
410 jsonw_end_object(jw
);
417 static void sigchild(int signo
)
421 static void update_db(int interval
)
423 struct nstat_ent
*n
, *h
;
436 for (n
= kern_db
; n
; n
= n
->next
) {
437 struct nstat_ent
*h1
;
439 for (h1
= h
; h1
; h1
= h1
->next
) {
440 if (strcmp(h1
->id
, n
->id
) == 0) {
442 unsigned long long incr
= h1
->val
- n
->val
;
445 sample
= (double)incr
* 1000.0 / interval
;
446 if (interval
>= scan_interval
) {
447 n
->rate
+= W
*(sample
-n
->rate
);
448 } else if (interval
>= 1000) {
449 if (interval
>= time_constant
) {
452 double w
= W
*(double)interval
/scan_interval
;
454 n
->rate
+= w
*(sample
-n
->rate
);
459 struct nstat_ent
*tmp
= h
;
474 #define T_DIFF(a, b) (((a).tv_sec-(b).tv_sec)*1000 + ((a).tv_usec-(b).tv_usec)/1000)
477 static void server_loop(int fd
)
479 struct timeval snaptime
= { 0 };
483 p
.events
= p
.revents
= POLLIN
;
485 sprintf(info_source
, "%d.%lu sampling_interval=%d time_const=%d",
486 getpid(), (unsigned long)random(), scan_interval
/1000, time_constant
/1000);
498 gettimeofday(&now
, NULL
);
499 tdiff
= T_DIFF(now
, snaptime
);
500 if (tdiff
>= scan_interval
) {
505 if (poll(&p
, 1, scan_interval
- tdiff
) > 0
506 && (p
.revents
&POLLIN
)) {
507 int clnt
= accept(fd
, NULL
, NULL
);
514 } else if ((pid
= fork()) != 0) {
519 FILE *fp
= fdopen(clnt
, "w");
527 while (children
&& waitpid(-1, &status
, WNOHANG
) > 0)
532 static int verify_forging(int fd
)
535 socklen_t olen
= sizeof(cred
);
537 if (getsockopt(fd
, SOL_SOCKET
, SO_PEERCRED
, (void *)&cred
, &olen
) ||
540 if (cred
.uid
== getuid() || cred
.uid
== 0)
545 static void usage(void) __attribute__((noreturn
));
547 static void usage(void)
550 "Usage: nstat [OPTION] [ PATTERN [ PATTERN ] ]\n"
551 " -h, --help this message\n"
552 " -a, --ignore ignore history\n"
553 " -d, --scan=SECS sample every statistics every SECS\n"
554 " -j, --json format output in JSON\n"
555 " -n, --nooutput do history only\n"
556 " -p, --pretty pretty print\n"
557 " -r, --reset reset history\n"
558 " -s, --noupdate don't update history\n"
559 " -t, --interval=SECS report average over the last SECS\n"
560 " -V, --version output version information\n"
561 " -z, --zeros show entries with zero activity\n");
565 static const struct option longopts
[] = {
566 { "help", 0, 0, 'h' },
567 { "ignore", 0, 0, 'a' },
568 { "scan", 1, 0, 'd'},
569 { "nooutput", 0, 0, 'n' },
570 { "json", 0, 0, 'j' },
571 { "reset", 0, 0, 'r' },
572 { "noupdate", 0, 0, 's' },
573 { "pretty", 0, 0, 'p' },
574 { "interval", 1, 0, 't' },
575 { "version", 0, 0, 'V' },
576 { "zeros", 0, 0, 'z' },
580 int main(int argc
, char *argv
[])
583 struct sockaddr_un sun
;
584 FILE *hist_fp
= NULL
;
588 while ((ch
= getopt_long(argc
, argv
, "h?vVzrnasd:t:jp",
589 longopts
, NULL
)) != EOF
) {
607 scan_interval
= 1000*atoi(optarg
);
610 if (sscanf(optarg
, "%d", &time_constant
) != 1 ||
611 time_constant
<= 0) {
612 fprintf(stderr
, "nstat: invalid time constant divisor\n");
624 printf("nstat utility, iproute2-%s\n", version
);
636 sun
.sun_family
= AF_UNIX
;
638 sprintf(sun
.sun_path
+1, "nstat%d", getuid());
640 if (scan_interval
> 0) {
641 if (time_constant
== 0)
643 time_constant
*= 1000;
644 W
= 1 - 1/exp(log(10)*(double)scan_interval
/time_constant
);
645 if ((fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0) {
646 perror("nstat: socket");
649 if (bind(fd
, (struct sockaddr
*)&sun
, 2+1+strlen(sun
.sun_path
+1)) < 0) {
650 perror("nstat: bind");
653 if (listen(fd
, 5) < 0) {
654 perror("nstat: listen");
658 perror("nstat: daemon");
661 signal(SIGPIPE
, SIG_IGN
);
662 signal(SIGCHLD
, sigchild
);
670 if ((hist_name
= getenv("NSTAT_HISTORY")) == NULL
) {
671 hist_name
= malloc(128);
672 sprintf(hist_name
, "/tmp/.nstat.u%d", getuid());
678 if (!ignore_history
|| !no_update
) {
681 fd
= open(hist_name
, O_RDWR
|O_CREAT
|O_NOFOLLOW
, 0600);
683 perror("nstat: open history file");
686 if ((hist_fp
= fdopen(fd
, "r+")) == NULL
) {
687 perror("nstat: fdopen history file");
690 if (flock(fileno(hist_fp
), LOCK_EX
)) {
691 perror("nstat: flock history file");
694 if (fstat(fileno(hist_fp
), &stb
) != 0) {
695 perror("nstat: fstat history file");
698 if (stb
.st_nlink
!= 1 || stb
.st_uid
!= getuid()) {
699 fprintf(stderr
, "nstat: something is so wrong with history file, that I prefer not to proceed.\n");
702 if (!ignore_history
) {
706 if ((tfp
= fopen("/proc/uptime", "r")) != NULL
) {
707 if (fscanf(tfp
, "%ld", &uptime
) != 1)
711 if (uptime
>= 0 && time(NULL
) >= stb
.st_mtime
+uptime
) {
712 fprintf(stderr
, "nstat: history is aged out, resetting\n");
713 if (ftruncate(fileno(hist_fp
), 0) < 0)
714 perror("nstat: ftruncate");
718 load_good_table(hist_fp
);
724 if ((fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) >= 0 &&
725 (connect(fd
, (struct sockaddr
*)&sun
, 2+1+strlen(sun
.sun_path
+1)) == 0
726 || (strcpy(sun
.sun_path
+1, "nstat0"),
727 connect(fd
, (struct sockaddr
*)&sun
, 2+1+strlen(sun
.sun_path
+1)) == 0))
728 && verify_forging(fd
) == 0) {
729 FILE *sfp
= fdopen(fd
, "r");
732 fprintf(stderr
, "nstat: fdopen failed: %s\n",
736 load_good_table(sfp
);
737 if (hist_db
&& source_mismatch
) {
738 fprintf(stderr
, "nstat: history is stale, ignoring it.\n");
746 if (hist_db
&& info_source
[0] && strcmp(info_source
, "kernel")) {
747 fprintf(stderr
, "nstat: history is stale, ignoring it.\n");
755 if (info_source
[0] == 0)
756 strcpy(info_source
, "kernel");
760 if (ignore_history
|| hist_db
== NULL
)
761 dump_kern_db(stdout
, 0);
763 dump_incr_db(stdout
);
766 if (ftruncate(fileno(hist_fp
), 0) < 0)
767 perror("nstat: ftruncate");
771 dump_kern_db(hist_fp
, 1);