]>
git.proxmox.com Git - mirror_iproute2.git/blob - misc/nstat.c
99705286d279cb47d2a12a78fac23851a5f0082e
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>
35 int reset_history
= 0;
36 int ignore_history
= 0;
41 int scan_interval
= 0;
42 int time_constant
= 0;
47 char info_source
[128];
50 static int generic_proc_open(const char *env
, char *name
)
53 char *p
= getenv(env
);
55 p
= getenv("PROC_ROOT") ? : "/proc";
56 snprintf(store
, sizeof(store
)-1, "%s/%s", p
, name
);
59 return open(p
, O_RDONLY
);
62 static int net_netstat_open(void)
64 return generic_proc_open("PROC_NET_NETSTAT", "net/netstat");
67 static int net_snmp_open(void)
69 return generic_proc_open("PROC_NET_SNMP", "net/snmp");
72 static int net_snmp6_open(void)
74 return generic_proc_open("PROC_NET_SNMP6", "net/snmp6");
79 struct nstat_ent
*next
;
81 unsigned long long val
;
85 struct nstat_ent
*kern_db
;
86 struct nstat_ent
*hist_db
;
88 static const char *useless_numbers
[] = {
89 "IpForwarding", "IpDefaultTTL",
90 "TcpRtoAlgorithm", "TcpRtoMin", "TcpRtoMax",
91 "TcpMaxConn", "TcpCurrEstab"
94 static int useless_number(const char *id
)
97 for (i
=0; i
<sizeof(useless_numbers
)/sizeof(*useless_numbers
); i
++)
98 if (strcmp(id
, useless_numbers
[i
]) == 0)
103 static int match(const char *id
)
110 for (i
=0; i
<npatterns
; i
++) {
111 if (!fnmatch(patterns
[i
], id
, 0))
117 static void load_good_table(FILE *fp
)
120 struct nstat_ent
*db
= NULL
;
123 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
125 unsigned long long val
;
127 char idbuf
[sizeof(buf
)];
129 buf
[strlen(buf
)-1] = 0;
130 if (info_source
[0] && strcmp(info_source
, buf
+1))
133 strncat(info_source
, buf
+1, sizeof(info_source
)-1);
136 /* idbuf is as big as buf, so this is safe */
137 nr
= sscanf(buf
, "%s%llu%lg", idbuf
, &val
, &rate
);
142 if (useless_number(idbuf
))
144 if ((n
= malloc(sizeof(*n
))) == NULL
)
146 n
->id
= strdup(idbuf
);
161 static int count_spaces(const char *line
)
166 while ((c
= *line
++) != 0)
167 count
+= c
== ' ' || c
== '\n';
171 static void load_ugly_table(FILE *fp
)
174 struct nstat_ent
*db
= NULL
;
177 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
178 char idbuf
[sizeof(buf
)];
181 int count1
, count2
, skip
= 0;
183 p
= strchr(buf
, ':');
186 count1
= count_spaces(buf
);
189 strncat(idbuf
, buf
, sizeof(idbuf
) - 1);
195 if ((next
= strchr(p
, ' ')) != NULL
)
197 else if ((next
= strchr(p
, '\n')) != NULL
)
199 if (off
< sizeof(idbuf
)) {
201 strncat(idbuf
, p
, sizeof(idbuf
) - off
- 1);
203 n
= malloc(sizeof(*n
));
206 n
->id
= strdup(idbuf
);
213 if (fgets(buf
, sizeof(buf
), fp
) == NULL
)
215 count2
= count_spaces(buf
);
217 skip
= count2
- count1
;
219 p
= strrchr(buf
, ' ');
223 if (sscanf(p
+1, "%llu", &n
->val
) != 1)
225 /* Trick to skip "dummy" trailing ICMP MIB in 2.4 */
230 } while (p
> buf
+ off
+ 2);
236 if (useless_number(n
->id
)) {
246 static void load_snmp(void)
248 FILE *fp
= fdopen(net_snmp_open(), "r");
255 static void load_snmp6(void)
257 FILE *fp
= fdopen(net_snmp6_open(), "r");
264 static void load_netstat(void)
266 FILE *fp
= fdopen(net_netstat_open(), "r");
274 static void dump_kern_db(FILE *fp
, int to_hist
)
276 json_writer_t
*jw
= json_output
? jsonw_new(fp
) : NULL
;
277 struct nstat_ent
*n
, *h
;
281 jsonw_pretty(jw
, pretty
);
282 jsonw_name(jw
, info_source
);
283 jsonw_start_object(jw
);
285 fprintf(fp
, "#%s\n", info_source
);
287 for (n
=kern_db
; n
; n
=n
->next
) {
288 unsigned long long val
= n
->val
;
289 if (!dump_zeros
&& !val
&& !n
->rate
)
292 struct nstat_ent
*h1
;
295 for (h1
= h
; h1
; h1
= h1
->next
) {
296 if (strcmp(h1
->id
, n
->id
) == 0) {
305 jsonw_uint_field(jw
, n
->id
, val
);
307 fprintf(fp
, "%-32s%-16llu%6.1f\n", n
->id
, val
, n
->rate
);
311 jsonw_end_object(jw
);
316 static void dump_incr_db(FILE *fp
)
318 json_writer_t
*jw
= json_output
? jsonw_new(fp
) : NULL
;
319 struct nstat_ent
*n
, *h
;
323 jsonw_pretty(jw
, pretty
);
324 jsonw_name(jw
, info_source
);
325 jsonw_start_object(jw
);
327 fprintf(fp
, "#%s\n", info_source
);
329 for (n
=kern_db
; n
; n
=n
->next
) {
331 unsigned long long val
= n
->val
;
332 struct nstat_ent
*h1
;
333 for (h1
= h
; h1
; h1
= h1
->next
) {
334 if (strcmp(h1
->id
, n
->id
) == 0) {
344 if (!dump_zeros
&& !val
&& !n
->rate
)
350 jsonw_uint_field(jw
, n
->id
, val
);
352 fprintf(fp
, "%-32s%-16llu%6.1f%s\n", n
->id
, val
,
353 n
->rate
, ovfl
?" (overflow)":"");
357 jsonw_end_object(jw
);
364 static void sigchild(int signo
)
368 static void update_db(int interval
)
370 struct nstat_ent
*n
, *h
;
382 for (n
= kern_db
; n
; n
= n
->next
) {
383 struct nstat_ent
*h1
;
384 for (h1
= h
; h1
; h1
= h1
->next
) {
385 if (strcmp(h1
->id
, n
->id
) == 0) {
387 unsigned long long incr
= h1
->val
- n
->val
;
390 sample
= (double)incr
* 1000.0 / interval
;
391 if (interval
>= scan_interval
) {
392 n
->rate
+= W
*(sample
-n
->rate
);
393 } else if (interval
>= 1000) {
394 if (interval
>= time_constant
) {
397 double w
= W
*(double)interval
/scan_interval
;
398 n
->rate
+= w
*(sample
-n
->rate
);
403 struct nstat_ent
*tmp
= h
;
417 #define T_DIFF(a,b) (((a).tv_sec-(b).tv_sec)*1000 + ((a).tv_usec-(b).tv_usec)/1000)
420 static void server_loop(int fd
)
422 struct timeval snaptime
= { 0 };
425 p
.events
= p
.revents
= POLLIN
;
427 sprintf(info_source
, "%d.%lu sampling_interval=%d time_const=%d",
428 getpid(), (unsigned long)random(), scan_interval
/1000, time_constant
/1000);
438 gettimeofday(&now
, NULL
);
439 tdiff
= T_DIFF(now
, snaptime
);
440 if (tdiff
>= scan_interval
) {
445 if (poll(&p
, 1, tdiff
+ scan_interval
) > 0
446 && (p
.revents
&POLLIN
)) {
447 int clnt
= accept(fd
, NULL
, NULL
);
452 } else if ((pid
= fork()) != 0) {
457 FILE *fp
= fdopen(clnt
, "w");
467 while (children
&& waitpid(-1, &status
, WNOHANG
) > 0)
472 static int verify_forging(int fd
)
475 socklen_t olen
= sizeof(cred
);
477 if (getsockopt(fd
, SOL_SOCKET
, SO_PEERCRED
, (void*)&cred
, &olen
) ||
480 if (cred
.uid
== getuid() || cred
.uid
== 0)
485 static void usage(void) __attribute__((noreturn
));
487 static void usage(void)
490 "Usage: nstat [OPTION] [ PATTERN [ PATTERN ] ]\n"
491 " -h, --help this message\n"
492 " -a, --ignore ignore history\n"
493 " -d, --scan=SECS sample every statistics every SECS\n"
494 " -j, --json format output in JSON\n"
495 " -n, --nooutput do history only\n"
496 " -p, --pretty pretty print\n"
497 " -r, --reset reset history\n"
498 " -s, --noupdate don\'t update history\n"
499 " -t, --interval=SECS report average over the last SECS\n"
500 " -V, --version output version information\n"
501 " -z, --zeros show entries with zero activity\n");
505 static const struct option longopts
[] = {
506 { "help", 0, 0, 'h' },
507 { "ignore", 0, 0, 'a' },
508 { "scan", 1, 0, 'd'},
509 { "nooutput", 0, 0, 'n' },
510 { "json", 0, 0, 'j' },
511 { "reset", 0, 0, 'r' },
512 { "noupdate", 0, 0, 's' },
513 { "pretty", 0, 0, 'p' },
514 { "interval", 1, 0, 't' },
515 { "version", 0, 0, 'V' },
516 { "zeros", 0, 0, 'z' },
520 int main(int argc
, char *argv
[])
523 struct sockaddr_un sun
;
524 FILE *hist_fp
= NULL
;
528 while ((ch
= getopt_long(argc
, argv
, "h?vVzrnasd:t:jp",
529 longopts
, NULL
)) != EOF
) {
547 scan_interval
= 1000*atoi(optarg
);
550 if (sscanf(optarg
, "%d", &time_constant
) != 1 ||
551 time_constant
<= 0) {
552 fprintf(stderr
, "nstat: invalid time constant divisor\n");
564 printf("nstat utility, iproute2-ss%s\n", SNAPSHOT
);
576 sun
.sun_family
= AF_UNIX
;
578 sprintf(sun
.sun_path
+1, "nstat%d", getuid());
580 if (scan_interval
> 0) {
581 if (time_constant
== 0)
583 time_constant
*= 1000;
584 W
= 1 - 1/exp(log(10)*(double)scan_interval
/time_constant
);
585 if ((fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0) {
586 perror("nstat: socket");
589 if (bind(fd
, (struct sockaddr
*)&sun
, 2+1+strlen(sun
.sun_path
+1)) < 0) {
590 perror("nstat: bind");
593 if (listen(fd
, 5) < 0) {
594 perror("nstat: listen");
598 perror("nstat: daemon");
601 signal(SIGPIPE
, SIG_IGN
);
602 signal(SIGCHLD
, sigchild
);
610 if ((hist_name
= getenv("NSTAT_HISTORY")) == NULL
) {
611 hist_name
= malloc(128);
612 sprintf(hist_name
, "/tmp/.nstat.u%d", getuid());
618 if (!ignore_history
|| !no_update
) {
621 fd
= open(hist_name
, O_RDWR
|O_CREAT
|O_NOFOLLOW
, 0600);
623 perror("nstat: open history file");
626 if ((hist_fp
= fdopen(fd
, "r+")) == NULL
) {
627 perror("nstat: fdopen history file");
630 if (flock(fileno(hist_fp
), LOCK_EX
)) {
631 perror("nstat: flock history file");
634 if (fstat(fileno(hist_fp
), &stb
) != 0) {
635 perror("nstat: fstat history file");
638 if (stb
.st_nlink
!= 1 || stb
.st_uid
!= getuid()) {
639 fprintf(stderr
, "nstat: something is so wrong with history file, that I prefer not to proceed.\n");
642 if (!ignore_history
) {
645 if ((tfp
= fopen("/proc/uptime", "r")) != NULL
) {
646 if (fscanf(tfp
, "%ld", &uptime
) != 1)
650 if (uptime
>= 0 && time(NULL
) >= stb
.st_mtime
+uptime
) {
651 fprintf(stderr
, "nstat: history is aged out, resetting\n");
652 if (ftruncate(fileno(hist_fp
), 0) < 0)
653 perror("nstat: ftruncate");
657 load_good_table(hist_fp
);
663 if ((fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) >= 0 &&
664 (connect(fd
, (struct sockaddr
*)&sun
, 2+1+strlen(sun
.sun_path
+1)) == 0
665 || (strcpy(sun
.sun_path
+1, "nstat0"),
666 connect(fd
, (struct sockaddr
*)&sun
, 2+1+strlen(sun
.sun_path
+1)) == 0))
667 && verify_forging(fd
) == 0) {
668 FILE *sfp
= fdopen(fd
, "r");
669 load_good_table(sfp
);
670 if (hist_db
&& source_mismatch
) {
671 fprintf(stderr
, "nstat: history is stale, ignoring it.\n");
678 if (hist_db
&& info_source
[0] && strcmp(info_source
, "kernel")) {
679 fprintf(stderr
, "nstat: history is stale, ignoring it.\n");
686 if (info_source
[0] == 0)
687 strcpy(info_source
, "kernel");
691 if (ignore_history
|| hist_db
== NULL
)
692 dump_kern_db(stdout
, 0);
694 dump_incr_db(stdout
);
697 if (ftruncate(fileno(hist_fp
), 0) < 0)
698 perror("nstat: ftruncate");
702 dump_kern_db(hist_fp
, 1);