osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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> |
osdl.org!shemminger | 9f3ea25 | 2004-06-09 17:37:28 +0000 | [diff] [blame] | 29 | #include <getopt.h> |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 30 | |
| 31 | #include <libnetlink.h> |
| 32 | #include <linux/netdevice.h> |
| 33 | |
| 34 | #include <SNAPSHOT.h> |
| 35 | |
| 36 | int dump_zeros = 0; |
| 37 | int reset_history = 0; |
| 38 | int ignore_history = 0; |
| 39 | int no_output = 0; |
| 40 | int no_update = 0; |
| 41 | int scan_interval = 0; |
| 42 | int time_constant = 0; |
| 43 | int show_errors = 0; |
| 44 | double W; |
| 45 | char **patterns; |
| 46 | int npatterns; |
| 47 | |
| 48 | char info_source[128]; |
| 49 | int source_mismatch; |
| 50 | |
| 51 | #define MAXS (sizeof(struct net_device_stats)/sizeof(unsigned long)) |
| 52 | |
| 53 | struct ifstat_ent |
| 54 | { |
| 55 | struct ifstat_ent *next; |
| 56 | char *name; |
| 57 | int ifindex; |
| 58 | unsigned long long val[MAXS]; |
| 59 | double rate[MAXS]; |
| 60 | unsigned long ival[MAXS]; |
| 61 | }; |
| 62 | |
| 63 | struct ifstat_ent *kern_db; |
| 64 | struct ifstat_ent *hist_db; |
| 65 | |
osdl.net!shemminger | 50772dc | 2004-12-07 21:48:29 +0000 | [diff] [blame] | 66 | static int match(const char *id) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 67 | { |
| 68 | int i; |
| 69 | |
| 70 | if (npatterns == 0) |
| 71 | return 1; |
| 72 | |
| 73 | for (i=0; i<npatterns; i++) { |
| 74 | if (!fnmatch(patterns[i], id, 0)) |
| 75 | return 1; |
| 76 | } |
| 77 | return 0; |
| 78 | } |
| 79 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 80 | static int get_nlmsg(const struct sockaddr_nl *who, |
osdl.net!shemminger | 50772dc | 2004-12-07 21:48:29 +0000 | [diff] [blame] | 81 | struct nlmsghdr *m, void *arg) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 82 | { |
| 83 | struct ifinfomsg *ifi = NLMSG_DATA(m); |
| 84 | struct rtattr * tb[IFLA_MAX+1]; |
| 85 | int len = m->nlmsg_len; |
| 86 | struct ifstat_ent *n; |
| 87 | int i; |
| 88 | |
| 89 | if (m->nlmsg_type != RTM_NEWLINK) |
| 90 | return 0; |
| 91 | |
| 92 | len -= NLMSG_LENGTH(sizeof(*ifi)); |
| 93 | if (len < 0) |
| 94 | return -1; |
| 95 | |
| 96 | if (!(ifi->ifi_flags&IFF_UP)) |
| 97 | return 0; |
| 98 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 99 | parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len); |
| 100 | if (tb[IFLA_IFNAME] == NULL || tb[IFLA_STATS] == NULL) |
| 101 | return 0; |
| 102 | |
| 103 | n = malloc(sizeof(*n)); |
| 104 | if (!n) |
| 105 | abort(); |
| 106 | n->ifindex = ifi->ifi_index; |
| 107 | n->name = strdup(RTA_DATA(tb[IFLA_IFNAME])); |
| 108 | memcpy(&n->ival, RTA_DATA(tb[IFLA_STATS]), sizeof(n->ival)); |
| 109 | memset(&n->rate, 0, sizeof(n->rate)); |
| 110 | for (i=0; i<MAXS; i++) |
| 111 | n->val[i] = n->ival[i]; |
| 112 | n->next = kern_db; |
| 113 | kern_db = n; |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | void load_info(void) |
| 118 | { |
| 119 | struct ifstat_ent *db, *n; |
| 120 | struct rtnl_handle rth; |
| 121 | |
| 122 | if (rtnl_open(&rth, 0) < 0) |
| 123 | exit(1); |
| 124 | |
| 125 | if (rtnl_wilddump_request(&rth, AF_INET, RTM_GETLINK) < 0) { |
| 126 | perror("Cannot send dump request"); |
| 127 | exit(1); |
| 128 | } |
| 129 | |
| 130 | if (rtnl_dump_filter(&rth, get_nlmsg, NULL, NULL, NULL) < 0) { |
| 131 | fprintf(stderr, "Dump terminated\n"); |
| 132 | exit(1); |
| 133 | } |
| 134 | |
| 135 | rtnl_close(&rth); |
| 136 | |
| 137 | db = kern_db; |
| 138 | kern_db = NULL; |
| 139 | |
| 140 | while (db) { |
| 141 | n = db; |
| 142 | db = db->next; |
| 143 | n->next = kern_db; |
| 144 | kern_db = n; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | void load_raw_table(FILE *fp) |
| 149 | { |
| 150 | char buf[4096]; |
| 151 | struct ifstat_ent *db = NULL; |
| 152 | struct ifstat_ent *n; |
| 153 | |
| 154 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
| 155 | char *p; |
| 156 | char *next; |
| 157 | int i; |
| 158 | |
| 159 | if (buf[0] == '#') { |
| 160 | buf[strlen(buf)-1] = 0; |
| 161 | if (info_source[0] && strcmp(info_source, buf+1)) |
| 162 | source_mismatch = 1; |
| 163 | strncpy(info_source, buf+1, sizeof(info_source)-1); |
| 164 | continue; |
| 165 | } |
| 166 | if ((n = malloc(sizeof(*n))) == NULL) |
| 167 | abort(); |
| 168 | |
| 169 | if (!(p = strchr(buf, ' '))) |
| 170 | abort(); |
| 171 | *p++ = 0; |
| 172 | |
| 173 | if (sscanf(buf, "%d", &n->ifindex) != 1) |
| 174 | abort(); |
| 175 | if (!(next = strchr(p, ' '))) |
| 176 | abort(); |
| 177 | *next++ = 0; |
| 178 | |
| 179 | n->name = strdup(p); |
| 180 | p = next; |
| 181 | |
| 182 | for (i=0; i<MAXS; i++) { |
| 183 | unsigned rate; |
| 184 | if (!(next = strchr(p, ' '))) |
| 185 | abort(); |
| 186 | *next++ = 0; |
| 187 | if (sscanf(p, "%llu", n->val+i) != 1) |
| 188 | abort(); |
| 189 | n->ival[i] = (unsigned long)n->val[i]; |
| 190 | p = next; |
| 191 | if (!(next = strchr(p, ' '))) |
| 192 | abort(); |
| 193 | *next++ = 0; |
| 194 | if (sscanf(p, "%u", &rate) != 1) |
| 195 | abort(); |
| 196 | n->rate[i] = rate; |
| 197 | p = next; |
| 198 | } |
| 199 | n->next = db; |
| 200 | db = n; |
| 201 | } |
| 202 | |
| 203 | while (db) { |
| 204 | n = db; |
| 205 | db = db->next; |
| 206 | n->next = kern_db; |
| 207 | kern_db = n; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | void dump_raw_db(FILE *fp, int to_hist) |
| 212 | { |
| 213 | struct ifstat_ent *n, *h; |
| 214 | h = hist_db; |
| 215 | fprintf(fp, "#%s\n", info_source); |
| 216 | |
| 217 | for (n=kern_db; n; n=n->next) { |
| 218 | int i; |
| 219 | unsigned long long *vals = n->val; |
| 220 | double *rates = n->rate; |
| 221 | if (!match(n->name)) { |
| 222 | struct ifstat_ent *h1; |
| 223 | if (!to_hist) |
| 224 | continue; |
| 225 | for (h1 = h; h1; h1 = h1->next) { |
| 226 | if (h1->ifindex == n->ifindex) { |
| 227 | vals = h1->val; |
| 228 | rates = h1->rate; |
| 229 | h = h1->next; |
| 230 | break; |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | fprintf(fp, "%d %s ", n->ifindex, n->name); |
| 235 | for (i=0; i<MAXS; i++) |
| 236 | fprintf(fp, "%llu %u ", vals[i], (unsigned)rates[i]); |
| 237 | fprintf(fp, "\n"); |
| 238 | } |
| 239 | } |
| 240 | |
osdl.org!shemminger | 9f3ea25 | 2004-06-09 17:37:28 +0000 | [diff] [blame] | 241 | /* use communication definitions of meg/kilo etc */ |
| 242 | static const unsigned long long giga = 1000000000ull; |
osdl.net!shemminger | bb6a21a | 2004-10-06 23:17:10 +0000 | [diff] [blame] | 243 | static const unsigned long long mega = 1000000; |
| 244 | static const unsigned long long kilo = 1000; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 245 | |
| 246 | void format_rate(FILE *fp, unsigned long long *vals, double *rates, int i) |
| 247 | { |
| 248 | char temp[64]; |
osdl.org!shemminger | 9f3ea25 | 2004-06-09 17:37:28 +0000 | [diff] [blame] | 249 | if (vals[i] > giga) |
| 250 | fprintf(fp, "%7lluM ", vals[i]/mega); |
| 251 | else if (vals[i] > mega) |
| 252 | fprintf(fp, "%7lluK ", vals[i]/kilo); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 253 | else |
| 254 | fprintf(fp, "%8llu ", vals[i]); |
| 255 | |
osdl.org!shemminger | 9f3ea25 | 2004-06-09 17:37:28 +0000 | [diff] [blame] | 256 | if (rates[i] > mega) { |
| 257 | sprintf(temp, "%uM", (unsigned)(rates[i]/mega)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 258 | fprintf(fp, "%-6s ", temp); |
osdl.org!shemminger | 9f3ea25 | 2004-06-09 17:37:28 +0000 | [diff] [blame] | 259 | } else if (rates[i] > kilo) { |
| 260 | sprintf(temp, "%uK", (unsigned)(rates[i]/kilo)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 261 | fprintf(fp, "%-6s ", temp); |
| 262 | } else |
| 263 | fprintf(fp, "%-6u ", (unsigned)rates[i]); |
| 264 | } |
| 265 | |
| 266 | void format_pair(FILE *fp, unsigned long long *vals, int i, int k) |
| 267 | { |
| 268 | char temp[64]; |
osdl.org!shemminger | 9f3ea25 | 2004-06-09 17:37:28 +0000 | [diff] [blame] | 269 | if (vals[i] > giga) |
| 270 | fprintf(fp, "%7lluM ", vals[i]/mega); |
| 271 | else if (vals[i] > mega) |
| 272 | fprintf(fp, "%7lluK ", vals[i]/kilo); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 273 | else |
| 274 | fprintf(fp, "%8llu ", vals[i]); |
| 275 | |
osdl.org!shemminger | 9f3ea25 | 2004-06-09 17:37:28 +0000 | [diff] [blame] | 276 | if (vals[k] > giga) { |
| 277 | sprintf(temp, "%uM", (unsigned)(vals[k]/mega)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 278 | fprintf(fp, "%-6s ", temp); |
osdl.org!shemminger | 9f3ea25 | 2004-06-09 17:37:28 +0000 | [diff] [blame] | 279 | } else if (vals[k] > mega) { |
| 280 | sprintf(temp, "%uK", (unsigned)(vals[k]/kilo)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 281 | fprintf(fp, "%-6s ", temp); |
| 282 | } else |
| 283 | fprintf(fp, "%-6u ", (unsigned)vals[k]); |
| 284 | } |
| 285 | |
| 286 | void print_head(FILE *fp) |
| 287 | { |
| 288 | fprintf(fp, "#%s\n", info_source); |
| 289 | fprintf(fp, "%-15s ", "Interface"); |
| 290 | |
| 291 | fprintf(fp, "%8s/%-6s ", "RX Pkts", "Rate"); |
| 292 | fprintf(fp, "%8s/%-6s ", "TX Pkts", "Rate"); |
| 293 | fprintf(fp, "%8s/%-6s ", "RX Data", "Rate"); |
| 294 | fprintf(fp, "%8s/%-6s\n","TX Data", "Rate"); |
| 295 | |
| 296 | if (!show_errors) { |
| 297 | fprintf(fp, "%-15s ", ""); |
| 298 | fprintf(fp, "%8s/%-6s ", "RX Errs", "Drop"); |
| 299 | fprintf(fp, "%8s/%-6s ", "TX Errs", "Drop"); |
| 300 | fprintf(fp, "%8s/%-6s ", "RX Over", "Rate"); |
| 301 | fprintf(fp, "%8s/%-6s\n","TX Coll", "Rate"); |
| 302 | } else { |
| 303 | fprintf(fp, "%-15s ", ""); |
| 304 | fprintf(fp, "%8s/%-6s ", "RX Errs", "Rate"); |
| 305 | fprintf(fp, "%8s/%-6s ", "RX Drop", "Rate"); |
| 306 | fprintf(fp, "%8s/%-6s ", "RX Over", "Rate"); |
| 307 | fprintf(fp, "%8s/%-6s\n","RX Leng", "Rate"); |
| 308 | |
| 309 | fprintf(fp, "%-15s ", ""); |
| 310 | fprintf(fp, "%8s/%-6s ", "RX Crc", "Rate"); |
| 311 | fprintf(fp, "%8s/%-6s ", "RX Frm", "Rate"); |
| 312 | fprintf(fp, "%8s/%-6s ", "RX Fifo", "Rate"); |
| 313 | fprintf(fp, "%8s/%-6s\n","RX Miss", "Rate"); |
| 314 | |
| 315 | fprintf(fp, "%-15s ", ""); |
| 316 | fprintf(fp, "%8s/%-6s ", "TX Errs", "Rate"); |
| 317 | fprintf(fp, "%8s/%-6s ", "TX Drop", "Rate"); |
| 318 | fprintf(fp, "%8s/%-6s ", "TX Coll", "Rate"); |
| 319 | fprintf(fp, "%8s/%-6s\n","TX Carr", "Rate"); |
| 320 | |
| 321 | fprintf(fp, "%-15s ", ""); |
| 322 | fprintf(fp, "%8s/%-6s ", "TX Abrt", "Rate"); |
| 323 | fprintf(fp, "%8s/%-6s ", "TX Fifo", "Rate"); |
| 324 | fprintf(fp, "%8s/%-6s ", "TX Hear", "Rate"); |
| 325 | fprintf(fp, "%8s/%-6s\n","TX Wind", "Rate"); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | void print_one_if(FILE *fp, struct ifstat_ent *n, unsigned long long *vals) |
| 330 | { |
| 331 | int i; |
| 332 | fprintf(fp, "%-15s ", n->name); |
| 333 | for (i=0; i<4; i++) |
| 334 | format_rate(fp, vals, n->rate, i); |
| 335 | fprintf(fp, "\n"); |
| 336 | |
| 337 | if (!show_errors) { |
| 338 | fprintf(fp, "%-15s ", ""); |
| 339 | format_pair(fp, vals, 4, 6); |
| 340 | format_pair(fp, vals, 5, 7); |
| 341 | format_rate(fp, vals, n->rate, 11); |
| 342 | format_rate(fp, vals, n->rate, 9); |
| 343 | fprintf(fp, "\n"); |
| 344 | } else { |
| 345 | fprintf(fp, "%-15s ", ""); |
| 346 | format_rate(fp, vals, n->rate, 4); |
| 347 | format_rate(fp, vals, n->rate, 6); |
| 348 | format_rate(fp, vals, n->rate, 11); |
| 349 | format_rate(fp, vals, n->rate, 10); |
| 350 | fprintf(fp, "\n"); |
| 351 | |
| 352 | fprintf(fp, "%-15s ", ""); |
| 353 | format_rate(fp, vals, n->rate, 12); |
| 354 | format_rate(fp, vals, n->rate, 13); |
| 355 | format_rate(fp, vals, n->rate, 14); |
| 356 | format_rate(fp, vals, n->rate, 15); |
| 357 | fprintf(fp, "\n"); |
| 358 | |
| 359 | fprintf(fp, "%-15s ", ""); |
| 360 | format_rate(fp, vals, n->rate, 5); |
| 361 | format_rate(fp, vals, n->rate, 7); |
| 362 | format_rate(fp, vals, n->rate, 9); |
| 363 | format_rate(fp, vals, n->rate, 17); |
| 364 | fprintf(fp, "\n"); |
| 365 | |
| 366 | fprintf(fp, "%-15s ", ""); |
| 367 | format_rate(fp, vals, n->rate, 16); |
| 368 | format_rate(fp, vals, n->rate, 18); |
| 369 | format_rate(fp, vals, n->rate, 19); |
| 370 | format_rate(fp, vals, n->rate, 20); |
| 371 | fprintf(fp, "\n"); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | |
| 376 | void dump_kern_db(FILE *fp) |
| 377 | { |
| 378 | struct ifstat_ent *n, *h; |
| 379 | h = hist_db; |
| 380 | |
| 381 | print_head(fp); |
| 382 | |
| 383 | for (n=kern_db; n; n=n->next) { |
| 384 | if (!match(n->name)) |
| 385 | continue; |
| 386 | print_one_if(fp, n, n->val); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | |
| 391 | void dump_incr_db(FILE *fp) |
| 392 | { |
| 393 | struct ifstat_ent *n, *h; |
| 394 | h = hist_db; |
| 395 | |
| 396 | print_head(fp); |
| 397 | |
| 398 | for (n=kern_db; n; n=n->next) { |
| 399 | int i; |
| 400 | unsigned long long vals[MAXS]; |
| 401 | struct ifstat_ent *h1; |
| 402 | |
| 403 | memcpy(vals, n->val, sizeof(vals)); |
| 404 | |
| 405 | for (h1 = h; h1; h1 = h1->next) { |
| 406 | if (h1->ifindex == n->ifindex) { |
| 407 | for (i = 0; i < MAXS; i++) |
| 408 | vals[i] -= h1->val[i]; |
| 409 | h = h1->next; |
| 410 | break; |
| 411 | } |
| 412 | } |
| 413 | if (!match(n->name)) |
| 414 | continue; |
| 415 | print_one_if(fp, n, vals); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | |
| 420 | static int children; |
| 421 | |
| 422 | void sigchild(int signo) |
| 423 | { |
| 424 | } |
| 425 | |
| 426 | void update_db(int interval) |
| 427 | { |
| 428 | struct ifstat_ent *n, *h; |
| 429 | |
| 430 | n = kern_db; |
| 431 | kern_db = NULL; |
| 432 | |
| 433 | load_info(); |
| 434 | |
| 435 | h = kern_db; |
| 436 | kern_db = n; |
| 437 | |
| 438 | for (n = kern_db; n; n = n->next) { |
| 439 | struct ifstat_ent *h1; |
| 440 | for (h1 = h; h1; h1 = h1->next) { |
| 441 | if (h1->ifindex == n->ifindex) { |
| 442 | int i; |
| 443 | for (i = 0; i < MAXS; i++) { |
| 444 | if ((long)(h1->ival[i] - n->ival[i]) < 0) { |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 445 | memset(n->ival, 0, sizeof(n->ival)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 446 | break; |
| 447 | } |
| 448 | } |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 449 | for (i = 0; i < MAXS; i++) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 450 | double sample; |
| 451 | unsigned long incr = h1->ival[i] - n->ival[i]; |
| 452 | n->val[i] += incr; |
| 453 | n->ival[i] = h1->ival[i]; |
| 454 | sample = (double)(incr*1000)/interval; |
| 455 | if (interval >= scan_interval) { |
| 456 | n->rate[i] += W*(sample-n->rate[i]); |
| 457 | } else if (interval >= 1000) { |
| 458 | if (interval >= time_constant) { |
| 459 | n->rate[i] = sample; |
| 460 | } else { |
| 461 | double w = W*(double)interval/scan_interval; |
| 462 | n->rate[i] += w*(sample-n->rate[i]); |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | while (h != h1) { |
| 468 | struct ifstat_ent *tmp = h; |
| 469 | h = h->next; |
| 470 | free(tmp->name); |
| 471 | free(tmp); |
| 472 | }; |
| 473 | h = h1->next; |
| 474 | free(h1->name); |
| 475 | free(h1); |
| 476 | break; |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | #define T_DIFF(a,b) (((a).tv_sec-(b).tv_sec)*1000 + ((a).tv_usec-(b).tv_usec)/1000) |
| 483 | |
| 484 | |
| 485 | void server_loop(int fd) |
| 486 | { |
shemminger | 737f15f | 2005-07-08 22:08:47 +0000 | [diff] [blame] | 487 | struct timeval snaptime = { 0 }; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 488 | struct pollfd p; |
| 489 | p.fd = fd; |
| 490 | p.events = p.revents = POLLIN; |
| 491 | |
| 492 | sprintf(info_source, "%d.%lu sampling_interval=%d time_const=%d", |
| 493 | getpid(), (unsigned long)random(), scan_interval/1000, time_constant/1000); |
| 494 | |
| 495 | load_info(); |
| 496 | |
| 497 | for (;;) { |
| 498 | int status; |
| 499 | int tdiff; |
| 500 | struct timeval now; |
shemminger | 737f15f | 2005-07-08 22:08:47 +0000 | [diff] [blame] | 501 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 502 | gettimeofday(&now, NULL); |
| 503 | tdiff = T_DIFF(now, snaptime); |
| 504 | if (tdiff >= scan_interval) { |
| 505 | update_db(tdiff); |
| 506 | snaptime = now; |
| 507 | tdiff = 0; |
| 508 | } |
shemminger | 737f15f | 2005-07-08 22:08:47 +0000 | [diff] [blame] | 509 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 510 | if (poll(&p, 1, tdiff + scan_interval) > 0 |
| 511 | && (p.revents&POLLIN)) { |
| 512 | int clnt = accept(fd, NULL, NULL); |
| 513 | if (clnt >= 0) { |
| 514 | pid_t pid; |
| 515 | if (children >= 5) { |
| 516 | close(clnt); |
| 517 | } else if ((pid = fork()) != 0) { |
| 518 | if (pid>0) |
| 519 | children++; |
| 520 | close(clnt); |
| 521 | } else { |
| 522 | FILE *fp = fdopen(clnt, "w"); |
| 523 | if (fp) { |
| 524 | if (tdiff > 0) |
| 525 | update_db(tdiff); |
| 526 | dump_raw_db(fp, 0); |
| 527 | } |
| 528 | exit(0); |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | while (children && waitpid(-1, &status, WNOHANG) > 0) |
| 533 | children--; |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | int verify_forging(int fd) |
| 538 | { |
| 539 | struct ucred cred; |
shemminger | 737f15f | 2005-07-08 22:08:47 +0000 | [diff] [blame] | 540 | socklen_t olen = sizeof(cred); |
| 541 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 542 | if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, (void*)&cred, &olen) || |
| 543 | olen < sizeof(cred)) |
| 544 | return -1; |
| 545 | if (cred.uid == getuid() || cred.uid == 0) |
| 546 | return 0; |
| 547 | return -1; |
| 548 | } |
| 549 | |
osdl.org!shemminger | d7e0809 | 2004-06-09 18:09:48 +0000 | [diff] [blame] | 550 | static void usage(void) __attribute__((noreturn)); |
| 551 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 552 | static void usage(void) |
| 553 | { |
osdl.org!shemminger | d7e0809 | 2004-06-09 18:09:48 +0000 | [diff] [blame] | 554 | fprintf(stderr, |
| 555 | "Usage: ifstat [OPTION] [ PATTERN [ PATTERN ] ]\n" |
| 556 | " -h, --help this message\n" |
| 557 | " -a, --ignore ignore history\n" |
| 558 | " -d, --scan=SECS sample every statistics every SECS\n" |
| 559 | " -e, --errors show errors\n" |
| 560 | " -n, --nooutput do history only\n" |
| 561 | " -r, --reset reset history\n" |
| 562 | " -s, --noupdate don;t update history\n" |
| 563 | " -t, --interval=SECS report average over the last SECS\n" |
| 564 | " -V, --version output version information\n" |
| 565 | " -z, --zeros show entries with zero activity\n" |
| 566 | " -e, --errors show errors\n" |
| 567 | " -z, --zeros show entries with zero activity\n"); |
| 568 | |
| 569 | exit(-1); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 570 | } |
| 571 | |
osdl.org!shemminger | 9f3ea25 | 2004-06-09 17:37:28 +0000 | [diff] [blame] | 572 | static const struct option longopts[] = { |
| 573 | { "help", 0, 0, 'h' }, |
| 574 | { "ignore", 0, 0, 'a' }, |
| 575 | { "scan", 1, 0, 'd'}, |
| 576 | { "errors", 0, 0, 'e' }, |
| 577 | { "nooutput", 0, 0, 'n' }, |
| 578 | { "reset", 0, 0, 'r' }, |
| 579 | { "noupdate", 0, 0, 's' }, |
| 580 | { "interval", 1, 0, 't' }, |
| 581 | { "version", 0, 0, 'V' }, |
| 582 | { "zeros", 0, 0, 'z' }, |
| 583 | { "errors", 0, 0, 'e' }, |
| 584 | { "zeros", 0, 0, 'z' }, |
| 585 | { 0 } |
| 586 | }; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 587 | |
| 588 | int main(int argc, char *argv[]) |
| 589 | { |
| 590 | char hist_name[128]; |
| 591 | struct sockaddr_un sun; |
| 592 | FILE *hist_fp = NULL; |
| 593 | int ch; |
| 594 | int fd; |
| 595 | |
osdl.org!shemminger | 9f3ea25 | 2004-06-09 17:37:28 +0000 | [diff] [blame] | 596 | while ((ch = getopt_long(argc, argv, "hvVzrnasd:t:eK", |
| 597 | longopts, NULL)) != EOF) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 598 | switch(ch) { |
| 599 | case 'z': |
| 600 | dump_zeros = 1; |
| 601 | break; |
| 602 | case 'r': |
| 603 | reset_history = 1; |
| 604 | break; |
| 605 | case 'a': |
| 606 | ignore_history = 1; |
| 607 | break; |
| 608 | case 's': |
| 609 | no_update = 1; |
| 610 | break; |
| 611 | case 'n': |
| 612 | no_output = 1; |
| 613 | break; |
| 614 | case 'e': |
| 615 | show_errors = 1; |
| 616 | break; |
| 617 | case 'd': |
osdl.org!shemminger | 9f3ea25 | 2004-06-09 17:37:28 +0000 | [diff] [blame] | 618 | scan_interval = atoi(optarg) * 1000; |
| 619 | if (scan_interval <= 0) { |
| 620 | fprintf(stderr, "ifstat: invalid scan interval\n"); |
| 621 | exit(-1); |
| 622 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 623 | break; |
| 624 | case 't': |
osdl.org!shemminger | 9f3ea25 | 2004-06-09 17:37:28 +0000 | [diff] [blame] | 625 | time_constant = atoi(optarg); |
| 626 | if (time_constant <= 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 627 | fprintf(stderr, "ifstat: invalid time constant divisor\n"); |
| 628 | exit(-1); |
| 629 | } |
| 630 | break; |
| 631 | case 'v': |
| 632 | case 'V': |
| 633 | printf("ifstat utility, iproute2-ss%s\n", SNAPSHOT); |
| 634 | exit(0); |
| 635 | case 'h': |
| 636 | case '?': |
| 637 | default: |
| 638 | usage(); |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | argc -= optind; |
| 643 | argv += optind; |
| 644 | |
| 645 | sun.sun_family = AF_UNIX; |
| 646 | sun.sun_path[0] = 0; |
| 647 | sprintf(sun.sun_path+1, "ifstat%d", getuid()); |
| 648 | |
| 649 | if (scan_interval > 0) { |
| 650 | if (time_constant == 0) |
| 651 | time_constant = 60; |
| 652 | time_constant *= 1000; |
| 653 | W = 1 - 1/exp(log(10)*(double)scan_interval/time_constant); |
| 654 | if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { |
| 655 | perror("ifstat: socket"); |
| 656 | exit(-1); |
| 657 | } |
| 658 | if (bind(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) < 0) { |
| 659 | perror("ifstat: bind"); |
| 660 | exit(-1); |
| 661 | } |
| 662 | if (listen(fd, 5) < 0) { |
| 663 | perror("ifstat: listen"); |
| 664 | exit(-1); |
| 665 | } |
| 666 | if (fork()) |
| 667 | exit(0); |
| 668 | chdir("/"); |
| 669 | close(0); close(1); close(2); setsid(); |
| 670 | signal(SIGPIPE, SIG_IGN); |
| 671 | signal(SIGCHLD, sigchild); |
| 672 | server_loop(fd); |
| 673 | exit(0); |
| 674 | } |
| 675 | |
| 676 | patterns = argv; |
| 677 | npatterns = argc; |
| 678 | |
| 679 | if (getenv("IFSTAT_HISTORY")) |
| 680 | snprintf(hist_name, sizeof(hist_name), getenv("IFSTAT_HISTORY")); |
| 681 | else |
osdl.org!shemminger | c0076e7 | 2004-06-08 22:51:46 +0000 | [diff] [blame] | 682 | sprintf(hist_name, "%s/.ifstat.u%d", P_tmpdir, getuid()); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 683 | |
| 684 | if (reset_history) |
| 685 | unlink(hist_name); |
| 686 | |
| 687 | if (!ignore_history || !no_update) { |
| 688 | struct stat stb; |
| 689 | |
| 690 | fd = open(hist_name, O_RDWR|O_CREAT|O_NOFOLLOW, 0600); |
| 691 | if (fd < 0) { |
| 692 | perror("ifstat: open history file"); |
| 693 | exit(-1); |
| 694 | } |
| 695 | if ((hist_fp = fdopen(fd, "r+")) == NULL) { |
| 696 | perror("ifstat: fdopen history file"); |
| 697 | exit(-1); |
| 698 | } |
| 699 | if (flock(fileno(hist_fp), LOCK_EX)) { |
| 700 | perror("ifstat: flock history file"); |
| 701 | exit(-1); |
| 702 | } |
| 703 | if (fstat(fileno(hist_fp), &stb) != 0) { |
| 704 | perror("ifstat: fstat history file"); |
| 705 | exit(-1); |
| 706 | } |
| 707 | if (stb.st_nlink != 1 || stb.st_uid != getuid()) { |
| 708 | fprintf(stderr, "ifstat: something is so wrong with history file, that I prefer not to proceed.\n"); |
| 709 | exit(-1); |
| 710 | } |
| 711 | if (!ignore_history) { |
| 712 | FILE *tfp; |
| 713 | long uptime; |
| 714 | if ((tfp = fopen("/proc/uptime", "r")) != NULL) { |
| 715 | if (fscanf(tfp, "%ld", &uptime) != 1) |
| 716 | uptime = -1; |
| 717 | fclose(tfp); |
| 718 | } |
| 719 | if (uptime >= 0 && time(NULL) >= stb.st_mtime+uptime) { |
| 720 | fprintf(stderr, "ifstat: history is aged out, resetting\n"); |
| 721 | ftruncate(fileno(hist_fp), 0); |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | load_raw_table(hist_fp); |
| 726 | |
| 727 | hist_db = kern_db; |
| 728 | kern_db = NULL; |
| 729 | } |
| 730 | |
| 731 | if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) >= 0 && |
| 732 | (connect(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) == 0 |
| 733 | || (strcpy(sun.sun_path+1, "ifstat0"), |
| 734 | connect(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) == 0)) |
| 735 | && verify_forging(fd) == 0) { |
| 736 | FILE *sfp = fdopen(fd, "r"); |
| 737 | load_raw_table(sfp); |
| 738 | if (hist_db && source_mismatch) { |
| 739 | fprintf(stderr, "ifstat: history is stale, ignoring it.\n"); |
| 740 | hist_db = NULL; |
| 741 | } |
| 742 | fclose(sfp); |
| 743 | } else { |
| 744 | if (fd >= 0) |
| 745 | close(fd); |
| 746 | if (hist_db && info_source[0] && strcmp(info_source, "kernel")) { |
| 747 | fprintf(stderr, "ifstat: history is stale, ignoring it.\n"); |
| 748 | hist_db = NULL; |
| 749 | info_source[0] = 0; |
| 750 | } |
| 751 | load_info(); |
| 752 | if (info_source[0] == 0) |
| 753 | strcpy(info_source, "kernel"); |
| 754 | } |
| 755 | |
| 756 | if (!no_output) { |
| 757 | if (ignore_history || hist_db == NULL) |
| 758 | dump_kern_db(stdout); |
| 759 | else |
| 760 | dump_incr_db(stdout); |
| 761 | } |
| 762 | if (!no_update) { |
| 763 | ftruncate(fileno(hist_fp), 0); |
| 764 | rewind(hist_fp); |
| 765 | dump_raw_db(hist_fp, 1); |
| 766 | fflush(hist_fp); |
| 767 | } |
| 768 | exit(0); |
| 769 | } |