]>
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>
48 char info_source
[128];
51 static int generic_proc_open(const char *env
, char *name
)
54 char *p
= getenv(env
);
57 p
= getenv("PROC_ROOT") ? : "/proc";
58 snprintf(store
, sizeof(store
)-1, "%s/%s", p
, name
);
61 return open(p
, O_RDONLY
);
64 static int net_netstat_open(void)
66 return generic_proc_open("PROC_NET_NETSTAT", "net/netstat");
69 static int net_snmp_open(void)
71 return generic_proc_open("PROC_NET_SNMP", "net/snmp");
74 static int net_snmp6_open(void)
76 return generic_proc_open("PROC_NET_SNMP6", "net/snmp6");
79 static int net_sctp_snmp_open(void)
81 return generic_proc_open("PROC_NET_SCTP_SNMP", "net/sctp/snmp");
85 struct nstat_ent
*next
;
87 unsigned long long val
;
91 struct nstat_ent
*kern_db
;
92 struct nstat_ent
*hist_db
;
94 static const char *useless_numbers
[] = {
95 "IpForwarding", "IpDefaultTTL",
96 "TcpRtoAlgorithm", "TcpRtoMin", "TcpRtoMax",
97 "TcpMaxConn", "TcpCurrEstab"
100 static int useless_number(const char *id
)
104 for (i
= 0; i
< ARRAY_SIZE(useless_numbers
); i
++)
105 if (strcmp(id
, useless_numbers
[i
]) == 0)
110 static int match(const char *id
)
117 for (i
= 0; i
< npatterns
; i
++) {
118 if (!fnmatch(patterns
[i
], id
, 0))
124 static void load_good_table(FILE *fp
)
127 struct nstat_ent
*db
= NULL
;
130 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
132 unsigned long long val
;
134 char idbuf
[sizeof(buf
)];
137 buf
[strlen(buf
)-1] = 0;
138 if (info_source
[0] && strcmp(info_source
, buf
+1))
141 strncat(info_source
, buf
+1, sizeof(info_source
)-1);
144 /* idbuf is as big as buf, so this is safe */
145 nr
= sscanf(buf
, "%s%llu%lg", idbuf
, &val
, &rate
);
150 if (useless_number(idbuf
))
152 if ((n
= malloc(sizeof(*n
))) == NULL
)
154 n
->id
= strdup(idbuf
);
169 static int count_spaces(const char *line
)
174 while ((c
= *line
++) != 0)
175 count
+= c
== ' ' || c
== '\n';
179 static void load_ugly_table(FILE *fp
)
182 struct nstat_ent
*db
= NULL
;
185 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
186 char idbuf
[sizeof(buf
)];
189 int count1
, count2
, skip
= 0;
191 p
= strchr(buf
, ':');
194 count1
= count_spaces(buf
);
197 strncat(idbuf
, buf
, sizeof(idbuf
) - 1);
204 if ((next
= strchr(p
, ' ')) != NULL
)
206 else if ((next
= strchr(p
, '\n')) != NULL
)
208 if (off
< sizeof(idbuf
)) {
210 strncat(idbuf
, p
, sizeof(idbuf
) - off
- 1);
212 n
= malloc(sizeof(*n
));
215 n
->id
= strdup(idbuf
);
222 if (fgets(buf
, sizeof(buf
), fp
) == NULL
)
224 count2
= count_spaces(buf
);
226 skip
= count2
- count1
;
228 p
= strrchr(buf
, ' ');
232 if (sscanf(p
+1, "%llu", &n
->val
) != 1)
234 /* Trick to skip "dummy" trailing ICMP MIB in 2.4 */
239 } while (p
> buf
+ off
+ 2);
245 if (useless_number(n
->id
)) {
255 static void load_sctp_snmp(void)
257 FILE *fp
= fdopen(net_sctp_snmp_open(), "r");
265 static void load_snmp(void)
267 FILE *fp
= fdopen(net_snmp_open(), "r");
275 static void load_snmp6(void)
277 FILE *fp
= fdopen(net_snmp6_open(), "r");
285 static void load_netstat(void)
287 FILE *fp
= fdopen(net_netstat_open(), "r");
296 static void dump_kern_db(FILE *fp
, int to_hist
)
298 json_writer_t
*jw
= json_output
? jsonw_new(fp
) : NULL
;
299 struct nstat_ent
*n
, *h
;
303 jsonw_start_object(jw
);
304 jsonw_pretty(jw
, pretty
);
305 jsonw_name(jw
, info_source
);
306 jsonw_start_object(jw
);
308 fprintf(fp
, "#%s\n", info_source
);
310 for (n
= kern_db
; n
; n
= n
->next
) {
311 unsigned long long val
= n
->val
;
313 if (!dump_zeros
&& !val
&& !n
->rate
)
316 struct nstat_ent
*h1
;
320 for (h1
= h
; h1
; h1
= h1
->next
) {
321 if (strcmp(h1
->id
, n
->id
) == 0) {
330 jsonw_uint_field(jw
, n
->id
, val
);
332 fprintf(fp
, "%-32s%-16llu%6.1f\n", n
->id
, val
, n
->rate
);
336 jsonw_end_object(jw
);
338 jsonw_end_object(jw
);
343 static void dump_incr_db(FILE *fp
)
345 json_writer_t
*jw
= json_output
? jsonw_new(fp
) : NULL
;
346 struct nstat_ent
*n
, *h
;
350 jsonw_start_object(jw
);
351 jsonw_pretty(jw
, pretty
);
352 jsonw_name(jw
, info_source
);
353 jsonw_start_object(jw
);
355 fprintf(fp
, "#%s\n", info_source
);
357 for (n
= kern_db
; n
; n
= n
->next
) {
359 unsigned long long val
= n
->val
;
360 struct nstat_ent
*h1
;
362 for (h1
= h
; h1
; h1
= h1
->next
) {
363 if (strcmp(h1
->id
, n
->id
) == 0) {
373 if (!dump_zeros
&& !val
&& !n
->rate
)
379 jsonw_uint_field(jw
, n
->id
, val
);
381 fprintf(fp
, "%-32s%-16llu%6.1f%s\n", n
->id
, val
,
382 n
->rate
, ovfl
?" (overflow)":"");
386 jsonw_end_object(jw
);
388 jsonw_end_object(jw
);
395 static void sigchild(int signo
)
399 static void update_db(int interval
)
401 struct nstat_ent
*n
, *h
;
414 for (n
= kern_db
; n
; n
= n
->next
) {
415 struct nstat_ent
*h1
;
417 for (h1
= h
; h1
; h1
= h1
->next
) {
418 if (strcmp(h1
->id
, n
->id
) == 0) {
420 unsigned long long incr
= h1
->val
- n
->val
;
423 sample
= (double)incr
* 1000.0 / interval
;
424 if (interval
>= scan_interval
) {
425 n
->rate
+= W
*(sample
-n
->rate
);
426 } else if (interval
>= 1000) {
427 if (interval
>= time_constant
) {
430 double w
= W
*(double)interval
/scan_interval
;
432 n
->rate
+= w
*(sample
-n
->rate
);
437 struct nstat_ent
*tmp
= h
;
452 #define T_DIFF(a, b) (((a).tv_sec-(b).tv_sec)*1000 + ((a).tv_usec-(b).tv_usec)/1000)
455 static void server_loop(int fd
)
457 struct timeval snaptime
= { 0 };
461 p
.events
= p
.revents
= POLLIN
;
463 sprintf(info_source
, "%d.%lu sampling_interval=%d time_const=%d",
464 getpid(), (unsigned long)random(), scan_interval
/1000, time_constant
/1000);
476 gettimeofday(&now
, NULL
);
477 tdiff
= T_DIFF(now
, snaptime
);
478 if (tdiff
>= scan_interval
) {
483 if (poll(&p
, 1, scan_interval
- tdiff
) > 0
484 && (p
.revents
&POLLIN
)) {
485 int clnt
= accept(fd
, NULL
, NULL
);
492 } else if ((pid
= fork()) != 0) {
497 FILE *fp
= fdopen(clnt
, "w");
505 while (children
&& waitpid(-1, &status
, WNOHANG
) > 0)
510 static int verify_forging(int fd
)
513 socklen_t olen
= sizeof(cred
);
515 if (getsockopt(fd
, SOL_SOCKET
, SO_PEERCRED
, (void *)&cred
, &olen
) ||
518 if (cred
.uid
== getuid() || cred
.uid
== 0)
523 static void usage(void) __attribute__((noreturn
));
525 static void usage(void)
528 "Usage: nstat [OPTION] [ PATTERN [ PATTERN ] ]\n"
529 " -h, --help this message\n"
530 " -a, --ignore ignore history\n"
531 " -d, --scan=SECS sample every statistics every SECS\n"
532 " -j, --json format output in JSON\n"
533 " -n, --nooutput do history only\n"
534 " -p, --pretty pretty print\n"
535 " -r, --reset reset history\n"
536 " -s, --noupdate don't update history\n"
537 " -t, --interval=SECS report average over the last SECS\n"
538 " -V, --version output version information\n"
539 " -z, --zeros show entries with zero activity\n");
543 static const struct option longopts
[] = {
544 { "help", 0, 0, 'h' },
545 { "ignore", 0, 0, 'a' },
546 { "scan", 1, 0, 'd'},
547 { "nooutput", 0, 0, 'n' },
548 { "json", 0, 0, 'j' },
549 { "reset", 0, 0, 'r' },
550 { "noupdate", 0, 0, 's' },
551 { "pretty", 0, 0, 'p' },
552 { "interval", 1, 0, 't' },
553 { "version", 0, 0, 'V' },
554 { "zeros", 0, 0, 'z' },
558 int main(int argc
, char *argv
[])
561 struct sockaddr_un sun
;
562 FILE *hist_fp
= NULL
;
566 while ((ch
= getopt_long(argc
, argv
, "h?vVzrnasd:t:jp",
567 longopts
, NULL
)) != EOF
) {
585 scan_interval
= 1000*atoi(optarg
);
588 if (sscanf(optarg
, "%d", &time_constant
) != 1 ||
589 time_constant
<= 0) {
590 fprintf(stderr
, "nstat: invalid time constant divisor\n");
602 printf("nstat utility, iproute2-ss%s\n", SNAPSHOT
);
614 sun
.sun_family
= AF_UNIX
;
616 sprintf(sun
.sun_path
+1, "nstat%d", getuid());
618 if (scan_interval
> 0) {
619 if (time_constant
== 0)
621 time_constant
*= 1000;
622 W
= 1 - 1/exp(log(10)*(double)scan_interval
/time_constant
);
623 if ((fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0) {
624 perror("nstat: socket");
627 if (bind(fd
, (struct sockaddr
*)&sun
, 2+1+strlen(sun
.sun_path
+1)) < 0) {
628 perror("nstat: bind");
631 if (listen(fd
, 5) < 0) {
632 perror("nstat: listen");
636 perror("nstat: daemon");
639 signal(SIGPIPE
, SIG_IGN
);
640 signal(SIGCHLD
, sigchild
);
648 if ((hist_name
= getenv("NSTAT_HISTORY")) == NULL
) {
649 hist_name
= malloc(128);
650 sprintf(hist_name
, "/tmp/.nstat.u%d", getuid());
656 if (!ignore_history
|| !no_update
) {
659 fd
= open(hist_name
, O_RDWR
|O_CREAT
|O_NOFOLLOW
, 0600);
661 perror("nstat: open history file");
664 if ((hist_fp
= fdopen(fd
, "r+")) == NULL
) {
665 perror("nstat: fdopen history file");
668 if (flock(fileno(hist_fp
), LOCK_EX
)) {
669 perror("nstat: flock history file");
672 if (fstat(fileno(hist_fp
), &stb
) != 0) {
673 perror("nstat: fstat history file");
676 if (stb
.st_nlink
!= 1 || stb
.st_uid
!= getuid()) {
677 fprintf(stderr
, "nstat: something is so wrong with history file, that I prefer not to proceed.\n");
680 if (!ignore_history
) {
684 if ((tfp
= fopen("/proc/uptime", "r")) != NULL
) {
685 if (fscanf(tfp
, "%ld", &uptime
) != 1)
689 if (uptime
>= 0 && time(NULL
) >= stb
.st_mtime
+uptime
) {
690 fprintf(stderr
, "nstat: history is aged out, resetting\n");
691 if (ftruncate(fileno(hist_fp
), 0) < 0)
692 perror("nstat: ftruncate");
696 load_good_table(hist_fp
);
702 if ((fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) >= 0 &&
703 (connect(fd
, (struct sockaddr
*)&sun
, 2+1+strlen(sun
.sun_path
+1)) == 0
704 || (strcpy(sun
.sun_path
+1, "nstat0"),
705 connect(fd
, (struct sockaddr
*)&sun
, 2+1+strlen(sun
.sun_path
+1)) == 0))
706 && verify_forging(fd
) == 0) {
707 FILE *sfp
= fdopen(fd
, "r");
710 fprintf(stderr
, "nstat: fdopen failed: %s\n",
714 load_good_table(sfp
);
715 if (hist_db
&& source_mismatch
) {
716 fprintf(stderr
, "nstat: history is stale, ignoring it.\n");
724 if (hist_db
&& info_source
[0] && strcmp(info_source
, "kernel")) {
725 fprintf(stderr
, "nstat: history is stale, ignoring it.\n");
733 if (info_source
[0] == 0)
734 strcpy(info_source
, "kernel");
738 if (ignore_history
|| hist_db
== NULL
)
739 dump_kern_db(stdout
, 0);
741 dump_incr_db(stdout
);
744 if (ftruncate(fileno(hist_fp
), 0) < 0)
745 perror("nstat: ftruncate");
749 dump_kern_db(hist_fp
, 1);