]>
git.proxmox.com Git - mirror_iproute2.git/blob - misc/ifstat.c
2 * ifstat.c handy utility to read net interface statistics
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 <libnetlink.h>
32 #include <json_writer.h>
34 #include <linux/if_link.h>
52 char info_source
[128];
55 #define MAXS (sizeof(struct rtnl_link_stats)/sizeof(__u32))
58 struct ifstat_ent
*next
;
61 unsigned long long val
[MAXS
];
66 static const char *stats
[MAXS
] = {
86 "tx_heartbeat_errors",
92 struct ifstat_ent
*kern_db
;
93 struct ifstat_ent
*hist_db
;
95 static int match(const char *id
)
102 for (i
= 0; i
< npatterns
; i
++) {
103 if (!fnmatch(patterns
[i
], id
, 0))
109 static int get_nlmsg(const struct sockaddr_nl
*who
,
110 struct nlmsghdr
*m
, void *arg
)
112 struct ifinfomsg
*ifi
= NLMSG_DATA(m
);
113 struct rtattr
*tb
[IFLA_MAX
+1];
114 int len
= m
->nlmsg_len
;
115 struct ifstat_ent
*n
;
118 if (m
->nlmsg_type
!= RTM_NEWLINK
)
121 len
-= NLMSG_LENGTH(sizeof(*ifi
));
125 if (!(ifi
->ifi_flags
&IFF_UP
))
128 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
129 if (tb
[IFLA_IFNAME
] == NULL
|| tb
[IFLA_STATS
] == NULL
)
132 n
= malloc(sizeof(*n
));
135 n
->ifindex
= ifi
->ifi_index
;
136 n
->name
= strdup(RTA_DATA(tb
[IFLA_IFNAME
]));
137 memcpy(&n
->ival
, RTA_DATA(tb
[IFLA_STATS
]), sizeof(n
->ival
));
138 memset(&n
->rate
, 0, sizeof(n
->rate
));
139 for (i
= 0; i
< MAXS
; i
++)
140 n
->val
[i
] = n
->ival
[i
];
146 static void load_info(void)
148 struct ifstat_ent
*db
, *n
;
149 struct rtnl_handle rth
;
151 if (rtnl_open(&rth
, 0) < 0)
154 if (rtnl_wilddump_request(&rth
, AF_INET
, RTM_GETLINK
) < 0) {
155 perror("Cannot send dump request");
159 if (rtnl_dump_filter(&rth
, get_nlmsg
, NULL
) < 0) {
160 fprintf(stderr
, "Dump terminated\n");
177 static void load_raw_table(FILE *fp
)
180 struct ifstat_ent
*db
= NULL
;
181 struct ifstat_ent
*n
;
183 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
189 buf
[strlen(buf
)-1] = 0;
190 if (info_source
[0] && strcmp(info_source
, buf
+1))
192 strncpy(info_source
, buf
+1, sizeof(info_source
)-1);
195 if ((n
= malloc(sizeof(*n
))) == NULL
)
198 if (!(p
= strchr(buf
, ' ')))
202 if (sscanf(buf
, "%d", &n
->ifindex
) != 1)
204 if (!(next
= strchr(p
, ' ')))
211 for (i
= 0; i
< MAXS
; i
++) {
214 if (!(next
= strchr(p
, ' ')))
217 if (sscanf(p
, "%llu", n
->val
+i
) != 1)
219 n
->ival
[i
] = (__u32
)n
->val
[i
];
221 if (!(next
= strchr(p
, ' ')))
224 if (sscanf(p
, "%u", &rate
) != 1)
241 static void dump_raw_db(FILE *fp
, int to_hist
)
243 json_writer_t
*jw
= json_output
? jsonw_new(fp
) : NULL
;
244 struct ifstat_ent
*n
, *h
;
248 jsonw_start_object(jw
);
249 jsonw_pretty(jw
, pretty
);
250 jsonw_name(jw
, info_source
);
251 jsonw_start_object(jw
);
253 fprintf(fp
, "#%s\n", info_source
);
255 for (n
= kern_db
; n
; n
= n
->next
) {
257 unsigned long long *vals
= n
->val
;
258 double *rates
= n
->rate
;
260 if (!match(n
->name
)) {
261 struct ifstat_ent
*h1
;
265 for (h1
= h
; h1
; h1
= h1
->next
) {
266 if (h1
->ifindex
== n
->ifindex
) {
276 jsonw_name(jw
, n
->name
);
277 jsonw_start_object(jw
);
279 for (i
= 0; i
< MAXS
&& stats
[i
]; i
++)
280 jsonw_uint_field(jw
, stats
[i
], vals
[i
]);
281 jsonw_end_object(jw
);
283 fprintf(fp
, "%d %s ", n
->ifindex
, n
->name
);
284 for (i
= 0; i
< MAXS
; i
++)
285 fprintf(fp
, "%llu %u ", vals
[i
],
286 (unsigned int)rates
[i
]);
291 jsonw_end_object(jw
);
293 jsonw_end_object(jw
);
298 /* use communication definitions of meg/kilo etc */
299 static const unsigned long long giga
= 1000000000ull;
300 static const unsigned long long mega
= 1000000;
301 static const unsigned long long kilo
= 1000;
303 static void format_rate(FILE *fp
, const unsigned long long *vals
,
304 const double *rates
, int i
)
309 fprintf(fp
, "%7lluM ", vals
[i
]/mega
);
310 else if (vals
[i
] > mega
)
311 fprintf(fp
, "%7lluK ", vals
[i
]/kilo
);
313 fprintf(fp
, "%8llu ", vals
[i
]);
315 if (rates
[i
] > mega
) {
316 sprintf(temp
, "%uM", (unsigned int)(rates
[i
]/mega
));
317 fprintf(fp
, "%-6s ", temp
);
318 } else if (rates
[i
] > kilo
) {
319 sprintf(temp
, "%uK", (unsigned int)(rates
[i
]/kilo
));
320 fprintf(fp
, "%-6s ", temp
);
322 fprintf(fp
, "%-6u ", (unsigned int)rates
[i
]);
325 static void format_pair(FILE *fp
, const unsigned long long *vals
, int i
, int k
)
330 fprintf(fp
, "%7lluM ", vals
[i
]/mega
);
331 else if (vals
[i
] > mega
)
332 fprintf(fp
, "%7lluK ", vals
[i
]/kilo
);
334 fprintf(fp
, "%8llu ", vals
[i
]);
336 if (vals
[k
] > giga
) {
337 sprintf(temp
, "%uM", (unsigned int)(vals
[k
]/mega
));
338 fprintf(fp
, "%-6s ", temp
);
339 } else if (vals
[k
] > mega
) {
340 sprintf(temp
, "%uK", (unsigned int)(vals
[k
]/kilo
));
341 fprintf(fp
, "%-6s ", temp
);
343 fprintf(fp
, "%-6u ", (unsigned int)vals
[k
]);
346 static void print_head(FILE *fp
)
348 fprintf(fp
, "#%s\n", info_source
);
349 fprintf(fp
, "%-15s ", "Interface");
351 fprintf(fp
, "%8s/%-6s ", "RX Pkts", "Rate");
352 fprintf(fp
, "%8s/%-6s ", "TX Pkts", "Rate");
353 fprintf(fp
, "%8s/%-6s ", "RX Data", "Rate");
354 fprintf(fp
, "%8s/%-6s\n", "TX Data", "Rate");
357 fprintf(fp
, "%-15s ", "");
358 fprintf(fp
, "%8s/%-6s ", "RX Errs", "Drop");
359 fprintf(fp
, "%8s/%-6s ", "TX Errs", "Drop");
360 fprintf(fp
, "%8s/%-6s ", "RX Over", "Rate");
361 fprintf(fp
, "%8s/%-6s\n", "TX Coll", "Rate");
363 fprintf(fp
, "%-15s ", "");
364 fprintf(fp
, "%8s/%-6s ", "RX Errs", "Rate");
365 fprintf(fp
, "%8s/%-6s ", "RX Drop", "Rate");
366 fprintf(fp
, "%8s/%-6s ", "RX Over", "Rate");
367 fprintf(fp
, "%8s/%-6s\n", "RX Leng", "Rate");
369 fprintf(fp
, "%-15s ", "");
370 fprintf(fp
, "%8s/%-6s ", "RX Crc", "Rate");
371 fprintf(fp
, "%8s/%-6s ", "RX Frm", "Rate");
372 fprintf(fp
, "%8s/%-6s ", "RX Fifo", "Rate");
373 fprintf(fp
, "%8s/%-6s\n", "RX Miss", "Rate");
375 fprintf(fp
, "%-15s ", "");
376 fprintf(fp
, "%8s/%-6s ", "TX Errs", "Rate");
377 fprintf(fp
, "%8s/%-6s ", "TX Drop", "Rate");
378 fprintf(fp
, "%8s/%-6s ", "TX Coll", "Rate");
379 fprintf(fp
, "%8s/%-6s\n", "TX Carr", "Rate");
381 fprintf(fp
, "%-15s ", "");
382 fprintf(fp
, "%8s/%-6s ", "TX Abrt", "Rate");
383 fprintf(fp
, "%8s/%-6s ", "TX Fifo", "Rate");
384 fprintf(fp
, "%8s/%-6s ", "TX Hear", "Rate");
385 fprintf(fp
, "%8s/%-6s\n", "TX Wind", "Rate");
389 static void print_one_json(json_writer_t
*jw
, const struct ifstat_ent
*n
,
390 const unsigned long long *vals
)
392 int i
, m
= show_errors
? 20 : 10;
394 jsonw_name(jw
, n
->name
);
395 jsonw_start_object(jw
);
397 for (i
= 0; i
< m
&& stats
[i
]; i
++)
398 jsonw_uint_field(jw
, stats
[i
], vals
[i
]);
400 jsonw_end_object(jw
);
403 static void print_one_if(FILE *fp
, const struct ifstat_ent
*n
,
404 const unsigned long long *vals
)
408 fprintf(fp
, "%-15s ", n
->name
);
409 for (i
= 0; i
< 4; i
++)
410 format_rate(fp
, vals
, n
->rate
, i
);
414 fprintf(fp
, "%-15s ", "");
415 format_pair(fp
, vals
, 4, 6);
416 format_pair(fp
, vals
, 5, 7);
417 format_rate(fp
, vals
, n
->rate
, 11);
418 format_rate(fp
, vals
, n
->rate
, 9);
421 fprintf(fp
, "%-15s ", "");
422 format_rate(fp
, vals
, n
->rate
, 4);
423 format_rate(fp
, vals
, n
->rate
, 6);
424 format_rate(fp
, vals
, n
->rate
, 11);
425 format_rate(fp
, vals
, n
->rate
, 10);
428 fprintf(fp
, "%-15s ", "");
429 format_rate(fp
, vals
, n
->rate
, 12);
430 format_rate(fp
, vals
, n
->rate
, 13);
431 format_rate(fp
, vals
, n
->rate
, 14);
432 format_rate(fp
, vals
, n
->rate
, 15);
435 fprintf(fp
, "%-15s ", "");
436 format_rate(fp
, vals
, n
->rate
, 5);
437 format_rate(fp
, vals
, n
->rate
, 7);
438 format_rate(fp
, vals
, n
->rate
, 9);
439 format_rate(fp
, vals
, n
->rate
, 17);
442 fprintf(fp
, "%-15s ", "");
443 format_rate(fp
, vals
, n
->rate
, 16);
444 format_rate(fp
, vals
, n
->rate
, 18);
445 format_rate(fp
, vals
, n
->rate
, 19);
446 format_rate(fp
, vals
, n
->rate
, 20);
451 static void dump_kern_db(FILE *fp
)
453 json_writer_t
*jw
= json_output
? jsonw_new(fp
) : NULL
;
454 struct ifstat_ent
*n
;
457 jsonw_start_object(jw
);
458 jsonw_pretty(jw
, pretty
);
459 jsonw_name(jw
, info_source
);
460 jsonw_start_object(jw
);
464 for (n
= kern_db
; n
; n
= n
->next
) {
469 print_one_json(jw
, n
, n
->val
);
471 print_one_if(fp
, n
, n
->val
);
474 fprintf(fp
, "\n} }\n");
477 static void dump_incr_db(FILE *fp
)
479 struct ifstat_ent
*n
, *h
;
480 json_writer_t
*jw
= json_output
? jsonw_new(fp
) : NULL
;
484 jsonw_start_object(jw
);
485 jsonw_pretty(jw
, pretty
);
486 jsonw_name(jw
, info_source
);
487 jsonw_start_object(jw
);
491 for (n
= kern_db
; n
; n
= n
->next
) {
493 unsigned long long vals
[MAXS
];
494 struct ifstat_ent
*h1
;
496 memcpy(vals
, n
->val
, sizeof(vals
));
498 for (h1
= h
; h1
; h1
= h1
->next
) {
499 if (h1
->ifindex
== n
->ifindex
) {
500 for (i
= 0; i
< MAXS
; i
++)
501 vals
[i
] -= h1
->val
[i
];
510 print_one_json(jw
, n
, n
->val
);
512 print_one_if(fp
, n
, vals
);
516 jsonw_end_object(jw
);
518 jsonw_end_object(jw
);
525 static void sigchild(int signo
)
529 static void update_db(int interval
)
531 struct ifstat_ent
*n
, *h
;
541 for (n
= kern_db
; n
; n
= n
->next
) {
542 struct ifstat_ent
*h1
;
544 for (h1
= h
; h1
; h1
= h1
->next
) {
545 if (h1
->ifindex
== n
->ifindex
) {
548 for (i
= 0; i
< MAXS
; i
++) {
549 if ((long)(h1
->ival
[i
] - n
->ival
[i
]) < 0) {
550 memset(n
->ival
, 0, sizeof(n
->ival
));
554 for (i
= 0; i
< MAXS
; i
++) {
556 unsigned long incr
= h1
->ival
[i
] - n
->ival
[i
];
559 n
->ival
[i
] = h1
->ival
[i
];
560 sample
= (double)(incr
*1000)/interval
;
561 if (interval
>= scan_interval
) {
562 n
->rate
[i
] += W
*(sample
-n
->rate
[i
]);
563 } else if (interval
>= 1000) {
564 if (interval
>= time_constant
) {
567 double w
= W
*(double)interval
/scan_interval
;
569 n
->rate
[i
] += w
*(sample
-n
->rate
[i
]);
575 struct ifstat_ent
*tmp
= h
;
590 #define T_DIFF(a, b) (((a).tv_sec-(b).tv_sec)*1000 + ((a).tv_usec-(b).tv_usec)/1000)
593 static void server_loop(int fd
)
595 struct timeval snaptime
= { 0 };
599 p
.events
= p
.revents
= POLLIN
;
601 sprintf(info_source
, "%d.%lu sampling_interval=%d time_const=%d",
602 getpid(), (unsigned long)random(), scan_interval
/1000, time_constant
/1000);
611 gettimeofday(&now
, NULL
);
612 tdiff
= T_DIFF(now
, snaptime
);
613 if (tdiff
>= scan_interval
) {
619 if (poll(&p
, 1, scan_interval
- tdiff
) > 0
620 && (p
.revents
&POLLIN
)) {
621 int clnt
= accept(fd
, NULL
, NULL
);
628 } else if ((pid
= fork()) != 0) {
633 FILE *fp
= fdopen(clnt
, "w");
641 while (children
&& waitpid(-1, &status
, WNOHANG
) > 0)
646 static int verify_forging(int fd
)
649 socklen_t olen
= sizeof(cred
);
651 if (getsockopt(fd
, SOL_SOCKET
, SO_PEERCRED
, (void *)&cred
, &olen
) ||
654 if (cred
.uid
== getuid() || cred
.uid
== 0)
659 static void usage(void) __attribute__((noreturn
));
661 static void usage(void)
664 "Usage: ifstat [OPTION] [ PATTERN [ PATTERN ] ]\n"
665 " -h, --help this message\n"
666 " -a, --ignore ignore history\n"
667 " -d, --scan=SECS sample every statistics every SECS\n"
668 " -e, --errors show errors\n"
669 " -j, --json format output in JSON\n"
670 " -n, --nooutput do history only\n"
671 " -p, --pretty pretty print\n"
672 " -r, --reset reset history\n"
673 " -s, --noupdate don\'t update history\n"
674 " -t, --interval=SECS report average over the last SECS\n"
675 " -V, --version output version information\n"
676 " -z, --zeros show entries with zero activity\n");
681 static const struct option longopts
[] = {
682 { "help", 0, 0, 'h' },
683 { "ignore", 0, 0, 'a' },
684 { "scan", 1, 0, 'd'},
685 { "errors", 0, 0, 'e' },
686 { "nooutput", 0, 0, 'n' },
687 { "json", 0, 0, 'j' },
688 { "reset", 0, 0, 'r' },
689 { "pretty", 0, 0, 'p' },
690 { "noupdate", 0, 0, 's' },
691 { "interval", 1, 0, 't' },
692 { "version", 0, 0, 'V' },
693 { "zeros", 0, 0, 'z' },
697 int main(int argc
, char *argv
[])
700 struct sockaddr_un sun
;
701 FILE *hist_fp
= NULL
;
705 while ((ch
= getopt_long(argc
, argv
, "hjpvVzrnasd:t:e",
706 longopts
, NULL
)) != EOF
) {
733 scan_interval
= atoi(optarg
) * 1000;
734 if (scan_interval
<= 0) {
735 fprintf(stderr
, "ifstat: invalid scan interval\n");
740 time_constant
= atoi(optarg
);
741 if (time_constant
<= 0) {
742 fprintf(stderr
, "ifstat: invalid time constant divisor\n");
748 printf("ifstat utility, iproute2-ss%s\n", SNAPSHOT
);
760 sun
.sun_family
= AF_UNIX
;
762 sprintf(sun
.sun_path
+1, "ifstat%d", getuid());
764 if (scan_interval
> 0) {
765 if (time_constant
== 0)
767 time_constant
*= 1000;
768 W
= 1 - 1/exp(log(10)*(double)scan_interval
/time_constant
);
769 if ((fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0) {
770 perror("ifstat: socket");
773 if (bind(fd
, (struct sockaddr
*)&sun
, 2+1+strlen(sun
.sun_path
+1)) < 0) {
774 perror("ifstat: bind");
777 if (listen(fd
, 5) < 0) {
778 perror("ifstat: listen");
782 perror("ifstat: daemon");
785 signal(SIGPIPE
, SIG_IGN
);
786 signal(SIGCHLD
, sigchild
);
794 if (getenv("IFSTAT_HISTORY"))
795 snprintf(hist_name
, sizeof(hist_name
),
796 "%s", getenv("IFSTAT_HISTORY"));
798 snprintf(hist_name
, sizeof(hist_name
),
799 "%s/.ifstat.u%d", P_tmpdir
, getuid());
804 if (!ignore_history
|| !no_update
) {
807 fd
= open(hist_name
, O_RDWR
|O_CREAT
|O_NOFOLLOW
, 0600);
809 perror("ifstat: open history file");
812 if ((hist_fp
= fdopen(fd
, "r+")) == NULL
) {
813 perror("ifstat: fdopen history file");
816 if (flock(fileno(hist_fp
), LOCK_EX
)) {
817 perror("ifstat: flock history file");
820 if (fstat(fileno(hist_fp
), &stb
) != 0) {
821 perror("ifstat: fstat history file");
824 if (stb
.st_nlink
!= 1 || stb
.st_uid
!= getuid()) {
825 fprintf(stderr
, "ifstat: something is so wrong with history file, that I prefer not to proceed.\n");
828 if (!ignore_history
) {
832 if ((tfp
= fopen("/proc/uptime", "r")) != NULL
) {
833 if (fscanf(tfp
, "%ld", &uptime
) != 1)
837 if (uptime
>= 0 && time(NULL
) >= stb
.st_mtime
+uptime
) {
838 fprintf(stderr
, "ifstat: history is aged out, resetting\n");
839 if (ftruncate(fileno(hist_fp
), 0))
840 perror("ifstat: ftruncate");
844 load_raw_table(hist_fp
);
850 if ((fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) >= 0 &&
851 (connect(fd
, (struct sockaddr
*)&sun
, 2+1+strlen(sun
.sun_path
+1)) == 0
852 || (strcpy(sun
.sun_path
+1, "ifstat0"),
853 connect(fd
, (struct sockaddr
*)&sun
, 2+1+strlen(sun
.sun_path
+1)) == 0))
854 && verify_forging(fd
) == 0) {
855 FILE *sfp
= fdopen(fd
, "r");
858 if (hist_db
&& source_mismatch
) {
859 fprintf(stderr
, "ifstat: history is stale, ignoring it.\n");
866 if (hist_db
&& info_source
[0] && strcmp(info_source
, "kernel")) {
867 fprintf(stderr
, "ifstat: history is stale, ignoring it.\n");
872 if (info_source
[0] == 0)
873 strcpy(info_source
, "kernel");
877 if (ignore_history
|| hist_db
== NULL
)
878 dump_kern_db(stdout
);
880 dump_incr_db(stdout
);
884 if (ftruncate(fileno(hist_fp
), 0))
885 perror("ifstat: ftruncate");
889 dump_raw_db(hist_fp
, 1);