]>
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
)
181 struct nstat_ent
*db
= NULL
;
184 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
188 int count1
, count2
, skip
= 0;
190 p
= strchr(buf
, ':');
193 count1
= count_spaces(buf
);
196 strncat(idbuf
, buf
, sizeof(idbuf
) - 1);
203 if ((next
= strchr(p
, ' ')) != NULL
)
205 else if ((next
= strchr(p
, '\n')) != NULL
)
207 if (off
< sizeof(idbuf
)) {
209 strncat(idbuf
, p
, sizeof(idbuf
) - off
- 1);
211 n
= malloc(sizeof(*n
));
214 n
->id
= strdup(idbuf
);
221 if (fgets(buf
, sizeof(buf
), fp
) == NULL
)
223 count2
= count_spaces(buf
);
225 skip
= count2
- count1
;
227 p
= strrchr(buf
, ' ');
231 if (sscanf(p
+1, "%llu", &n
->val
) != 1)
233 /* Trick to skip "dummy" trailing ICMP MIB in 2.4 */
238 } while (p
> buf
+ off
+ 2);
244 if (useless_number(n
->id
)) {
254 static void load_sctp_snmp(void)
256 FILE *fp
= fdopen(net_sctp_snmp_open(), "r");
264 static void load_snmp(void)
266 FILE *fp
= fdopen(net_snmp_open(), "r");
274 static void load_snmp6(void)
276 FILE *fp
= fdopen(net_snmp6_open(), "r");
284 static void load_netstat(void)
286 FILE *fp
= fdopen(net_netstat_open(), "r");
295 static void dump_kern_db(FILE *fp
, int to_hist
)
297 json_writer_t
*jw
= json_output
? jsonw_new(fp
) : NULL
;
298 struct nstat_ent
*n
, *h
;
302 jsonw_start_object(jw
);
303 jsonw_pretty(jw
, pretty
);
304 jsonw_name(jw
, info_source
);
305 jsonw_start_object(jw
);
307 fprintf(fp
, "#%s\n", info_source
);
309 for (n
= kern_db
; n
; n
= n
->next
) {
310 unsigned long long val
= n
->val
;
312 if (!dump_zeros
&& !val
&& !n
->rate
)
315 struct nstat_ent
*h1
;
319 for (h1
= h
; h1
; h1
= h1
->next
) {
320 if (strcmp(h1
->id
, n
->id
) == 0) {
329 jsonw_uint_field(jw
, n
->id
, val
);
331 fprintf(fp
, "%-32s%-16llu%6.1f\n", n
->id
, val
, n
->rate
);
335 jsonw_end_object(jw
);
337 jsonw_end_object(jw
);
342 static void dump_incr_db(FILE *fp
)
344 json_writer_t
*jw
= json_output
? jsonw_new(fp
) : NULL
;
345 struct nstat_ent
*n
, *h
;
349 jsonw_start_object(jw
);
350 jsonw_pretty(jw
, pretty
);
351 jsonw_name(jw
, info_source
);
352 jsonw_start_object(jw
);
354 fprintf(fp
, "#%s\n", info_source
);
356 for (n
= kern_db
; n
; n
= n
->next
) {
358 unsigned long long val
= n
->val
;
359 struct nstat_ent
*h1
;
361 for (h1
= h
; h1
; h1
= h1
->next
) {
362 if (strcmp(h1
->id
, n
->id
) == 0) {
372 if (!dump_zeros
&& !val
&& !n
->rate
)
378 jsonw_uint_field(jw
, n
->id
, val
);
380 fprintf(fp
, "%-32s%-16llu%6.1f%s\n", n
->id
, val
,
381 n
->rate
, ovfl
?" (overflow)":"");
385 jsonw_end_object(jw
);
387 jsonw_end_object(jw
);
394 static void sigchild(int signo
)
398 static void update_db(int interval
)
400 struct nstat_ent
*n
, *h
;
413 for (n
= kern_db
; n
; n
= n
->next
) {
414 struct nstat_ent
*h1
;
416 for (h1
= h
; h1
; h1
= h1
->next
) {
417 if (strcmp(h1
->id
, n
->id
) == 0) {
419 unsigned long long incr
= h1
->val
- n
->val
;
422 sample
= (double)incr
* 1000.0 / interval
;
423 if (interval
>= scan_interval
) {
424 n
->rate
+= W
*(sample
-n
->rate
);
425 } else if (interval
>= 1000) {
426 if (interval
>= time_constant
) {
429 double w
= W
*(double)interval
/scan_interval
;
431 n
->rate
+= w
*(sample
-n
->rate
);
436 struct nstat_ent
*tmp
= h
;
451 #define T_DIFF(a, b) (((a).tv_sec-(b).tv_sec)*1000 + ((a).tv_usec-(b).tv_usec)/1000)
454 static void server_loop(int fd
)
456 struct timeval snaptime
= { 0 };
460 p
.events
= p
.revents
= POLLIN
;
462 sprintf(info_source
, "%d.%lu sampling_interval=%d time_const=%d",
463 getpid(), (unsigned long)random(), scan_interval
/1000, time_constant
/1000);
475 gettimeofday(&now
, NULL
);
476 tdiff
= T_DIFF(now
, snaptime
);
477 if (tdiff
>= scan_interval
) {
482 if (poll(&p
, 1, scan_interval
- tdiff
) > 0
483 && (p
.revents
&POLLIN
)) {
484 int clnt
= accept(fd
, NULL
, NULL
);
491 } else if ((pid
= fork()) != 0) {
496 FILE *fp
= fdopen(clnt
, "w");
504 while (children
&& waitpid(-1, &status
, WNOHANG
) > 0)
509 static int verify_forging(int fd
)
512 socklen_t olen
= sizeof(cred
);
514 if (getsockopt(fd
, SOL_SOCKET
, SO_PEERCRED
, (void *)&cred
, &olen
) ||
517 if (cred
.uid
== getuid() || cred
.uid
== 0)
522 static void usage(void) __attribute__((noreturn
));
524 static void usage(void)
527 "Usage: nstat [OPTION] [ PATTERN [ PATTERN ] ]\n"
528 " -h, --help this message\n"
529 " -a, --ignore ignore history\n"
530 " -d, --scan=SECS sample every statistics every SECS\n"
531 " -j, --json format output in JSON\n"
532 " -n, --nooutput do history only\n"
533 " -p, --pretty pretty print\n"
534 " -r, --reset reset history\n"
535 " -s, --noupdate don't update history\n"
536 " -t, --interval=SECS report average over the last SECS\n"
537 " -V, --version output version information\n"
538 " -z, --zeros show entries with zero activity\n");
542 static const struct option longopts
[] = {
543 { "help", 0, 0, 'h' },
544 { "ignore", 0, 0, 'a' },
545 { "scan", 1, 0, 'd'},
546 { "nooutput", 0, 0, 'n' },
547 { "json", 0, 0, 'j' },
548 { "reset", 0, 0, 'r' },
549 { "noupdate", 0, 0, 's' },
550 { "pretty", 0, 0, 'p' },
551 { "interval", 1, 0, 't' },
552 { "version", 0, 0, 'V' },
553 { "zeros", 0, 0, 'z' },
557 int main(int argc
, char *argv
[])
560 struct sockaddr_un sun
;
561 FILE *hist_fp
= NULL
;
565 while ((ch
= getopt_long(argc
, argv
, "h?vVzrnasd:t:jp",
566 longopts
, NULL
)) != EOF
) {
584 scan_interval
= 1000*atoi(optarg
);
587 if (sscanf(optarg
, "%d", &time_constant
) != 1 ||
588 time_constant
<= 0) {
589 fprintf(stderr
, "nstat: invalid time constant divisor\n");
601 printf("nstat utility, iproute2-ss%s\n", SNAPSHOT
);
613 sun
.sun_family
= AF_UNIX
;
615 sprintf(sun
.sun_path
+1, "nstat%d", getuid());
617 if (scan_interval
> 0) {
618 if (time_constant
== 0)
620 time_constant
*= 1000;
621 W
= 1 - 1/exp(log(10)*(double)scan_interval
/time_constant
);
622 if ((fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0) {
623 perror("nstat: socket");
626 if (bind(fd
, (struct sockaddr
*)&sun
, 2+1+strlen(sun
.sun_path
+1)) < 0) {
627 perror("nstat: bind");
630 if (listen(fd
, 5) < 0) {
631 perror("nstat: listen");
635 perror("nstat: daemon");
638 signal(SIGPIPE
, SIG_IGN
);
639 signal(SIGCHLD
, sigchild
);
647 if ((hist_name
= getenv("NSTAT_HISTORY")) == NULL
) {
648 hist_name
= malloc(128);
649 sprintf(hist_name
, "/tmp/.nstat.u%d", getuid());
655 if (!ignore_history
|| !no_update
) {
658 fd
= open(hist_name
, O_RDWR
|O_CREAT
|O_NOFOLLOW
, 0600);
660 perror("nstat: open history file");
663 if ((hist_fp
= fdopen(fd
, "r+")) == NULL
) {
664 perror("nstat: fdopen history file");
667 if (flock(fileno(hist_fp
), LOCK_EX
)) {
668 perror("nstat: flock history file");
671 if (fstat(fileno(hist_fp
), &stb
) != 0) {
672 perror("nstat: fstat history file");
675 if (stb
.st_nlink
!= 1 || stb
.st_uid
!= getuid()) {
676 fprintf(stderr
, "nstat: something is so wrong with history file, that I prefer not to proceed.\n");
679 if (!ignore_history
) {
683 if ((tfp
= fopen("/proc/uptime", "r")) != NULL
) {
684 if (fscanf(tfp
, "%ld", &uptime
) != 1)
688 if (uptime
>= 0 && time(NULL
) >= stb
.st_mtime
+uptime
) {
689 fprintf(stderr
, "nstat: history is aged out, resetting\n");
690 if (ftruncate(fileno(hist_fp
), 0) < 0)
691 perror("nstat: ftruncate");
695 load_good_table(hist_fp
);
701 if ((fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) >= 0 &&
702 (connect(fd
, (struct sockaddr
*)&sun
, 2+1+strlen(sun
.sun_path
+1)) == 0
703 || (strcpy(sun
.sun_path
+1, "nstat0"),
704 connect(fd
, (struct sockaddr
*)&sun
, 2+1+strlen(sun
.sun_path
+1)) == 0))
705 && verify_forging(fd
) == 0) {
706 FILE *sfp
= fdopen(fd
, "r");
709 fprintf(stderr
, "nstat: fdopen failed: %s\n",
713 load_good_table(sfp
);
714 if (hist_db
&& source_mismatch
) {
715 fprintf(stderr
, "nstat: history is stale, ignoring it.\n");
723 if (hist_db
&& info_source
[0] && strcmp(info_source
, "kernel")) {
724 fprintf(stderr
, "nstat: history is stale, ignoring it.\n");
732 if (info_source
[0] == 0)
733 strcpy(info_source
, "kernel");
737 if (ignore_history
|| hist_db
== NULL
)
738 dump_kern_db(stdout
, 0);
740 dump_incr_db(stdout
);
743 if (ftruncate(fileno(hist_fp
), 0) < 0)
744 perror("nstat: ftruncate");
748 dump_kern_db(hist_fp
, 1);