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

📄 snmpfunc.cpp

📁 linux下网络管理协议snmp的实现
💻 CPP
字号:
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/library/system.h> 
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

#include "Mibdefine.h"
#include "snmpfunc.h"
#include "snmpSendTrap.h"



using namespace std;

struct snmp_session *
InitSNMPv2(struct snmp_session *session, char *IPaddr )
{
	struct snmp_session *ss;
	
	snmp_sess_init( session );
	
	session->version = SNMP_VERSION_2c;
//	session->peername = IPaddr;  //now is not changed 
		session->peername = IPaddr;  //now is not changed 
	session->community = (u_char *)"public";
	session->community_len = strlen( (char *)session->community );

	SOCK_STARTUP;
	
	ss = snmp_open( session );
	if( ss == NULL )
	{
		snmp_sess_perror( "snmpget", session );
		SOCK_CLEANUP;
		return NULL;
	}	
	return ss;
}

int 
CheckRetStatus(int *retval, struct snmp_session *session, struct snmp_pdu *response)
{
	struct variable_list *vars;
	int status, count;
	int exitval;
	
	status = *retval;	
	if (status == STAT_SUCCESS){
		
    		if (response->errstat == SNMP_ERR_NOERROR){

				exitval=0;

    		}

    		else {
       			fprintf(stderr, "Error in packet\nReason: %s\n",
            	snmp_errstring(response->errstat));
      			if (response->errindex != 0){
        				fprintf(stderr, "Failed object: ");

        				for(count = 1, vars = response->variables;

              		vars && count != response->errindex;

              		vars = vars->next_variable, count++)

            /*EMPTY*/ ;

        			if (vars)
          				fprint_objid(stderr, vars->name, vars->name_length);
         				 fprintf(stderr, "\n");
        		}

						exitval = -1;

     		}  /* endif -- SNMP_ERR_NOERROR */

 	 }

     else if (status == STAT_TIMEOUT){

		fprintf(stderr,"Timeout: No Response from %s.\n", session->peername);

		exitval=-2;

    	} 
	 else {    /* status == STAT_ERROR */

//      		snmp_sess_perror("snmpget", ss);

      		exitval=-1;

     }  /* endif -- STAT_SUCCESS */
	
	
	 return exitval;
}

int
TrialSNMPGET(struct snmp_session *ss, oid *name, int len )
{
	struct snmp_pdu *pdu, *res;
	int status;
	
	pdu = snmp_pdu_create(SNMP_MSG_GET);
	snmp_add_null_var( pdu, name, len);
	status = snmp_synch_response(ss, pdu, &res);
	if(res)  snmp_free_pdu(res);
	
	return status;
}

void 
CloseSNMPv2(struct snmp_pdu *response, struct snmp_session *ss)
{
	if (response) snmp_free_pdu(response);

  	snmp_close(ss);

  	SOCK_CLEANUP;
}


int 
snmp_get_ASIStatus(u_long bladeNo, 
				   u_long subboardNo,
				   u_long asiNo,
				   int *stream)
{
	struct snmp_session session, *ss;	
	struct snmp_pdu *pdu, *response;	
	struct variable_list *vars;
	int  status ;

	char remoteIP[20] = {0};	
	int exitval=0;
	
	oid name[DVBVIDEOPORTINFOENTRY_LEN + 4] 
			= {DVBVIDEOPORTINFOENTRY, 0, bladeNo, subboardNo, asiNo };
	BladeToIPAddr(bladeNo, remoteIP );
	if ( (ss = InitSNMPv2( &session,  remoteIP)) == NULL )
	{
//		perror("Init SNMP Error.\n");
		return exitval = -1;
	}

	pdu = snmp_pdu_create(SNMP_MSG_GET);
	
	name[ DVBVIDEOPORTINFOENTRY_LEN ] = DVBVIDEOPORTSTATUS ;
	snmp_add_null_var( pdu, name, DVBSIENTRY_LEN + 4);
	
//	name[ DVBVIDEOPORTINFOENTRY_LEN ] = DVBVIDEOPORTSTATUS ;
	snmp_add_null_var( pdu, name, DVBSIENTRY_LEN + 4);
	
	status = snmp_synch_response(ss, pdu, &response);
	if( (exitval = CheckRetStatus(&status, &session, response)) != 0 )
	{
//		perror("Check SNMP Response Error.\n");
		CloseSNMPv2(response, ss);
		return 	exitval;
	}
	
	vars = response->variables;				
	if( vars->val.integer == NULL )
	{
			CloseSNMPv2(response, ss);
			return -1;
	}
								
	vars = response->variables;
	if( *(vars->val.integer) == 1 )
			*stream = 1;
	else if( *(vars->val.integer) == 2 )
			*stream = 0;
						
	CloseSNMPv2(response, ss);	

  	return exitval;	
}

int 
snmp_get_BladeStatus(u_long bladeNo, int *blade_status)
{
	struct snmp_session session, *ss;	
	struct snmp_pdu *pdu, *response;	
	struct variable_list *vars;
	int  status ;

	char remoteIP[20] = {0};	
	int exitval=0;
	
	oid name[DVBMIBOBJECTS_LEN + 2 ] 
			= {DVBMIBOBJECTS, 0, 0 };
	BladeToIPAddr(bladeNo, remoteIP );
	if ( (ss = InitSNMPv2( &session,  remoteIP)) == NULL )
	{
//		perror("Init SNMP Error.\n");
		return exitval = -1;
	}

	pdu = snmp_pdu_create(SNMP_MSG_GET);
	
	name[ DVBMIBOBJECTS_LEN ] = DVBMASTERBLADESTATUS ;
	snmp_add_null_var( pdu, name, DVBMIBOBJECTS_LEN + 2);
	
	status = snmp_synch_response(ss, pdu, &response);
	if( (exitval = CheckRetStatus(&status, &session, response)) != 0 )
	{
//		perror("Check SNMP Response Error.\n");
		CloseSNMPv2(response, ss);
		return 	exitval;
	}
	
	vars = response->variables;				
	if( vars->val.integer == NULL )
	{
			CloseSNMPv2(response, ss);
			return -1;
	}
	
	*blade_status = *(vars->val.integer);
						
	CloseSNMPv2(response, ss);	

  	return exitval;	
}

int 
snmp_get_MasterMCUStatus(u_long bladeNo, long *MCU_status)
{
	struct snmp_session session, *ss;	
	struct snmp_pdu *pdu, *response;	
	struct variable_list *vars;
	int  status ;

	char remoteIP[20] = {0};	
	int exitval=0;
	
	oid name[DVBMIBOBJECTS_LEN + 2 ] 
			= {DVBMIBOBJECTS, 0, 0 };

	BladeToIPAddr(bladeNo, remoteIP);																		
		
	if ( (ss = InitSNMPv2( &session,  remoteIP )) == NULL )
	{
//		perror("Init SNMP Error.\n");
		return exitval = -1;
	}

	pdu = snmp_pdu_create(SNMP_MSG_GET);
	
	name[ DVBMIBOBJECTS_LEN ] = DVBMASTERMCUSTATUS;
	snmp_add_null_var( pdu, name, DVBMIBOBJECTS_LEN + 2);
	
	status = snmp_synch_response(ss, pdu, &response);
	if( (exitval = CheckRetStatus(&status, &session, response)) != 0 )
	{
//		perror("Check SNMP Response Error.\n");
		CloseSNMPv2(response, ss);
		return 	exitval;
	}
	
	vars = response->variables;				
	if( vars->val.integer == NULL )
		{
			CloseSNMPv2(response, ss);
			return -1;
		}
	
	*MCU_status = *(vars->val.integer);
						
	CloseSNMPv2(response, ss);	

  	return exitval;	
	
}

int 
snmp_set_MasterMCUStatus(u_long bladeNo, long  MCU_status)
{
	struct snmp_session session, *ss;	
	struct snmp_pdu *pdu, *response, *pduget, *resget;	
	struct variable_list *vars;
	int  status ;

	char remoteIP[20] = {0};	
	int exitval=0;
	

	char type;
	char *pvalue = NULL;	
	
	oid  name[DVBMIBOBJECTS_LEN + 2] = 
					{DVBMIBOBJECTS, 0, 0};
					
	BladeToIPAddr(bladeNo, remoteIP);
	
//	InitSNMPv2(&session, ss, remoteIP);
	if ( (ss = InitSNMPv2(&session, remoteIP)) == NULL )
	{
//		perror("Init SNMP Error.\n");
		return exitval = -1;
	}
	
	name[DVBMIBOBJECTS_LEN ] = DVBMASTERMCUSTATUS;
	if( (exitval = TrialSNMPGET(ss, name, DVBMIBOBJECTS_LEN + 2)) != 0 )
	{
//		perror("Trial SNMP GET Error.\n");
		CloseSNMPv2(response, ss);
		return exitval;	
	}
	
	pdu = snmp_pdu_create(SNMP_MSG_SET);

	type = 'i';
	name[DVBMIBOBJECTS_LEN] = DVBMASTERMCUSTATUS;
	if( MCU_status == 1 )
	{
		pvalue = "1";
		if(snmp_add_var(pdu, name, DVBMIBOBJECTS_LEN + 2, type, pvalue)) {
  	   		snmp_perror((char*)name);
		  	snmp_close(ss);
			SOCK_CLEANUP;

  			return exitval=-1;
		}
	}
	else if( MCU_status == 2 )
	{
		pvalue = "2";
		if(snmp_add_var(pdu, name, DVBMIBOBJECTS_LEN + 2, type, pvalue)) {
  	   		snmp_perror((char*)name);
		  	snmp_close(ss);
			SOCK_CLEANUP;

  			return exitval=-1;
		}
	}
	else if( MCU_status == 3)
	{
		pvalue = "3";
		if(snmp_add_var(pdu, name, DVBMIBOBJECTS_LEN + 2, type, pvalue)) {
  	   		snmp_perror((char*)name);
		  	snmp_close(ss);
			SOCK_CLEANUP;

  			return exitval=-1;
		}
	}
	else
	{
		/* other status operation */
		
	}
	status = snmp_synch_response(ss, pdu, &response); //send the pdu
	
	if( (exitval = CheckRetStatus(&status, &session, response)) != 0 )
	{
//		perror("Check SNMP Response Error.\n");
		return 	exitval;
	}
  	
  	CloseSNMPv2(response, ss);					

  	return exitval;
	
}

int 
snmp_set_SlaveMCUStatus(u_long bladeNo, long  MCU_status)
{
	struct snmp_session session, *ss;	
	struct snmp_pdu *pdu, *response, *pduget, *resget;	
	struct variable_list *vars;
	int  status ;

	char remoteIP[20] = {0};	
	int exitval=0;
	

	char type;
	char *pvalue = NULL;	
	
	oid  name[DVBMIBOBJECTS_LEN + 2] = 
					{DVBMIBOBJECTS, 0, 0};
					
	BladeToIPAddr(bladeNo, remoteIP);
	
//	InitSNMPv2(&session, ss, remoteIP);
	if ( (ss = InitSNMPv2(&session, remoteIP)) == NULL )
	{
//		perror("Init SNMP Error.\n");
		return exitval = -1;
	}
	
	name[DVBMIBOBJECTS_LEN ] = DVBSLAVEMCUSTATUS;
	if( (exitval = TrialSNMPGET(ss, name, DVBMIBOBJECTS_LEN + 2)) != 0 )
	{
//		perror("Trial SNMP GET Error.\n");
	//	CloseSNMPv2(response, ss);
  //	changed by xubo 07.02.27
		 	snmp_close(ss);
		return exitval;	
	}
	
	pdu = snmp_pdu_create(SNMP_MSG_SET);

	type = 'i';
	name[DVBMIBOBJECTS_LEN] = DVBSLAVEMCUSTATUS;
	if( MCU_status == 1 )
	{
		pvalue = "1";
		if(snmp_add_var(pdu, name, DVBMIBOBJECTS_LEN + 2, type, pvalue)) {
  	   		snmp_perror((char*)name);
		  	snmp_close(ss);
			SOCK_CLEANUP;

  			return exitval=-1;
		}
	}
	else if( MCU_status == 2 )
	{
		pvalue = "2";
		if(snmp_add_var(pdu, name, DVBMIBOBJECTS_LEN + 2, type, pvalue)) {
  	   		snmp_perror((char*)name);
		  	snmp_close(ss);
			SOCK_CLEANUP;

  			return exitval=-1;
		}
	}
	else if( MCU_status == 3 )
	{
		pvalue = "3";
		if(snmp_add_var(pdu, name, DVBMIBOBJECTS_LEN + 2, type, pvalue)) {
  	   		snmp_perror((char*)name);
		  	snmp_close(ss);
			SOCK_CLEANUP;

  			return exitval=-1;
		}
	}
	else
	{
		/* other status operation */
	}
	
	status = snmp_synch_response(ss, pdu, &response); //send the pdu
	
	if( (exitval = CheckRetStatus(&status, &session, response)) != 0 )
	{
//		perror("Check SNMP Response Error.\n");
		return 	exitval;
	}
  	
  	CloseSNMPv2(response, ss);					

  	return exitval;
	
}


int 
snmp_get_SubboardStatus(u_long bladeNo, u_long subboardNo, long *bladestatus)
{
	struct snmp_session session, *ss;	
	struct snmp_pdu *pdu, *response;	
	struct variable_list *vars;
	int  status ;

	char remoteIP[20] = {0};	
	int exitval=0;
	
	oid name[DVBSUBBOARDINFOENTRY_LEN + 3 ] 
			= {DVBSUBBOARDINFOENTRY, 0, bladeNo, subboardNo };

	BladeToIPAddr(bladeNo, remoteIP);																		
		
	if ( (ss = InitSNMPv2( &session,  remoteIP )) == NULL )
	{
//		perror("Init SNMP Error.\n");
		return exitval = -1;
	}

	pdu = snmp_pdu_create(SNMP_MSG_GET);
	
	name[ DVBSUBBOARDINFOENTRY_LEN ] = DVBSUBBOARDSTATUS  ;
	snmp_add_null_var( pdu, name, DVBMIBOBJECTS_LEN + 3);
	
	status = snmp_synch_response(ss, pdu, &response);
	if( (exitval = CheckRetStatus(&status, &session, response)) != 0 )
	{
//		perror("Check SNMP Response Error.\n");
		CloseSNMPv2(response, ss);
		return 	exitval;
	}
	
	vars = response->variables;				
	if( vars->val.integer == NULL )
	{
			CloseSNMPv2(response, ss);
			return -1;
	}
	
	*bladestatus = *(vars->val.integer);
						
	CloseSNMPv2(response, ss);	

  	return exitval;	
	
}

int 
snmp_check(u_long bladeNo )
{
	struct snmp_session session, *ss;	
	struct snmp_pdu *pdu, *response;	
	struct variable_list *vars;
	int  status ;

	char remoteIP[20] = {0};	
	int exitval=0;
	
	oid name[DVBMIBOBJECTS_LEN + 2 ] 
			= {DVBMIBOBJECTS, 0, 0 };

	BladeToIPAddr(bladeNo, remoteIP);																		
		
	if ( (ss = InitSNMPv2( &session,  remoteIP )) == NULL )
	{
//		perror("Init SNMP Error.\n");
		return exitval = -1;
	}

	pdu = snmp_pdu_create(SNMP_MSG_GET);
	
	name[ DVBMIBOBJECTS_LEN ] = DVBMASTERBALDENUMBER;
	snmp_add_null_var( pdu, name, DVBMIBOBJECTS_LEN + 2);
	
	status = snmp_synch_response(ss, pdu, &response);
	if( (exitval = CheckRetStatus(&status, &session, response)) != 0 )
	{
//		perror("Check SNMP Response Error.\n");
		CloseSNMPv2(response, ss);
		return 	exitval;
	}
	
	vars = response->variables;				
	if( vars->val.integer == NULL )
		{
			CloseSNMPv2(response, ss);
			return -1;
		}
							
	CloseSNMPv2(response, ss);	

  	return exitval;	
	
}

⌨️ 快捷键说明

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