Debian Codegraph @

apps/snmpnetstat/if.c

raw
1 /*
2 * $OpenBSD: if.c,v 1.42 2005/03/13 16:05:50 mpf Exp $
3 */
4 /*
5 * $NetBSD: if.c,v 1.16.4.2 1996/06/07 21:46:46 thorpej Exp $
6 */
7
8 /*
9 * Copyright (c) 1983, 1988, 1993
10 * The Regents of the University of California. All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #include <net-snmp/net-snmp-config.h>
38 #include <net-snmp/net-snmp-includes.h>
39
40 #include <stdint.h>
41 #ifdef HAVE_UNISTD_H
42 #include <unistd.h>
43 #endif
44 #ifdef HAVE_NET_IF_H
45 #include <net/if.h>
46 #endif
47 #ifndef _XOPEN_SOURCE
48 #define _XOPEN_SOURCE 1
49 #endif
50 #ifndef _XOPEN_SOURCE_EXTENDED
51 #define _XOPEN_SOURCE_EXTENDED 1
52 #endif
53 #include <signal.h>
54
55 #include "main.h"
56 #include "netstat.h"
57
58 #define YES 1
59 #define NO 0
60
61 static void sidewaysintpr(u_int);
62
63 struct _if_info {
64 char name[128];
65 char descr[128];
66 char ip[128], route[128];
67 int mtu;
68 int drops;
69 unsigned int ifindex;
70 /*
71 * Save "expandable" fields as string values
72 * rather than integer statistics
73 */
74 char s_ipkts[20], s_ierrs[20];
75 char s_opkts[20], s_oerrs[20];
76 char s_ibytes[20], s_obytes[20];
77 char s_outq[20];
78 unsigned long ipkts, opkts; /* Need to combine 2 MIB values */
79 int operstatus;
80 /*
81 * u_long netmask;
82 * struct in_addr ifip, ifroute;
83 */
84 struct _if_info *next;
85 };
86
87
88 static struct timeval deadline;
89
90 static void
91 timerSet(int interval_seconds)
92 {
93 const struct timeval interval = { interval_seconds, 0 };
94
95 netsnmp_get_monotonic_clock(&deadline);
96 NETSNMP_TIMERADD(&deadline, &interval, &deadline);
97 }
98
99 static void
100 timerPause(void)
101 {
102 struct timeval now, delta;
103
104 netsnmp_get_monotonic_clock(&now);
105 NETSNMP_TIMERSUB(&deadline, &now, &delta);
106 if (delta.tv_sec < 0)
107 return;
108 #ifdef WIN32
109 Sleep(delta.tv_sec * 1000 + delta.tv_usec / 1000);
110 #else
111 if (select(0, NULL, NULL, NULL, &delta) < 0)
112 snmp_perror("select");
113 #endif
114 }
115
116 /*
117 * Retrieve the interface addressing information
118 * XXX - This could also be extended to handle non-IP interfaces
119 */
120 void
121 _set_address(struct _if_info *cur_if)
122 {
123 oid ipaddr_oid[] = { 1, 3, 6, 1, 2, 1, 4, 20, 1, 0 };
124 size_t ipaddr_len = OID_LENGTH(ipaddr_oid);
125 static netsnmp_variable_list *addr_if_var = NULL;
126 static netsnmp_variable_list *addr_mask_var = NULL;
127 netsnmp_variable_list *vp, *vp2;
128 union {
129 in_addr_t addr;
130 char data[4];
131 } tmpAddr;
132 char *cp;
133 in_addr_t ifAddr, mask;
134
135 /*
136 * Note that this information only needs to be retrieved
137 * once, and can be re-used for subsequent calls.
138 */
139 if (addr_if_var == NULL) {
140 ipaddr_oid[9] = 2; /* ipAdEntIfIndex */
141 snmp_varlist_add_variable(&addr_if_var, ipaddr_oid, ipaddr_len,
142 ASN_NULL, NULL, 0);
143 netsnmp_query_walk(addr_if_var, ss);
144
145 ipaddr_oid[9] = 3; /* ipAdEntNetMask */
146 snmp_varlist_add_variable(&addr_mask_var, ipaddr_oid, ipaddr_len,
147 ASN_NULL, NULL, 0);
148 netsnmp_query_walk(addr_mask_var, ss);
149 }
150
151 /*
152 * Find the address row relevant to this interface
153 */
154 for (vp = addr_if_var, vp2 = addr_mask_var; vp;
155 vp = vp->next_variable, vp2 = vp2->next_variable) {
156 if (vp->val.integer && *vp->val.integer == (int) cur_if->ifindex)
157 break;
158 }
159 if (vp2) {
160 /*
161 * Always want a numeric interface IP address
162 */
163 snprintf(cur_if->ip, 128, "%" NETSNMP_PRIo "u.%" NETSNMP_PRIo "u."
164 "%" NETSNMP_PRIo "u.%" NETSNMP_PRIo "u",
165 vp2->name[10],
166 vp2->name[11], vp2->name[12], vp2->name[13]);
167
168 /*
169 * But re-use the routing table utilities/code for
170 * displaying the local network information
171 */
172 cp = tmpAddr.data;
173 cp[0] = (uint8_t) vp2->name[10];
174 cp[1] = (uint8_t) vp2->name[11];
175 cp[2] = (uint8_t) vp2->name[12];
176 cp[3] = (uint8_t) vp2->name[13];
177 ifAddr = tmpAddr.addr;
178 cp = tmpAddr.data;
179 cp[0] = (uint8_t) vp2->val.string[0];
180 cp[1] = (uint8_t) vp2->val.string[1];
181 cp[2] = (uint8_t) vp2->val.string[2];
182 cp[3] = (uint8_t) vp2->val.string[3];
183 mask = tmpAddr.addr;
184 snprintf(cur_if->route, 128, "%s", netname(ifAddr, mask));
185 }
186 }
187
188
189 /*
190 * Print a description of the network interfaces.
191 */
192 void
193 intpr(int interval)
194 {
195 oid ifcol_oid[] = { 1, 3, 6, 1, 2, 1, 2, 2, 1, 0 };
196 size_t ifcol_len = OID_LENGTH(ifcol_oid);
197 oid ifxcol_oid[] = { 1, 3, 6, 1, 2, 1, 31, 1, 1, 1, 0 };
198 size_t ifxcol_len = OID_LENGTH(ifxcol_oid);
199
200 struct _if_info *if_head, *if_tail, *cur_if;
201 netsnmp_variable_list *var, *vp;
202 /*
203 * Track maximum field widths, expanding as necessary
204 * This is one reason why results can't be
205 * displayed immediately they are retrieved.
206 */
207 int max_name = 4, max_ip = 7, max_route = 7, max_outq = 5;
208 int max_ipkts = 5, max_ierrs = 5, max_opkts =
209 5, max_oerrs = 5;
210 int max_ibytes = 6, max_obytes = 6;
211 int i;
212
213
214 if (interval) {
215 sidewaysintpr((unsigned) interval);
216 return;
217 }
218
219 /*
220 * The traditional "netstat -i" output combines information
221 * from two SNMP tables:
222 * ipAddrTable (for the IP address/network)
223 * ifTable (for the interface statistics)
224 *
225 * The previous approach was to retrieve (and save) the
226 * address information first. Then walk the main ifTable,
227 * add the relevant stored addresses, and saving the
228 * full information for each interface, before displaying
229 * the results as a separate pass.
230 *
231 * This code reverses this general structure, by first retrieving
232 * (and storing) the interface statistics for the whole table,
233 * then inserting the address information obtained from the
234 * ipAddrTable, and finally displaying the results.
235 * Such an arrangement should make it easier to extend this
236 * to handle non-IP interfaces (hence not in ipAddrTable)
237 */
238 if_head = NULL;
239 if_tail = NULL;
240 var = NULL;
241
242 #define ADD_IFVAR( x ) ifcol_oid[ ifcol_len-1 ] = x; \
243 snmp_varlist_add_variable( &var, ifcol_oid, ifcol_len, ASN_NULL, NULL, 0)
244 ADD_IFVAR(2); /* ifName */
245 ADD_IFVAR(4); /* ifMtu */
246 ADD_IFVAR(8); /* ifOperStatus */
247 /*
248 * The Net/Open-BSD behaviour is to display *either* byte
249 * counts *or* packet/error counts (but not both). FreeBSD
250 * integrates the byte counts into the traditional display.
251 *
252 * The previous 'snmpnetstat' implementation followed the
253 * separatist model. This re-write offers an opportunity
254 * to adopt the (more useful, IMO) Free-BSD approach.
255 *
256 * Or we could perhaps support both styles? :-)
257 */
258 if (bflag || oflag) {
259 ADD_IFVAR(10); /* ifInOctets */
260 ADD_IFVAR(16); /* ifOutOctets */
261 }
262 if (!oflag) {
263 ADD_IFVAR(11); /* ifInUcastPkts */
264 ADD_IFVAR(12); /* ifInNUcastPkts */
265 ADD_IFVAR(14); /* ifInErrors */
266 ADD_IFVAR(17); /* ifOutUcastPkts */
267 ADD_IFVAR(18); /* ifOutNUcastPkts */
268 ADD_IFVAR(20); /* ifOutErrors */
269 ADD_IFVAR(21); /* ifOutQLen */
270 }
271 #if 0
272 if (tflag) {
273 ADD_IFVAR(XX); /* ??? */
274 }
275 #endif
276 if (dflag) {
277 ADD_IFVAR(19); /* ifOutDiscards */
278 }
279 #undef ADD_IFVAR
280 #define ADD_IFXVAR( x ) ifxcol_oid[ ifxcol_len-1 ] = x; \
281 snmp_varlist_add_variable( &var, ifxcol_oid, ifxcol_len, ASN_NULL, NULL, 0)
282 ADD_IFXVAR(1); /* ifName */
283 #undef ADD_IFXVAR
284
285 /*
286 * Now walk the ifTable, creating a list of interfaces
287 */
288 while (1) {
289 if (netsnmp_query_getnext(var, ss) != SNMP_ERR_NOERROR)
290 break;
291 if ((var->type & 0xF0) == 0x80) /* Exception */
292 break;
293 ifcol_oid[ifcol_len - 1] = 2; /* ifDescr */
294 if (snmp_oid_compare(ifcol_oid, ifcol_len,
295 var->name, ifcol_len) != 0)
296 break; /* End of Table */
297 cur_if = SNMP_MALLOC_TYPEDEF(struct _if_info);
298 if (!cur_if)
299 break;
300 cur_if->ifindex = var->name[var->name_length - 1];
301 for (vp = var; vp; vp = vp->next_variable) {
302 if ((vp->type & 0xF0) == 0x80) /* Exception */
303 continue;
304 if (!vp->val.integer)
305 continue;
306 if (var->name[var->name_length - 1] != cur_if->ifindex) {
307 /*
308 * Inconsistent index information
309 * XXX - Try to recover ?
310 */
311 SNMP_FREE(cur_if);
312 break; /* not for now, no */
313 }
314 switch (vp->name[var->name_length - 2]) {
315 case 2: /* ifDescr */
316 if (vp->val_len >= sizeof(cur_if->name))
317 vp->val_len = sizeof(cur_if->name) - 1;
318 memmove(cur_if->name, vp->val.string, vp->val_len);
319 cur_if->name[vp->val_len] = 0;
320 memmove(cur_if->descr, vp->val.string, vp->val_len);
321 cur_if->descr[vp->val_len] = 0;
322 break;
323 case 4: /* ifMtu */
324 cur_if->mtu = *vp->val.integer;
325 break;
326 case 8: /* ifOperStatus */
327 cur_if->operstatus = *vp->val.integer;
328 /*
329 * XXX - any special processing ??
330 */
331 break;
332 case 10: /* ifInOctets */
333 snprintf(cur_if->s_ibytes, sizeof cur_if->s_ibytes, "%lu", *vp->val.integer);
334 i = strlen(cur_if->s_ibytes);
335 if (i > max_ibytes)
336 max_ibytes = i;
337 break;
338 case 11: /* ifInUcastPkts */
339 cur_if->ipkts += *vp->val.integer;
340 snprintf(cur_if->s_ipkts, sizeof cur_if->s_ipkts, "%lu", cur_if->ipkts);
341 i = strlen(cur_if->s_ipkts);
342 if (i > max_ipkts)
343 max_ipkts = i;
344 break;
345 case 12: /* ifInNUcastPkts */
346 cur_if->ipkts += *vp->val.integer;
347 snprintf(cur_if->s_ipkts, sizeof cur_if->s_ipkts, "%lu", cur_if->ipkts);
348 i = strlen(cur_if->s_ipkts);
349 if (i > max_ipkts)
350 max_ipkts = i;
351 break;
352 case 14: /* ifInErrors */
353 snprintf(cur_if->s_ierrs, sizeof cur_if->s_ierrs, "%lu", *vp->val.integer);
354 i = strlen(cur_if->s_ierrs);
355 if (i > max_ierrs)
356 max_ierrs = i;
357 break;
358 case 16: /* ifOutOctets */
359 snprintf(cur_if->s_obytes, sizeof cur_if->s_obytes, "%lu", *vp->val.integer);
360 i = strlen(cur_if->s_obytes);
361 if (i > max_obytes)
362 max_obytes = i;
363 break;
364 case 17: /* ifOutUcastPkts */
365 cur_if->opkts += *vp->val.integer;
366 snprintf(cur_if->s_opkts, sizeof cur_if->s_opkts, "%lu", cur_if->opkts);
367 i = strlen(cur_if->s_opkts);
368 if (i > max_opkts)
369 max_opkts = i;
370 break;
371 case 18: /* ifOutNUcastPkts */
372 cur_if->opkts += *vp->val.integer;
373 snprintf(cur_if->s_opkts, sizeof cur_if->s_opkts, "%lu", cur_if->opkts);
374 i = strlen(cur_if->s_opkts);
375 if (i > max_opkts)
376 max_opkts = i;
377 break;
378 case 19: /* ifOutDiscards */
379 cur_if->drops = *vp->val.integer;
380 break;
381 case 20: /* ifOutErrors */
382 snprintf(cur_if->s_oerrs, sizeof cur_if->s_oerrs, "%lu", *vp->val.integer);
383 i = strlen(cur_if->s_oerrs);
384 if (i > max_oerrs)
385 max_oerrs = i;
386 break;
387 case 21: /* ifOutQLen */
388 snprintf(cur_if->s_outq, sizeof cur_if->s_outq, "%lu", *vp->val.integer);
389 i = strlen(cur_if->s_outq);
390 if (i > max_outq)
391 max_outq = i;
392 break;
393 case 1: /* ifName */
394 if (vp->val_len >= sizeof(cur_if->name))
395 vp->val_len = sizeof(cur_if->name) - 1;
396 memmove(cur_if->name, vp->val.string, vp->val_len);
397 cur_if->name[vp->val_len] = 0;
398 if ((i = strlen(cur_if->name) + 1) > max_name)
399 max_name = i;
400 break;
401 }
402 }
403
404 /*
405 * XXX - Perhaps query ifXTable for additional info ??
406 * (ifName/ifAlias, or HC counters)
407 */
408
409 /*
410 * If we're to monitor a particular interface, then
411 * ignore all others. It would be more efficient
412 * to check this earlier (as part of processing
413 * the varbind list). But performing this test here
414 * means we can recognise ifXTable names as well)
415 */
416 if (intrface && strcmp(cur_if->name, intrface) != 0
417 && strcmp(cur_if->descr, intrface) != 0) {
418 SNMP_FREE(cur_if);
419 }
420
421 /*
422 * Insert the IP address and network settings, and
423 * add the new _if_stat structure to the list.
424 */
425 if (cur_if) {
426 if ((i = strlen(cur_if->name) + 1) > max_name)
427 max_name = i;
428 _set_address(cur_if);
429 i = strlen(cur_if->ip);
430 if (i > max_ip)
431 max_ip = i;
432 i = strlen(cur_if->route);
433 if (i > max_route)
434 max_route = i;
435
436 if (if_tail) {
437 if_tail->next = cur_if;
438 if_tail = cur_if;
439 } else {
440 if_head = cur_if;
441 if_tail = cur_if;
442 }
443 }
444 } /* while (1) */
445 snmp_free_varbind(var);
446
447 /*
448 * Now display the specified results (in Free-BSD format)
449 * setting the field widths appropriately....
450 */
451 printf("%*.*s %5.5s %*.*s %*.*s",
452 -max_name, max_name, "Name", "Mtu",
453 -max_route, max_route, "Network", -max_ip, max_ip, "Address");
454 if (oflag) {
455 printf(" %*s %*s", max_ibytes, "Ibytes", max_obytes, "Obytes");
456 } else {
457 printf(" %*s %*s", max_ipkts, "Ipkts", max_ierrs, "Ierrs");
458 if (bflag)
459 printf(" %*s", max_ibytes, "Ibytes");
460
461 printf(" %*s %*s", max_opkts, "Opkts", max_oerrs, "Oerrs");
462 if (bflag)
463 printf(" %*s", max_obytes, "Obytes");
464
465 printf(" %*s", max_outq, "Queue");
466 }
467 /*
468 * if (tflag)
469 * printf(" %s", "Time");
470 */
471 if (dflag)
472 printf(" %s", "Drop");
473 putchar('\n');
474
475 for (cur_if = if_head; cur_if; cur_if = cur_if->next) {
476 if (cur_if->name[0] == 0)
477 continue;
478 printf("%*.*s %5d", -max_name, max_name, cur_if->name,
479 cur_if->mtu);
480 printf(" %*.*s", -max_route, max_route, cur_if->route);
481 printf(" %*.*s", -max_ip, max_ip, cur_if->ip);
482
483 if (oflag) {
484 printf(" %*s %*s", max_ibytes, cur_if->s_ibytes,
485 max_obytes, cur_if->s_obytes);
486 } else {
487 printf(" %*s %*s", max_ipkts, cur_if->s_ipkts,
488 max_ierrs, cur_if->s_ierrs);
489 if (bflag)
490 printf(" %*s", max_ibytes, cur_if->s_ibytes);
491
492 printf(" %*s %*s", max_opkts, cur_if->s_opkts,
493 max_oerrs, cur_if->s_oerrs);
494 if (bflag)
495 printf(" %*s", max_obytes, cur_if->s_obytes);
496 printf(" %*s", max_outq, cur_if->s_outq);
497 }
498 /*
499 * if (tflag)
500 * printf(" %4d", cur_if->???);
501 */
502 if (dflag)
503 printf(" %4d", cur_if->drops);
504 putchar('\n');
505 }
506
507 /*
508 * ... and tidy up.
509 */
510 for (cur_if = if_head; cur_if; cur_if = if_head) {
511 if_head = cur_if->next;
512 cur_if->next = NULL;
513 SNMP_FREE(cur_if);
514 }
515 }
516
517
518 #define MAXIF 100
519 struct iftot {
520 char ift_name[128]; /* interface name */
521 int ifIndex;
522 u_long ift_ip; /* input packets */
523 u_long ift_ib; /* input bytes */
524 u_long ift_ie; /* input errors */
525 u_long ift_op; /* output packets */
526 u_long ift_ob; /* output bytes */
527 u_long ift_oe; /* output errors */
528 u_long ift_co; /* collisions */
529 u_long ift_dr; /* drops */
530 };
531
532 int signalled; /* set if alarm goes off "early" */
533
534 /*
535 * Print a running summary of interface statistics.
536 * Repeat display every interval seconds, showing statistics
537 * collected over that interval. Assumes that interval is non-zero.
538 * First line printed at top of screen is always cumulative.
539 */
540 static void
541 sidewaysintpr(unsigned int interval)
542 {
543 /*
544 * As with the "one-shot" interface display, there are
545 * two different possible output formats. The Net/
546 * Open-BSD style displays both information about a
547 * single interface *and* the overall totals.
548 * The equivalent Free-BSD approach is to report on one
549 * or the other (rather than both). This is probably
550 * more useful (IMO), and significantly more efficient.
551 * So that's the style implemented here.
552 *
553 * Note that the 'ifcol' OID buffer can represent a full
554 * instance (including ifIndex), rather than just a
555 * column object OID, as with the one-shot code.
556 */
557 oid ifcol_oid[] = { 1, 3, 6, 1, 2, 1, 2, 2, 1, 0, 0 };
558 size_t ifcol_len = OID_LENGTH(ifcol_oid);
559 netsnmp_variable_list *var, *vp;
560 struct iftot *ip = NULL, *cur_if = NULL; /* single I/F display */
561 struct iftot *sum = NULL, *total = NULL; /* overall summary */
562 int line;
563 int first;
564 size_t i;
565
566 var = NULL;
567 if (intrface) {
568 /*
569 * Locate the ifIndex of the interface to monitor,
570 * by walking the ifDescr column of the ifTable
571 */
572 ifcol_oid[ifcol_len - 2] = 2; /* ifDescr */
573 snmp_varlist_add_variable(&var, ifcol_oid, ifcol_len - 1,
574 ASN_NULL, NULL, 0);
575 i = strlen(intrface);
576 netsnmp_query_walk(var, ss);
577 for (vp = var; vp; vp = vp->next_variable) {
578 if (strncmp(intrface, (char *) vp->val.string, i) == 0 &&
579 i == vp->val_len)
580 break; /* found requested interface */
581 }
582 /*
583 * XXX - Might be worth searching ifName/ifAlias as well
584 */
585 if (!vp) {
586 oid ifname_oid[] =
587 { 1, 3, 6, 1, 2, 1, 31, 1, 1, 1, 1, 0 };
588 size_t ifname_len = OID_LENGTH(ifname_oid);
589 snmp_free_var(var);
590 var = NULL;
591 snmp_varlist_add_variable(&var, ifname_oid, ifname_len - 1,
592 ASN_NULL, NULL, 0);
593 i = strlen(intrface);
594 netsnmp_query_walk(var, ss);
595 for (vp = var; vp; vp = vp->next_variable) {
596 if (strncmp(intrface, (char *) vp->val.string, i) == 0 &&
597 i == vp->val_len)
598 break; /* found requested interface */
599 }
600 }
601 if (!vp) {
602 oid ifalias_oid[] =
603 { 1, 3, 6, 1, 2, 1, 31, 1, 1, 1, 18, 0 };
604 size_t ifalias_len = OID_LENGTH(ifalias_oid);
605 snmp_free_var(var);
606 var = NULL;
607 snmp_varlist_add_variable(&var, ifalias_oid, ifalias_len - 1,
608 ASN_NULL, NULL, 0);
609 i = strlen(intrface);
610 netsnmp_query_walk(var, ss);
611 for (vp = var; vp; vp = vp->next_variable) {
612 if (strncmp(intrface, (char *) vp->val.string, i) == 0 &&
613 i == vp->val_len)
614 break; /* found requested interface */
615 }
616 }
617 if (!vp) {
618 fprintf(stderr, "%s: unknown interface\n", intrface);
619 exit(1);
620 }
621
622 /*
623 * Prepare the current and previous 'iftot' structures,
624 * and set the ifIndex value in the OID buffer.
625 */
626 ip = SNMP_MALLOC_TYPEDEF(struct iftot);
627 cur_if = SNMP_MALLOC_TYPEDEF(struct iftot);
628 if (!ip || !cur_if) {
629 fprintf(stderr, "internal error\n");
630 exit(1);
631 }
632 ifcol_oid[ifcol_len - 1] = vp->name[ifcol_len - 1];
633 snmp_free_var(var);
634 var = NULL;
635 } else {
636 /*
637 * Prepare the current and previous 'iftot' structures.
638 * (using different pointers, for consistency with *BSD code)
639 */
640 sum = SNMP_MALLOC_TYPEDEF(struct iftot);
641 total = SNMP_MALLOC_TYPEDEF(struct iftot);
642 if (!sum || !total) {
643 fprintf(stderr, "internal error\n");
644 exit(1);
645 }
646 }
647
648 timerSet(interval);
649 first = 1;
650 banner:
651 printf("%17s %14s %16s", "input",
652 intrface ? intrface : "(Total)", "output");
653 putchar('\n');
654 printf("%10s %5s %10s %10s %5s %10s %5s",
655 "packets", "errs", "bytes", "packets", "errs", "bytes",
656 "colls");
657 if (dflag)
658 printf(" %5.5s", "drops");
659 putchar('\n');
660 fflush(stdout);
661 line = 0;
662 loop:
663 if (intrface) {
664 #define ADD_IFVAR( x ) ifcol_oid[ ifcol_len-2 ] = x; \
665 snmp_varlist_add_variable( &var, ifcol_oid, ifcol_len, ASN_NULL, NULL, 0)
666 /*
667 * if (bflag) {
668 */
669 ADD_IFVAR(10); /* ifInOctets */
670 ADD_IFVAR(16); /* ifOutOctets */
671 /*
672 * }
673 */
674 ADD_IFVAR(11); /* ifInUcastPkts */
675 ADD_IFVAR(12); /* ifInNUcastPkts */
676 ADD_IFVAR(14); /* ifInErrors */
677 ADD_IFVAR(17); /* ifOutUcastPkts */
678 ADD_IFVAR(18); /* ifOutNUcastPkts */
679 ADD_IFVAR(20); /* ifOutErrors */
680 ADD_IFVAR(21); /* ifOutQLen */
681 if (dflag) {
682 ADD_IFVAR(19); /* ifOutDiscards */
683 }
684 #undef ADD_IFVAR
685
686 netsnmp_query_get(var, ss); /* Or parallel walk ?? */
687 cur_if->ift_ip = 0;
688 cur_if->ift_ib = 0;
689 cur_if->ift_ie = 0;
690 cur_if->ift_op = 0;
691 cur_if->ift_ob = 0;
692 cur_if->ift_oe = 0;
693 cur_if->ift_co = 0;
694 cur_if->ift_dr = 0;
695 cur_if->ifIndex = var->name[ifcol_len - 1];
696 for (vp = var; vp; vp = vp->next_variable) {
697 if ((var->type & 0xF0) == 0x80) /* Exception */
698 continue;
699 if (!vp->val.integer)
700 continue;
701 switch (vp->name[ifcol_len - 2]) {
702 case 10: /* ifInOctets */
703 cur_if->ift_ib = *vp->val.integer;
704 break;
705 case 11: /* ifInUcastPkts */
706 cur_if->ift_ip += *vp->val.integer;
707 break;
708 case 12: /* ifInNUcastPkts */
709 cur_if->ift_ip += *vp->val.integer;
710 break;
711 case 14: /* ifInErrors */
712 cur_if->ift_ie = *vp->val.integer;
713 break;
714 case 16: /* ifOutOctets */
715 cur_if->ift_ob = *vp->val.integer;
716 break;
717 case 17: /* ifOutUcastPkts */
718 cur_if->ift_op += *vp->val.integer;
719 break;
720 case 18: /* ifOutNUcastPkts */
721 cur_if->ift_op += *vp->val.integer;
722 break;
723 case 19: /* ifOutDiscards */
724 cur_if->ift_dr = *vp->val.integer;
725 break;
726 case 20: /* ifOutErrors */
727 cur_if->ift_oe = *vp->val.integer;
728 break;
729 case 21: /* ifOutQLen */
730 cur_if->ift_co = *vp->val.integer;
731 break;
732 }
733 }
734 snmp_free_varbind(var);
735 var = NULL;
736
737 if (!first) {
738 printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu",
739 cur_if->ift_ip - ip->ift_ip,
740 cur_if->ift_ie - ip->ift_ie,
741 cur_if->ift_ib - ip->ift_ib,
742 cur_if->ift_op - ip->ift_op,
743 cur_if->ift_oe - ip->ift_oe,
744 cur_if->ift_ob - ip->ift_ob,
745 cur_if->ift_co - ip->ift_co);
746 if (dflag)
747 printf(" %5lu", cur_if->ift_dr - ip->ift_dr);
748 putchar('\n');
749 fflush(stdout);
750 }
751 ip->ift_ip = cur_if->ift_ip;
752 ip->ift_ie = cur_if->ift_ie;
753 ip->ift_ib = cur_if->ift_ib;
754 ip->ift_op = cur_if->ift_op;
755 ip->ift_oe = cur_if->ift_oe;
756 ip->ift_ob = cur_if->ift_ob;
757 ip->ift_co = cur_if->ift_co;
758 ip->ift_dr = cur_if->ift_dr;
759 } /* (single) interface */
760 else {
761 sum->ift_ip = 0;
762 sum->ift_ib = 0;
763 sum->ift_ie = 0;
764 sum->ift_op = 0;
765 sum->ift_ob = 0;
766 sum->ift_oe = 0;
767 sum->ift_co = 0;
768 sum->ift_dr = 0;
769 #define ADD_IFVAR( x ) ifcol_oid[ ifcol_len-2 ] = x; \
770 snmp_varlist_add_variable( &var, ifcol_oid, ifcol_len-1, ASN_NULL, NULL, 0)
771 ADD_IFVAR(11); /* ifInUcastPkts */
772 ADD_IFVAR(12); /* ifInNUcastPkts */
773 ADD_IFVAR(14); /* ifInErrors */
774 ADD_IFVAR(17); /* ifOutUcastPkts */
775 ADD_IFVAR(18); /* ifOutNUcastPkts */
776 ADD_IFVAR(20); /* ifOutErrors */
777 ADD_IFVAR(21); /* ifOutQLen */
778 /*
779 * if (bflag) {
780 */
781 ADD_IFVAR(10); /* ifInOctets */
782 ADD_IFVAR(16); /* ifOutOctets */
783 /*
784 * }
785 */
786 if (dflag) {
787 ADD_IFVAR(19); /* ifOutDiscards */
788 }
789 #undef ADD_IFVAR
790
791 ifcol_oid[ifcol_len - 2] = 11; /* ifInUcastPkts */
792 while (1) {
793 if (netsnmp_query_getnext(var, ss) != SNMP_ERR_NOERROR)
794 break;
795 if ((var->type & 0xF0) == 0x80) /* Exception */
796 break;
797 if (snmp_oid_compare(ifcol_oid, ifcol_len - 2,
798 var->name, ifcol_len - 2) != 0)
799 break; /* End of Table */
800
801 for (vp = var; vp; vp = vp->next_variable) {
802 if ((vp->type & 0xF0) == 0x80) /* Exception */
803 continue;
804 if (!vp->val.integer)
805 continue;
806 switch (vp->name[ifcol_len - 2]) {
807 case 10: /* ifInOctets */
808 sum->ift_ib += *vp->val.integer;
809 break;
810 case 11: /* ifInUcastPkts */
811 sum->ift_ip += *vp->val.integer;
812 break;
813 case 12: /* ifInNUcastPkts */
814 sum->ift_ip += *vp->val.integer;
815 break;
816 case 14: /* ifInErrors */
817 sum->ift_ie += *vp->val.integer;
818 break;
819 case 16: /* ifOutOctets */
820 sum->ift_ob += *vp->val.integer;
821 break;
822 case 17: /* ifOutUcastPkts */
823 sum->ift_op += *vp->val.integer;
824 break;
825 case 18: /* ifOutNUcastPkts */
826 sum->ift_op += *vp->val.integer;
827 break;
828 case 19: /* ifOutDiscards */
829 sum->ift_dr += *vp->val.integer;
830 break;
831 case 20: /* ifOutErrors */
832 sum->ift_oe += *vp->val.integer;
833 break;
834 case 21: /* ifOutQLen */
835 sum->ift_co += *vp->val.integer;
836 break;
837 }
838 }
839 /*
840 * Now loop to retrieve the next entry from the table.
841 */
842 } /* while (1) */
843
844 snmp_free_varbind(var);
845 var = NULL;
846
847 if (!first) {
848 printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu",
849 sum->ift_ip - total->ift_ip,
850 sum->ift_ie - total->ift_ie,
851 sum->ift_ib - total->ift_ib,
852 sum->ift_op - total->ift_op,
853 sum->ift_oe - total->ift_oe,
854 sum->ift_ob - total->ift_ob,
855 sum->ift_co - total->ift_co);
856 if (dflag)
857 printf(" %5lu", sum->ift_dr - total->ift_dr);
858 putchar('\n');
859 fflush(stdout);
860 }
861 total->ift_ip = sum->ift_ip;
862 total->ift_ie = sum->ift_ie;
863 total->ift_ib = sum->ift_ib;
864 total->ift_op = sum->ift_op;
865 total->ift_oe = sum->ift_oe;
866 total->ift_ob = sum->ift_ob;
867 total->ift_co = sum->ift_co;
868 total->ift_dr = sum->ift_dr;
869 } /* overall summary */
870
871 timerPause();
872 timerSet(interval);
873 line++;
874 first = 0;
875 if (line == 21)
876 goto banner;
877 else
878 goto loop;
879 /*NOTREACHED*/
880 }