Exemple #1
0
int init_snmp_agent(void)
{
	int result;

	if (user_settings.is_master_agent) {
		netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE, 1);
	} else {
		netsnmp_enable_subagent();
	}

	if (user_settings.agentx_address != NULL) {
		netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_X_SOCKET, user_settings.agentx_address);
	}

	if (user_settings.daemonize)
		snmp_enable_syslog();
	else
		snmp_enable_stderrlog();	

	SOCK_STARTUP;

	if ((result = init_agent("collectd-agent")) != 0) {
		return -1;
	}

	if ((result = register_value_handlers()) != 0) {
		return -1;
	}

	if ((result = register_table_handlers()) != 0) {
		return -1;
	}

	if (user_settings.is_master_agent) {
		init_vacm_vars();
		init_usmUser();
	}

	init_snmp("collectd-snmpd");

	if (user_settings.is_master_agent) {
		init_master_agent();
	}

	return 0;
}
Exemple #2
0
int main(int argc, char *argv[])
{
	int		agentx_subagent;	/* change this if you want to be a SNMP master agent */
	int		background;			/* change this if you want to run in the background */
	int		syslog;				/* change this if you want to use syslog */
	int		dRet, dOldThreadCount;
	time_t	tOldThreadCheck, tThreadCheck;

	agentx_subagent	= 1;
	background		= 0;
	syslog			= 1;

	// Initialize Log
	dRet = log_init(S_SSHM_LOG_LEVEL, getpid(), SEQ_PROC_SNMPIF, LOG_PATH"/SNMPIF", "SNMPIF");
	if(dRet < 0)
	{
		log_print(LOGN_WARN, LH"MAIN : Failed in Initialize LOGLIB Info [%d]", LT,  dRet);
		return -1;
	}

#if 0	
	if( (dRet = Init_shm_common()) < 0)
	{
		log_print(LOGN_CRI, LH"ERROR IN Init_shm_common() [errno:%d-%s]", LT, -dRet, strerror(-dRet));
		return -2;
	}
#endif

	// Set version
	if((dRet = set_version(S_SSHM_VERSION, SEQ_PROC_SNMPIF, szVersion)) < 0)
	{
        log_print(LOGN_WARN, LH"ERROR IN set_version() dRet[%d]", LT, dRet);
		return -3;
    }

    if( (dRet = dGetBlocks(FILE_MC_INIT_M_PRI, SWLST_TAM_APP)) < 0)
    {
        log_print(LOGN_CRI, LH"ERROR IN dGetBlocks(%s) dRet[%d]", LT, FILE_MC_INIT_M_PRI, dRet);
		return -4;
    }

    if( (dRet = dGetBlocks(FILE_MC_INIT_F_PRI, SWLST_TAF_RP)) < 0)
    {
        log_print(LOGN_CRI, LH"ERROR IN dGetBlocks(%s) dRet[%d]", LT, FILE_MC_INIT_F_PRI, dRet);
		return -5;
    }

    if( (dRet = dGetBlocks(FILE_MC_INIT_F_SEC, SWLST_TAF_PI)) < 0)
    {
        log_print(LOGN_CRI, LH"ERROR IN dGetBlocks(%s) dRet[%d]", LT, FILE_MC_INIT_F_SEC, dRet);
		return -6;
    }

	if( (dRet = dReadSubSysListFromFile(&stSubSysList)) < 0)
	{
		log_print(LOGN_CRI, LH"ERROR IN dReadSubSysListFromFile() dRet[%d]", LT, dRet);
		return -7;
	}

	putenv("MIBS=ALL");

	/* print log errors to syslog or stderr */
	if(syslog)
		snmp_enable_calllog();
	else
		snmp_enable_stderrlog();

	/* we're an agentx subagent? */
	if(agentx_subagent)
	{
		/* make us a agentx client. */
		netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE, 1);
	}

	/* run in background, if requested */
	if(background && netsnmp_daemonize(1, !syslog))
		exit(-1);

	/* initialize tcpip, if necessary */
	SOCK_STARTUP;

	/* initialize the agent library */
	init_agent(TARGET);

	/* initialize mib code here */

	/* mib code: init_hardware from hardware.C */
	init_chnTable();
	init_dskTable();
	init_edTable();
	init_ethTable();
	init_fanTable();
	init_memTable();
	init_ntpTable();
	init_prTable();
	init_pwrTable();
	init_queTable();
	init_cpuTable();
	init_nifoTable();


	/* initialize vacm/usm access control  */
	if(!agentx_subagent)
	{
		init_vacm_vars();
		init_usmUser();
	}

	/* Initialize fidb */
	if((dRet = dInitFidb()) < 0)
	{
		log_print(LOGN_CRI, LH"ERROR IN dInitFidb() dRet=%d", LT, dRet);
		exit(-4);
	}

	/* Initialize sfdb */
	if( (dRet = init_sfdb()) < 0)
	{
		log_print(LOGN_CRI, LH"ERROR IN init_sfdb() dRet[%d]", LT, dRet);
		exit(-5);
	}

	if( (dRet = pthread_create(&t_agntx, NULL, p_agntx, NULL)) != 0)
	{
		log_print(LOGN_CRI, LH"FAILED IN pthread_create(t_agntx[%lu]) dRet[%d] errno[%d-%s]", LT,
			t_agntx, dRet, errno, strerror(errno));
		exit(-6);
	}
	else if( (dRet = pthread_detach(t_agntx)) != 0)
	{
		log_print(LOGN_CRI, LH"FAILED IN pthread_detach(t_agntx[%lu]) dRet[%d] errno[%d-%s]", LT,
			t_agntx, dRet, errno, strerror(errno));
		exit(-7);
	}

	tOldThreadCheck = time(NULL);
	dOldThreadCount	= dThreadStop;

	/* In case we recevie a request to stop (kill -TERM or kill -INT) */
	gJiSTOPFlag = 1;
	SetUpSignal();
	send_start_trap();

	log_print(LOGN_CRI, "SNMPIF[%s] START", szVersion);
	while(gJiSTOPFlag)
	{
		if( ((tThreadCheck = time(NULL)) - tOldThreadCheck) > THREAD_CHECK_INTERVAL)
		{
			tOldThreadCheck = tThreadCheck;
			if(dThreadStop == dOldThreadCount)
			{
				log_print(LOGN_CRI, LH"STOPED thread(p_agntx) dThreadStop[%d] dOldThreadCount[%d]", LT,
					dThreadStop, dOldThreadCount);
				if( (dRet = pthread_create(&t_agntx, NULL, p_agntx, NULL)) != 0)
				{
					log_print(LOGN_CRI, LH"FAILED IN pthread_create(t_agntx[%lu]) dRet[%d] errno[%d-%s]", LT,
						t_agntx, dRet, errno, strerror(errno));
					exit(-7);
				}
				else
				{
					log_print(LOGN_CRI, LH"SUCCESS IN pthread_create(t_agntx[%lu])", LT, t_agntx);
					dThreadStop		= 0;
				}
			}
			log_print(LOGN_DEBUG, LH"dThreadStop[%d] dOldThreadCount[%d]", LT, dThreadStop, dOldThreadCount);
			dOldThreadCount = dThreadStop;
		}

		/* if you use select(), see snmp_select_info() in snmp_api(3) */
		/*     --- OR ---  */
		alarm(1);
		agent_check_and_process(1); /* 0 == don't block */
		alarm(0);
	}

	/* at shutdown time */
	send_stop_trap();
	snmp_shutdown("SNMPIF");
	SOCK_CLEANUP;
	log_print(LOGN_CRI, "SNMPIF[%s] END", szVersion);

	return 0;
}
Exemple #3
0
int
main (int argc, char **argv) {
    int agentx_subagent=1; /* change this if you want to be a SNMP master agent */
    int background = 0; /* change this if you want to run in the background */
    int syslog = 0; /* change this if you want to use syslog */

    /* print log errors to syslog or stderr */
    if (syslog)
        snmp_enable_calllog();
    else
        snmp_enable_stderrlog();

    /* we're an agentx subagent? */
    if (agentx_subagent) {
        /* make us a agentx client. */
        netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE, 1);
    }

    /* run in background, if requested */
    if (background && netsnmp_daemonize(1, !syslog))
        exit(1);

    /* initialize tcpip, if necessary */
    SOCK_STARTUP;

    /* initialize the agent library */
    init_agent("example-demon");

    /* initialize mib code here */

    /* mib code: init_example from example.c */
    init_example();

    /* initialize vacm/usm access control  */
    if (!agentx_subagent) {
        init_vacm_vars();
        init_usmUser();
    }

    /* example-demon will be used to read example-demon.conf files. */
    init_snmp("example-demon");

    /* If we're going to be a snmp master agent, initial the ports */
    if (!agentx_subagent)
        init_master_agent();  /* open the port to listen on (defaults to udp:161) */

    /* In case we recevie a request to stop (kill -TERM or kill -INT) */
    keep_running = 1;
    signal(SIGTERM, stop_server);
    signal(SIGINT, stop_server);

    snmp_log(LOG_INFO,"example-demon is up and running.\n");

    /* your main loop here... */
    while(keep_running) {
        /* if you use select(), see snmp_select_info() in snmp_api(3) */
        /*     --- OR ---  */
        agent_check_and_process(1); /* 0 == don't block */
    }

    /* at shutdown time */
    snmp_shutdown("example-demon");
    SOCK_CLEANUP;

    return 0;
}