📄 ifstat.c
字号:
/* $Header: /usr/cvsroot/target/src/wrn/wm/demo/snmptalk/ifstat.c,v 1.3 2002/03/11 19:17:08 josh Exp $ *//* * Copyright (C) 1999-2005 Wind River Systems, Inc. * All rights reserved. Provided under license only. * Distribution or other use of this software is only * permitted pursuant to the terms of a license agreement * from Wind River Systems (and is otherwise prohibited). * Refer to that license agreement for terms of use. *//**************************************************************************** * Copyright 1991-1997 Epilogue Technology Corporation. * Copyright 1998 Integrated Systems, Inc. * All rights reserved. ****************************************************************************//* get interface statistics with SNMP *//* * $Log: ifstat.c,v $ * Revision 1.3 2002/03/11 19:17:08 josh * snmptalk's destination address is now stored and passed around * as an ipaddr_t. This required changes to a lot of internal function * calls that used to expect an inaddr_t to be passed around. Also, * snmptalk is now capable of using IPv6 sockets * * Revision 1.2 2001/11/08 16:47:24 tneale * Updated for newset file layout * * Revision 1.1.1.1 2001/11/05 17:49:11 tneale * Tornado shuffle * * Revision 7.10 2001/01/19 22:24:46 paul * Update copyright. * * Revision 7.9 2000/03/17 00:14:33 meister * Update copyright message * * Revision 7.8 1998/06/23 20:56:44 sar * Moved parse.h to snark/h/parse.h * * Revision 7.7 1998/02/25 04:57:55 sra * Update copyrights. * * Revision 7.6 1997/03/20 06:53:45 sra * DFARS-safe copyright text. Zap! * * Revision 7.5 1997/02/25 10:58:16 sra * Update copyright notice, dust under the bed. * * Revision 7.4 1997/02/19 08:10:29 sra * More fun merging snmptalk into snark, general snark cleanup. * * Revision 7.3 1997/01/09 22:44:19 sar * Modified the command calling sequence again, this time to mathc * what snark was using. * * Revision 7.2 1997/01/08 23:26:53 sar * Updated include files to use envoy/h as appropriate after * move from envoy/utils to snark/snmptalk * * Revision 7.1 1997/01/08 01:55:00 sar * Modified to use the snark sty driver for in/output * * Revision 7.0 1996/03/18 20:19:20 sar * Updated rev to 7.0 and copyright to 96 * * Revision 6.1 1995/10/20 22:39:59 sar * Modified call to snmptalk_create_request * * Revision 6.0 1995/05/31 21:50:03 sra * Release 6.0. * * Revision 5.0 1994/05/16 15:57:40 sar * Updated revision to 5.0 and copyright to include 1994 * * Revision 4.0 1993/06/24 17:14:56 sar * Updated rev to 4.0 and copyright to 93 * * Revision 3.3 1993/05/19 20:38:22 sar * Added procedure prototypes to some of the routines. (#if !defined(NO_PP)...) * * Revision 3.2 1993/02/22 21:04:10 dab * Changed to use SNMPTalk_Create_Request so does either v1 or v2. * * Revision 3.1 1993/02/05 18:47:56 dab * Changed "system.h" to <snmptalk.h> * * Revision 3.0 1992/04/03 19:53:14 dab * Release 3.0 * * Revision 2.106 92/02/11 10:56:19 dab * Casts to make Microsoft C happier. * * Revision 2.105 91/11/06 13:54:59 dab * Added function prototypes for internal functions and other fixups so * pickier compilers would be happy. * * Revision 2.104 91/11/04 16:14:37 dab * Improved for use with higher warning levels on the compiler. * * Revision 2.103 91/10/30 09:20:48 dab * Don't do command if destination isn't set. * * Revision 2.102 91/09/30 14:09:23 dab * Made the changes so this command wokrs with the async changes. * * Revision 2.101 91/09/12 13:23:44 dab * Temporarily removed due to asynch change. * * Revision 2.100 91/08/09 14:05:11 dab * Update version number. This is just before the work to upgrade to * release 3.0. * * Revision 1.1 91/07/11 18:13:20 romkey * Initial revision * *//* [clearcase]modification history-------------------01a,19apr05,job update copyright notices*/#include <snmptalk.h>#include <stdio.h>#include <wrn/wm/snmp/engine/asn1.h>#include <wrn/wm/snmp/engine/localio.h>#include <wrn/wm/snmp/engine/buffer.h>#include <wrn/wm/snmp/engine/objectid.h>#include <wrn/wm/snmp/engine/snmp.h>#include <wrn/wm/snmp/engine/buildpkt.h>#include "md.h"#include "snmpint.h"#include "mibutils.h"#include <wrn/wm/demo/snarklib.h>#include <wrn/wm/demo/snmpconf.h>enum help_level { help_none, help_short, help_long };/* Make the typing a bit easier */#define MIB2 1, 3, 6, 1, 2, 1#define Interfaces MIB2, 2#define ifEntry Interfaces, 2, 1#define OIDSZ(oid) (sizeof(oid)/sizeof(OIDC_T))#define IF_INSTANCE(oid) (oid + (OIDSZ(oid) - 1))static void if_response(struct sty *, SNMP_PKT_T *);OIDC_T ifNumber[] = { Interfaces, 1, 0 };OIDC_T ifIndex[] = { ifEntry, 1, 0 };OIDC_T ifDescr[] = { ifEntry, 2, 0 };OIDC_T ifType[] = { ifEntry, 3, 0 };OIDC_T ifMtu[] = { ifEntry, 4, 0 };OIDC_T ifSpeed[] = { ifEntry, 5, 0 };OIDC_T ifPhysAddress[] = { ifEntry, 6, 0 };OIDC_T ifAdminStatus[] = { ifEntry, 7, 0 };OIDC_T ifOperStatus[] = { ifEntry, 8, 0 };OIDC_T ifLastChange[] = { ifEntry, 9, 0 };OIDC_T ifInOctets[] = { ifEntry, 10, 0 };OIDC_T ifInUcastPkts[] = { ifEntry, 11, 0 };OIDC_T ifInNUcastPkts[] = { ifEntry, 12, 0 };OIDC_T ifInDiscards[] = { ifEntry, 13, 0 };OIDC_T ifInErrors[] = { ifEntry, 14, 0 };OIDC_T ifInUnknownProtos[] = { ifEntry, 15, 0 };OIDC_T ifOutOctets[] = { ifEntry, 16, 0 };OIDC_T ifOutUcastPkts[] = { ifEntry, 17, 0 };OIDC_T ifOutNUcastPkts[] = { ifEntry, 18, 0 };OIDC_T ifOutDiscards[] = { ifEntry, 19, 0 };OIDC_T ifOutErrors[] = { ifEntry, 20, 0 };OIDC_T ifOutQLen[] = { ifEntry, 21, 0 };extern int req_id;static int ifcount;static long num_interfaces;static void if_error(struct sty *sty, char *err_msg){ sty_printf(sty, "%s\n", err_msg); command_next(sty);}static void send_ifstat_pkt(struct sty *sty, int if_index){ SNMP_PKT_T *tpkt; char *err_msg; if ((long)if_index > num_interfaces) { command_next(sty); return; } tpkt = SNMPTalk_Create_Request(GET_REQUEST_PDU, 2, 0, 0); *IF_INSTANCE(ifDescr) = ifcount; SNMP_Bind_Null(tpkt, 0, OIDSZ(ifDescr), ifDescr); *IF_INSTANCE(ifOperStatus) = ifcount; SNMP_Bind_Null(tpkt, 1, OIDSZ(ifOperStatus), ifOperStatus); err_msg = snmp_send_rec(sty, tpkt, &dest_addr, if_response, if_error); if (err_msg) { sty_printf(sty, "%s\n",err_msg); command_next(sty); }}static void if_response(struct sty *sty, SNMP_PKT_T *rpkt){ VB_T *vbl; unsigned char *ifname; int ifname_len; static char bad_type[] = "<type>"; char pbuf[20]; /* If we get an error back from the agent. */ if (rpkt->pdu.std_pdu.error_status != NO_ERROR) { print_snmp_error(sty, rpkt); command_next(sty); } else { vbl = rpkt->pdu.std_pdu.std_vbl.vblist; sty_printf(sty, "%2d: ", ifcount); if (vbl == 0) sty_puts(sty, "<< empty varbindlist >>\n"); else { /* ifDescr */ if (vbl->vb_data_flags_n_type != VT_STRING) { ifname = (unsigned char *)bad_type; ifname_len = strlen(bad_type); } else { ifname = vbl->value_u.v_string.start_bp; ifname_len = EBufferUsed(&vbl->value_u.v_string); } /* ifOperStatus */ vbl++; if (vbl->vb_data_flags_n_type != VT_NUMBER) sty_printf(sty, "%6s ", bad_type); else { int2string(ifOperStatus, OIDSZ(ifOperStatus), vbl->value_u.v_number, pbuf, sizeof(pbuf), 0); sty_printf(sty, "%-6s ", pbuf); } /* print name now */ while (ifname_len--) sty_putc(sty, *ifname++); sty_puts(sty, "\n"); /* send of request for next interface */ send_ifstat_pkt(sty, ++ifcount); } } SNMP_Free(rpkt);}static void num_int_response(struct sty *sty, SNMP_PKT_T *rpkt){ /* If we get an error back from the agent. */ if (rpkt->pdu.std_pdu.error_status != NO_ERROR) { print_snmp_error(sty, rpkt); command_next(sty); } else { switch (rpkt->pdu.std_pdu.std_vbl.vblist->vb_data_flags_n_type) { case VT_NUMBER: case VT_COUNTER: case VT_GAUGE: case VT_TIMETICKS: num_interfaces = rpkt->pdu.std_pdu.std_vbl.vblist->value_u.v_number; sty_printf(sty, "Number Interfaces: %lu\n", num_interfaces); sty_printf(sty, " %6s %s\n", "Status", "Name"); ifcount = 1; send_ifstat_pkt(sty, ifcount); return; default: sty_printf(sty, "Bad type %d for ifNumber\n", rpkt->pdu.std_pdu.std_vbl.vblist->vb_data_flags_n_type); command_next(sty); break; } } SNMP_Free(rpkt);}boolean_t do_ifstat(struct sty *sty, enum help_level help, int argc, char *argv[]){SNMP_PKT_T *tpkt;char *err_msg;switch (help) { case help_short: sty_puts(sty, "ifstat - print interface statistics\n"); break; case help_long: sty_puts(sty, "\The 'ifstat' command walks over the SNMP Interfaces group\n\and prints out what it finds.\n"); break; case help_none: if (dest_addr.type == IPNONE) { sty_puts(sty, "Need to specify destination first\n"); return 1; } tpkt = SNMPTalk_Create_Request(GET_REQUEST_PDU, 1, 0, 0); /* Build the name for an object with the appropriate OID */ SNMP_Bind_Null(tpkt, /* index to set */ 0, OIDSZ(ifNumber), ifNumber); err_msg = snmp_send_rec(sty, tpkt, &dest_addr, num_int_response, if_error); if (err_msg) { sty_printf(sty, "%s\n", err_msg); return(1); } return(0); }return(1);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -