]> git.proxmox.com Git - mirror_iproute2.git/blame - misc/ifstat.c
man ip-link: Add more explanation about vlan reordering
[mirror_iproute2.git] / misc / ifstat.c
CommitLineData
aba5acdf
SH
1/*
2 * ifstat.c handy utility to read net interface statistics
3 *
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.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 */
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <unistd.h>
15#include <fcntl.h>
16#include <string.h>
17#include <errno.h>
18#include <time.h>
19#include <sys/time.h>
20#include <fnmatch.h>
21#include <sys/file.h>
22#include <sys/socket.h>
23#include <sys/un.h>
24#include <sys/poll.h>
25#include <sys/wait.h>
26#include <sys/stat.h>
27#include <signal.h>
28#include <math.h>
9f3ea250 29#include <getopt.h>
aba5acdf
SH
30
31#include <libnetlink.h>
daf7bd5c
SH
32#include <linux/if.h>
33#include <linux/if_link.h>
aba5acdf
SH
34
35#include <SNAPSHOT.h>
36
37int dump_zeros = 0;
38int reset_history = 0;
39int ignore_history = 0;
40int no_output = 0;
ec3e625c 41int json_output = 0;
aba5acdf
SH
42int no_update = 0;
43int scan_interval = 0;
44int time_constant = 0;
45int show_errors = 0;
46double W;
47char **patterns;
48int npatterns;
49
50char info_source[128];
51int source_mismatch;
52
a571587d 53#define MAXS (sizeof(struct rtnl_link_stats)/sizeof(__u32))
aba5acdf
SH
54
55struct ifstat_ent
56{
57 struct ifstat_ent *next;
58 char *name;
59 int ifindex;
60 unsigned long long val[MAXS];
61 double rate[MAXS];
a571587d 62 __u32 ival[MAXS];
aba5acdf
SH
63};
64
ec3e625c
SH
65static const char *stats[MAXS] = {
66 "rx_packets",
67 "tx_packets",
68 "rx_bytes",
69 "tx_bytes",
70 "rx_errors",
71 "tx_errors",
72 "rx_dropped",
73 "tx_dropped",
74 "multicast",
75 "collisions",
76 "rx_length_errors",
77 "rx_over_errors",
78 "rx_crc_errors",
79 "rx_frame_errors",
80 "rx_fifo_errors",
81 "rx_missed_errors",
82 "tx_aborted_errors",
83 "tx_carrier_errors",
84 "tx_fifo_errors",
85 "tx_heartbeat_errors",
86 "tx_window_errors",
87 "rx_compressed",
88 "tx_compressed"
89};
90
aba5acdf
SH
91struct ifstat_ent *kern_db;
92struct ifstat_ent *hist_db;
93
50772dc5 94static int match(const char *id)
aba5acdf
SH
95{
96 int i;
97
98 if (npatterns == 0)
99 return 1;
100
101 for (i=0; i<npatterns; i++) {
102 if (!fnmatch(patterns[i], id, 0))
103 return 1;
104 }
105 return 0;
106}
107
ae665a52 108static int get_nlmsg(const struct sockaddr_nl *who,
50772dc5 109 struct nlmsghdr *m, void *arg)
aba5acdf
SH
110{
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;
115 int i;
116
117 if (m->nlmsg_type != RTM_NEWLINK)
118 return 0;
119
120 len -= NLMSG_LENGTH(sizeof(*ifi));
121 if (len < 0)
122 return -1;
123
124 if (!(ifi->ifi_flags&IFF_UP))
125 return 0;
126
aba5acdf
SH
127 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
128 if (tb[IFLA_IFNAME] == NULL || tb[IFLA_STATS] == NULL)
129 return 0;
130
131 n = malloc(sizeof(*n));
132 if (!n)
133 abort();
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];
140 n->next = kern_db;
141 kern_db = n;
142 return 0;
143}
144
d1f28cf1 145static void load_info(void)
aba5acdf
SH
146{
147 struct ifstat_ent *db, *n;
148 struct rtnl_handle rth;
149
150 if (rtnl_open(&rth, 0) < 0)
151 exit(1);
152
153 if (rtnl_wilddump_request(&rth, AF_INET, RTM_GETLINK) < 0) {
154 perror("Cannot send dump request");
155 exit(1);
156 }
157
cd70f3f5 158 if (rtnl_dump_filter(&rth, get_nlmsg, NULL) < 0) {
aba5acdf
SH
159 fprintf(stderr, "Dump terminated\n");
160 exit(1);
161 }
162
163 rtnl_close(&rth);
164
165 db = kern_db;
166 kern_db = NULL;
167
168 while (db) {
169 n = db;
170 db = db->next;
171 n->next = kern_db;
172 kern_db = n;
173 }
174}
175
d1f28cf1 176static void load_raw_table(FILE *fp)
aba5acdf
SH
177{
178 char buf[4096];
179 struct ifstat_ent *db = NULL;
180 struct ifstat_ent *n;
181
182 while (fgets(buf, sizeof(buf), fp) != NULL) {
183 char *p;
184 char *next;
185 int i;
186
187 if (buf[0] == '#') {
188 buf[strlen(buf)-1] = 0;
189 if (info_source[0] && strcmp(info_source, buf+1))
190 source_mismatch = 1;
191 strncpy(info_source, buf+1, sizeof(info_source)-1);
192 continue;
193 }
194 if ((n = malloc(sizeof(*n))) == NULL)
195 abort();
196
197 if (!(p = strchr(buf, ' ')))
198 abort();
199 *p++ = 0;
200
201 if (sscanf(buf, "%d", &n->ifindex) != 1)
202 abort();
203 if (!(next = strchr(p, ' ')))
204 abort();
205 *next++ = 0;
206
207 n->name = strdup(p);
208 p = next;
209
210 for (i=0; i<MAXS; i++) {
211 unsigned rate;
212 if (!(next = strchr(p, ' ')))
213 abort();
214 *next++ = 0;
215 if (sscanf(p, "%llu", n->val+i) != 1)
216 abort();
a571587d 217 n->ival[i] = (__u32)n->val[i];
aba5acdf
SH
218 p = next;
219 if (!(next = strchr(p, ' ')))
220 abort();
221 *next++ = 0;
222 if (sscanf(p, "%u", &rate) != 1)
223 abort();
224 n->rate[i] = rate;
225 p = next;
226 }
227 n->next = db;
228 db = n;
229 }
230
231 while (db) {
232 n = db;
233 db = db->next;
234 n->next = kern_db;
235 kern_db = n;
236 }
237}
238
d1f28cf1 239static void dump_raw_db(FILE *fp, int to_hist)
aba5acdf
SH
240{
241 struct ifstat_ent *n, *h;
ec3e625c
SH
242 const char *eol = "\n";
243
aba5acdf 244 h = hist_db;
ec3e625c
SH
245 if (json_output)
246 fprintf(fp, "{ \"%s\":{", info_source);
247 else
248 fprintf(fp, "#%s\n", info_source);
aba5acdf
SH
249
250 for (n=kern_db; n; n=n->next) {
251 int i;
252 unsigned long long *vals = n->val;
253 double *rates = n->rate;
254 if (!match(n->name)) {
255 struct ifstat_ent *h1;
256 if (!to_hist)
257 continue;
258 for (h1 = h; h1; h1 = h1->next) {
259 if (h1->ifindex == n->ifindex) {
260 vals = h1->val;
261 rates = h1->rate;
262 h = h1->next;
263 break;
264 }
265 }
266 }
ec3e625c
SH
267
268 if (json_output) {
269 fprintf(fp, "%s \"%s\":{",
270 eol, n->name);
271 eol = ",\n";
272 for (i=0; i<MAXS && stats[i]; i++)
3d0b7439 273 fprintf(fp, " \"%s\":%llu",
ec3e625c
SH
274 stats[i], vals[i]);
275 fprintf(fp, "}");
276 } else {
277 fprintf(fp, "%d %s ", n->ifindex, n->name);
278 for (i=0; i<MAXS; i++)
3d0b7439 279 fprintf(fp, "%llu %u ", vals[i],
ec3e625c
SH
280 (unsigned)rates[i]);
281 fprintf(fp, "\n");
282 }
aba5acdf
SH
283 }
284}
285
9f3ea250
SH
286/* use communication definitions of meg/kilo etc */
287static const unsigned long long giga = 1000000000ull;
bb6a21a4
SH
288static const unsigned long long mega = 1000000;
289static const unsigned long long kilo = 1000;
aba5acdf 290
ec3e625c
SH
291static void format_rate(FILE *fp, const unsigned long long *vals,
292 const double *rates, int i)
aba5acdf
SH
293{
294 char temp[64];
ec3e625c 295
9f3ea250
SH
296 if (vals[i] > giga)
297 fprintf(fp, "%7lluM ", vals[i]/mega);
298 else if (vals[i] > mega)
299 fprintf(fp, "%7lluK ", vals[i]/kilo);
aba5acdf
SH
300 else
301 fprintf(fp, "%8llu ", vals[i]);
302
9f3ea250
SH
303 if (rates[i] > mega) {
304 sprintf(temp, "%uM", (unsigned)(rates[i]/mega));
aba5acdf 305 fprintf(fp, "%-6s ", temp);
9f3ea250
SH
306 } else if (rates[i] > kilo) {
307 sprintf(temp, "%uK", (unsigned)(rates[i]/kilo));
aba5acdf
SH
308 fprintf(fp, "%-6s ", temp);
309 } else
310 fprintf(fp, "%-6u ", (unsigned)rates[i]);
311}
312
ec3e625c 313static void format_pair(FILE *fp, const unsigned long long *vals, int i, int k)
aba5acdf
SH
314{
315 char temp[64];
9f3ea250
SH
316 if (vals[i] > giga)
317 fprintf(fp, "%7lluM ", vals[i]/mega);
318 else if (vals[i] > mega)
319 fprintf(fp, "%7lluK ", vals[i]/kilo);
aba5acdf
SH
320 else
321 fprintf(fp, "%8llu ", vals[i]);
322
9f3ea250
SH
323 if (vals[k] > giga) {
324 sprintf(temp, "%uM", (unsigned)(vals[k]/mega));
aba5acdf 325 fprintf(fp, "%-6s ", temp);
9f3ea250
SH
326 } else if (vals[k] > mega) {
327 sprintf(temp, "%uK", (unsigned)(vals[k]/kilo));
aba5acdf
SH
328 fprintf(fp, "%-6s ", temp);
329 } else
330 fprintf(fp, "%-6u ", (unsigned)vals[k]);
331}
332
d1f28cf1 333static void print_head(FILE *fp)
aba5acdf
SH
334{
335 fprintf(fp, "#%s\n", info_source);
336 fprintf(fp, "%-15s ", "Interface");
337
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");
342
343 if (!show_errors) {
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");
349 } else {
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");
355
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");
361
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");
367
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");
373 }
374}
375
ec3e625c
SH
376static void print_one_json(FILE *fp, const struct ifstat_ent *n,
377 const unsigned long long *vals)
378{
379 int i, m;
380 const char *sep = " ";
3d0b7439 381
ec3e625c
SH
382 m = show_errors ? 20 : 10;
383 fprintf(fp, " \"%s\":{", n->name);
384 for (i=0; i < m && stats[i]; i++) {
3d0b7439 385 fprintf(fp, "%s\"%s\":%llu",
ec3e625c
SH
386 sep, stats[i], vals[i]);
387 sep = ", ";
388 }
389 fprintf(fp, " }");
390}
391
392static void print_one_if(FILE *fp, const struct ifstat_ent *n,
393 const unsigned long long *vals)
aba5acdf
SH
394{
395 int i;
ec3e625c 396
aba5acdf
SH
397 fprintf(fp, "%-15s ", n->name);
398 for (i=0; i<4; i++)
399 format_rate(fp, vals, n->rate, i);
400 fprintf(fp, "\n");
401
402 if (!show_errors) {
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);
408 fprintf(fp, "\n");
409 } else {
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);
415 fprintf(fp, "\n");
416
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);
422 fprintf(fp, "\n");
423
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);
429 fprintf(fp, "\n");
430
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);
436 fprintf(fp, "\n");
437 }
438}
439
d1f28cf1 440static void dump_kern_db(FILE *fp)
aba5acdf 441{
dc484542 442 struct ifstat_ent *n;
ec3e625c 443 const char *eol = "\n";
aba5acdf 444
ec3e625c
SH
445 if (json_output)
446 fprintf(fp, "{ \"%s\": {", info_source);
447 else
448 print_head(fp);
aba5acdf
SH
449
450 for (n=kern_db; n; n=n->next) {
451 if (!match(n->name))
452 continue;
ec3e625c
SH
453
454 if (json_output) {
455 fprintf(fp, "%s", eol);
456 eol = ",\n";
457 print_one_json(fp, n, n->val);
458 } else
459 print_one_if(fp, n, n->val);
aba5acdf 460 }
ec3e625c
SH
461 if (json_output)
462 fprintf(fp, "\n} }\n");
aba5acdf
SH
463}
464
465
d1f28cf1 466static void dump_incr_db(FILE *fp)
aba5acdf
SH
467{
468 struct ifstat_ent *n, *h;
ec3e625c 469 const char *eol = "\n";
aba5acdf 470
ec3e625c
SH
471 h = hist_db;
472 if (json_output)
473 fprintf(fp, "{ \"%s\":{", info_source);
474 else
475 print_head(fp);
aba5acdf
SH
476
477 for (n=kern_db; n; n=n->next) {
478 int i;
479 unsigned long long vals[MAXS];
480 struct ifstat_ent *h1;
481
482 memcpy(vals, n->val, sizeof(vals));
483
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];
488 h = h1->next;
489 break;
490 }
491 }
492 if (!match(n->name))
493 continue;
ec3e625c
SH
494
495 if (json_output) {
496 fprintf(fp, "%s", eol);
497 eol = ",\n";
498 print_one_json(fp, n, n->val);
499 } else
500 print_one_if(fp, n, vals);
aba5acdf 501 }
ec3e625c
SH
502 if (json_output)
503 fprintf(fp, "\n} }\n");
aba5acdf
SH
504}
505
506
507static int children;
508
d1f28cf1 509static void sigchild(int signo)
aba5acdf
SH
510{
511}
512
d1f28cf1 513static void update_db(int interval)
aba5acdf
SH
514{
515 struct ifstat_ent *n, *h;
516
517 n = kern_db;
518 kern_db = NULL;
519
520 load_info();
521
522 h = kern_db;
523 kern_db = n;
524
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) {
529 int i;
530 for (i = 0; i < MAXS; i++) {
531 if ((long)(h1->ival[i] - n->ival[i]) < 0) {
ae665a52 532 memset(n->ival, 0, sizeof(n->ival));
aba5acdf
SH
533 break;
534 }
535 }
ae665a52 536 for (i = 0; i < MAXS; i++) {
aba5acdf
SH
537 double sample;
538 unsigned long incr = h1->ival[i] - n->ival[i];
539 n->val[i] += incr;
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) {
546 n->rate[i] = sample;
547 } else {
548 double w = W*(double)interval/scan_interval;
549 n->rate[i] += w*(sample-n->rate[i]);
550 }
551 }
552 }
553
554 while (h != h1) {
555 struct ifstat_ent *tmp = h;
556 h = h->next;
557 free(tmp->name);
558 free(tmp);
559 };
560 h = h1->next;
561 free(h1->name);
562 free(h1);
563 break;
564 }
565 }
566 }
567}
568
569#define T_DIFF(a,b) (((a).tv_sec-(b).tv_sec)*1000 + ((a).tv_usec-(b).tv_usec)/1000)
570
571
d1f28cf1 572static void server_loop(int fd)
aba5acdf 573{
737f15f6 574 struct timeval snaptime = { 0 };
aba5acdf
SH
575 struct pollfd p;
576 p.fd = fd;
577 p.events = p.revents = POLLIN;
578
579 sprintf(info_source, "%d.%lu sampling_interval=%d time_const=%d",
580 getpid(), (unsigned long)random(), scan_interval/1000, time_constant/1000);
581
582 load_info();
583
584 for (;;) {
585 int status;
586 int tdiff;
587 struct timeval now;
737f15f6 588
aba5acdf
SH
589 gettimeofday(&now, NULL);
590 tdiff = T_DIFF(now, snaptime);
591 if (tdiff >= scan_interval) {
592 update_db(tdiff);
593 snaptime = now;
594 tdiff = 0;
595 }
737f15f6 596
aba5acdf
SH
597 if (poll(&p, 1, tdiff + scan_interval) > 0
598 && (p.revents&POLLIN)) {
599 int clnt = accept(fd, NULL, NULL);
600 if (clnt >= 0) {
601 pid_t pid;
602 if (children >= 5) {
603 close(clnt);
604 } else if ((pid = fork()) != 0) {
605 if (pid>0)
606 children++;
607 close(clnt);
608 } else {
609 FILE *fp = fdopen(clnt, "w");
610 if (fp) {
611 if (tdiff > 0)
612 update_db(tdiff);
613 dump_raw_db(fp, 0);
614 }
615 exit(0);
616 }
617 }
618 }
619 while (children && waitpid(-1, &status, WNOHANG) > 0)
620 children--;
621 }
622}
623
d1f28cf1 624static int verify_forging(int fd)
aba5acdf
SH
625{
626 struct ucred cred;
737f15f6
SH
627 socklen_t olen = sizeof(cred);
628
aba5acdf
SH
629 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, (void*)&cred, &olen) ||
630 olen < sizeof(cred))
631 return -1;
632 if (cred.uid == getuid() || cred.uid == 0)
633 return 0;
634 return -1;
635}
636
d7e0809e
SH
637static void usage(void) __attribute__((noreturn));
638
aba5acdf
SH
639static void usage(void)
640{
d7e0809e
SH
641 fprintf(stderr,
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"
ec3e625c 647" -j, --json format output in JSON\n"
d7e0809e
SH
648" -n, --nooutput do history only\n"
649" -r, --reset reset history\n"
ec3e625c 650" -s, --noupdate don\'t update history\n"
d7e0809e
SH
651" -t, --interval=SECS report average over the last SECS\n"
652" -V, --version output version information\n"
d7e0809e
SH
653" -z, --zeros show entries with zero activity\n");
654
655 exit(-1);
aba5acdf
SH
656}
657
9f3ea250
SH
658static 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' },
ec3e625c 664 { "json", 0, 0, 'j' },
9f3ea250
SH
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' },
9f3ea250
SH
670 { 0 }
671};
aba5acdf
SH
672
673int main(int argc, char *argv[])
674{
675 char hist_name[128];
676 struct sockaddr_un sun;
677 FILE *hist_fp = NULL;
678 int ch;
679 int fd;
680
ec3e625c 681 while ((ch = getopt_long(argc, argv, "hjvVzrnasd:t:e",
9f3ea250 682 longopts, NULL)) != EOF) {
aba5acdf
SH
683 switch(ch) {
684 case 'z':
685 dump_zeros = 1;
686 break;
687 case 'r':
688 reset_history = 1;
689 break;
690 case 'a':
691 ignore_history = 1;
692 break;
693 case 's':
694 no_update = 1;
695 break;
696 case 'n':
697 no_output = 1;
698 break;
699 case 'e':
700 show_errors = 1;
701 break;
ec3e625c
SH
702 case 'j':
703 json_output = 1;
704 break;
aba5acdf 705 case 'd':
9f3ea250
SH
706 scan_interval = atoi(optarg) * 1000;
707 if (scan_interval <= 0) {
708 fprintf(stderr, "ifstat: invalid scan interval\n");
709 exit(-1);
710 }
aba5acdf
SH
711 break;
712 case 't':
9f3ea250
SH
713 time_constant = atoi(optarg);
714 if (time_constant <= 0) {
aba5acdf
SH
715 fprintf(stderr, "ifstat: invalid time constant divisor\n");
716 exit(-1);
717 }
718 break;
719 case 'v':
720 case 'V':
721 printf("ifstat utility, iproute2-ss%s\n", SNAPSHOT);
722 exit(0);
723 case 'h':
724 case '?':
725 default:
726 usage();
727 }
728 }
729
730 argc -= optind;
731 argv += optind;
732
733 sun.sun_family = AF_UNIX;
734 sun.sun_path[0] = 0;
735 sprintf(sun.sun_path+1, "ifstat%d", getuid());
736
737 if (scan_interval > 0) {
738 if (time_constant == 0)
739 time_constant = 60;
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");
744 exit(-1);
745 }
746 if (bind(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) < 0) {
747 perror("ifstat: bind");
748 exit(-1);
749 }
750 if (listen(fd, 5) < 0) {
751 perror("ifstat: listen");
752 exit(-1);
753 }
a7a9ddbb
MF
754 if (daemon(0, 0)) {
755 perror("ifstat: daemon");
756 exit(-1);
757 }
aba5acdf
SH
758 signal(SIGPIPE, SIG_IGN);
759 signal(SIGCHLD, sigchild);
760 server_loop(fd);
761 exit(0);
762 }
763
764 patterns = argv;
765 npatterns = argc;
766
767 if (getenv("IFSTAT_HISTORY"))
daf7bd5c
SH
768 snprintf(hist_name, sizeof(hist_name),
769 "%s", getenv("IFSTAT_HISTORY"));
aba5acdf 770 else
daf7bd5c
SH
771 snprintf(hist_name, sizeof(hist_name),
772 "%s/.ifstat.u%d", P_tmpdir, getuid());
aba5acdf
SH
773
774 if (reset_history)
775 unlink(hist_name);
776
777 if (!ignore_history || !no_update) {
778 struct stat stb;
779
780 fd = open(hist_name, O_RDWR|O_CREAT|O_NOFOLLOW, 0600);
781 if (fd < 0) {
782 perror("ifstat: open history file");
783 exit(-1);
784 }
785 if ((hist_fp = fdopen(fd, "r+")) == NULL) {
786 perror("ifstat: fdopen history file");
787 exit(-1);
788 }
789 if (flock(fileno(hist_fp), LOCK_EX)) {
790 perror("ifstat: flock history file");
791 exit(-1);
792 }
793 if (fstat(fileno(hist_fp), &stb) != 0) {
794 perror("ifstat: fstat history file");
795 exit(-1);
796 }
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");
799 exit(-1);
800 }
801 if (!ignore_history) {
802 FILE *tfp;
9a230771 803 long uptime = -1;
aba5acdf
SH
804 if ((tfp = fopen("/proc/uptime", "r")) != NULL) {
805 if (fscanf(tfp, "%ld", &uptime) != 1)
806 uptime = -1;
807 fclose(tfp);
808 }
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);
812 }
813 }
814
815 load_raw_table(hist_fp);
816
817 hist_db = kern_db;
818 kern_db = NULL;
819 }
820
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");
827 load_raw_table(sfp);
828 if (hist_db && source_mismatch) {
829 fprintf(stderr, "ifstat: history is stale, ignoring it.\n");
830 hist_db = NULL;
831 }
832 fclose(sfp);
833 } else {
834 if (fd >= 0)
835 close(fd);
836 if (hist_db && info_source[0] && strcmp(info_source, "kernel")) {
837 fprintf(stderr, "ifstat: history is stale, ignoring it.\n");
838 hist_db = NULL;
839 info_source[0] = 0;
840 }
841 load_info();
842 if (info_source[0] == 0)
843 strcpy(info_source, "kernel");
844 }
845
846 if (!no_output) {
847 if (ignore_history || hist_db == NULL)
848 dump_kern_db(stdout);
849 else
850 dump_incr_db(stdout);
851 }
ec3e625c 852
aba5acdf
SH
853 if (!no_update) {
854 ftruncate(fileno(hist_fp), 0);
855 rewind(hist_fp);
ec3e625c
SH
856
857 json_output = 0;
aba5acdf 858 dump_raw_db(hist_fp, 1);
ec3e625c 859 fclose(hist_fp);
aba5acdf
SH
860 }
861 exit(0);
862}