]>
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>
39 int reset_history
= 0;
40 int ignore_history
= 0;
44 int scan_interval
= 0;
45 int time_constant
= 0;
52 char info_source
[128];
55 #define MAXS (sizeof(struct rtnl_link_stats)/sizeof(__u32))
59 struct ifstat_ent
*next
;
62 unsigned long long val
[MAXS
];
67 static const char *stats
[MAXS
] = {
87 "tx_heartbeat_errors",
93 struct ifstat_ent
*kern_db
;
94 struct ifstat_ent
*hist_db
;
96 static int match(const char *id
)
103 for (i
=0; i
<npatterns
; i
++) {
104 if (!fnmatch(patterns
[i
], id
, 0))
110 static int get_nlmsg(const struct sockaddr_nl
*who
,
111 struct nlmsghdr
*m
, void *arg
)
113 struct ifinfomsg
*ifi
= NLMSG_DATA(m
);
114 struct rtattr
* tb
[IFLA_MAX
+1];
115 int len
= m
->nlmsg_len
;
116 struct ifstat_ent
*n
;
119 if (m
->nlmsg_type
!= RTM_NEWLINK
)
122 len
-= NLMSG_LENGTH(sizeof(*ifi
));
126 if (!(ifi
->ifi_flags
&IFF_UP
))
129 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
130 if (tb
[IFLA_IFNAME
] == NULL
|| tb
[IFLA_STATS
] == NULL
)
133 n
= malloc(sizeof(*n
));
136 n
->ifindex
= ifi
->ifi_index
;
137 n
->name
= strdup(RTA_DATA(tb
[IFLA_IFNAME
]));
138 memcpy(&n
->ival
, RTA_DATA(tb
[IFLA_STATS
]), sizeof(n
->ival
));
139 memset(&n
->rate
, 0, sizeof(n
->rate
));
140 for (i
=0; i
<MAXS
; i
++)
141 n
->val
[i
] = n
->ival
[i
];
147 static void load_info(void)
149 struct ifstat_ent
*db
, *n
;
150 struct rtnl_handle rth
;
152 if (rtnl_open(&rth
, 0) < 0)
155 if (rtnl_wilddump_request(&rth
, AF_INET
, RTM_GETLINK
) < 0) {
156 perror("Cannot send dump request");
160 if (rtnl_dump_filter(&rth
, get_nlmsg
, NULL
) < 0) {
161 fprintf(stderr
, "Dump terminated\n");
178 static void load_raw_table(FILE *fp
)
181 struct ifstat_ent
*db
= NULL
;
182 struct ifstat_ent
*n
;
184 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
190 buf
[strlen(buf
)-1] = 0;
191 if (info_source
[0] && strcmp(info_source
, buf
+1))
193 strncpy(info_source
, buf
+1, sizeof(info_source
)-1);
196 if ((n
= malloc(sizeof(*n
))) == NULL
)
199 if (!(p
= strchr(buf
, ' ')))
203 if (sscanf(buf
, "%d", &n
->ifindex
) != 1)
205 if (!(next
= strchr(p
, ' ')))
212 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_pretty(jw
, pretty
);
249 jsonw_name(jw
, info_source
);
250 jsonw_start_object(jw
);
252 fprintf(fp
, "#%s\n", info_source
);
254 for (n
=kern_db
; n
; n
=n
->next
) {
256 unsigned long long *vals
= n
->val
;
257 double *rates
= n
->rate
;
258 if (!match(n
->name
)) {
259 struct ifstat_ent
*h1
;
262 for (h1
= h
; h1
; h1
= h1
->next
) {
263 if (h1
->ifindex
== n
->ifindex
) {
273 jsonw_name(jw
, n
->name
);
274 jsonw_start_object(jw
);
276 for (i
=0; i
<MAXS
&& stats
[i
]; i
++)
277 jsonw_uint_field(jw
, stats
[i
], vals
[i
]);
278 jsonw_end_object(jw
);
280 fprintf(fp
, "%d %s ", n
->ifindex
, n
->name
);
281 for (i
=0; i
<MAXS
; i
++)
282 fprintf(fp
, "%llu %u ", vals
[i
],
288 jsonw_end_object(jw
);
293 /* use communication definitions of meg/kilo etc */
294 static const unsigned long long giga
= 1000000000ull;
295 static const unsigned long long mega
= 1000000;
296 static const unsigned long long kilo
= 1000;
298 static void format_rate(FILE *fp
, const unsigned long long *vals
,
299 const double *rates
, int i
)
304 fprintf(fp
, "%7lluM ", vals
[i
]/mega
);
305 else if (vals
[i
] > mega
)
306 fprintf(fp
, "%7lluK ", vals
[i
]/kilo
);
308 fprintf(fp
, "%8llu ", vals
[i
]);
310 if (rates
[i
] > mega
) {
311 sprintf(temp
, "%uM", (unsigned)(rates
[i
]/mega
));
312 fprintf(fp
, "%-6s ", temp
);
313 } else if (rates
[i
] > kilo
) {
314 sprintf(temp
, "%uK", (unsigned)(rates
[i
]/kilo
));
315 fprintf(fp
, "%-6s ", temp
);
317 fprintf(fp
, "%-6u ", (unsigned)rates
[i
]);
320 static void format_pair(FILE *fp
, const unsigned long long *vals
, int i
, int k
)
324 fprintf(fp
, "%7lluM ", vals
[i
]/mega
);
325 else if (vals
[i
] > mega
)
326 fprintf(fp
, "%7lluK ", vals
[i
]/kilo
);
328 fprintf(fp
, "%8llu ", vals
[i
]);
330 if (vals
[k
] > giga
) {
331 sprintf(temp
, "%uM", (unsigned)(vals
[k
]/mega
));
332 fprintf(fp
, "%-6s ", temp
);
333 } else if (vals
[k
] > mega
) {
334 sprintf(temp
, "%uK", (unsigned)(vals
[k
]/kilo
));
335 fprintf(fp
, "%-6s ", temp
);
337 fprintf(fp
, "%-6u ", (unsigned)vals
[k
]);
340 static void print_head(FILE *fp
)
342 fprintf(fp
, "#%s\n", info_source
);
343 fprintf(fp
, "%-15s ", "Interface");
345 fprintf(fp
, "%8s/%-6s ", "RX Pkts", "Rate");
346 fprintf(fp
, "%8s/%-6s ", "TX Pkts", "Rate");
347 fprintf(fp
, "%8s/%-6s ", "RX Data", "Rate");
348 fprintf(fp
, "%8s/%-6s\n","TX Data", "Rate");
351 fprintf(fp
, "%-15s ", "");
352 fprintf(fp
, "%8s/%-6s ", "RX Errs", "Drop");
353 fprintf(fp
, "%8s/%-6s ", "TX Errs", "Drop");
354 fprintf(fp
, "%8s/%-6s ", "RX Over", "Rate");
355 fprintf(fp
, "%8s/%-6s\n","TX Coll", "Rate");
357 fprintf(fp
, "%-15s ", "");
358 fprintf(fp
, "%8s/%-6s ", "RX Errs", "Rate");
359 fprintf(fp
, "%8s/%-6s ", "RX Drop", "Rate");
360 fprintf(fp
, "%8s/%-6s ", "RX Over", "Rate");
361 fprintf(fp
, "%8s/%-6s\n","RX Leng", "Rate");
363 fprintf(fp
, "%-15s ", "");
364 fprintf(fp
, "%8s/%-6s ", "RX Crc", "Rate");
365 fprintf(fp
, "%8s/%-6s ", "RX Frm", "Rate");
366 fprintf(fp
, "%8s/%-6s ", "RX Fifo", "Rate");
367 fprintf(fp
, "%8s/%-6s\n","RX Miss", "Rate");
369 fprintf(fp
, "%-15s ", "");
370 fprintf(fp
, "%8s/%-6s ", "TX Errs", "Rate");
371 fprintf(fp
, "%8s/%-6s ", "TX Drop", "Rate");
372 fprintf(fp
, "%8s/%-6s ", "TX Coll", "Rate");
373 fprintf(fp
, "%8s/%-6s\n","TX Carr", "Rate");
375 fprintf(fp
, "%-15s ", "");
376 fprintf(fp
, "%8s/%-6s ", "TX Abrt", "Rate");
377 fprintf(fp
, "%8s/%-6s ", "TX Fifo", "Rate");
378 fprintf(fp
, "%8s/%-6s ", "TX Hear", "Rate");
379 fprintf(fp
, "%8s/%-6s\n","TX Wind", "Rate");
383 static void print_one_json(json_writer_t
*jw
, const struct ifstat_ent
*n
,
384 const unsigned long long *vals
)
386 int i
, m
= show_errors
? 20 : 10;
388 jsonw_name(jw
, n
->name
);
389 jsonw_start_object(jw
);
391 for (i
=0; i
< m
&& stats
[i
]; i
++)
392 jsonw_uint_field(jw
, stats
[i
], vals
[i
]);
394 jsonw_end_object(jw
);
397 static void print_one_if(FILE *fp
, const struct ifstat_ent
*n
,
398 const unsigned long long *vals
)
402 fprintf(fp
, "%-15s ", n
->name
);
404 format_rate(fp
, vals
, n
->rate
, i
);
408 fprintf(fp
, "%-15s ", "");
409 format_pair(fp
, vals
, 4, 6);
410 format_pair(fp
, vals
, 5, 7);
411 format_rate(fp
, vals
, n
->rate
, 11);
412 format_rate(fp
, vals
, n
->rate
, 9);
415 fprintf(fp
, "%-15s ", "");
416 format_rate(fp
, vals
, n
->rate
, 4);
417 format_rate(fp
, vals
, n
->rate
, 6);
418 format_rate(fp
, vals
, n
->rate
, 11);
419 format_rate(fp
, vals
, n
->rate
, 10);
422 fprintf(fp
, "%-15s ", "");
423 format_rate(fp
, vals
, n
->rate
, 12);
424 format_rate(fp
, vals
, n
->rate
, 13);
425 format_rate(fp
, vals
, n
->rate
, 14);
426 format_rate(fp
, vals
, n
->rate
, 15);
429 fprintf(fp
, "%-15s ", "");
430 format_rate(fp
, vals
, n
->rate
, 5);
431 format_rate(fp
, vals
, n
->rate
, 7);
432 format_rate(fp
, vals
, n
->rate
, 9);
433 format_rate(fp
, vals
, n
->rate
, 17);
436 fprintf(fp
, "%-15s ", "");
437 format_rate(fp
, vals
, n
->rate
, 16);
438 format_rate(fp
, vals
, n
->rate
, 18);
439 format_rate(fp
, vals
, n
->rate
, 19);
440 format_rate(fp
, vals
, n
->rate
, 20);
445 static void dump_kern_db(FILE *fp
)
447 json_writer_t
*jw
= json_output
? jsonw_new(fp
) : NULL
;
448 struct ifstat_ent
*n
;
451 jsonw_pretty(jw
, pretty
);
452 jsonw_name(jw
, info_source
);
453 jsonw_start_object(jw
);
457 for (n
=kern_db
; n
; n
=n
->next
) {
462 print_one_json(jw
, n
, n
->val
);
464 print_one_if(fp
, n
, n
->val
);
467 fprintf(fp
, "\n} }\n");
470 static void dump_incr_db(FILE *fp
)
472 struct ifstat_ent
*n
, *h
;
473 json_writer_t
*jw
= json_output
? jsonw_new(fp
) : NULL
;
477 jsonw_pretty(jw
, pretty
);
478 jsonw_name(jw
, info_source
);
479 jsonw_start_object(jw
);
483 for (n
=kern_db
; n
; n
=n
->next
) {
485 unsigned long long vals
[MAXS
];
486 struct ifstat_ent
*h1
;
488 memcpy(vals
, n
->val
, sizeof(vals
));
490 for (h1
= h
; h1
; h1
= h1
->next
) {
491 if (h1
->ifindex
== n
->ifindex
) {
492 for (i
= 0; i
< MAXS
; i
++)
493 vals
[i
] -= h1
->val
[i
];
502 print_one_json(jw
, n
, n
->val
);
504 print_one_if(fp
, n
, vals
);
508 jsonw_end_object(jw
);
515 static void sigchild(int signo
)
519 static void update_db(int interval
)
521 struct ifstat_ent
*n
, *h
;
531 for (n
= kern_db
; n
; n
= n
->next
) {
532 struct ifstat_ent
*h1
;
533 for (h1
= h
; h1
; h1
= h1
->next
) {
534 if (h1
->ifindex
== n
->ifindex
) {
536 for (i
= 0; i
< MAXS
; i
++) {
537 if ((long)(h1
->ival
[i
] - n
->ival
[i
]) < 0) {
538 memset(n
->ival
, 0, sizeof(n
->ival
));
542 for (i
= 0; i
< MAXS
; i
++) {
544 unsigned long incr
= h1
->ival
[i
] - n
->ival
[i
];
546 n
->ival
[i
] = h1
->ival
[i
];
547 sample
= (double)(incr
*1000)/interval
;
548 if (interval
>= scan_interval
) {
549 n
->rate
[i
] += W
*(sample
-n
->rate
[i
]);
550 } else if (interval
>= 1000) {
551 if (interval
>= time_constant
) {
554 double w
= W
*(double)interval
/scan_interval
;
555 n
->rate
[i
] += w
*(sample
-n
->rate
[i
]);
561 struct ifstat_ent
*tmp
= h
;
575 #define T_DIFF(a,b) (((a).tv_sec-(b).tv_sec)*1000 + ((a).tv_usec-(b).tv_usec)/1000)
578 static void server_loop(int fd
)
580 struct timeval snaptime
= { 0 };
583 p
.events
= p
.revents
= POLLIN
;
585 sprintf(info_source
, "%d.%lu sampling_interval=%d time_const=%d",
586 getpid(), (unsigned long)random(), scan_interval
/1000, time_constant
/1000);
595 gettimeofday(&now
, NULL
);
596 tdiff
= T_DIFF(now
, snaptime
);
597 if (tdiff
>= scan_interval
) {
603 if (poll(&p
, 1, tdiff
+ scan_interval
) > 0
604 && (p
.revents
&POLLIN
)) {
605 int clnt
= accept(fd
, NULL
, NULL
);
610 } else if ((pid
= fork()) != 0) {
615 FILE *fp
= fdopen(clnt
, "w");
625 while (children
&& waitpid(-1, &status
, WNOHANG
) > 0)
630 static int verify_forging(int fd
)
633 socklen_t olen
= sizeof(cred
);
635 if (getsockopt(fd
, SOL_SOCKET
, SO_PEERCRED
, (void*)&cred
, &olen
) ||
638 if (cred
.uid
== getuid() || cred
.uid
== 0)
643 static void usage(void) __attribute__((noreturn
));
645 static void usage(void)
648 "Usage: ifstat [OPTION] [ PATTERN [ PATTERN ] ]\n"
649 " -h, --help this message\n"
650 " -a, --ignore ignore history\n"
651 " -d, --scan=SECS sample every statistics every SECS\n"
652 " -e, --errors show errors\n"
653 " -j, --json format output in JSON\n"
654 " -n, --nooutput do history only\n"
655 " -p, --pretty pretty print\n"
656 " -r, --reset reset history\n"
657 " -s, --noupdate don\'t update history\n"
658 " -t, --interval=SECS report average over the last SECS\n"
659 " -V, --version output version information\n"
660 " -z, --zeros show entries with zero activity\n");
665 static const struct option longopts
[] = {
666 { "help", 0, 0, 'h' },
667 { "ignore", 0, 0, 'a' },
668 { "scan", 1, 0, 'd'},
669 { "errors", 0, 0, 'e' },
670 { "nooutput", 0, 0, 'n' },
671 { "json", 0, 0, 'j' },
672 { "reset", 0, 0, 'r' },
673 { "pretty", 0, 0, 'p' },
674 { "noupdate", 0, 0, 's' },
675 { "interval", 1, 0, 't' },
676 { "version", 0, 0, 'V' },
677 { "zeros", 0, 0, 'z' },
681 int main(int argc
, char *argv
[])
684 struct sockaddr_un sun
;
685 FILE *hist_fp
= NULL
;
689 while ((ch
= getopt_long(argc
, argv
, "hjpvVzrnasd:t:e",
690 longopts
, NULL
)) != EOF
) {
717 scan_interval
= atoi(optarg
) * 1000;
718 if (scan_interval
<= 0) {
719 fprintf(stderr
, "ifstat: invalid scan interval\n");
724 time_constant
= atoi(optarg
);
725 if (time_constant
<= 0) {
726 fprintf(stderr
, "ifstat: invalid time constant divisor\n");
732 printf("ifstat utility, iproute2-ss%s\n", SNAPSHOT
);
744 sun
.sun_family
= AF_UNIX
;
746 sprintf(sun
.sun_path
+1, "ifstat%d", getuid());
748 if (scan_interval
> 0) {
749 if (time_constant
== 0)
751 time_constant
*= 1000;
752 W
= 1 - 1/exp(log(10)*(double)scan_interval
/time_constant
);
753 if ((fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0) {
754 perror("ifstat: socket");
757 if (bind(fd
, (struct sockaddr
*)&sun
, 2+1+strlen(sun
.sun_path
+1)) < 0) {
758 perror("ifstat: bind");
761 if (listen(fd
, 5) < 0) {
762 perror("ifstat: listen");
766 perror("ifstat: daemon");
769 signal(SIGPIPE
, SIG_IGN
);
770 signal(SIGCHLD
, sigchild
);
778 if (getenv("IFSTAT_HISTORY"))
779 snprintf(hist_name
, sizeof(hist_name
),
780 "%s", getenv("IFSTAT_HISTORY"));
782 snprintf(hist_name
, sizeof(hist_name
),
783 "%s/.ifstat.u%d", P_tmpdir
, getuid());
788 if (!ignore_history
|| !no_update
) {
791 fd
= open(hist_name
, O_RDWR
|O_CREAT
|O_NOFOLLOW
, 0600);
793 perror("ifstat: open history file");
796 if ((hist_fp
= fdopen(fd
, "r+")) == NULL
) {
797 perror("ifstat: fdopen history file");
800 if (flock(fileno(hist_fp
), LOCK_EX
)) {
801 perror("ifstat: flock history file");
804 if (fstat(fileno(hist_fp
), &stb
) != 0) {
805 perror("ifstat: fstat history file");
808 if (stb
.st_nlink
!= 1 || stb
.st_uid
!= getuid()) {
809 fprintf(stderr
, "ifstat: something is so wrong with history file, that I prefer not to proceed.\n");
812 if (!ignore_history
) {
815 if ((tfp
= fopen("/proc/uptime", "r")) != NULL
) {
816 if (fscanf(tfp
, "%ld", &uptime
) != 1)
820 if (uptime
>= 0 && time(NULL
) >= stb
.st_mtime
+uptime
) {
821 fprintf(stderr
, "ifstat: history is aged out, resetting\n");
822 ftruncate(fileno(hist_fp
), 0);
826 load_raw_table(hist_fp
);
832 if ((fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) >= 0 &&
833 (connect(fd
, (struct sockaddr
*)&sun
, 2+1+strlen(sun
.sun_path
+1)) == 0
834 || (strcpy(sun
.sun_path
+1, "ifstat0"),
835 connect(fd
, (struct sockaddr
*)&sun
, 2+1+strlen(sun
.sun_path
+1)) == 0))
836 && verify_forging(fd
) == 0) {
837 FILE *sfp
= fdopen(fd
, "r");
839 if (hist_db
&& source_mismatch
) {
840 fprintf(stderr
, "ifstat: history is stale, ignoring it.\n");
847 if (hist_db
&& info_source
[0] && strcmp(info_source
, "kernel")) {
848 fprintf(stderr
, "ifstat: history is stale, ignoring it.\n");
853 if (info_source
[0] == 0)
854 strcpy(info_source
, "kernel");
858 if (ignore_history
|| hist_db
== NULL
)
859 dump_kern_db(stdout
);
861 dump_incr_db(stdout
);
865 ftruncate(fileno(hist_fp
), 0);
869 dump_raw_db(hist_fp
, 1);