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>
30 #include <libnetlink.h>
31 #include <linux/netdevice.h>
36 int reset_history = 0;
37 int ignore_history = 0;
40 int scan_interval = 0;
41 int time_constant = 0;
47 char info_source[128];
50 #define MAXS (sizeof(struct net_device_stats)/sizeof(unsigned long))
54 struct ifstat_ent *next;
57 unsigned long long val[MAXS];
59 unsigned long ival[MAXS];
62 struct ifstat_ent *kern_db;
63 struct ifstat_ent *hist_db;
72 for (i=0; i<npatterns; i++) {
73 if (!fnmatch(patterns[i], id, 0))
79 int get_nlmsg(struct sockaddr_nl *who, struct nlmsghdr *m, void *arg)
81 struct ifinfomsg *ifi = NLMSG_DATA(m);
82 struct rtattr * tb[IFLA_MAX+1];
83 int len = m->nlmsg_len;
87 if (m->nlmsg_type != RTM_NEWLINK)
90 len -= NLMSG_LENGTH(sizeof(*ifi));
94 if (!(ifi->ifi_flags&IFF_UP))
97 memset(tb, 0, sizeof(tb));
98 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
99 if (tb[IFLA_IFNAME] == NULL || tb[IFLA_STATS] == NULL)
102 n = malloc(sizeof(*n));
105 n->ifindex = ifi->ifi_index;
106 n->name = strdup(RTA_DATA(tb[IFLA_IFNAME]));
107 memcpy(&n->ival, RTA_DATA(tb[IFLA_STATS]), sizeof(n->ival));
108 memset(&n->rate, 0, sizeof(n->rate));
109 for (i=0; i<MAXS; i++)
110 n->val[i] = n->ival[i];
118 struct ifstat_ent *db, *n;
119 struct rtnl_handle rth;
121 if (rtnl_open(&rth, 0) < 0)
124 if (rtnl_wilddump_request(&rth, AF_INET, RTM_GETLINK) < 0) {
125 perror("Cannot send dump request");
129 if (rtnl_dump_filter(&rth, get_nlmsg, NULL, NULL, NULL) < 0) {
130 fprintf(stderr, "Dump terminated\n");
147 void load_raw_table(FILE *fp)
150 struct ifstat_ent *db = NULL;
151 struct ifstat_ent *n;
153 while (fgets(buf, sizeof(buf), fp) != NULL) {
159 buf[strlen(buf)-1] = 0;
160 if (info_source[0] && strcmp(info_source, buf+1))
162 strncpy(info_source, buf+1, sizeof(info_source)-1);
165 if ((n = malloc(sizeof(*n))) == NULL)
168 if (!(p = strchr(buf, ' ')))
172 if (sscanf(buf, "%d", &n->ifindex) != 1)
174 if (!(next = strchr(p, ' ')))
181 for (i=0; i<MAXS; i++) {
183 if (!(next = strchr(p, ' ')))
186 if (sscanf(p, "%llu", n->val+i) != 1)
188 n->ival[i] = (unsigned long)n->val[i];
190 if (!(next = strchr(p, ' ')))
193 if (sscanf(p, "%u", &rate) != 1)
210 void dump_raw_db(FILE *fp, int to_hist)
212 struct ifstat_ent *n, *h;
214 fprintf(fp, "#%s\n", info_source);
216 for (n=kern_db; n; n=n->next) {
218 unsigned long long *vals = n->val;
219 double *rates = n->rate;
220 if (!match(n->name)) {
221 struct ifstat_ent *h1;
224 for (h1 = h; h1; h1 = h1->next) {
225 if (h1->ifindex == n->ifindex) {
233 fprintf(fp, "%d %s ", n->ifindex, n->name);
234 for (i=0; i<MAXS; i++)
235 fprintf(fp, "%llu %u ", vals[i], (unsigned)rates[i]);
241 void format_rate(FILE *fp, unsigned long long *vals, double *rates, int i)
244 if (vals[i] > 1024*1024*1024)
245 fprintf(fp, "%7lluM ", vals[i]/(1024*1024));
246 else if (vals[i] > 1024*1024)
247 fprintf(fp, "%7lluK ", vals[i]/1024);
249 fprintf(fp, "%8llu ", vals[i]);
251 if (rates[i] > 1024*1024) {
252 sprintf(temp, "%uM", (unsigned)(rates[i]/(1024*1024)));
253 fprintf(fp, "%-6s ", temp);
254 } else if (rates[i] > 1024) {
255 sprintf(temp, "%uK", (unsigned)(rates[i]/1024));
256 fprintf(fp, "%-6s ", temp);
258 fprintf(fp, "%-6u ", (unsigned)rates[i]);
261 void format_pair(FILE *fp, unsigned long long *vals, int i, int k)
264 if (vals[i] > 1024*1024*1024)
265 fprintf(fp, "%7lluM ", vals[i]/(1024*1024));
266 else if (vals[i] > 1024*1024)
267 fprintf(fp, "%7lluK ", vals[i]/1024);
269 fprintf(fp, "%8llu ", vals[i]);
271 if (vals[k] > 1024*1024*1024) {
272 sprintf(temp, "%uM", (unsigned)(vals[k]/(1024*1024)));
273 fprintf(fp, "%-6s ", temp);
274 } else if (vals[k] > 1024*1024) {
275 sprintf(temp, "%uK", (unsigned)(vals[k]/1024));
276 fprintf(fp, "%-6s ", temp);
278 fprintf(fp, "%-6u ", (unsigned)vals[k]);
281 void print_head(FILE *fp)
283 fprintf(fp, "#%s\n", info_source);
284 fprintf(fp, "%-15s ", "Interface");
286 fprintf(fp, "%8s/%-6s ", "RX Pkts", "Rate");
287 fprintf(fp, "%8s/%-6s ", "TX Pkts", "Rate");
288 fprintf(fp, "%8s/%-6s ", "RX Data", "Rate");
289 fprintf(fp, "%8s/%-6s\n","TX Data", "Rate");
292 fprintf(fp, "%-15s ", "");
293 fprintf(fp, "%8s/%-6s ", "RX Errs", "Drop");
294 fprintf(fp, "%8s/%-6s ", "TX Errs", "Drop");
295 fprintf(fp, "%8s/%-6s ", "RX Over", "Rate");
296 fprintf(fp, "%8s/%-6s\n","TX Coll", "Rate");
298 fprintf(fp, "%-15s ", "");
299 fprintf(fp, "%8s/%-6s ", "RX Errs", "Rate");
300 fprintf(fp, "%8s/%-6s ", "RX Drop", "Rate");
301 fprintf(fp, "%8s/%-6s ", "RX Over", "Rate");
302 fprintf(fp, "%8s/%-6s\n","RX Leng", "Rate");
304 fprintf(fp, "%-15s ", "");
305 fprintf(fp, "%8s/%-6s ", "RX Crc", "Rate");
306 fprintf(fp, "%8s/%-6s ", "RX Frm", "Rate");
307 fprintf(fp, "%8s/%-6s ", "RX Fifo", "Rate");
308 fprintf(fp, "%8s/%-6s\n","RX Miss", "Rate");
310 fprintf(fp, "%-15s ", "");
311 fprintf(fp, "%8s/%-6s ", "TX Errs", "Rate");
312 fprintf(fp, "%8s/%-6s ", "TX Drop", "Rate");
313 fprintf(fp, "%8s/%-6s ", "TX Coll", "Rate");
314 fprintf(fp, "%8s/%-6s\n","TX Carr", "Rate");
316 fprintf(fp, "%-15s ", "");
317 fprintf(fp, "%8s/%-6s ", "TX Abrt", "Rate");
318 fprintf(fp, "%8s/%-6s ", "TX Fifo", "Rate");
319 fprintf(fp, "%8s/%-6s ", "TX Hear", "Rate");
320 fprintf(fp, "%8s/%-6s\n","TX Wind", "Rate");
324 void print_one_if(FILE *fp, struct ifstat_ent *n, unsigned long long *vals)
327 fprintf(fp, "%-15s ", n->name);
329 format_rate(fp, vals, n->rate, i);
333 fprintf(fp, "%-15s ", "");
334 format_pair(fp, vals, 4, 6);
335 format_pair(fp, vals, 5, 7);
336 format_rate(fp, vals, n->rate, 11);
337 format_rate(fp, vals, n->rate, 9);
340 fprintf(fp, "%-15s ", "");
341 format_rate(fp, vals, n->rate, 4);
342 format_rate(fp, vals, n->rate, 6);
343 format_rate(fp, vals, n->rate, 11);
344 format_rate(fp, vals, n->rate, 10);
347 fprintf(fp, "%-15s ", "");
348 format_rate(fp, vals, n->rate, 12);
349 format_rate(fp, vals, n->rate, 13);
350 format_rate(fp, vals, n->rate, 14);
351 format_rate(fp, vals, n->rate, 15);
354 fprintf(fp, "%-15s ", "");
355 format_rate(fp, vals, n->rate, 5);
356 format_rate(fp, vals, n->rate, 7);
357 format_rate(fp, vals, n->rate, 9);
358 format_rate(fp, vals, n->rate, 17);
361 fprintf(fp, "%-15s ", "");
362 format_rate(fp, vals, n->rate, 16);
363 format_rate(fp, vals, n->rate, 18);
364 format_rate(fp, vals, n->rate, 19);
365 format_rate(fp, vals, n->rate, 20);
371 void dump_kern_db(FILE *fp)
373 struct ifstat_ent *n, *h;
378 for (n=kern_db; n; n=n->next) {
381 print_one_if(fp, n, n->val);
386 void dump_incr_db(FILE *fp)
388 struct ifstat_ent *n, *h;
393 for (n=kern_db; n; n=n->next) {
395 unsigned long long vals[MAXS];
396 struct ifstat_ent *h1;
398 memcpy(vals, n->val, sizeof(vals));
400 for (h1 = h; h1; h1 = h1->next) {
401 if (h1->ifindex == n->ifindex) {
402 for (i = 0; i < MAXS; i++)
403 vals[i] -= h1->val[i];
410 print_one_if(fp, n, vals);
417 void sigchild(int signo)
421 void update_db(int interval)
423 struct ifstat_ent *n, *h;
433 for (n = kern_db; n; n = n->next) {
434 struct ifstat_ent *h1;
435 for (h1 = h; h1; h1 = h1->next) {
436 if (h1->ifindex == n->ifindex) {
438 for (i = 0; i < MAXS; i++) {
439 if ((long)(h1->ival[i] - n->ival[i]) < 0) {
440 memset(n->ival, 0, sizeof(n->ival));
444 for (i = 0; i < MAXS; i++) {
446 unsigned long incr = h1->ival[i] - n->ival[i];
448 n->ival[i] = h1->ival[i];
449 sample = (double)(incr*1000)/interval;
450 if (interval >= scan_interval) {
451 n->rate[i] += W*(sample-n->rate[i]);
452 } else if (interval >= 1000) {
453 if (interval >= time_constant) {
456 double w = W*(double)interval/scan_interval;
457 n->rate[i] += w*(sample-n->rate[i]);
463 struct ifstat_ent *tmp = h;
477 #define T_DIFF(a,b) (((a).tv_sec-(b).tv_sec)*1000 + ((a).tv_usec-(b).tv_usec)/1000)
480 void server_loop(int fd)
482 struct timeval snaptime;
485 p.events = p.revents = POLLIN;
487 sprintf(info_source, "%d.%lu sampling_interval=%d time_const=%d",
488 getpid(), (unsigned long)random(), scan_interval/1000, time_constant/1000);
496 gettimeofday(&now, NULL);
497 tdiff = T_DIFF(now, snaptime);
498 if (tdiff >= scan_interval) {
503 if (poll(&p, 1, tdiff + scan_interval) > 0
504 && (p.revents&POLLIN)) {
505 int clnt = accept(fd, NULL, NULL);
510 } else if ((pid = fork()) != 0) {
515 FILE *fp = fdopen(clnt, "w");
525 while (children && waitpid(-1, &status, WNOHANG) > 0)
530 int verify_forging(int fd)
533 int olen = sizeof(cred);
534 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, (void*)&cred, &olen) ||
537 if (cred.uid == getuid() || cred.uid == 0)
542 static void usage(void) __attribute__((noreturn));
544 static void usage(void)
547 "Usage: ifstat [ -h?vVzrnasd:t: ] [ PATTERN [ PATTERN ] ]\n"
553 int main(int argc, char *argv[])
556 struct sockaddr_un sun;
557 FILE *hist_fp = NULL;
561 while ((ch = getopt(argc, argv, "h?vVzrnasd:t:e")) != EOF) {
582 scan_interval = 1000*atoi(optarg);
585 if (sscanf(optarg, "%d", &time_constant) != 1 ||
586 time_constant <= 0) {
587 fprintf(stderr, "ifstat: invalid time constant divisor\n");
593 printf("ifstat utility, iproute2-ss%s\n", SNAPSHOT);
605 sun.sun_family = AF_UNIX;
607 sprintf(sun.sun_path+1, "ifstat%d", getuid());
609 if (scan_interval > 0) {
610 if (time_constant == 0)
612 time_constant *= 1000;
613 W = 1 - 1/exp(log(10)*(double)scan_interval/time_constant);
614 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
615 perror("ifstat: socket");
618 if (bind(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) < 0) {
619 perror("ifstat: bind");
622 if (listen(fd, 5) < 0) {
623 perror("ifstat: listen");
629 close(0); close(1); close(2); setsid();
630 signal(SIGPIPE, SIG_IGN);
631 signal(SIGCHLD, sigchild);
639 if (getenv("IFSTAT_HISTORY"))
640 snprintf(hist_name, sizeof(hist_name), getenv("IFSTAT_HISTORY"));
642 sprintf(hist_name, "/tmp/.ifstat.u%d", getuid());
647 if (!ignore_history || !no_update) {
650 fd = open(hist_name, O_RDWR|O_CREAT|O_NOFOLLOW, 0600);
652 perror("ifstat: open history file");
655 if ((hist_fp = fdopen(fd, "r+")) == NULL) {
656 perror("ifstat: fdopen history file");
659 if (flock(fileno(hist_fp), LOCK_EX)) {
660 perror("ifstat: flock history file");
663 if (fstat(fileno(hist_fp), &stb) != 0) {
664 perror("ifstat: fstat history file");
667 if (stb.st_nlink != 1 || stb.st_uid != getuid()) {
668 fprintf(stderr, "ifstat: something is so wrong with history file, that I prefer not to proceed.\n");
671 if (!ignore_history) {
674 if ((tfp = fopen("/proc/uptime", "r")) != NULL) {
675 if (fscanf(tfp, "%ld", &uptime) != 1)
679 if (uptime >= 0 && time(NULL) >= stb.st_mtime+uptime) {
680 fprintf(stderr, "ifstat: history is aged out, resetting\n");
681 ftruncate(fileno(hist_fp), 0);
685 load_raw_table(hist_fp);
691 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) >= 0 &&
692 (connect(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) == 0
693 || (strcpy(sun.sun_path+1, "ifstat0"),
694 connect(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) == 0))
695 && verify_forging(fd) == 0) {
696 FILE *sfp = fdopen(fd, "r");
698 if (hist_db && source_mismatch) {
699 fprintf(stderr, "ifstat: history is stale, ignoring it.\n");
706 if (hist_db && info_source[0] && strcmp(info_source, "kernel")) {
707 fprintf(stderr, "ifstat: history is stale, ignoring it.\n");
712 if (info_source[0] == 0)
713 strcpy(info_source, "kernel");
717 if (ignore_history || hist_db == NULL)
718 dump_kern_db(stdout);
720 dump_incr_db(stdout);
723 ftruncate(fileno(hist_fp), 0);
725 dump_raw_db(hist_fp, 1);