Пример #1
0
void main()
{
	int retries=0;
	if(netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID,NETSNMP_DS_AGENT_ROLE,1))
	{

            printf("netsnmp_ds_set_boolean failed");
            return;
	}

	//initialize the agent
	while(0!=init_agent("mysnmpagent"))
	{
	     printf("init_agent failed\n");
	     sleep(1);
	}
	netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID,NETSNMP_DS_AGENT_AGENTX_PING_INTERVAL,5);

	//mib initialization code
	init_mysnmpagent();
	init_snmp("mysnmpagent");
	keep_running=1;
	printf("My agent starting...\n");
	while(keep_running)
	{
	  if(-1==agent_check_and_process(1))
	     printf("agent_check_and process failed\n");
	}
	stop_server();
	return;
}
Пример #2
0
void *agent_thread(void *arg)
{
	ast_verb(2, "Starting %sAgent\n", res_snmp_agentx_subagent ? "Sub" : "");

	snmp_enable_stderrlog();

	if (res_snmp_agentx_subagent)
		netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID,
							   NETSNMP_DS_AGENT_ROLE,
							   1);

	init_agent("asterisk");

	init_asterisk_mib();

	init_snmp("asterisk");

	if (!res_snmp_agentx_subagent)
		init_master_agent();

	while (res_snmp_dont_stop)
		agent_check_and_process(1);

	snmp_shutdown("asterisk");

	ast_verb(2, "Terminating %sAgent\n", res_snmp_agentx_subagent ? "Sub" : "");

	return NULL;
}
Пример #3
0
void run_snmp_agent(void)
{
	agent_running = 1;

	while (agent_running) {
		agent_check_and_process(1);
	}
}
Пример #4
0
void* snmp_thread(void* data)
{
  while (1)
  {
    agent_check_and_process(1);
  }
  return NULL;
};
Пример #5
0
static void *pool(void *v)
{
  while(running)
    {
      agent_check_and_process(1);       /* 0 == don't block */
    }
  return NULL;
}
Пример #6
0
int ifs_mainloop_nonblocking()
{
  /* your main loop here... */
  /* while(keep_running) { */
    /* if you use select(), see snmp_select_info() in snmp_api(3) */
    /*     --- OR ---  */
    agent_check_and_process(0); /* 0 == don't block ; 1 == block */
  /* } */
}
Пример #7
0
static void *snmp_thread(void *a)
{
	sigset_t set;

	sigfillset(&set);
	sigdelset(&set, SIGKILL);
	sigdelset(&set, SIGSTOP);
	sigdelset(&set, 32);
	pthread_sigmask(SIG_BLOCK, &set, NULL);

	if (unshare(CLONE_FILES) < 0) {
		log_error("net-snmp: impossible to start SNMP thread:"
			  " unshare(CLONE_FILES) failed (%s)\n",
			  strerror(errno));

		return NULL;
	}

	snmp_register_callback(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_LOGGING, agent_log, NULL);
  snmp_disable_log();
	snmp_enable_calllog();
	//snmp_set_do_debugging(1);
	//netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE, 1);

	if (!conf_master)
		netsnmp_enable_subagent();

	init_agent(conf_agent_name);

	init_statCore();
	init_statPPP();
	init_statPPTP();
	init_statL2TP();
	init_statPPPOE();
	init_statIPOE();
	init_statSSTP();
	init_terminate();
	init_shutdown();
	init_sessionTable();
	init_cli();

	init_snmp(conf_agent_name);

	if (conf_master)
		init_master_agent();

	while (!snmp_term) {
    agent_check_and_process(1);
	}

	snmp_shutdown(conf_agent_name);

  SOCK_CLEANUP;

	return NULL;
}
Пример #8
0
/*! This function:
 *
 *   1) Registers itself with the Master Agent
 *   2) Initializes all of the SNMPStats modules scalars and tables, while
 *      simultaneously registering their respective SNMP OID's and handlers
 *      with the master agent.
 *   3) Repeatedly checks for new SNMP messages to process
 *
 * \note This function never returns, so it should always be called from a
 *       sub-process.
 */
static int initialize_agentx(void)
{
    /* We register with a master agent */
    register_with_master_agent(AGENT_PROCESS_NAME);

    LM_DBG("Initializing Kamailio OID's for SNMPD MasterX\n");

    /* Initialize all scalars, and let the master agent know we want to
     * handle all OID's pertaining to these scalars. */
    init_kamailioSIPCommonObjects();
    init_kamailioSIPServerObjects();
    init_kamailioObjects();

    /* Initialiaze all the tables, and let the master agent know we want to
     * handle all the OID's pertaining to these tables */
    init_kamailioSIPPortTable();
    init_kamailioSIPMethodSupportedTable();
    init_kamailioSIPStatusCodesTable();
    init_kamailioSIPRegUserTable();
    init_kamailioSIPContactTable();
    init_kamailioSIPRegUserLookupTable();
    init_kamailioServer();
    init_kamailioNet();
    init_kamailioNetConfig();
    LM_DBG("Done initializing Kamailio OID's for SNMPD MasterX\n");

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

    while(keep_running) {
        /* update the local config framework structures */
        cfg_update();

        agent_check_and_process(1); /* 0 == don't block */
    }

    LM_DBG("Shutting down Kamailio SNMPD MasterX sub agent.\n");
    snmp_shutdown(AGENT_PROCESS_NAME);
    SOCK_CLEANUP;
    exit (0);

    return 0;
}
Пример #9
0
/* FIXME Returning EINVAL is not right */
int main(int argc,  char *argv[])
{
    const char *master_agentx_host = NULL;
    uint32_t master_agentx_port = 0;

    /* agentx host should always be provided */
    if (argc < 4)
        return EINVAL;

    if (sscanf(argv[1], "%u", &node_id) != 1)
        return EINVAL;
    node_name = argv[2];
    master_agentx_host = argv[3];

    /* The port parameter is optional: 0 is a valid value for agentx port
       (means default port) */
    if (argc >= 5)
	master_agentx_port = atoi(argv[4]);

    if (master_agentx_port == 0)
	master_agentx_port = MD_DEFAULT_MASTER_AGENTX_PORT;

    init_snmp_subagent(master_agentx_host,
		       master_agentx_port);
    init_md_handler();
    init_md_heartbeat();

    while (!stop)
    {
        os_sleep(1);
        agent_check_and_process(0); /* 0 == don't block */
    }

    snmp_log(LOG_INFO, "exit exa_agentx.\n");
    snmp_shutdown("exa_agentx");
    SOCK_CLEANUP;

    return EXA_SUCCESS;
}
Пример #10
0
static int check_and_process(void)
{
        int             numfds;
        fd_set          fdset;
        struct timeval  timeout = { LONG_MAX, 0 };
	struct timeval *tvp = &timeout;
        int             count;
        int             fakeblock = 0;

        numfds = 0;
        FD_ZERO(&fdset);
        snmp_select_info(&numfds, &fdset, tvp, &fakeblock);

        if (fakeblock != 0)
                tvp = NULL;

	/* select may block, so make this a cancelation point */
	pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
        count = select(numfds, &fdset, 0, 0, tvp);
	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);

	return agent_check_and_process(0);
}
Пример #11
0
int
main(int argc, char **argv)
{
	int agentx_subagent = 1;
	//Change to make an SNMP master agent

	/* print log errors to stderr */
		snmp_enable_stderrlog();

	/* we're an agentx subagent? */
	if (agentx_subagent) {
		/*
		 * This is an agentx client. Specify 0 for
		 * NETSNMP_DS_AGENT_ROLE if the agent is master agent.
		 * Specify 1 for NETSNMP_DS_AGENT_ROLE, if the agent is a
		 * client.
		 */
		netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID,
				NETSNMP_DS_AGENT_ROLE, 1);
		DEBUGMSGTL(("demo_module_8",
				"NETSNMP_DS_APPLICATION_ID = %s:\n",
				NETSNMP_DS_APPLICATION_ID));
		DEBUGMSGTL(("demo_module_8", "NETSNMP_DS_AGENT_ROLE = %s:\n",
				NETSNMP_DS_AGENT_ROLE));
	}
	/*
	 * Initializes the embedded agent.  Call this function before the
	 * init_snmp() call. The string name specifies which .conf file to
	 * read when init_snmp() is called later.
	 */
	init_agent("demo_module_8");
	DEBUGMSGTL(("demo_module_8", "CALLING init_agent\n"));

	/* initialize mib code here */

	/* mib code: init_me1LoadGroup from me1LoadGroup.c */
	init_me1LoadGroup();

	/*
	 * Initializes the SNMP library, which causes the agent to read your
	 * application's configuration files. The agent first tries to read
	 * the configuration files named by the string passed as an argument.
	 * You might use this to configure  access  control, for example.
	 */
	init_snmp("demo_module_8");

	/*
	 * Initializes the master agent and causes it to listen for SNMP
	 * requests on its default UDP port of 161. Open the port to listen
	 * (defaults to udp: 161)
	 */
	if (!agentx_subagent)
		init_master_agent();

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

	/*
	 * The main loop. If you use select(), see snmp_select_info() in
	 * snmp_api(3). This checks for packets arriving on the SNMP port and
	 * processes them if some are found. If block is non zero, the
	 * function call blocks until a packet arrives or an alarm must be
	 * run (see snmp_alarm(3)).
	 *
	 * The return value from this function is a positive integer if packets
	 * were processed, zero if an alarm occurre, and -1 if an error
	 * occured.
	 */

	while (keep_running) {
		/* OR */
		agent_check_and_process(1);	/* 0 == don't block */
	}
	/*
	 * Shuts down the agent, saving any needed persistent storage.
	 */
	snmp_shutdown("demo_module_8");
}
Пример #12
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;
}
Пример #13
0
int main(int argc, char** argv) {
    int agentx_subagent =
        1; /* change this if you want to be a SNMP master agent */
    /* Defs for arg-handling code: handles setting of policy-related variables
     */
    int ch;
    extern char* optarg;
    int dont_fork = 0, use_syslog = 0;
    char const* agentx_socket = NULL;
    char const* app_name = "moninor-agent";
    char const* directory = "/home/";

    while ((ch = getopt(argc, argv, "a:p:hdD:fHLMx:")) != EOF) {
        switch (ch) {
        case 'a':
            app_name = optarg;
            break;
        case 'p':
            directory = optarg;
            break;
        case 'h':
            print_usage();
            exit(0);
        case 'd':
            print_tokens();
            exit(0);
            break;
        case 'D':
            debug_register_tokens(optarg);
            snmp_set_do_debugging(1);
            break;
        case 'f':
            dont_fork = 1;
            break;
        case 'M':
            agentx_subagent = 0;
            break;
        case 'L':
            use_syslog = 0; /* use stderr */
            break;
        case 'x':
            agentx_socket = optarg;
            break;
        default:
            fprintf(stderr, "unknown option %c\n", ch);
            print_usage();
        }
    }

    if (optind < argc) {
        int i;
        /*
            * There are optional transport addresses on the command line.
            */
        DEBUGMSGTL(("snmpd/main", "optind %d, argc %d\n", optind, argc));
        for (i = optind; i < argc; i++) {
            char* c, *astring;
            if ((c = netsnmp_ds_get_string(NETSNMP_DS_APPLICATION_ID,
                                           NETSNMP_DS_AGENT_PORTS))) {
                astring = malloc(strlen(c) + 2 + strlen(argv[i]));
                if (astring == NULL) {
                    fprintf(stderr, "malloc failure processing argv[%d]\n", i);
                    exit(1);
                }
                sprintf(astring, "%s,%s", c, argv[i]);
                netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID,
                                      NETSNMP_DS_AGENT_PORTS, astring);
                SNMP_FREE(astring);
            } else {
                netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID,
                                      NETSNMP_DS_AGENT_PORTS, argv[i]);
            }
        }
        DEBUGMSGTL(("snmpd/main", "port spec: %s\n",
                    netsnmp_ds_get_string(NETSNMP_DS_APPLICATION_ID,
                                          NETSNMP_DS_AGENT_PORTS)));
    }

    /* check directory existence */
    DIR* dir = opendir(directory);
    if (dir) {
        closedir(dir);
    } else if (ENOENT == errno) {
        puts("Directory doesn't not exist.");
        exit(-1);
    } else {
        puts("Cannot open direcory.");
        exit(-1);
    }

    /* we're an agentx subagent? */
    if (agentx_subagent) {
        /* make us a agentx client. */
        netsnmp_enable_subagent();
        if (NULL != agentx_socket) {
            netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID,
                                  NETSNMP_DS_AGENT_X_SOCKET, agentx_socket);
        }
    }

    snmp_disable_log();
    if (use_syslog)
        snmp_enable_calllog();
    else
        snmp_enable_stderrlog();

    /* daemonize */
    if (!dont_fork) {
        int rc = netsnmp_daemonize(1, !use_syslog);
        if (rc) exit(-1);
    }

    /* initialize tcp/ip if necessary */
    SOCK_STARTUP;

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

    /* init mib code */
    global_settings_t* settings = get_mutable_global_settings();
    strcpy(settings->path, directory);

    void* data[4] = {NULL, NULL, NULL, NULL};
    init_Directory(data + 0);
    init_DirectoryStateNotification(data + 1);
    init_NumDirectories(data + 2);
    init_NumFiles(data + 3);
    init_DirectoryContentTable();

    /* read app_name.conf files. */
    init_snmp(app_name);

    /* 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) */
    g_keep_running = 1;
    signal(SIGTERM, stop_server);
    signal(SIGINT, stop_server);

    /* you're main loop here... */
    while (g_keep_running) {
        agent_check_and_process(1); /* 0 == don't block */
    }

    shutdown_DirectoryContentTable();
    shutdown_NumFiles(data[3]);
    shutdown_NumDirectories(data[2]);
    shutdown_DirectoryStateNotification(data[1]);
    shutdown_Directory(data[0]);

    /* at shutdown time */
    snmp_shutdown(app_name);
    SOCK_CLEANUP;
    exit(0);
}
Пример #14
0
int
main (int argc, char **argv) {
  int agentx_subagent=1; /* change this if you want to be a SNMP master agent */
  /* Defs for arg-handling code: handles setting of policy-related variables */
  int          ch;
  extern char *optarg;
  int dont_fork = 0, use_stderr = 0;
  char *agentx_socket = NULL;

  while ((ch = getopt(argc, argv, "D:fHLMx:")) != EOF)
    switch(ch) {
    case 'D':
      debug_register_tokens(optarg);
      snmp_set_do_debugging(1);
      break;
    case 'f':
      dont_fork = 1;
      break;
    case 'H':
      netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID,
	                     NETSNMP_DS_AGENT_NO_ROOT_ACCESS, 1);
      init_agent("libvirtGuestTable");        /* register our .conf handlers */
      init_libvirtGuestTable();
      init_snmp("libvirtGuestTable");
      fprintf(stderr, "Configuration directives understood:\n");
      read_config_print_usage("  ");
      snmp_shutdown("libvirtGuestTable");
      shutdown_libvirtGuestTable();
      exit(0);
    case 'M':
      agentx_subagent = 0;
      break;
    case 'L':
      use_stderr = 1;
      break;
    case 'x':
      agentx_socket = optarg;
      break;
    default:
      fprintf(stderr,"unknown option %c\n", ch);
      usage();
  }

  if (optind < argc) {
      int i;
      /*
       * There are optional transport addresses on the command line.
       */
      DEBUGMSGTL(("snmpd/main", "optind %d, argc %d\n", optind, argc));
      for (i = optind; i < argc; i++) {
          char *c, *astring;
          if ((c = netsnmp_ds_get_string(NETSNMP_DS_APPLICATION_ID,
                                         NETSNMP_DS_AGENT_PORTS))) {
              astring = malloc(strlen(c) + 2 + strlen(argv[i]));
              if (astring == NULL) {
                  fprintf(stderr, "malloc failure processing argv[%d]\n", i);
                  exit(1);
              }
              sprintf(astring, "%s,%s", c, argv[i]);
              netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID,
                                    NETSNMP_DS_AGENT_PORTS, astring);
              SNMP_FREE(astring);
          } else {
              netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID,
                                    NETSNMP_DS_AGENT_PORTS, argv[i]);
          }
      }
      DEBUGMSGTL(("snmpd/main", "port spec: %s\n",
                  netsnmp_ds_get_string(NETSNMP_DS_APPLICATION_ID,
                                        NETSNMP_DS_AGENT_PORTS)));
  }

  /* we're an agentx subagent? */
  if (agentx_subagent) {
    /* make us a agentx client. */
    netsnmp_enable_subagent();
    if (NULL != agentx_socket)
        netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID,
                              NETSNMP_DS_AGENT_X_SOCKET, agentx_socket);
  }

  snmp_disable_log();
  if (use_stderr)
      snmp_enable_stderrlog();
  else
      snmp_enable_calllog();

  /* daemonize */
  if(!dont_fork) {
    int rc = netsnmp_daemonize(1, use_stderr);
    if(rc)
       exit(-1);
  }

  /* initialize tcp/ip if necessary */
  SOCK_STARTUP;

  /* initialize the agent library */
  init_agent("libvirtGuestTable");

  /* init libvirtGuestTable mib code */
  init_libvirtGuestTable();

  /* read libvirtGuestTable.conf files. */
  init_snmp("libvirtGuestTable");

  /* 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);

  /* you're 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("libvirtGuestTable");
  shutdown_libvirtGuestTable();
  SOCK_CLEANUP;
  exit(0);
}
Пример #15
0
int
main (int argc, char **argv)
{
  int agentx_subagent = AGENT_TRUE;
  char c;
  int rc = 0;

  pid_t child;
  /* change this if you want to be a SNMP master agent */


  while ((c = getopt (argc, argv, "fds?")) != EOF)
    switch (c)
      {
      case 'f':
	do_fork = AGENT_TRUE;
	break;
      case 'd':
	debug_register_tokens (AGENT);
	snmp_enable_stderrlog ();
	snmp_set_do_debugging (1);
	break;
	break;
      case 's':
	do_syslog = AGENT_FALSE;
	break;
      default:
	printf ("Usage %s [-dfs]\n", argv[0]);
	printf ("where -d enables debug mode\n");
	printf ("where -f enables forking\n");
	printf ("where -s disables logging via syslog facility.\n");
	exit (1);
      }
  init_snmp_logging ();

  if (do_syslog == AGENT_TRUE)
    {
      snmp_enable_calllog ();
      snmp_enable_syslog_ident (AGENT, LOG_DAEMON);
    }
  snmp_log (LOG_INFO, "Starting %s\n", version);
  /* we're an agentx subagent? */
  if (agentx_subagent)
    {
      /* make us a agentx client. */
      rc = netsnmp_ds_set_boolean (NETSNMP_DS_APPLICATION_ID,
				   NETSNMP_DS_AGENT_ROLE, 1);
    }

  /* initialize the agent library */
  rc = init_agent (AGENT);
  if (rc != 0)
    {
      snmp_log (LOG_ERR, "Could not initialize connection to SNMP daemon. \n"
		"Perhaps you are running %s as non-root?\n", argv[0]);
      exit (rc);
    }

  /* Read configuration information here, before we initialize */

  snmpd_register_config_handler (TRAPS_TOKEN,
				 hpiSubagent_parse_config_traps,
				 NULL,
				 "hpiSubagent on/off switch for sending events upon startup");

  snmpd_register_config_handler (INTERVAL_TOKEN,
				 hpiSubagent_parse_config_interval,
				 NULL,
				 "hpiSubagent time in seconds before HPI API is queried for information.");

  snmpd_register_config_handler (MAX_EVENT_TOKEN,
				 hpiSubagent_parse_config_max_event,
				 NULL,
				 "hpiSubagent MAX number of rows for Events.");

  init_snmp (AGENT);
  /* Initialize tables */
  initialize_table_saHpiTable ();
  initialize_table_saHpiRdrTable ();
  initialize_table_saHpiSensorTable ();
  initialize_table_saHpiCtrlTable ();
  initialize_table_saHpiInventoryTable ();
  initialize_table_saHpiWatchdogTable ();
  initialize_table_saHpiHotSwapTable ();

  initialize_table_saHpiSystemEventLogTable ();
  initialize_table_saHpiEventTable ();

  initialize_table_saHpiSensorReadingCurrentTable ();
  initialize_table_saHpiSensorReadingMaxTable ();
  initialize_table_saHpiSensorReadingMinTable ();
  initialize_table_saHpiSensorReadingNominalTable ();
  initialize_table_saHpiSensorReadingNormalMaxTable ();
  initialize_table_saHpiSensorReadingNormalMinTable ();

  initialize_table_saHpiSensorThdLowCriticalTable ();
  initialize_table_saHpiSensorThdLowMajorTable ();
  initialize_table_saHpiSensorThdLowMinorTable ();

  initialize_table_saHpiSensorThdUpCriticalTable ();
  initialize_table_saHpiSensorThdUpMajorTable ();
  initialize_table_saHpiSensorThdUpMinorTable ();

  initialize_table_saHpiSensorThdNegHysteresisTable ();
  initialize_table_saHpiSensorThdPosHysteresisTable ();

  if (send_traps_on_startup == AGENT_TRUE)
    send_traps = AGENT_TRUE;

  if (populate_rpt () != AGENT_ERR_NOERROR)
    {
      snmp_log (LOG_ERR, "Could not retrieve RPT entries. Exiting\n.");
      rc = -1;
      goto stop;
    }
  populate_event ();
  if (init_alarm () != AGENT_ERR_NOERROR)
    {
      snmp_log (LOG_ERR, "Could not start our internal loop . Exiting\n.");
      rc = -1;
      goto stop;
    }

  send_traps = AGENT_TRUE;
  /* 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) */

  if (do_fork == AGENT_TRUE)
    {
      if ((child = fork ()) < 0)
	{
	  snmp_log (LOG_ERR, "Could not fork!\n");
	  exit (-1);
	}
      if (child != 0)
	exit (0);
    }
  /* In case we recevie a request to stop (kill -TERM or kill -INT) */
  keep_running = 1;
  signal (SIGTERM, stop_server);
  signal (SIGINT, stop_server);

  /* you're main loop here... */
  while (keep_running)
    {
      /* if you use select(), see snmp_select_info() in snmp_api(3) */
      /*     --- OR ---  */
      rc = agent_check_and_process (1);	/* 0 == don't block */
    }
stop:
  /* at shutdown time */
  snmp_log (LOG_INFO, "Stopping %s\n", version);
  snmp_shutdown (AGENT);

  return rc;
}
Пример #16
0
/* This function will be called periodically from an OpenSIPS timer.  The first
 * time it is called, it will query OPENSER-MIB for configured thresholds.
 */
void run_alarm_check(unsigned int ticks, void * attr) 
{
	static int msg_queue_minor_threshold;
	static int msg_queue_major_threshold;		

	static int dialog_minor_threshold;
	static int dialog_major_threshold;

	static char firstRun = 1;
	
	int bytesInMsgQueue;
	int numActiveDialogs;

	/* We only need to retrieve our thresholds the first time around */
	if (firstRun) 
	{
		register_with_master_agent(ALARM_AGENT_NAME);

		msg_queue_minor_threshold = get_msg_queue_minor_threshold();
		msg_queue_major_threshold = get_msg_queue_major_threshold();

		dialog_minor_threshold = get_dialog_minor_threshold();
		dialog_major_threshold = get_dialog_major_threshold();

		firstRun = 0;
	}
	
	/* We need to have this here in case the master agent fails and is
	 * restarted.  Without it, we won't be able to re-establish or AgentX
	 * connection */
	agent_check_and_process(0); 

	/* Check for MsgQueue alarm conditions */

	/* The retrieved number of bytes will be zero unless
	 * there is an alarm condition.  In this case the number
	 * of bytes will be returned. */
	bytesInMsgQueue = check_msg_queue_alarm(msg_queue_minor_threshold);

	if (bytesInMsgQueue != 0) 
	{
		send_openserMsgQueueDepthMinorEvent_trap(bytesInMsgQueue, 
						msg_queue_minor_threshold);
	}

	bytesInMsgQueue = check_msg_queue_alarm(msg_queue_major_threshold);


	if (bytesInMsgQueue != 0) 
	{
		send_openserMsgQueueDepthMajorEvent_trap(bytesInMsgQueue, 
						msg_queue_major_threshold);
	}

	/* Check for Dialog alarm conditions: */

	numActiveDialogs = 	check_dialog_alarm(dialog_minor_threshold);

	if (numActiveDialogs != 0)
	{
		send_openserDialogLimitMinorEvent_trap(numActiveDialogs,
						dialog_minor_threshold);
	}
	
	numActiveDialogs = check_dialog_alarm(dialog_major_threshold);

	if (numActiveDialogs != 0)
	{
		send_openserDialogLimitMajorEvent_trap(numActiveDialogs,
						dialog_major_threshold);
	}
}
Пример #17
0
int
main (int argc, char **argv)
{
	
	  	int agentx_subagent = AGENT_TRUE;
	  	int c;
	  	int rc = 0;
	  
	  	SaErrorT 	rv = SA_OK;
		SaHpiVersionT	hpiVer;
		SaHpiSessionIdT sessionid;
                SaHpiDomainInfoT        domain_info;  		
		SaHpiBoolT      run_threaded = TRUE;
		
	  	pid_t child;
		
		char * env;
	  		  	
	  	/* change this if you want to be a SNMP master agent */
	  	
	  	debug_register_tokens (AGENT);
                snmp_enable_stderrlog ();
                snmp_set_do_debugging (1);

	  	while ((c = getopt (argc, argv, "fdsCx:h?")) != EOF) {
	    switch (c) {
	   	case 'f':
			do_fork = AGENT_TRUE;
		   	break;
	
	    case 'd':
			debug_register_tokens (AGENT);
			snmp_enable_stderrlog ();
			snmp_set_do_debugging (1);
	      	break;
	
	  	case 's':
			do_syslog = AGENT_FALSE;
	      	break;
	
	   	case 'C':
			netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID,
				       NETSNMP_DS_LIB_DONT_READ_CONFIGS,
				       1);
	     	break;
	
	  	case 'x':
			netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID,
				      NETSNMP_DS_AGENT_X_SOCKET,
				      optarg);
	      	break;
	
	 	case 'h':
	    default:
			usage(argv[0]);
			exit(1);
	      	break;
	    }
	  }
	
	  if (do_syslog == AGENT_TRUE) {
	      snmp_enable_calllog ();
	      snmp_enable_syslog_ident (AGENT, LOG_DAEMON);
	  }
	  snmp_log (LOG_INFO, "Starting %s\n", version);
	  /* we're an agentx subagent? */
	  if (agentx_subagent) {
	      /* make us a agentx client. */
	      rc = netsnmp_ds_set_boolean (NETSNMP_DS_APPLICATION_ID,
					   NETSNMP_DS_AGENT_ROLE, 1);
	  }
	
	  /* initialize the agent library */
	  rc = init_agent (AGENT);
	  if (rc != 0) {
	      snmp_log (LOG_ERR, "Could not initialize connection to SNMP daemon. \n"
			"Perhaps you are running %s as non-root?\n", argv[0]);
	      exit (rc);
	  }

	  /* Read configuration information here, before we initialize */
	
	  snmpd_register_config_handler (TRAPS_TOKEN,
					 hpiSubagent_parse_config_traps,
					 NULL,
					 "hpiSubagent on/off switch for sending events upon startup");
	
	  snmpd_register_config_handler (MAX_EVENT_TOKEN,
					 hpiSubagent_parse_config_max_event,
					 NULL,
					 "hpiSubagent MAX number of rows for Events.");
					 
	init_snmp (AGENT); 	
	
	/* 
	 * Initialize HPI library
	 */
	hpiVer = saHpiVersionGet();
	DEBUGMSGTL ((AGENT, "Hpi Version %d Implemented.\n", hpiVer));   	 

	rv = saHpiSessionOpen( SAHPI_UNSPECIFIED_DOMAIN_ID, &sessionid, NULL );
	if (rv != SA_OK) {
		DEBUGMSGTL ((AGENT, "saHpiSessionOpen Error: returns %s\n",
			oh_lookup_error(rv)));
		exit(-1);
	}
   	DEBUGMSGTL ((AGENT, "saHpiSessionOpen returns with SessionId %d\n", 
   		sessionid)); 

        /* Get the DomainInfo structur,  This is how we get theDomainId for this Session */
	rv = saHpiDomainInfoGet(sessionid, &domain_info);
	if (rv != SA_OK) {
                DEBUGMSGTL ((AGENT, "saHpiSessionOpen Error: returns %s\n",
                        oh_lookup_error(rv)));
                exit(-1);
	}

        /* store session numbers */
   	store_session_info(sessionid, domain_info.DomainId);	
   				
	/* subscribe all sessions/events */
        subcsribe_all_sessions();

	/* Resource discovery */
	rv = saHpiDiscover(sessionid);
	
	if (rv != SA_OK) {
		DEBUGMSGTL ((AGENT, "saHpiDiscover Error: returns %s\n",oh_lookup_error(rv)));
		exit(-1);
	}
	DEBUGMSGTL ((AGENT, "saHpiDiscover Success!\n"));	

	/* Initialize subagent tables */		
	init_saHpiDomainInfoTable(); 
	init_saHpiDomainAlarmTable();
	init_saHpiDomainReferenceTable();
	
	init_saHpiResourceTable();
	init_saHpiRdrTable();
	init_saHpiCtrlDigitalTable();
	init_saHpiCtrlDiscreteTable();
	init_saHpiCtrlAnalogTable();
	init_saHpiCtrlStreamTable();
	init_saHpiCtrlTextTable();
	init_saHpiCtrlOemTable();
	init_saHpiSensorTable();
	init_saHpiCurrentSensorStateTable();
	init_saHpiSensorReadingMaxTable();
	init_saHpiSensorReadingMinTable();
	init_saHpiSensorReadingNominalTable();
	init_saHpiSensorReadingNormalMaxTable();
	init_saHpiSensorReadingNormalMinTable();
	init_saHpiSensorThdLowCriticalTable();
	init_saHpiSensorThdLowMajorTable();
	init_saHpiSensorThdLowMinorTable();
	init_saHpiSensorThdUpCriticalTable();
	init_saHpiSensorThdUpMajorTable();
	init_saHpiSensorThdUpMinorTable();
	init_saHpiSensorThdPosHysteresisTable();
	init_saHpiSensorThdNegHysteresisTable();
	init_saHpiInventoryTable();
	init_saHpiWatchdogTable();
	init_saHpiAnnunciatorTable();
	init_saHpiAreaTable();
	init_saHpiFieldTable();

	init_saHpiEventTable();
	init_saHpiResourceEventTable();
	init_saHpiDomainEventTable();
	init_saHpiSensorEventTable();
	init_saHpiOEMEventTable();
	init_saHpiHotSwapEventTable();
	init_saHpiWatchdogEventTable();		
	init_saHpiSoftwareEventTable();
	init_saHpiSensorEnableChangeEventTable();
	init_saHpiUserEventTable();

	init_saHpiEventLogInfoTable();
	init_saHpiEventLogTable();
	init_saHpiResourceEventLogTable();
	init_saHpiSensorEventLogTable();
	
	init_saHpiHotSwapEventLogTable();
	init_saHpiWatchdogEventLogTable();
	init_saHpiSoftwareEventLogTable();
	init_saHpiOEMEventLogTable();
	init_saHpiUserEventLogTable();
	init_saHpiSensorEnableChangeEventLogTable();
	init_saHpiDomainEventLogTable();
	
	init_saHpiHotSwapTable();
        init_saHpiAutoInsertTimeoutTable();
	init_saHpiAutoInsertTimeoutTable();
	init_saHpiAnnouncementTable();
	init_saHpiAnnouncementEventLogTable();


	if (send_traps_on_startup == AGENT_TRUE)
		send_traps = AGENT_TRUE;
	/* after initialization populate tables */
	populate_saHpiDomainInfoTable(sessionid);

	populate_saHpiDomainAlarmTable(sessionid);

	poplulate_saHpiDomainReferenceTable(sessionid);	

	populate_saHpiResourceTable(sessionid);
	    /* populate_saHpiResourceTable() calls:
	     *     populate_saHpiRdrTable(); calls:
	     *         populate_saHpiCtrlDigitalTable();		
	     *	       populate_saHpiCtrlDiscreteTable();		
	     *	       populate_saHpiCtrlAnalogTable();		
	     *	       populate_saHpiCtrlStreamTable();		
	     *	       populate_saHpiCtrlTextTable();		
	     *	       populate_saHpiCtrlOemTable();		
	     *	       populate_saHpiSensorTable();		
	     *	           populate_saHpiSesnorReadingMaxTable();		
	     *	           populate_saHpiSesnorReadingMinTable();		
	     *	           populate_saHpiSesnorReadingNominalTable();		
	     *	           populate_saHpiSesnorReadingNormalMaxTable();		
	     *	           populate_saHpiSesnorReadingNormalMinTable();		
	     *	           populate_saHpiSensorThdLowCriticalTable();		
	     *	           populate_saHpiSensorThdLowMajorTable();		
	     *	           populate_saHpiSensorThdLowMinorTable();		
	     *	           populate_saHpiSensorThdUpCriticalTable();		
	     *	           populate_saHpiSensorThdUpMajorTable();		
	     *	           populate_saHpiSensorThdUpMinorTable();		
	     *	           populate_saHpiSensorThdPosHysteresisTable();		
	     *	           populate_saHpiSensorThdNegHysteresisTable();		
	     *	       populate_saHpiCurrentSensorStateTable();		
	     *	       populate_saHpiInventoyTable();		
	     *	       populate_saHpiWatchdogTable();		
	     *	       populate_saHpiAnnunciatorTable();		
	     *	       populate_saHpiAreaTable();		
	     *	           populate_saHpiFieldTable(); 
             *         populate_saHpiHotSwapTable();
             *             populate_saHpiAutoInsertTimeoutTable();             
  	     *         populate_saHpiAnnouncementTable();
	     */
            
	populate_saHpiEventTable(sessionid);
            /* populate_saHpiResourceEventTable();
	     * populate_saHpiDomainEventTable();
	     * populate_saHpiSensorEventTable();
	     * populate_saHpiOemEventTable();
	     * populate_saHpiHotSwapEventTable();
	     * populate_saHpiWatchdogEventTable();
	     * populate_saHpiSoftwareEventTable();
	     * populate_saHpiSensorEnableChangeEventTable();
	     * populate_saHpiUserEventTable();
	     */
        populate_saHpiEventLogInfo(sessionid);
        populate_saHpiEventLog (sessionid);
            /*
             * populate_saHpiResourceEventLogTable();
             * populate_saHpiSensorEventLogTable();
	     * populate_saHpiHotSwapEventLogTable();
	     * populate_saHpiWatchdogEventLogTable();
	     * populate_saHpiSoftwareEventLogTable();
	     * populate_saHpiOemEventLogTable();
	     * populate_saHpiUserEventLogTable();
	     * populate_saHpiSensorEnableChangeEventLogTable();
	     * populate_saHpiDomainEventLogTable();	     
             */

        /* Determine whether or not we're in threaded mode */
	env = getenv("OPENHPI_THREADED");
	if ((env == (char *)NULL) || (strcmp(env, "NO") == 0)) {
                DEBUGMSGTL ((AGENT, "Running in nonthreaded mode.  Configuring polling mechanism\n"));  
                run_threaded = SAHPI_FALSE;
		if (init_alarm() != AGENT_ERR_NOERROR) {
                    snmp_log (LOG_ERR, "Could not initialize polling mechanism. Exiting\n.");
                    rc = -1;
                    goto stop;
                }
	}	
	else {
                DEBUGMSGTL ((AGENT, "Running in threaded mode.  Spawing thread\n"));
		/* start event thread */
                set_run_threaded(TRUE);
                if (start_event_thread(&sessionid) != AGENT_ERR_NOERROR) {
                        snmp_log (LOG_ERR, "Could not start our internal loop . Exiting\n.");
                        rc = -1;
                        goto stop;
                }
	}	

        send_traps = AGENT_TRUE;
        /* 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) */
                
        if (do_fork == AGENT_TRUE) {
                if ((child = fork ()) < 0) {
                        snmp_log (LOG_ERR, "Could not fork!\n");
                        exit (-1);
                }
                if (child != 0)
                        exit (0);
        }

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

                /* you're main loop here... */
        while (keep_running) {
                /* if you use select(), see snmp_select_info() in snmp_api(3) */
                /*     --- OR ---  */		
                rc = agent_check_and_process (1);
		
        }
stop:
        DEBUGMSGTL ((AGENT,
        "WARNING: closeSaHpiSession: hpiSubagent.c: nolong implemented!"));
        //closeSaHpiSession();
        /* at shutdown time */
        snmp_log (LOG_INFO, "Stopping %s\n", version);
        snmp_shutdown (AGENT);
  
  return rc;
}
Пример #18
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;
}
int main (int argc, char **argv)
{
  std::vector<std::string> trap_ips;
  char* community = NULL;
  int c;

  opterr = 0;
  while ((c = getopt (argc, argv, "i:c:")) != -1)
  {
    switch (c)
      {
      case 'c':
        community = optarg;
        break;
      case 'i':
        Utils::split_string(optarg, ',', trap_ips);
        break;
      default:
        abort ();
      }
  }

  // Log SNMP library output to syslog
  snmp_enable_calllog();

  // Set ourselves up as a subagent
  netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE, 1);
  init_agent("clearwater-alarms");

  // Connect to the informsinks
  for (std::vector<std::string>::iterator ii = trap_ips.begin();
       ii != trap_ips.end();
       ii++)
  {
    create_trap_session(const_cast<char*>(ii->c_str()), 161, community,
                        SNMP_VERSION_2c, SNMP_MSG_INFORM);  
  }

  // Initialise the ZMQ listeners and alarm tables
  // Pull in any local alarm definitions off the node. This is currently a 
  // hard coded path to a single file - this should be a (configurable?) path
  // to a folder. 
  std::string local_alarms_path = "/usr/share/clearwater/infrastructure/local_alarms.json";
  AlarmTableDefs::get_instance().initialize(local_alarms_path);
  AlarmReqListener::get_instance().start();
  init_alarmModelTable();
  init_ituAlarmTable();

  // Run forever
  init_snmp("clearwater-alarms");

  signal(SIGTERM, terminate_handler);
  
  while (!done)
  {
    agent_check_and_process(1);
  }

  snmp_shutdown("clearwater-alarms");

  return 0;
}
Пример #20
0
int
main (int argc, char **argv)
{
	
	  	int agentx_subagent = AGENT_TRUE;
	  	int c;
	  	int rc = 0;
        	  
	  	SaErrorT 	rv = SA_OK;
		SaHpiVersionT	hpiVer;
		SaHpiSessionIdT sessionid;
		
		
		
	  	pid_t child;
		
		/* for simulator */
		struct oh_handler_state *state = NULL;
		struct oh_event *event = (struct oh_event *)g_malloc0(sizeof(struct oh_event));
		struct oh_event *event2= (struct oh_event *)g_malloc0(sizeof(struct oh_event));
		struct oh_event *event3= (struct oh_event *)g_malloc0(sizeof(struct oh_event));		
		struct oh_event *event4= (struct oh_event *)g_malloc0(sizeof(struct oh_event));
		struct oh_event *event5= (struct oh_event *)g_malloc0(sizeof(struct oh_event));	
		struct oh_event *event6= (struct oh_event *)g_malloc0(sizeof(struct oh_event));	
		struct oh_event *event7= (struct oh_event *)g_malloc0(sizeof(struct oh_event));	
		struct oh_event *event8= (struct oh_event *)g_malloc0(sizeof(struct oh_event));			
							
		SaErrorT rc1;
	  	char OemString[] = "MY Oem EVENT";
	  	char SWString[] = "MY SW EVENT";			  	
	  	/* change this if you want to be a SNMP master agent */
	  	
	  	debug_register_tokens (AGENT);
                snmp_enable_stderrlog ();
                snmp_set_do_debugging (1);

	  	while ((c = getopt (argc, argv, "fdsCx:h?")) != EOF) {
	    switch (c) {
	   	case 'f':
			do_fork = AGENT_TRUE;
		   	break;
	
	    case 'd':
			debug_register_tokens (AGENT);
			snmp_enable_stderrlog ();
			snmp_set_do_debugging (1);
	      	break;
	
	  	case 's':
			do_syslog = AGENT_FALSE;
	      	break;
	
	   	case 'C':
			netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID,
				       NETSNMP_DS_LIB_DONT_READ_CONFIGS,
				       1);
	     	break;
	
	  	case 'x':
			netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID,
				      NETSNMP_DS_AGENT_X_SOCKET,
				      optarg);
	      	break;
	
	 	case 'h':
	    default:
			usage(argv[0]);
			exit(1);
	      	break;
	    }
	  }
	
	  if (do_syslog == AGENT_TRUE) {
	      snmp_enable_calllog ();
	      snmp_enable_syslog_ident (AGENT, LOG_DAEMON);
	  }
	  snmp_log (LOG_INFO, "Starting %s\n", version);
	  /* we're an agentx subagent? */
	  if (agentx_subagent) {
	      /* make us a agentx client. */
	      rc = netsnmp_ds_set_boolean (NETSNMP_DS_APPLICATION_ID,
					   NETSNMP_DS_AGENT_ROLE, 1);
	  }
	
	  /* initialize the agent library */
	  rc = init_agent (AGENT);
	  if (rc != 0) {
	      snmp_log (LOG_ERR, "Could not initialize connection to SNMP daemon. \n"
			"Perhaps you are running %s as non-root?\n", argv[0]);
	      exit (rc);
	  }

	  /* Read configuration information here, before we initialize */
	
	  snmpd_register_config_handler (TRAPS_TOKEN,
					 hpiSubagent_parse_config_traps,
					 NULL,
					 "hpiSubagent on/off switch for sending events upon startup");
	
	  snmpd_register_config_handler (MAX_EVENT_TOKEN,
					 hpiSubagent_parse_config_max_event,
					 NULL,
					 "hpiSubagent MAX number of rows for Events.");
					 
	init_snmp (AGENT); 	
	
	/* 
	 * Initialize HPI library
	 */
	hpiVer = saHpiVersionGet();
	DEBUGMSGTL ((AGENT, "Hpi Version %d Implemented.\n", hpiVer));   	 

	rv = saHpiSessionOpen( SAHPI_UNSPECIFIED_DOMAIN_ID, &sessionid, NULL );
	if (rv != SA_OK) {
		DEBUGMSGTL ((AGENT, "saHpiSessionOpen Error: returns %s\n",
			oh_lookup_error(rv)));
		exit(-1);
	}
   	DEBUGMSGTL ((AGENT, "saHpiSessionOpen returns with SessionId %d\n", 
   		sessionid));  

        /* store session numbers */
   	store_session_info(sessionid, SAHPI_UNSPECIFIED_DOMAIN_ID);	
   				
	/* subscribe all sessions/events */
        subcsribe_all_sessions();

	/* Resource discovery */
	rv = saHpiDiscover(sessionid);
	
	if (rv != SA_OK) {
		DEBUGMSGTL ((AGENT, "saHpiDiscover Error: returns %s\n",oh_lookup_error(rv)));
		exit(-1);
	}
	DEBUGMSGTL ((AGENT, "saHpiDiscover Success!\n"));	

	/* Initialize subagent tables */		
	init_saHpiDomainInfoTable(); 
	init_saHpiDomainAlarmTable();
	init_saHpiDomainReferenceTable();
	
	init_saHpiResourceTable();
	init_saHpiRdrTable();
	init_saHpiCtrlDigitalTable();
	init_saHpiCtrlDiscreteTable();
	init_saHpiCtrlAnalogTable();
	init_saHpiCtrlStreamTable();
	init_saHpiCtrlTextTable();
	init_saHpiCtrlOemTable();
	init_saHpiSensorTable();
	init_saHpiCurrentSensorStateTable();
	init_saHpiSensorReadingMaxTable();
	init_saHpiSensorReadingMinTable();
	init_saHpiSensorReadingNominalTable();
	init_saHpiSensorReadingNormalMaxTable();
	init_saHpiSensorReadingNormalMinTable();
	init_saHpiSensorThdLowCriticalTable();
	init_saHpiSensorThdLowMajorTable();
	init_saHpiSensorThdLowMinorTable();
	init_saHpiSensorThdUpCriticalTable();
	init_saHpiSensorThdUpMajorTable();
	init_saHpiSensorThdUpMinorTable();
	init_saHpiSensorThdPosHysteresisTable();
	init_saHpiSensorThdNegHysteresisTable();
	init_saHpiInventoryTable();
	init_saHpiWatchdogTable();
	init_saHpiAnnunciatorTable();
	init_saHpiAreaTable();
	init_saHpiFieldTable();

	init_saHpiEventTable();
	init_saHpiResourceEventTable();
	init_saHpiDomainEventTable();
	init_saHpiSensorEventTable();
	init_saHpiOemEventTable();
	init_saHpiHotSwapEventTable();
	init_saHpiWatchdogEventTable();		
	init_saHpiSoftwareEventTable();
	init_saHpiSensorEnableChangeEventTable();
	init_saHpiUserEventTable();

	init_saHpiEventLogInfoTable();
	init_saHpiEventLogTable();
	init_saHpiResourceEventLogTable();
	init_saHpiSensorEventLogTable();
	
	init_saHpiHotSwapEventLogTable();
	init_saHpiWatchdogEventLogTable();
	init_saHpiSoftwareEventLogTable();
	init_saHpiOemEventLogTable();
	init_saHpiUserEventLogTable();
	init_saHpiSensorEnableChangeEventLogTable();
	init_saHpiDomainEventLogTable();
	
	init_saHpiHotSwapTable();
	init_saHpiAutoInsertTimeoutTable();
	init_saHpiAnnouncementTable();
/*	init_saHpiAnnouncementEventLogTable();	*/


	if (send_traps_on_startup == AGENT_TRUE)
		send_traps = AGENT_TRUE;
	/* after initialization populate tables */
	populate_saHpiDomainInfoTable(sessionid);

	populate_saHpiDomainAlarmTable(sessionid);

	poplulate_saHpiDomainReferenceTable(sessionid);	

	populate_saHpiResourceTable(sessionid);
	    /* populate_saHpiResourceTable() calls:
	     *     populate_saHpiRdrTable(); calls:
	     *         populate_saHpiCtrlDigitalTable();		
	     *	       populate_saHpiCtrlDiscreteTable();		
	     *	       populate_saHpiCtrlAnalogTable();		
	     *	       populate_saHpiCtrlStreamTable();		
	     *	       populate_saHpiCtrlTextTable();		
	     *	       populate_saHpiCtrlOemTable();		
	     *	       populate_saHpiSensorTable();		
	     *	           populate_saHpiSesnorReadingMaxTable();		
	     *	           populate_saHpiSesnorReadingMinTable();		
	     *	           populate_saHpiSesnorReadingNominalTable();		
	     *	           populate_saHpiSesnorReadingNormalMaxTable();		
	     *	           populate_saHpiSesnorReadingNormalMinTable();		
	     *	           populate_saHpiSensorThdLowCriticalTable();		
	     *	           populate_saHpiSensorThdLowMajorTable();		
	     *	           populate_saHpiSensorThdLowMinorTable();		
	     *	           populate_saHpiSensorThdUpCriticalTable();		
	     *	           populate_saHpiSensorThdUpMajorTable();		
	     *	           populate_saHpiSensorThdUpMinorTable();		
	     *	           populate_saHpiSensorThdPosHysteresisTable();		
	     *	           populate_saHpiSensorThdNegHysteresisTable();		
	     *	       populate_saHpiCurrentSensorStateTable();		
	     *	       populate_saHpiInventoyTable();		
	     *	       populate_saHpiWatchdogTable();		
	     *	       populate_saHpiAnnunciatorTable();		
	     *	       populate_saHpiAreaTable();		
	     *	           populate_saHpiFieldTable();
	     *         populate_saHpiHotSwapTable();
	     */

	populate_saHpiEventTable(sessionid);
            /* populate_saHpiResourceEventTable();
	     * populate_saHpiDomainEventTable();
	     * populate_saHpiSensorEventTable();
	     * populate_saHpiOemEventTable();
	     * populate_saHpiHotSwapEventTable();
	     * populate_saHpiWatchdogEventTable();
	     * populate_saHpiSoftwareEventTable();
	     * populate_saHpiSensorEnableChangeEventTable();
	     * populate_saHpiUserEventTable();
	     */
        populate_saHpiEventLogInfo(sessionid);
        populate_saHpiEventLog (sessionid);
            /*
             * populate_saHpiResourceEventLogTable();
             * populate_saHpiSensorEventLogTable();
	     * populate_saHpiHotSwapEventLogTable();
	     * populate_saHpiWatchdogEventLogTable();
	     * populate_saHpiSoftwareEventLogTable();
	     * populate_saHpiOemEventLogTable();
	     * populate_saHpiUserEventLogTable();
	     * populate_saHpiSensorEnableChangeEventLogTable();
	     * populate_saHpiDomainEventLogTable();	     
             */


        /* Generate Event Here */
		
	DEBUGMSGTL ((AGENT,
        "%%%%%%%% Generating simultor event %%%%%%%%\n"));
	
	state = sim_get_handler_by_name("eventGenerator");
	
	if (!state) {
                DEBUGMSGTL ((AGENT, "%%%%%%%% Couldn't find handler name %%%%%%%%\n"));
	}
	else { // build an event
		
	        event->did = 1; /* domain id for the event */
                event->type = OH_ET_HPI;
                event->u.hpi_event.res = sim_rpt_array[0].rpt;
                event->u.hpi_event.rdr.RecordId = 0; // Not sure here
		event->u.hpi_event.event.Source = sim_rpt_array[0].rpt.ResourceId;
		event->u.hpi_event.event.EventType = SAHPI_ET_WATCHDOG;
		event->u.hpi_event.event.Timestamp = 0;
		event->u.hpi_event.event.Severity = SAHPI_CRITICAL;
		event->u.hpi_event.event.EventDataUnion.WatchdogEvent.WatchdogNum = 1;
		event->u.hpi_event.event.EventDataUnion.WatchdogEvent.WatchdogAction = SAHPI_WAE_RESET;
		event->u.hpi_event.event.EventDataUnion.WatchdogEvent.WatchdogPreTimerAction = SAHPI_WPI_SMI;
		event->u.hpi_event.event.EventDataUnion.WatchdogEvent.WatchdogUse = SAHPI_WTU_BIOS_FRB2;
		
		
		rc1 = sim_inject_event(state, event);
                DEBUGMSGTL ((AGENT, "%%%%%%%% Return code from inject 1: %d %%%%%%%%\n", rc1));
		
	        event2->did = 1; /* domain id for the event */
                event2->type = OH_ET_HPI;
                event2->u.hpi_event.res = sim_rpt_array[0].rpt;
                event2->u.hpi_event.rdr.RecordId = 0; // Not sure here
		event2->u.hpi_event.event.Source = sim_rpt_array[0].rpt.ResourceId;
		event2->u.hpi_event.event.EventType = SAHPI_ET_HOTSWAP;
		event2->u.hpi_event.event.Timestamp = 0;
		event2->u.hpi_event.event.Severity = SAHPI_CRITICAL;
		event2->u.hpi_event.event.EventDataUnion.HotSwapEvent.HotSwapState = SAHPI_HS_STATE_INSERTION_PENDING;
		event2->u.hpi_event.event.EventDataUnion.HotSwapEvent.PreviousHotSwapState = SAHPI_HS_STATE_NOT_PRESENT;				

		rc1 = sim_inject_event(state, event2);
                DEBUGMSGTL ((AGENT, "%%%%%%%% Return code from inject 2: %d %%%%%%%%\n", rc1));
		
	        event3->did = 1; /* domain id for the event */
                event3->type = OH_ET_HPI;
                event3->u.hpi_event.res = sim_rpt_array[0].rpt;
                event3->u.hpi_event.rdr.RecordId = 0; // Not sure here
		event3->u.hpi_event.event.Source = sim_rpt_array[0].rpt.ResourceId;
		event3->u.hpi_event.event.EventType = SAHPI_ET_SENSOR;
		event3->u.hpi_event.event.Timestamp = 0;
		event3->u.hpi_event.event.Severity = SAHPI_CRITICAL;
		event3->u.hpi_event.event.EventDataUnion.SensorEvent.SensorNum = SAHPI_DEFAGSENS_PWR;
		event3->u.hpi_event.event.EventDataUnion.SensorEvent.SensorType = SAHPI_VOLTAGE;	   
		event3->u.hpi_event.event.EventDataUnion.SensorEvent.EventCategory = SAHPI_EC_GENERIC;   
		event3->u.hpi_event.event.EventDataUnion.SensorEvent.Assertion = 1;   
		event3->u.hpi_event.event.EventDataUnion.SensorEvent.EventState = SAHPI_ES_STATE_00;    
		event3->u.hpi_event.event.EventDataUnion.SensorEvent.OptionalDataPresent = SAHPI_SOD_TRIGGER_READING;
		event3->u.hpi_event.event.EventDataUnion.SensorEvent.TriggerReading.IsSupported = 1;
		event3->u.hpi_event.event.EventDataUnion.SensorEvent.TriggerReading.Type = SAHPI_SENSOR_READING_TYPE_UINT64;
		event3->u.hpi_event.event.EventDataUnion.SensorEvent.TriggerReading.Value.SensorUint64 = 0xFEEEEEEEEEEEEEED;
		
		rc1 = sim_inject_event(state, event3);
                DEBUGMSGTL ((AGENT, "%%%%%%%% Return code from inject 3: %d %%%%%%%%\n", rc1));	
		
	        event4->did = 1; /* domain id for the event */
                event4->type = OH_ET_HPI;
                event4->u.hpi_event.res = sim_rpt_array[0].rpt;
                event4->u.hpi_event.rdr.RecordId = 0; // Not sure here
		event4->u.hpi_event.event.Source = sim_rpt_array[0].rpt.ResourceId;
		event4->u.hpi_event.event.EventType = SAHPI_ET_OEM;
		event4->u.hpi_event.event.Timestamp = 0;
		event4->u.hpi_event.event.Severity = SAHPI_CRITICAL;
		event4->u.hpi_event.event.EventDataUnion.OemEvent.MId = SAHPI_MANUFACTURER_ID_UNSPECIFIED;		
		event4->u.hpi_event.event.EventDataUnion.OemEvent.OemEventData.DataType = SAHPI_TL_TYPE_BCDPLUS;
		event4->u.hpi_event.event.EventDataUnion.OemEvent.OemEventData.Language = SAHPI_LANG_ENGLISH;
		event4->u.hpi_event.event.EventDataUnion.OemEvent.OemEventData.DataLength = 12;
		memcpy(event4->u.hpi_event.event.EventDataUnion.OemEvent.OemEventData.Data, &OemString[0],
		       event4->u.hpi_event.event.EventDataUnion.OemEvent.OemEventData.DataLength);
		
		rc1 = sim_inject_event(state, event4);
                DEBUGMSGTL ((AGENT, "%%%%%%%% Return code from inject 4: %d %%%%%%%%\n", rc1));	
		
	        event5->did = 1; /* domain id for the event */
                event5->type = OH_ET_HPI;
                event5->u.hpi_event.res = sim_rpt_array[0].rpt;
                event5->u.hpi_event.rdr.RecordId = 0; // Not sure here
		event5->u.hpi_event.event.Source = sim_rpt_array[0].rpt.ResourceId;
		event5->u.hpi_event.event.EventType = SAHPI_ET_HPI_SW;
		event5->u.hpi_event.event.Timestamp = 0;
		event5->u.hpi_event.event.Severity = SAHPI_CRITICAL;
		event5->u.hpi_event.event.EventDataUnion.HpiSwEvent.MId = SAHPI_MANUFACTURER_ID_UNSPECIFIED;
		event5->u.hpi_event.event.EventDataUnion.HpiSwEvent.Type = SAHPI_HPIE_STARTUP;
		event5->u.hpi_event.event.EventDataUnion.HpiSwEvent.EventData.DataType = SAHPI_TL_TYPE_TEXT;
		event5->u.hpi_event.event.EventDataUnion.HpiSwEvent.EventData.Language = SAHPI_LANG_ENGLISH;
		event5->u.hpi_event.event.EventDataUnion.HpiSwEvent.EventData.DataLength = 11;  								     
		memcpy(event5->u.hpi_event.event.EventDataUnion.HpiSwEvent.EventData.Data, &SWString[0],
		       event5->u.hpi_event.event.EventDataUnion.HpiSwEvent.EventData.DataLength);

		rc1 = sim_inject_event(state, event5);
                DEBUGMSGTL ((AGENT, "%%%%%%%% Return code from inject 5: %d %%%%%%%%\n", rc1));	
		
	        event6->did = 1; /* domain id for the event */
                event6->type = OH_ET_HPI;
                event6->u.hpi_event.res = sim_rpt_array[0].rpt;
                event6->u.hpi_event.rdr.RecordId = 0; // Not sure here
		event6->u.hpi_event.event.Source = sim_rpt_array[0].rpt.ResourceId;
		event6->u.hpi_event.event.EventType = SAHPI_ET_SENSOR_ENABLE_CHANGE;
		event6->u.hpi_event.event.Timestamp = 0;
		event6->u.hpi_event.event.Severity = SAHPI_CRITICAL;
		event6->u.hpi_event.event.EventDataUnion.SensorEnableChangeEvent.SensorNum = SAHPI_DEFAGSENS_PWR;
		event6->u.hpi_event.event.EventDataUnion.SensorEnableChangeEvent.SensorType = SAHPI_VOLTAGE;	   
		event6->u.hpi_event.event.EventDataUnion.SensorEnableChangeEvent.EventCategory = SAHPI_EC_GENERIC;   
		event6->u.hpi_event.event.EventDataUnion.SensorEnableChangeEvent.SensorEnable = 1;
                event6->u.hpi_event.event.EventDataUnion.SensorEnableChangeEvent.SensorEventEnable = 1;
		event6->u.hpi_event.event.EventDataUnion.SensorEnableChangeEvent.AssertEventMask = SAHPI_ES_STATE_ASSERTED;
		event6->u.hpi_event.event.EventDataUnion.SensorEnableChangeEvent.DeassertEventMask = SAHPI_ES_STATE_DEASSERTED;
		event6->u.hpi_event.event.EventDataUnion.SensorEnableChangeEvent.OptionalDataPresent = SAHPI_SEOD_CURRENT_STATE;  
		event6->u.hpi_event.event.EventDataUnion.SensorEnableChangeEvent.CurrentState = SAHPI_ES_LIMIT_EXCEEDED; 
		
		rc1 = sim_inject_event(state, event6);
                DEBUGMSGTL ((AGENT, "%%%%%%%% Return code from inject 6: %d %%%%%%%%\n", rc1));	
		
	        event7->did = 1; /* domain id for the event */
                event7->type = OH_ET_HPI;
                event7->u.hpi_event.res = sim_rpt_array[0].rpt;
                event7->u.hpi_event.rdr.RecordId = 0; // Not sure here
		event7->u.hpi_event.event.Source = sim_rpt_array[0].rpt.ResourceId;
		event7->u.hpi_event.event.EventType = SAHPI_ET_DOMAIN;
		event7->u.hpi_event.event.Timestamp = 0;
		event7->u.hpi_event.event.Severity = SAHPI_CRITICAL;
		event7->u.hpi_event.event.EventDataUnion.DomainEvent.Type = SAHPI_DOMAIN_REF_ADDED;
		event7->u.hpi_event.event.EventDataUnion.DomainEvent.DomainId = SAHPI_UNSPECIFIED_DOMAIN_ID;
		
		rc1 = sim_inject_event(state, event7);
                DEBUGMSGTL ((AGENT, "%%%%%%%% Return code from inject 7: %d %%%%%%%%\n", rc1));	
		
	        event8->did = 1; /* domain id for the event */
                event8->type = OH_ET_HPI;
                event8->u.hpi_event.res = sim_rpt_array[0].rpt;
                event8->u.hpi_event.rdr.RecordId = 0; // Not sure here
		event8->u.hpi_event.event.Source = sim_rpt_array[0].rpt.ResourceId;
		event8->u.hpi_event.event.EventType = SAHPI_ET_RESOURCE;
		event8->u.hpi_event.event.Timestamp = 0;
		event8->u.hpi_event.event.Severity = SAHPI_CRITICAL;
		event8->u.hpi_event.event.EventDataUnion.ResourceEvent.ResourceEventType = SAHPI_RESE_RESOURCE_FAILURE;								  				       	        	     		
		
		rc1 = sim_inject_event(state, event8);
                DEBUGMSGTL ((AGENT, "%%%%%%%% Return code from inject 8: %d %%%%%%%%\n", rc1));	

	}


        /* start event thread */
        set_run_threaded(TRUE);
        if (start_event_thread(sessionid) != AGENT_ERR_NOERROR) {
                snmp_log (LOG_ERR, "Could not start our internal loop . Exiting\n.");
                rc = -1;
                goto stop;
        }

        send_traps = AGENT_TRUE;
        /* 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) */
                
        if (do_fork == AGENT_TRUE) {
                if ((child = fork ()) < 0) {
                        snmp_log (LOG_ERR, "Could not fork!\n");
                        exit (-1);
                }
                if (child != 0)
                        exit (0);
        }

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



        /* you're main loop here... */
        while (keep_running) {
                /* if you use select(), see snmp_select_info() in snmp_api(3) */
                /*     --- OR ---  */
                rc = agent_check_and_process (1);	/* 0 == don't block */
        }
stop:
        DEBUGMSGTL ((AGENT,
        "WARNING: closeSaHpiSession: hpiSubagent.c: nolong implemented!"));
        //closeSaHpiSession();
        /* at shutdown time */
        snmp_log (LOG_INFO, "Stopping %s\n", version);
        snmp_shutdown (AGENT);
  
  return rc;
}
Пример #21
0
int main (int argc, const char * argv []) 

{
	int c;
	int r;
	static const char *optv [] = 
	{
		"i:qv",
		"device [device] [...]",
		"Intellon INT6x00 NET-SNMPD SubAgent",

#if defined (WINPCAP) || defined (LIBPCAP)

		"i n\thost interface number is n [2]",

#else

		"i s\thost interface name is s [" CHANNEL_ETHDEVICE "]",

#endif

		"q\tquiet mode",
		"v\tverbose mode",
		(const char *) (0) 
	};
	if (getenv (PLCDEVICE)) 
	{
		channel.name = strdup (getenv (PLCDEVICE));
	}
	optmin = 0;
	while ((c = getoptv (argc, argv, optv)) != -1) 
	{
		switch ((char) (c)) 
		{
		case 'i':

#if defined (WINPCAP) || defined (LIBPCAP)

			channel.id = atoi (optarg);

#else

			channel.name = optarg;

#endif

			break;
		case 'q':
			_setbits (channel.flags, CHANNEL_SILENCE);
			_setbits (int6k.flags, INT6K_SILENCE);
			break;
		case 'v':
			_setbits (channel.flags, CHANNEL_VERBOSE);
			_setbits (int6k.flags, INT6K_VERBOSE);
			break;
		default:
			break;
		}
	}
	argc -= optind;
	argv += optind;
	if (geteuid ()) 
	{
		error (1, EPERM, INT6K_NEEDROOT);
	}
	openchannel (&channel);
	if (!(int6k.message = malloc (sizeof (struct message)))) 
	{
		error (1, errno, INT6K_ERR_MEMORY);
	}

/* use syslog instead of stderr */

	snmp_enable_calllog ();
	netsnmp_ds_set_boolean (NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE, 1);

/* initialize tcpip, if necessary */

	SOCK_STARTUP;

/* initialize the agent library and register with snmpd */

	r = init_agent (SUBAGENT_STR);
	if (r) error (1, 0, "could not initialize agent.");
	init_intellon ();

/* init snmp / read config */

	init_snmp (SUBAGENT_STR);

/* set up termination signal */

	keep_running = 1;
	signal (SIGTERM, stop_server);
	signal (SIGINT, stop_server);

/* register our update timer with snmpd */

	snmp_alarm_register (UPDATE_INTERVAL, SA_REPEAT, timer, &int6k);
	while (keep_running) 
	{

/* block for snmp messages */

		r = agent_check_and_process (1);
	}
	snmp_shutdown (SUBAGENT_STR);
	SOCK_CLEANUP;
	free (int6k.message);
	closechannel (&channel);
	return 0;
}
Пример #22
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

  const char *conninfo; // connection string (in libpq format)

  /* 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("pgstatmibd");

  /* initialize mib code here */

	/*
	 * If the user supplies a parameter on the command line, use it as the
	 * conninfo string; otherwise default to setting dbname=postgres and using
	 * environment variables or defaults for all other connection parameters.
	 */
	if (argc > 1)
		conninfo = argv[1];
	else
		conninfo = "dbname=postgres user=postgres";

	/* Make a connection to the database */
	dbconn = PQconnectdb(conninfo);

	/* Check to see that the backend connection was successfully made */
	if (PQstatus(dbconn) != CONNECTION_OK)
	{
		//printf("Connection to database failed: %s",
		//		PQerrorMessage(dbconn));
		snmp_log(LOG_ERR, "Connection to database failed: %s",
				PQerrorMessage(dbconn));
		//exit_nicely(conn);
	}

  /* mib code */
  init_pgstatServer();
  init_pgstatDatabaseTable();
  init_pgstatBgWriter();

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

  /* pgstatmibd will be used to read pgstatmibd.conf files. */
  init_snmp("pgstatmibd");

  /* 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, "pgstatmibd 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 */
  }

	/* close the connection to the database and cleanup */
	PQfinish(dbconn);

  /* at shutdown time */
  snmp_shutdown("pgstatmibd");
  SOCK_CLEANUP;

  return 0;
}
Пример #23
0
int
main (int argc, char **argv)
{
  int arg;
  char* cp = NULL;
  int dont_fork = 0, do_help = 0;

  while ((arg = getopt(argc, argv, "dD:fhHL:Xp"
#ifndef DISABLE_MIB_LOADING
                       "m:M:"
#endif /* DISABLE_MIB_LOADING */
                       "n:"
#ifndef DISABLE_MIB_LOADING
                       "P:"
#endif /* DISABLE_MIB_LOADING */
                       "vx:")) != EOF) {
    switch (arg) {
    case 'd':
      netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID,
                             NETSNMP_DS_LIB_DUMP_PACKET, 1);
      break;

    case 'D':
      debug_register_tokens(optarg);
      snmp_set_do_debugging(1);
      break;

    case 'f':
      dont_fork = 1;
      break;

    case 'h':
      usage(argv[0]);
      break;

    case 'H':
      do_help = 1;
      break;

    case 'L':
      if (snmp_log_options(optarg, argc, argv) < 0) {
        exit(1);
      }
      break;
#ifndef DISABLE_MIB_LOADING
    case 'm':
      if (optarg != NULL) {
        setenv("MIBS", optarg, 1);
      } else {
        usage(argv[0]);
      }
      break;

    case 'M':
      if (optarg != NULL) {
        setenv("MIBDIRS", optarg, 1);
      } else {
        usage(argv[0]);
      }
      break;
#endif /* DISABLE_MIB_LOADING */

    case 'n':
      if (optarg != NULL) {
        app_name = optarg;
        netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID,
                              NETSNMP_DS_LIB_APPTYPE, app_name);
      } else {
        usage(argv[0]);
      }
      break;

#ifndef DISABLE_MIB_LOADING
    case 'P':
      cp = snmp_mib_toggle_options(optarg);
      if (cp != NULL) {
        fprintf(stderr, "Unknown parser option to -P: %c.\n", *cp);
        usage(argv[0]);
      }
      break;
#endif /* DISABLE_MIB_LOADING */

    case 'v':
      version();
      break;

    case 'x':
      if (optarg != NULL) {
        netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID,
                              NETSNMP_DS_AGENT_X_SOCKET, optarg);
      } else {
        usage(argv[0]);
      }
      break;

    default:
      fprintf(stderr, "invalid option: -%c\n", arg);
      usage(argv[0]);
      break;
    }
  }

  if (do_help) {
    netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID,
                           NETSNMP_DS_AGENT_NO_ROOT_ACCESS, 1);
  } else {
    /* we are a subagent */
    netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID,
                           NETSNMP_DS_AGENT_ROLE, 1);

    if (!dont_fork) {
      if (netsnmp_daemonize(1, snmp_stderrlog_status()) != 0)
        exit(1);
    }

    /* initialize tcpip, if necessary */
    SOCK_STARTUP;
  }

  /* Parse file */
  m_parse(argv[argc-1]);

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

  /* Initialize SNMP */
  init_snmp(app_name);

  if (do_help) {
    fprintf(stderr, "Configuration directives understood:\n");
    read_config_print_usage("  ");
    exit(0);
  }

  /* In case we received a request to stop (kill -TERM or kill -INT) */
  netsnmp_running = 1;
#ifdef SIGTERM
  signal(SIGTERM, stop_server);
#endif
#ifdef SIGINT
  signal(SIGINT, stop_server);
#endif

  /* main loop here... */
  while(netsnmp_running) {
    agent_check_and_process(1);
  }

  /* at shutdown time */
  snmp_shutdown(app_name);
  SOCK_CLEANUP;
  exit(0);
}