]>
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
, 0))
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
);
149 if (useless_number(idbuf
))
151 if ((n
= malloc(sizeof(*n
))) == NULL
)
153 n
->id
= strdup(idbuf
);
168 static int count_spaces(const char *line
)
173 while ((c
= *line
++) != 0)
174 count
+= c
== ' ' || c
== '\n';
178 static void load_ugly_table(FILE *fp
)
183 struct nstat_ent
*db
= NULL
;
186 while ((nread
= getline(&buf
, &buflen
, fp
)) != -1) {
190 int count1
, count2
, skip
= 0;
192 p
= strchr(buf
, ':');
195 count1
= count_spaces(buf
);
198 strncat(idbuf
, buf
, sizeof(idbuf
) - 1);
205 if ((next
= strchr(p
, ' ')) != NULL
)
207 else if ((next
= strchr(p
, '\n')) != NULL
)
209 if (off
< sizeof(idbuf
)) {
211 strncat(idbuf
, p
, sizeof(idbuf
) - off
- 1);
213 n
= malloc(sizeof(*n
));
216 n
->id
= strdup(idbuf
);
223 nread
= getline(&buf
, &buflen
, fp
);
226 count2
= count_spaces(buf
);
228 skip
= count2
- count1
;
230 p
= strrchr(buf
, ' ');
234 if (sscanf(p
+1, "%llu", &n
->val
) != 1)
236 /* Trick to skip "dummy" trailing ICMP MIB in 2.4 */
241 } while (p
> buf
+ off
+ 2);
248 if (useless_number(n
->id
)) {
258 static void load_sctp_snmp(void)
260 FILE *fp
= fdopen(net_sctp_snmp_open(), "r");
268 static void load_snmp(void)
270 FILE *fp
= fdopen(net_snmp_open(), "r");
278 static void load_snmp6(void)
280 FILE *fp
= fdopen(net_snmp6_open(), "r");
288 static void load_netstat(void)
290 FILE *fp
= fdopen(net_netstat_open(), "r");
299 static void dump_kern_db(FILE *fp
, int to_hist
)
301 json_writer_t
*jw
= json_output
? jsonw_new(fp
) : NULL
;
302 struct nstat_ent
*n
, *h
;
306 jsonw_start_object(jw
);
307 jsonw_pretty(jw
, pretty
);
308 jsonw_name(jw
, info_source
);
309 jsonw_start_object(jw
);
311 fprintf(fp
, "#%s\n", info_source
);
313 for (n
= kern_db
; n
; n
= n
->next
) {
314 unsigned long long val
= n
->val
;
316 if (!dump_zeros
&& !val
&& !n
->rate
)
319 struct nstat_ent
*h1
;
323 for (h1
= h
; h1
; h1
= h1
->next
) {
324 if (strcmp(h1
->id
, n
->id
) == 0) {
333 jsonw_uint_field(jw
, n
->id
, val
);
335 fprintf(fp
, "%-32s%-16llu%6.1f\n", n
->id
, val
, n
->rate
);
339 jsonw_end_object(jw
);
341 jsonw_end_object(jw
);
346 static void dump_incr_db(FILE *fp
)
348 json_writer_t
*jw
= json_output
? jsonw_new(fp
) : NULL
;
349 struct nstat_ent
*n
, *h
;
353 jsonw_start_object(jw
);
354 jsonw_pretty(jw
, pretty
);
355 jsonw_name(jw
, info_source
);
356 jsonw_start_object(jw
);
358 fprintf(fp
, "#%s\n", info_source
);
360 for (n
= kern_db
; n
; n
= n
->next
) {
362 unsigned long long val
= n
->val
;
363 struct nstat_ent
*h1
;
365 for (h1
= h
; h1
; h1
= h1
->next
) {
366 if (strcmp(h1
->id
, n
->id
) == 0) {
376 if (!dump_zeros
&& !val
&& !n
->rate
)
382 jsonw_uint_field(jw
, n
->id
, val
);
384 fprintf(fp
, "%-32s%-16llu%6.1f%s\n", n
->id
, val
,
385 n
->rate
, ovfl
?" (overflow)":"");
389 jsonw_end_object(jw
);
391 jsonw_end_object(jw
);
398 static void sigchild(int signo
)
402 static void update_db(int interval
)
404 struct nstat_ent
*n
, *h
;
417 for (n
= kern_db
; n
; n
= n
->next
) {
418 struct nstat_ent
*h1
;
420 for (h1
= h
; h1
; h1
= h1
->next
) {
421 if (strcmp(h1
->id
, n
->id
) == 0) {
423 unsigned long long incr
= h1
->val
- n
->val
;
426 sample
= (double)incr
* 1000.0 / interval
;
427 if (interval
>= scan_interval
) {
428 n
->rate
+= W
*(sample
-n
->rate
);
429 } else if (interval
>= 1000) {
430 if (interval
>= time_constant
) {
433 double w
= W
*(double)interval
/scan_interval
;
435 n
->rate
+= w
*(sample
-n
->rate
);
440 struct nstat_ent
*tmp
= h
;
455 #define T_DIFF(a, b) (((a).tv_sec-(b).tv_sec)*1000 + ((a).tv_usec-(b).tv_usec)/1000)
458 static void server_loop(int fd
)
460 struct timeval snaptime
= { 0 };
464 p
.events
= p
.revents
= POLLIN
;
466 sprintf(info_source
, "%d.%lu sampling_interval=%d time_const=%d",
467 getpid(), (unsigned long)random(), scan_interval
/1000, time_constant
/1000);
479 gettimeofday(&now
, NULL
);
480 tdiff
= T_DIFF(now
, snaptime
);
481 if (tdiff
>= scan_interval
) {
486 if (poll(&p
, 1, scan_interval
- tdiff
) > 0
487 && (p
.revents
&POLLIN
)) {
488 int clnt
= accept(fd
, NULL
, NULL
);
495 } else if ((pid
= fork()) != 0) {
500 FILE *fp
= fdopen(clnt
, "w");
508 while (children
&& waitpid(-1, &status
, WNOHANG
) > 0)
513 static int verify_forging(int fd
)
516 socklen_t olen
= sizeof(cred
);
518 if (getsockopt(fd
, SOL_SOCKET
, SO_PEERCRED
, (void *)&cred
, &olen
) ||
521 if (cred
.uid
== getuid() || cred
.uid
== 0)
526 static void usage(void) __attribute__((noreturn
));
528 static void usage(void)
531 "Usage: nstat [OPTION] [ PATTERN [ PATTERN ] ]\n"
532 " -h, --help this message\n"
533 " -a, --ignore ignore history\n"
534 " -d, --scan=SECS sample every statistics every SECS\n"
535 " -j, --json format output in JSON\n"
536 " -n, --nooutput do history only\n"
537 " -p, --pretty pretty print\n"
538 " -r, --reset reset history\n"
539 " -s, --noupdate don't update history\n"
540 " -t, --interval=SECS report average over the last SECS\n"
541 " -V, --version output version information\n"
542 " -z, --zeros show entries with zero activity\n");
546 static const struct option longopts
[] = {
547 { "help", 0, 0, 'h' },
548 { "ignore", 0, 0, 'a' },
549 { "scan", 1, 0, 'd'},
550 { "nooutput", 0, 0, 'n' },
551 { "json", 0, 0, 'j' },
552 { "reset", 0, 0, 'r' },
553 { "noupdate", 0, 0, 's' },
554 { "pretty", 0, 0, 'p' },
555 { "interval", 1, 0, 't' },
556 { "version", 0, 0, 'V' },
557 { "zeros", 0, 0, 'z' },
561 int main(int argc
, char *argv
[])
564 struct sockaddr_un sun
;
565 FILE *hist_fp
= NULL
;
569 while ((ch
= getopt_long(argc
, argv
, "h?vVzrnasd:t:jp",
570 longopts
, NULL
)) != EOF
) {
588 scan_interval
= 1000*atoi(optarg
);
591 if (sscanf(optarg
, "%d", &time_constant
) != 1 ||
592 time_constant
<= 0) {
593 fprintf(stderr
, "nstat: invalid time constant divisor\n");
605 printf("nstat utility, iproute2-ss%s\n", SNAPSHOT
);
617 sun
.sun_family
= AF_UNIX
;
619 sprintf(sun
.sun_path
+1, "nstat%d", getuid());
621 if (scan_interval
> 0) {
622 if (time_constant
== 0)
624 time_constant
*= 1000;
625 W
= 1 - 1/exp(log(10)*(double)scan_interval
/time_constant
);
626 if ((fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0) {
627 perror("nstat: socket");
630 if (bind(fd
, (struct sockaddr
*)&sun
, 2+1+strlen(sun
.sun_path
+1)) < 0) {
631 perror("nstat: bind");
634 if (listen(fd
, 5) < 0) {
635 perror("nstat: listen");
639 perror("nstat: daemon");
642 signal(SIGPIPE
, SIG_IGN
);
643 signal(SIGCHLD
, sigchild
);
651 if ((hist_name
= getenv("NSTAT_HISTORY")) == NULL
) {
652 hist_name
= malloc(128);
653 sprintf(hist_name
, "/tmp/.nstat.u%d", getuid());
659 if (!ignore_history
|| !no_update
) {
662 fd
= open(hist_name
, O_RDWR
|O_CREAT
|O_NOFOLLOW
, 0600);
664 perror("nstat: open history file");
667 if ((hist_fp
= fdopen(fd
, "r+")) == NULL
) {
668 perror("nstat: fdopen history file");
671 if (flock(fileno(hist_fp
), LOCK_EX
)) {
672 perror("nstat: flock history file");
675 if (fstat(fileno(hist_fp
), &stb
) != 0) {
676 perror("nstat: fstat history file");
679 if (stb
.st_nlink
!= 1 || stb
.st_uid
!= getuid()) {
680 fprintf(stderr
, "nstat: something is so wrong with history file, that I prefer not to proceed.\n");
683 if (!ignore_history
) {
687 if ((tfp
= fopen("/proc/uptime", "r")) != NULL
) {
688 if (fscanf(tfp
, "%ld", &uptime
) != 1)
692 if (uptime
>= 0 && time(NULL
) >= stb
.st_mtime
+uptime
) {
693 fprintf(stderr
, "nstat: history is aged out, resetting\n");
694 if (ftruncate(fileno(hist_fp
), 0) < 0)
695 perror("nstat: ftruncate");
699 load_good_table(hist_fp
);
705 if ((fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) >= 0 &&
706 (connect(fd
, (struct sockaddr
*)&sun
, 2+1+strlen(sun
.sun_path
+1)) == 0
707 || (strcpy(sun
.sun_path
+1, "nstat0"),
708 connect(fd
, (struct sockaddr
*)&sun
, 2+1+strlen(sun
.sun_path
+1)) == 0))
709 && verify_forging(fd
) == 0) {
710 FILE *sfp
= fdopen(fd
, "r");
713 fprintf(stderr
, "nstat: fdopen failed: %s\n",
717 load_good_table(sfp
);
718 if (hist_db
&& source_mismatch
) {
719 fprintf(stderr
, "nstat: history is stale, ignoring it.\n");
727 if (hist_db
&& info_source
[0] && strcmp(info_source
, "kernel")) {
728 fprintf(stderr
, "nstat: history is stale, ignoring it.\n");
736 if (info_source
[0] == 0)
737 strcpy(info_source
, "kernel");
741 if (ignore_history
|| hist_db
== NULL
)
742 dump_kern_db(stdout
, 0);
744 dump_incr_db(stdout
);
747 if (ftruncate(fileno(hist_fp
), 0) < 0)
748 perror("nstat: ftruncate");
752 dump_kern_db(hist_fp
, 1);