예제 #1
0
파일: agent.c 프로젝트: themiron/accel-ppp
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;
}
예제 #2
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;
}
예제 #3
0
void
snmp_agent_init(oid *myoid, int len, char *name, struct variable *variables,
		int varsize, int varlen)
{
	log_message(LOG_INFO, "Starting SNMP subagent");
	netsnmp_enable_subagent();
	snmp_disable_log();
	snmp_enable_calllog();
	snmp_register_callback(SNMP_CALLBACK_LIBRARY,
			       SNMP_CALLBACK_LOGGING,
			       snmp_keepalived_log,
			       NULL);

	/* Do not handle persistent states */
	netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID,
	    NETSNMP_DS_LIB_DONT_PERSIST_STATE, TRUE);
	/* Do not load any MIB */
	setenv("MIBS", "", 1);
	/* Ping AgentX less often than every 15 seconds: pinging can
	   block keepalived. We check every 2 minutes. */
	netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID,
			   NETSNMP_DS_AGENT_AGENTX_PING_INTERVAL, 120);
	/* We also register a callback to modify default timeout and
	   retries value. */
	snmp_register_callback(SNMP_CALLBACK_LIBRARY,
			       SNMP_CALLBACK_SESSION_INIT,
			       snmp_setup_session_cb, NULL);

	init_agent(name);
	if (register_mib(name, (struct variable *) variables, varsize,
			 varlen, myoid, len) != MIB_REGISTERED_OK)
		log_message(LOG_WARNING, "Unable to register MIB");
	register_mib("Keepalived", (struct variable *) global_vars,
		     sizeof(struct variable8),
		     sizeof(global_vars)/sizeof(struct variable8),
		     global_oid, OID_LENGTH(global_oid));
	init_snmp(name);

	register_sysORTable(global_oid, OID_LENGTH(global_oid) - 1,
			    "The MIB module for Keepalived");
}
예제 #4
0
파일: snmpmgr.c 프로젝트: tidatida/lagopus
static void
initialize_internal(void) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  void *logger_client_arg = NULL;
  char *master_agentx_socket = NULL;
  char *ping_interval_string = NULL;
  uint16_t ping_interval = 0;

  state = SNMPMGR_NONE;

  /* `is_thread` is able to be changed only once here */
  is_thread = is_thread_dummy;
  if (is_thread == false && thdptr_dummy != NULL) {
    *thdptr_dummy = NULL;
  } else if (thdptr_dummy != NULL) {
    *thdptr_dummy = &snmpmgr;
  } else {
    /* never reached! */
    return;
  }

  if (lagopus_mutex_create(&snmp_lock) != LAGOPUS_RESULT_OK) {
    return;
  }

  if (lagopus_mutex_create(&snmp_state_lock) != LAGOPUS_RESULT_OK) {
    return;
  }

  snmp_set_do_debugging(false);

  /* setup Net-SNMP logger to use the lagopus logging function */
  if (snmp_register_callback(SNMP_CALLBACK_LIBRARY,
                             SNMP_CALLBACK_LOGGING,
                             snmp_log_callback_wrapper,
                             logger_client_arg) != SNMPERR_SUCCESS) {
    return;
  }
  snmp_enable_calllog();

  /* setup the SNMP module to be Agentx subagent */
  netsnmp_enable_subagent();

#ifdef THE_CONFSYS_ERA
  master_agentx_socket = config_get("snmp master-agentx-socket WORD");
  if (master_agentx_socket == NULL) {
    config_set_default("snmp master-agentx-socket WORD",
                       (char *)DEFAULT_SNMPMGR_AGENTX_SOCKET);
    master_agentx_socket = config_get("snmp master-agentx-socket WORD");
  }
#else
  /*
   * FIXME:
   *	Fetch it from the datastore.
   */
  master_agentx_socket = (char *)DEFAULT_SNMPMGR_AGENTX_SOCKET;
#endif /* THE_CONFSYS_ERA */
  lagopus_msg_debug(25, "master agentx socket is %s\n", master_agentx_socket);

  if (netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID,
                            NETSNMP_DS_AGENT_X_SOCKET,
                            master_agentx_socket)
      != SNMPERR_SUCCESS) {
    initialize_ret = LAGOPUS_RESULT_SNMP_API_ERROR;
    return;
  }

  /* don't read/write configuration files */
  if (netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID,
                             NETSNMP_DS_LIB_DONT_PERSIST_STATE,
                             true) != SNMPERR_SUCCESS) {
    initialize_ret = LAGOPUS_RESULT_SNMP_API_ERROR;
    return;
  }
  if (netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID,
                             NETSNMP_DS_LIB_DISABLE_PERSISTENT_LOAD,
                             true) != SNMPERR_SUCCESS) {
    initialize_ret = LAGOPUS_RESULT_SNMP_API_ERROR;
    return;
  }

  /* the interval [sec] to send ping to AgentX master agent */
#ifdef THE_CONFSYS_ERA
  ping_interval_string = config_get("snmp ping-interval-second 1-255");
  if (ping_interval_string != NULL &&
      (ret = check_ping_interval(ping_interval_string, &ping_interval))
      != LAGOPUS_RESULT_OK) {
    config_set_default("snmp ping-interval-second 1-255",
                       (char *) __STR__(DEFAULT_SNMPMGR_AGENTX_PING_INTERVAL_SEC));
    ping_interval_string = config_get("snmp ping-interval-second 1-255");
    ping_interval = DEFAULT_SNMPMGR_AGENTX_PING_INTERVAL_SEC; /* default value */
  }
#else
  /*
   * FIXME:
   *	Fetch it from the datastore.
   */
  ping_interval = DEFAULT_SNMPMGR_AGENTX_PING_INTERVAL_SEC;
#endif /* THE_CONFSYS_ERA */

  if (netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID,
                         NETSNMP_DS_AGENT_AGENTX_PING_INTERVAL,
                         (int)ping_interval)
      != SNMPERR_SUCCESS) {
    initialize_ret = LAGOPUS_RESULT_SNMP_API_ERROR;
    return;
  }

  if (init_agent(SNMP_TYPE) != SNMPERR_SUCCESS) {
    initialize_ret = LAGOPUS_RESULT_SNMP_API_ERROR;
    return;
  }

  if ((ret = lagopus_hashmap_create(&iftable_entry_hash,
                                    LAGOPUS_HASHMAP_TYPE_STRING,
                                    delete_entry)) != LAGOPUS_RESULT_OK) {
    lagopus_perror(ret);
    exit(1);
  }

  init_ifTable();
  init_ifNumber();
  init_dot1dBaseBridgeAddress();
  init_dot1dBaseNumPorts();
  init_dot1dBasePortTable();
  init_dot1dBaseType();

  if (is_thread == false) {
    initialize_ret = LAGOPUS_RESULT_OK;
  } else {
    keep_running = true;
    initialize_ret = lagopus_thread_create(&snmpmgr,
                                           snmpmgr_thread_loop,
                                           snmpmgr_thread_finally_called,
                                           NULL,
                                           "snmpmgr",
                                           NULL);
  }

  lagopus_msg_info("SNMP manager initialized.\n");
}
예제 #5
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);
}
예제 #6
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);
}