⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 if_mib.cpp

📁 JdonFramework need above jdk 1.4.0This version has passed under Tomcat 4.x/5.x JBoss 3.x/JBoss 4.0.0
💻 CPP
📖 第 1 页 / 共 3 页
字号:
{	// This table object is a singleton. In order to access it use	// the static pointer ifEntry::instance.	instance = this;	add_col(new MibLeaf(colIfIndex, READONLY, new SnmpInt32()));	add_col(new SnmpDisplayString(colIfDescr, READONLY, new OctetStr()));	add_col(new MibLeaf(colIfType, READONLY, new SnmpInt32()));	add_col(new MibLeaf(colIfMtu, READONLY, new SnmpInt32()));	add_col(new MibLeaf(colIfSpeed, READONLY, new Gauge32()));	add_col(new MibLeaf(colIfPhysAddress, READONLY, new OctetStr()));	add_col(new ifAdminStatus(colIfAdminStatus));	add_col(new MibLeaf(colIfOperStatus, READONLY, new SnmpInt32()));	add_col(new MibLeaf(colIfLastChange, READONLY, new TimeTicks()));	add_col(new MibLeaf(colIfInOctets, READONLY, new Counter32()));	add_col(new MibLeaf(colIfInUcastPkts, READONLY, new Counter32()));	add_col(new MibLeaf(colIfInNUcastPkts, READONLY, new Counter32()));	add_col(new MibLeaf(colIfInDiscards, READONLY, new Counter32()));	add_col(new MibLeaf(colIfInErrors, READONLY, new Counter32()));	add_col(new MibLeaf(colIfInUnknownProtos, READONLY, new Counter32()));	add_col(new MibLeaf(colIfOutOctets, READONLY, new Counter32()));	add_col(new MibLeaf(colIfOutUcastPkts, READONLY, new Counter32()));	add_col(new MibLeaf(colIfOutNUcastPkts, READONLY, new Counter32()));	add_col(new MibLeaf(colIfOutDiscards, READONLY, new Counter32()));	add_col(new MibLeaf(colIfOutErrors, READONLY, new Counter32()));	add_col(new MibLeaf(colIfOutQLen, READONLY, new Gauge32()));	add_col(new MibLeaf(colIfSpecific, READONLY, new Oid()));	//--AgentGen BEGIN=ifEntry::ifEntry#ifdef linux	lastUpdated = 0;	init_if_speeds();#endif	//--AgentGen END}ifEntry::~ifEntry(){	//--AgentGen BEGIN=ifEntry::~ifEntry	//--AgentGen END}void ifEntry::update(Request* req){	// This table needs to be updated.	//--AgentGen BEGIN=ifEntry::update#ifdef linux	if (sysUpTime::get_currentTime() - lastUpdated > 5) {		do_update();		lastUpdated = sysUpTime::get_currentTime();	}#endif	//--AgentGen END}//--AgentGen BEGIN=ifEntry#ifdef linuxconst char *scan_line_2_2="%lu %lu %lu %*lu %*lu %*lu %*lu %*lu %lu %lu %lu %*lu %*lu %lu";const char *scan_line_2_0="%lu %lu %*lu %*lu %*lu %lu %lu %*lu %*lu %lu";void ifEntry::do_update() {    // The following is derived from NET-SNMP 4.2    FILE *in;    char line [256], *cp1, *cp2, *ifname;    SnmpSyntax* if_entry[23];    struct ifreq ifrq;    int  use_2_2_scan_line;    int  fd;    if (! (in = fopen ("/proc/net/dev", "r"))) {	LOG_BEGIN(ERROR_LOG | 1);	LOG("IF-MIB: cannot open /proc/net/dev ...");	LOG_END;	return;    }    // clear queue for new rows    newRows.clearAll();	/*	 *  There are two formats for interface statistics output,	 *  corresponding to the 2.0 and 2.2 kernels.	 *  They can be distinguished by the format of the header	 *   (i.e. the first two lines) - the 2.2 format having	 *   a number of additional values reported.	 *	 *  It might be possible to analyse the fields to determine	 *    precisely which fields are needed, but frankly, it's	 *    not really worth it.  This format changes so infrequently	 *    that it's reasonable to hardwire the appropriate scan line	 *    for particular kernels.	 */    fgets(line, sizeof(line), in);		/* skip the first line */    fgets(line, sizeof(line), in);		/* this has the field names */    if (strstr(line, "compressed")) {	use_2_2_scan_line = TRUE;	LOG_BEGIN(INFO_LOG | 2);	LOG("IF-MIB: using linux 2.2 kernel /proc/net/dev");	LOG_END;    }     else {	use_2_2_scan_line = FALSE;	LOG_BEGIN(INFO_LOG | 2);	LOG("IF-MIB: using linux 2.0 kernel /proc/net/dev");	LOG_END;    } 	/*	 * We need a network socket to perform ioctls on,	 *   so let's open it now.	 */    if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) < 0) {	LOG_BEGIN(ERROR_LOG | 1);	LOG("IF-MIB: socket open failure");	LOG_END;	fclose(in);    } 		/*		 *  Read in the various interface statistics lines,		 *    and create 'IfList' entries for each one,		 *    linking them into a list.		 */    for (int i=0; i<23; i++) {	if_entry[i] = 0;    }    int n = 0;    while (fgets (line, sizeof(line), in)) {		/*		 * Extract the interface name		 *   (skipping leading blanks)		 */	cp1 = line;	while (isspace( *cp1 ))	    cp1++;	cp2 = strrchr( cp1, ':' );	*cp2 = '\0';	if_entry[atoi(colIfDescr)] = new OctetStr(cp1);	ifname = strdup(cp1);			/*		 * Extract the appropriate statistics		 * Note that the 2.0 kernel doesn't supply octet counts		 */	cp2++;	u_int if_ibytes, if_ipackets, if_ierrors,	      if_obytes, if_opackets, if_oerrors, if_collisions;	if ( use_2_2_scan_line ) {	    sscanf( cp2, scan_line_2_2,		    &if_ibytes, &if_ipackets, &if_ierrors,		    &if_obytes, &if_opackets, &if_oerrors,		    &if_collisions);	}	else {	    sscanf( cp2, scan_line_2_0,		    &if_ipackets, &if_ierrors,		    &if_opackets, &if_oerrors,		    &if_collisions);	    if_ibytes = if_obytes = 0;	}	if_entry[atoi(colIfInOctets)] = new Counter32(if_ibytes);	if_entry[atoi(colIfOutOctets)] = new Counter32(if_obytes);	if_entry[atoi(colIfInUcastPkts)] = new Counter32(if_ipackets);	if_entry[atoi(colIfOutUcastPkts)] = new Counter32(if_opackets);	if_entry[atoi(colIfInErrors)] = new Counter32(if_ierrors);	if_entry[atoi(colIfOutErrors)] = new Counter32(if_oerrors);		/*		 * Split the name into type and unit number		 */	cp2 = &(cp1[ strlen( cp1 ) -1 ]);	for ( cp2 = cp1 ; cp2 ; cp2++ )	    if ( isdigit( *cp2 ))		break;	if ( cp2 )	while ( isdigit( *cp2 )) {	   cp2--;	   if (cp2 == cp1 )		break;	}				/* XXX - do we actually need this ? */	if ( cp2 ) {	  //if_entry[colIfIndex] = new SnmpInt32(atoi(cp2));	    *cp2 = '\0';	}		/*		 *  Fill in the rest of the ifnet & ifaddr structures		 *   using suitable ioctl calls		 */		strcpy (ifrq.ifr_name, ifname);	short if_flags = ioctl (fd, SIOCGIFFLAGS, &ifrq) < 0 				? 0 : ifrq.ifr_flags;	if_entry[atoi(colIfAdminStatus)] = 	  new SnmpInt32((if_flags & IFF_RUNNING) ? 1 : 2);	if_entry[atoi(colIfOperStatus)] =	  new SnmpInt32((if_flags & IFF_UP) ? 1 : 2);		strcpy (ifrq.ifr_name, ifname);	if (ioctl(fd, SIOCGIFHWADDR, &ifrq) == 0) {	  if_entry[atoi(colIfPhysAddress)] = 	    new OctetStr((unsigned char*)ifrq.ifr_hwaddr.sa_data, 6);#ifdef ARPHRD_LOOPBACK	  switch (ifrq.ifr_hwaddr.sa_family) {	  case ARPHRD_TUNNEL:	  case ARPHRD_TUNNEL6:	  case ARPHRD_IPGRE:	  case ARPHRD_SIT:	      if_entry[atoi(colIfType)] = 		new SnmpInt32(131); break; /* tunnel */	  case ARPHRD_SLIP:	  case ARPHRD_CSLIP:	  case ARPHRD_SLIP6:	  case ARPHRD_CSLIP6:	      if_entry[atoi(colIfType)] = new SnmpInt32(28); break; /* slip */	  case ARPHRD_PPP:	      if_entry[atoi(colIfType)] = new SnmpInt32(23); break; /* ppp */	  case ARPHRD_LOOPBACK:	      if_entry[atoi(colIfType)] = new SnmpInt32(24); break; /* softwareLoopback */          /* XXX: more if_arp.h:ARPHDR_xxx to IANAifType mappings... */	  }#endif	}	/*	    	strcpy (ifrq.ifr_name, ifname);	if_entry[atoi(colIfMetric)] = 	  new SnmpInt32((ioctl (fd, SIOCGIFMETRIC, &ifrq) < 0			 ? 0 : ifrq.ifr_metric);	*/  #ifdef SIOCGIFMTU	strcpy (ifrq.ifr_name, ifname);	if_entry[atoi(colIfMtu)] = 	  new SnmpInt32((ioctl (fd, SIOCGIFMTU, &ifrq) < 0) 	                ? 0 : ifrq.ifr_mtu);#else	if_entry[atoi(colIfMtu)] = new SnmpInt32(0);#endif		int if_type = 0;	if_type  = if_type_from_name(ifname);	if_entry[atoi(colIfSpeed)] = 	  new Gauge32(if_speed_from_type(if_type));	if_entry[atoi(colIfType)] = new SnmpInt32(if_type);		/*		 * Add this to the table		 */	MibTableRow* r = new MibTableRow(generator);	for (int i=1; i<23; i++) {		if (if_entry[i]) {			r->get_nth(i-1)->replace_value(if_entry[i]);			if_entry[i] = 0;		}	}		if (n<size()) { // update		MibTableRow* upd = content.getNth(n);		r->set_index(upd->get_index());		*upd = *r;		delete r;	}	else {		// request index at master -> row added above will then		// registered when index_allocated is called back		newRows.add(r);		any_index();	}	n++;	delete ifname;    }	/* while fgets() */    // remove obsolete interfaces    if (n<content.size()) {	n = content.size()-n;	for (int i=0; (i<n); i++) {	  MibTableRow* r = content.last();	  if (r)	    delete content.remove(r);	}    }    fclose( in );}void ifEntry::index_allocated(const Oidx& ind, int, int) {	MibTableRow* n = newRows.removeFirst();	if (!n) {		// index reallocated -> reregister it		add_row(ind);		return;	}	MibTableRow* r = add_row(ind);	n->set_index(ind);	n->get_nth(0)->replace_value(new SnmpInt32(ind[0]));	*r = *n;	delete n;}/** * Overwrite init_row if you use index_allocation and * persistent storage to avoid allocating the same * index again after a restart of the agent. */MibTableRow* ifEntry::init_row(const Oidx& ind, Vbx* vbs) {	MibTableRow* row = new MibTableRow(generator);	newRows.add(row);	any_index();	for (int i=0; i<row->size(); i++) {	  		row->get_nth(i)->set_value(vbs[i]);	}	return row;}typedef struct _match_if {	int mi_type;	const char *mi_name;} match_if;static match_if lmatch_if[] = {	{ 24, "lo" },		/* loopback */	{  6, "eth" },		/* ethernet */	{  6, "le" },		/* Lance ethernet */	{  6, "qe" },		/* Quad ethernet */	{  6, "hme" },	{  9, "tr" },		/* Token Ring */	{ 23, "ppp" },		/* Point-to-Point */	{ 28, "sl" },	{ 37, "lane" },		/* LAN Emulation (ATM) */	{ 37, "fa" },		/* Fore ATM */	{ 37, "qa" },		/* Fore? ATM */	{ 62, "qfe" },		/* Quad Fast ethernet */	{  0, 0 }		/* end of list */};int ifEntry::if_type_from_name(const char *name){    int len;    match_if *pm;    for ( pm = lmatch_if ; pm->mi_name!= NULL ; pm++ ) {	len = strlen( pm->mi_name );	if ( !strncmp( name, pm->mi_name, len ))	    return (pm->mi_type);    }    return( 1 );	/* 'other' */}void ifEntry::init_if_speeds(){    int i;    for ( i = 0 ; i < MAX_IF_TYPES ; i++ )	if_speeds[i] = 0;#define MBIT 1000000    if_speeds[  6 ] =  10 * MBIT;		/* traditional ethernet */    if_speeds[  9 ] =   4 * MBIT;		/* Token Ring */    if_speeds[ 37 ] = 155 * MBIT;		/* ATM */    if_speeds[ 62 ] = 100 * MBIT;		/* fast ethernet */			/* XXXX - Fill in the rest */}int ifEntry::if_speed_from_type( int type ){    if (( type < MAX_IF_TYPES ) && ( type >= 0 ))	return if_speeds[ type ];    else	return 0;}int ifEntry::if_speeds[ MAX_IF_TYPES ];#endif//--AgentGen END/** *  ifXEntry * */ifXEntry* ifXEntry::instance = 0;const index_info indIfXEntry[1] = {	{ sNMP_SYNTAX_INT, FALSE, 1, 1 } };const Oidx indOidsIfXEntry[1] = {	"1.3.6.1.2.1.2.2.1.1"};ifXEntry::ifXEntry(const OctetStr& context, SubAgentXMib* mib):   AgentXSharedTable(oidIfXEntry, indIfXEntry, 1, indOidsIfXEntry, mib, context){	// This table object is a singleton. In order to access it use	// the static pointer ifXEntry::instance.	instance = this;	add_col(new SnmpDisplayString(colIfName, READONLY, new OctetStr()));	add_col(new MibLeaf(colIfInMulticastPkts, READONLY, new Counter32()));	add_col(new MibLeaf(colIfInBroadcastPkts, READONLY, new Counter32()));	add_col(new MibLeaf(colIfOutMulticastPkts, READONLY, new Counter32()));	add_col(new MibLeaf(colIfOutBroadcastPkts, READONLY, new Counter32()));	add_col(new MibLeaf(colIfHCInOctets, READONLY, new Counter64()));	add_col(new MibLeaf(colIfHCInUcastPkts, READONLY, new Counter64()));	add_col(new MibLeaf(colIfHCInMulticastPkts, READONLY, new Counter64()));	add_col(new MibLeaf(colIfHCInBroadcastPkts, READONLY, new Counter64()));	add_col(new MibLeaf(colIfHCOutOctets, READONLY, new Counter64()));	add_col(new MibLeaf(colIfHCOutUcastPkts, READONLY, new Counter64()));	add_col(new MibLeaf(colIfHCOutMulticastPkts, READONLY, new Counter64()));	add_col(new MibLeaf(colIfHCOutBroadcastPkts, READONLY, new Counter64()));	add_col(new ifLinkUpDownTrapEnable(colIfLinkUpDownTrapEnable));	add_col(new MibLeaf(colIfHighSpeed, READONLY, new Gauge32()));	add_col(new ifPromiscuousMode(colIfPromiscuousMode));	add_col(new MibLeaf(colIfConnectorPresent, READONLY, new SnmpInt32()));	add_col(new ifAlias(colIfAlias));	add_col(new MibLeaf(colIfCounterDiscontinuityTime, READONLY, new TimeTicks()));	//--AgentGen BEGIN=ifXEntry::ifXEntry	//--AgentGen END}ifXEntry::~ifXEntry(){	//--AgentGen BEGIN=ifXEntry::~ifXEntry	//--AgentGen END}void ifXEntry::update(Request* req){	// This table needs to be updated.	//--AgentGen BEGIN=ifXEntry::update	//--AgentGen END}//--AgentGen BEGIN=ifXEntry//--AgentGen END/** *  ifStackEntry * */ifStackEntry* ifStackEntry::instance = 0;const index_info indIfStackEntry[2] = {	{ sNMP_SYNTAX_INT, FALSE, 1, 1 },	 { sNMP_SYNTAX_INT, FALSE, 1, 1 } };const Oidx indOidsIfStackEntry[2] = {	"1.3.6.1.2.1.31.1.2.1.1",	"1.3.6.1.2.1.31.1.2.1.2"};ifStackEntry::ifStackEntry(const OctetStr& context, SubAgentXMib* mib):   AgentXSharedTable(oidIfStackEntry, indIfStackEntry, 2, indOidsIfStackEntry, mib, context){	// This table object is a singleton. In order to access it use	// the static pointer ifStackEntry::instance.	instance = this;	add_col(new ifStackStatus(colIfStackStatus));	//--AgentGen BEGIN=ifStackEntry::ifStackEntry	//--AgentGen END}ifStackEntry::~ifStackEntry(){	//--AgentGen BEGIN=ifStackEntry::~ifStackEntry	//--AgentGen END}void ifStackEntry::update(Request* req){	// This table needs to be updated.	//--AgentGen BEGIN=ifStackEntry::update	//--AgentGen END}//--AgentGen BEGIN=ifStackEntry//--AgentGen END/** *  ifTestEntry * */ifTestEntry* ifTestEntry::instance = 0;const index_info indIfTestEntry[1] = {	{ sNMP_SYNTAX_INT, FALSE, 1, 1 } };const Oidx indOidsIfTestEntry[1] = {	"1.3.6.1.2.1.2.2.1.1"};ifTestEntry::ifTestEntry(const OctetStr& context, SubAgentXMib* mib):   AgentXSharedTable(oidIfTestEntry, indIfTestEntry, 1, indOidsIfTestEntry, mib, context){	// This table object is a singleton. In order to access it use	// the static pointer ifTestEntry::instance.	instance = this;	add_col(new ifTestId(colIfTestId));	add_col(new ifTestStatus(colIfTestStatus));	add_col(new ifTestType(colIfTestType));	add_col(new MibLeaf(colIfTestResult, READONLY, new SnmpInt32()));	add_col(new MibLeaf(colIfTestCode, READONLY, new Oid()));	add_col(new ifTestOwner(colIfTestOwner));	//--AgentGen BEGIN=ifTestEntry::ifTestEntry	//--AgentGen END}ifTestEntry::~ifTestEntry(){	//--AgentGen BEGIN=ifTestEntry::~ifTestEntry	//--AgentGen END}void ifTestEntry::update(Request* req){	// This table needs to be updated.	//--AgentGen BEGIN=ifTestEntry::update	//--AgentGen END}//--AgentGen BEGIN=ifTestEntry//--AgentGen END/** *  ifRcvAddressEntry * */ifRcvAddressEntry* ifRcvAddressEntry::instance = 0;const index_info indIfRcvAddressEntry[2] = {	{ sNMP_SYNTAX_INT, FALSE, 1, 1 },	 { sNMP_SYNTAX_OCTETS, FALSE, 0, 128 } };const Oidx indOidsIfRcvAddressEntry[2] = {	"1.3.6.1.2.1.2.2.1.1",	"1.3.6.1.2.1.31.1.4.1.1"};ifRcvAddressEntry::ifRcvAddressEntry(const OctetStr& context, SubAgentXMib* mib):   AgentXSharedTable(oidIfRcvAddressEntry, indIfRcvAddressEntry, 2, indOidsIfRcvAddressEntry, mib, context){	// This table object is a singleton. In order to access it use	// the static pointer ifRcvAddressEntry::instance.	instance = this;	add_col(new ifRcvAddressStatus(colIfRcvAddressStatus));	add_col(new ifRcvAddressType(colIfRcvAddressType));	//--AgentGen BEGIN=ifRcvAddressEntry::ifRcvAddressEntry	//--AgentGen END}ifRcvAddressEntry::~ifRcvAddressEntry(){	//--AgentGen BEGIN=ifRcvAddressEntry::~ifRcvAddressEntry	//--AgentGen END}void ifRcvAddressEntry::update(Request* req){	// This table needs to be updated.	//--AgentGen BEGIN=ifRcvAddressEntry::update	//--AgentGen END}//--AgentGen BEGIN=ifRcvAddressEntry//--AgentGen ENDif_mib::if_mib(const OctetStr& context, SubAgentXMib* mib):   MibGroup("1.3.6.1.2.1.2", "ifMIB"){	add(new ifNumber());	add(new ifEntry(context, mib));	//	add(new ifXEntry(context, mib));	//	add(new ifStackEntry(context, mib));	//	add(new ifTestEntry(context, mib));	//	add(new ifRcvAddressEntry(context, mib));	//	add(new ifTableLastChange());	//	add(new ifStackLastChange());}//--AgentGen BEGIN=if_mib//--AgentGen END//--AgentGen BEGIN=_END#ifdef AGENTPP_NAMESPACE}#endif#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -