]>
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>
33 #include <linux/if_link.h>
38 int reset_history
= 0;
39 int ignore_history
= 0;
43 int scan_interval
= 0;
44 int time_constant
= 0;
50 char info_source
[128];
53 #define MAXS (sizeof(struct rtnl_link_stats)/sizeof(__u32))
57 struct ifstat_ent
*next
;
60 unsigned long long val
[MAXS
];
65 static const char *stats
[MAXS
] = {
85 "tx_heartbeat_errors",
91 struct ifstat_ent
*kern_db
;
92 struct ifstat_ent
*hist_db
;
94 static int match(const char *id
)
101 for (i
=0; i
<npatterns
; i
++) {
102 if (!fnmatch(patterns
[i
], id
, 0))
108 static int get_nlmsg(const struct sockaddr_nl
*who
,
109 struct nlmsghdr
*m
, void *arg
)
111 struct ifinfomsg
*ifi
= NLMSG_DATA(m
);
112 struct rtattr
* tb
[IFLA_MAX
+1];
113 int len
= m
->nlmsg_len
;
114 struct ifstat_ent
*n
;
117 if (m
->nlmsg_type
!= RTM_NEWLINK
)
120 len
-= NLMSG_LENGTH(sizeof(*ifi
));
124 if (!(ifi
->ifi_flags
&IFF_UP
))
127 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
128 if (tb
[IFLA_IFNAME
] == NULL
|| tb
[IFLA_STATS
] == NULL
)
131 n
= malloc(sizeof(*n
));
134 n
->ifindex
= ifi
->ifi_index
;
135 n
->name
= strdup(RTA_DATA(tb
[IFLA_IFNAME
]));
136 memcpy(&n
->ival
, RTA_DATA(tb
[IFLA_STATS
]), sizeof(n
->ival
));
137 memset(&n
->rate
, 0, sizeof(n
->rate
));
138 for (i
=0; i
<MAXS
; i
++)
139 n
->val
[i
] = n
->ival
[i
];
145 static void load_info(void)
147 struct ifstat_ent
*db
, *n
;
148 struct rtnl_handle rth
;
150 if (rtnl_open(&rth
, 0) < 0)
153 if (rtnl_wilddump_request(&rth
, AF_INET
, RTM_GETLINK
) < 0) {
154 perror("Cannot send dump request");
158 if (rtnl_dump_filter(&rth
, get_nlmsg
, NULL
) < 0) {
159 fprintf(stderr
, "Dump terminated\n");
176 static void load_raw_table(FILE *fp
)
179 struct ifstat_ent
*db
= NULL
;
180 struct ifstat_ent
*n
;
182 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
188 buf
[strlen(buf
)-1] = 0;
189 if (info_source
[0] && strcmp(info_source
, buf
+1))
191 strncpy(info_source
, buf
+1, sizeof(info_source
)-1);
194 if ((n
= malloc(sizeof(*n
))) == NULL
)
197 if (!(p
= strchr(buf
, ' ')))
201 if (sscanf(buf
, "%d", &n
->ifindex
) != 1)
203 if (!(next
= strchr(p
, ' ')))
210 for (i
=0; i
<MAXS
; i
++) {
212 if (!(next
= strchr(p
, ' ')))
215 if (sscanf(p
, "%llu", n
->val
+i
) != 1)
217 n
->ival
[i
] = (__u32
)n
->val
[i
];
219 if (!(next
= strchr(p
, ' ')))
222 if (sscanf(p
, "%u", &rate
) != 1)
239 static void dump_raw_db(FILE *fp
, int to_hist
)
241 struct ifstat_ent
*n
, *h
;
242 const char *eol
= "\n";
246 fprintf(fp
, "{ \"%s\":{", info_source
);
248 fprintf(fp
, "#%s\n", info_source
);
250 for (n
=kern_db
; n
; n
=n
->next
) {
252 unsigned long long *vals
= n
->val
;
253 double *rates
= n
->rate
;
254 if (!match(n
->name
)) {
255 struct ifstat_ent
*h1
;
258 for (h1
= h
; h1
; h1
= h1
->next
) {
259 if (h1
->ifindex
== n
->ifindex
) {
269 fprintf(fp
, "%s \"%s\":{",
272 for (i
=0; i
<MAXS
&& stats
[i
]; i
++)
273 fprintf(fp
, " \"%s\":%llu",
277 fprintf(fp
, "%d %s ", n
->ifindex
, n
->name
);
278 for (i
=0; i
<MAXS
; i
++)
279 fprintf(fp
, "%llu %u ", vals
[i
],
286 /* use communication definitions of meg/kilo etc */
287 static const unsigned long long giga
= 1000000000ull;
288 static const unsigned long long mega
= 1000000;
289 static const unsigned long long kilo
= 1000;
291 static void format_rate(FILE *fp
, const unsigned long long *vals
,
292 const double *rates
, int i
)
297 fprintf(fp
, "%7lluM ", vals
[i
]/mega
);
298 else if (vals
[i
] > mega
)
299 fprintf(fp
, "%7lluK ", vals
[i
]/kilo
);
301 fprintf(fp
, "%8llu ", vals
[i
]);
303 if (rates
[i
] > mega
) {
304 sprintf(temp
, "%uM", (unsigned)(rates
[i
]/mega
));
305 fprintf(fp
, "%-6s ", temp
);
306 } else if (rates
[i
] > kilo
) {
307 sprintf(temp
, "%uK", (unsigned)(rates
[i
]/kilo
));
308 fprintf(fp
, "%-6s ", temp
);
310 fprintf(fp
, "%-6u ", (unsigned)rates
[i
]);
313 static void format_pair(FILE *fp
, const unsigned long long *vals
, int i
, int k
)
317 fprintf(fp
, "%7lluM ", vals
[i
]/mega
);
318 else if (vals
[i
] > mega
)
319 fprintf(fp
, "%7lluK ", vals
[i
]/kilo
);
321 fprintf(fp
, "%8llu ", vals
[i
]);
323 if (vals
[k
] > giga
) {
324 sprintf(temp
, "%uM", (unsigned)(vals
[k
]/mega
));
325 fprintf(fp
, "%-6s ", temp
);
326 } else if (vals
[k
] > mega
) {
327 sprintf(temp
, "%uK", (unsigned)(vals
[k
]/kilo
));
328 fprintf(fp
, "%-6s ", temp
);
330 fprintf(fp
, "%-6u ", (unsigned)vals
[k
]);
333 static void print_head(FILE *fp
)
335 fprintf(fp
, "#%s\n", info_source
);
336 fprintf(fp
, "%-15s ", "Interface");
338 fprintf(fp
, "%8s/%-6s ", "RX Pkts", "Rate");
339 fprintf(fp
, "%8s/%-6s ", "TX Pkts", "Rate");
340 fprintf(fp
, "%8s/%-6s ", "RX Data", "Rate");
341 fprintf(fp
, "%8s/%-6s\n","TX Data", "Rate");
344 fprintf(fp
, "%-15s ", "");
345 fprintf(fp
, "%8s/%-6s ", "RX Errs", "Drop");
346 fprintf(fp
, "%8s/%-6s ", "TX Errs", "Drop");
347 fprintf(fp
, "%8s/%-6s ", "RX Over", "Rate");
348 fprintf(fp
, "%8s/%-6s\n","TX Coll", "Rate");
350 fprintf(fp
, "%-15s ", "");
351 fprintf(fp
, "%8s/%-6s ", "RX Errs", "Rate");
352 fprintf(fp
, "%8s/%-6s ", "RX Drop", "Rate");
353 fprintf(fp
, "%8s/%-6s ", "RX Over", "Rate");
354 fprintf(fp
, "%8s/%-6s\n","RX Leng", "Rate");
356 fprintf(fp
, "%-15s ", "");
357 fprintf(fp
, "%8s/%-6s ", "RX Crc", "Rate");
358 fprintf(fp
, "%8s/%-6s ", "RX Frm", "Rate");
359 fprintf(fp
, "%8s/%-6s ", "RX Fifo", "Rate");
360 fprintf(fp
, "%8s/%-6s\n","RX Miss", "Rate");
362 fprintf(fp
, "%-15s ", "");
363 fprintf(fp
, "%8s/%-6s ", "TX Errs", "Rate");
364 fprintf(fp
, "%8s/%-6s ", "TX Drop", "Rate");
365 fprintf(fp
, "%8s/%-6s ", "TX Coll", "Rate");
366 fprintf(fp
, "%8s/%-6s\n","TX Carr", "Rate");
368 fprintf(fp
, "%-15s ", "");
369 fprintf(fp
, "%8s/%-6s ", "TX Abrt", "Rate");
370 fprintf(fp
, "%8s/%-6s ", "TX Fifo", "Rate");
371 fprintf(fp
, "%8s/%-6s ", "TX Hear", "Rate");
372 fprintf(fp
, "%8s/%-6s\n","TX Wind", "Rate");
376 static void print_one_json(FILE *fp
, const struct ifstat_ent
*n
,
377 const unsigned long long *vals
)
380 const char *sep
= " ";
382 m
= show_errors
? 20 : 10;
383 fprintf(fp
, " \"%s\":{", n
->name
);
384 for (i
=0; i
< m
&& stats
[i
]; i
++) {
385 fprintf(fp
, "%s\"%s\":%llu",
386 sep
, stats
[i
], vals
[i
]);
392 static void print_one_if(FILE *fp
, const struct ifstat_ent
*n
,
393 const unsigned long long *vals
)
397 fprintf(fp
, "%-15s ", n
->name
);
399 format_rate(fp
, vals
, n
->rate
, i
);
403 fprintf(fp
, "%-15s ", "");
404 format_pair(fp
, vals
, 4, 6);
405 format_pair(fp
, vals
, 5, 7);
406 format_rate(fp
, vals
, n
->rate
, 11);
407 format_rate(fp
, vals
, n
->rate
, 9);
410 fprintf(fp
, "%-15s ", "");
411 format_rate(fp
, vals
, n
->rate
, 4);
412 format_rate(fp
, vals
, n
->rate
, 6);
413 format_rate(fp
, vals
, n
->rate
, 11);
414 format_rate(fp
, vals
, n
->rate
, 10);
417 fprintf(fp
, "%-15s ", "");
418 format_rate(fp
, vals
, n
->rate
, 12);
419 format_rate(fp
, vals
, n
->rate
, 13);
420 format_rate(fp
, vals
, n
->rate
, 14);
421 format_rate(fp
, vals
, n
->rate
, 15);
424 fprintf(fp
, "%-15s ", "");
425 format_rate(fp
, vals
, n
->rate
, 5);
426 format_rate(fp
, vals
, n
->rate
, 7);
427 format_rate(fp
, vals
, n
->rate
, 9);
428 format_rate(fp
, vals
, n
->rate
, 17);
431 fprintf(fp
, "%-15s ", "");
432 format_rate(fp
, vals
, n
->rate
, 16);
433 format_rate(fp
, vals
, n
->rate
, 18);
434 format_rate(fp
, vals
, n
->rate
, 19);
435 format_rate(fp
, vals
, n
->rate
, 20);
440 static void dump_kern_db(FILE *fp
)
442 struct ifstat_ent
*n
;
443 const char *eol
= "\n";
446 fprintf(fp
, "{ \"%s\": {", info_source
);
450 for (n
=kern_db
; n
; n
=n
->next
) {
455 fprintf(fp
, "%s", eol
);
457 print_one_json(fp
, n
, n
->val
);
459 print_one_if(fp
, n
, n
->val
);
462 fprintf(fp
, "\n} }\n");
466 static void dump_incr_db(FILE *fp
)
468 struct ifstat_ent
*n
, *h
;
469 const char *eol
= "\n";
473 fprintf(fp
, "{ \"%s\":{", info_source
);
477 for (n
=kern_db
; n
; n
=n
->next
) {
479 unsigned long long vals
[MAXS
];
480 struct ifstat_ent
*h1
;
482 memcpy(vals
, n
->val
, sizeof(vals
));
484 for (h1
= h
; h1
; h1
= h1
->next
) {
485 if (h1
->ifindex
== n
->ifindex
) {
486 for (i
= 0; i
< MAXS
; i
++)
487 vals
[i
] -= h1
->val
[i
];
496 fprintf(fp
, "%s", eol
);
498 print_one_json(fp
, n
, n
->val
);
500 print_one_if(fp
, n
, vals
);
503 fprintf(fp
, "\n} }\n");
509 static void sigchild(int signo
)
513 static void update_db(int interval
)
515 struct ifstat_ent
*n
, *h
;
525 for (n
= kern_db
; n
; n
= n
->next
) {
526 struct ifstat_ent
*h1
;
527 for (h1
= h
; h1
; h1
= h1
->next
) {
528 if (h1
->ifindex
== n
->ifindex
) {
530 for (i
= 0; i
< MAXS
; i
++) {
531 if ((long)(h1
->ival
[i
] - n
->ival
[i
]) < 0) {
532 memset(n
->ival
, 0, sizeof(n
->ival
));
536 for (i
= 0; i
< MAXS
; i
++) {
538 unsigned long incr
= h1
->ival
[i
] - n
->ival
[i
];
540 n
->ival
[i
] = h1
->ival
[i
];
541 sample
= (double)(incr
*1000)/interval
;
542 if (interval
>= scan_interval
) {
543 n
->rate
[i
] += W
*(sample
-n
->rate
[i
]);
544 } else if (interval
>= 1000) {
545 if (interval
>= time_constant
) {
548 double w
= W
*(double)interval
/scan_interval
;
549 n
->rate
[i
] += w
*(sample
-n
->rate
[i
]);
555 struct ifstat_ent
*tmp
= h
;
569 #define T_DIFF(a,b) (((a).tv_sec-(b).tv_sec)*1000 + ((a).tv_usec-(b).tv_usec)/1000)
572 static void server_loop(int fd
)
574 struct timeval snaptime
= { 0 };
577 p
.events
= p
.revents
= POLLIN
;
579 sprintf(info_source
, "%d.%lu sampling_interval=%d time_const=%d",
580 getpid(), (unsigned long)random(), scan_interval
/1000, time_constant
/1000);
589 gettimeofday(&now
, NULL
);
590 tdiff
= T_DIFF(now
, snaptime
);
591 if (tdiff
>= scan_interval
) {
597 if (poll(&p
, 1, tdiff
+ scan_interval
) > 0
598 && (p
.revents
&POLLIN
)) {
599 int clnt
= accept(fd
, NULL
, NULL
);
604 } else if ((pid
= fork()) != 0) {
609 FILE *fp
= fdopen(clnt
, "w");
619 while (children
&& waitpid(-1, &status
, WNOHANG
) > 0)
624 static int verify_forging(int fd
)
627 socklen_t olen
= sizeof(cred
);
629 if (getsockopt(fd
, SOL_SOCKET
, SO_PEERCRED
, (void*)&cred
, &olen
) ||
632 if (cred
.uid
== getuid() || cred
.uid
== 0)
637 static void usage(void) __attribute__((noreturn
));
639 static void usage(void)
642 "Usage: ifstat [OPTION] [ PATTERN [ PATTERN ] ]\n"
643 " -h, --help this message\n"
644 " -a, --ignore ignore history\n"
645 " -d, --scan=SECS sample every statistics every SECS\n"
646 " -e, --errors show errors\n"
647 " -j, --json format output in JSON\n"
648 " -n, --nooutput do history only\n"
649 " -r, --reset reset history\n"
650 " -s, --noupdate don\'t update history\n"
651 " -t, --interval=SECS report average over the last SECS\n"
652 " -V, --version output version information\n"
653 " -z, --zeros show entries with zero activity\n");
658 static const struct option longopts
[] = {
659 { "help", 0, 0, 'h' },
660 { "ignore", 0, 0, 'a' },
661 { "scan", 1, 0, 'd'},
662 { "errors", 0, 0, 'e' },
663 { "nooutput", 0, 0, 'n' },
664 { "json", 0, 0, 'j' },
665 { "reset", 0, 0, 'r' },
666 { "noupdate", 0, 0, 's' },
667 { "interval", 1, 0, 't' },
668 { "version", 0, 0, 'V' },
669 { "zeros", 0, 0, 'z' },
673 int main(int argc
, char *argv
[])
676 struct sockaddr_un sun
;
677 FILE *hist_fp
= NULL
;
681 while ((ch
= getopt_long(argc
, argv
, "hjvVzrnasd:t:e",
682 longopts
, NULL
)) != EOF
) {
706 scan_interval
= atoi(optarg
) * 1000;
707 if (scan_interval
<= 0) {
708 fprintf(stderr
, "ifstat: invalid scan interval\n");
713 time_constant
= atoi(optarg
);
714 if (time_constant
<= 0) {
715 fprintf(stderr
, "ifstat: invalid time constant divisor\n");
721 printf("ifstat utility, iproute2-ss%s\n", SNAPSHOT
);
733 sun
.sun_family
= AF_UNIX
;
735 sprintf(sun
.sun_path
+1, "ifstat%d", getuid());
737 if (scan_interval
> 0) {
738 if (time_constant
== 0)
740 time_constant
*= 1000;
741 W
= 1 - 1/exp(log(10)*(double)scan_interval
/time_constant
);
742 if ((fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0) {
743 perror("ifstat: socket");
746 if (bind(fd
, (struct sockaddr
*)&sun
, 2+1+strlen(sun
.sun_path
+1)) < 0) {
747 perror("ifstat: bind");
750 if (listen(fd
, 5) < 0) {
751 perror("ifstat: listen");
755 perror("ifstat: daemon");
758 signal(SIGPIPE
, SIG_IGN
);
759 signal(SIGCHLD
, sigchild
);
767 if (getenv("IFSTAT_HISTORY"))
768 snprintf(hist_name
, sizeof(hist_name
),
769 "%s", getenv("IFSTAT_HISTORY"));
771 snprintf(hist_name
, sizeof(hist_name
),
772 "%s/.ifstat.u%d", P_tmpdir
, getuid());
777 if (!ignore_history
|| !no_update
) {
780 fd
= open(hist_name
, O_RDWR
|O_CREAT
|O_NOFOLLOW
, 0600);
782 perror("ifstat: open history file");
785 if ((hist_fp
= fdopen(fd
, "r+")) == NULL
) {
786 perror("ifstat: fdopen history file");
789 if (flock(fileno(hist_fp
), LOCK_EX
)) {
790 perror("ifstat: flock history file");
793 if (fstat(fileno(hist_fp
), &stb
) != 0) {
794 perror("ifstat: fstat history file");
797 if (stb
.st_nlink
!= 1 || stb
.st_uid
!= getuid()) {
798 fprintf(stderr
, "ifstat: something is so wrong with history file, that I prefer not to proceed.\n");
801 if (!ignore_history
) {
804 if ((tfp
= fopen("/proc/uptime", "r")) != NULL
) {
805 if (fscanf(tfp
, "%ld", &uptime
) != 1)
809 if (uptime
>= 0 && time(NULL
) >= stb
.st_mtime
+uptime
) {
810 fprintf(stderr
, "ifstat: history is aged out, resetting\n");
811 ftruncate(fileno(hist_fp
), 0);
815 load_raw_table(hist_fp
);
821 if ((fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) >= 0 &&
822 (connect(fd
, (struct sockaddr
*)&sun
, 2+1+strlen(sun
.sun_path
+1)) == 0
823 || (strcpy(sun
.sun_path
+1, "ifstat0"),
824 connect(fd
, (struct sockaddr
*)&sun
, 2+1+strlen(sun
.sun_path
+1)) == 0))
825 && verify_forging(fd
) == 0) {
826 FILE *sfp
= fdopen(fd
, "r");
828 if (hist_db
&& source_mismatch
) {
829 fprintf(stderr
, "ifstat: history is stale, ignoring it.\n");
836 if (hist_db
&& info_source
[0] && strcmp(info_source
, "kernel")) {
837 fprintf(stderr
, "ifstat: history is stale, ignoring it.\n");
842 if (info_source
[0] == 0)
843 strcpy(info_source
, "kernel");
847 if (ignore_history
|| hist_db
== NULL
)
848 dump_kern_db(stdout
);
850 dump_incr_db(stdout
);
854 ftruncate(fileno(hist_fp
), 0);
858 dump_raw_db(hist_fp
, 1);