/** * Initialize the agent. Calls into init_agent_read_config to set tha app's * configuration file in the appropriate default storage space, * NETSNMP_DS_LIB_APPTYPE. Need to call init_agent before calling init_snmp. * * @param app the configuration file to be read in, gets stored in default * storage * * @return Returns non-zero on failure and zero on success. * * @see init_snmp */ int init_agent(const char *app) { int r = 0; if(++done_init_agent > 1) { snmp_log(LOG_WARNING, "ignoring extra call to init_agent (%d)\n", done_init_agent); return r; } /* * get current time (ie, the time the agent started) */ gettimeofday(&starttime, NULL); starttime.tv_sec--; starttime.tv_usec += 1000000L; /* * we handle alarm signals ourselves in the select loop */ netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_ALARM_DONT_USE_SIG, 1); #ifdef NETSNMP_CAN_USE_NLIST init_kmem("/dev/kmem"); #endif setup_tree(); /* add by frank */ /*´´½¨ÓëÊý¾Ý¿âÄ£¿é»¥³âͨѶµÄÍⲿSOCKET½Ó¿Ú*/ dbsdev = dbsOpen(MID_SNMP); if( NULL != dbsdev ) { dbsWaitModule(dbsdev, MF_CMM|MF_ALARM|MF_TM); //printf("init_agent: dbsOpen\n"); } else { fprintf(stderr,"ERROR: snmpd->dbsOpen error, exited !\n"); } if( CMM_SUCCESS != snmp2cmm_init() ) { printf("module snmpd snmp2cmm_init error\n"); dbs_sys_log(dbsdev, DBS_LOG_ERR, "module snmpd snmp2cmm_init error"); } init_agent_read_config(app); #ifdef TESTING auto_nlist_print_tree(-2, 0); #endif _init_agent_callback_transport(); netsnmp_init_helpers(); init_traps(); netsnmp_container_init_list(); #if defined(USING_AGENTX_SUBAGENT_MODULE) || defined(USING_AGENTX_MASTER_MODULE) /* * initialize agentx configs */ agentx_config_init(); if(netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE) == SUB_AGENT) subagent_init(); #endif /* * Register configuration tokens from transport modules. */ #ifdef NETSNMP_TRANSPORT_UDP_DOMAIN netsnmp_udp_agent_config_tokens_register(); #endif #ifdef NETSNMP_TRANSPORT_UDPIPV6_DOMAIN netsnmp_udp6_agent_config_tokens_register(); #endif #ifdef NETSNMP_TRANSPORT_UNIX_DOMAIN netsnmp_unix_agent_config_tokens_register(); #endif #ifdef NETSNMP_EMBEDDED_PERL init_perl(); #endif #ifdef USING_AGENTX_SUBAGENT_MODULE /* * don't init agent modules for a sub-agent */ if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE) == SUB_AGENT) return r; #endif # include "agent_module_inits.h" return r; } /* end init_agent() */
/** * Initialize the agent. Calls into init_agent_read_config to set tha app's * configuration file in the appropriate default storage space, * NETSNMP_DS_LIB_APPTYPE. Need to call init_agent before calling init_snmp. * * @param app the configuration file to be read in, gets stored in default * storage * * @return Returns non-zero on failure and zero on success. * * @see init_snmp */ int init_agent(const char *app) { int r = 0; if(++done_init_agent > 1) { snmp_log(LOG_WARNING, "ignoring extra call to init_agent (%d)\n", done_init_agent); return r; } /* * get current time (ie, the time the agent started) */ gettimeofday(&starttime, NULL); starttime.tv_sec--; starttime.tv_usec += 1000000L; /* * we handle alarm signals ourselves in the select loop */ netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_ALARM_DONT_USE_SIG, 1); #ifdef CAN_USE_NLIST init_kmem("/dev/kmem"); #endif setup_tree(); init_agent_read_config(app); #ifdef TESTING auto_nlist_print_tree(-2, 0); #endif _init_agent_callback_transport(); netsnmp_init_helpers(); init_traps(); netsnmp_container_init_list(); #if defined(USING_AGENTX_SUBAGENT_MODULE) || defined(USING_AGENTX_MASTER_MODULE) /* * initialize agentx configs */ agentx_config_init(); if(netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE) == SUB_AGENT) subagent_init(); #endif /* * Register configuration tokens from transport modules. */ #ifdef SNMP_TRANSPORT_UDP_DOMAIN netsnmp_udp_agent_config_tokens_register(); #endif #ifdef SNMP_TRANSPORT_UDPIPV6_DOMAIN netsnmp_udp6_agent_config_tokens_register(); #endif #ifdef SNMP_TRANSPORT_UNIX_DOMAIN netsnmp_unix_agent_config_tokens_register(); #endif #ifdef NETSNMP_EMBEDDED_PERL init_perl(); #endif #ifdef USING_AGENTX_SUBAGENT_MODULE /* * don't init agent modules for a sub-agent */ if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE) == SUB_AGENT) return r; #endif # include "agent_module_inits.h" return r; } /* end init_agent() */
int main(int argc, char *argv[]) { int arg; char *prognam; char *cp = NULL; const char* sysUpTime = NULL; /* initialize tcpip, if necessary */ SOCK_STARTUP; prognam = strrchr(argv[0], '/'); if (prognam) ++prognam; else prognam = argv[0]; putenv(strdup("POSIXLY_CORRECT=1")); netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DISABLE_PERSISTENT_LOAD, 1); netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DISABLE_PERSISTENT_SAVE, 1); while ((arg = getopt(argc, argv, ":Vhm:M:D:dP:L:U:c:x:")) != -1) { switch (arg) { case 'h': usage(prognam); result = 0; goto out; case 'm': setenv("MIBS", optarg, 1); break; case 'M': setenv("MIBDIRS", optarg, 1); break; case 'c': context = optarg; contextLen = strlen(context); break; case 'D': debug_register_tokens(optarg); snmp_set_do_debugging(1); break; case 'd': netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DUMP_PACKET, 1); break; case 'U': sysUpTime = optarg; break; case 'V': fprintf(stderr, "NET-SNMP version: %s\n", netsnmp_get_version()); result = 0; goto out; #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(prognam); goto out; } break; #endif /* DISABLE_MIB_LOADING */ case 'L': if (snmp_log_options(optarg, argc, argv) < 0) goto out; 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; case ':': fprintf(stderr, "Option -%c requires an operand\n", optopt); usage(prognam); goto out; case '?': fprintf(stderr, "Unrecognized option: -%c\n", optopt); usage(prognam); goto out; } } arg = optind; init_snmp(NETSNMP_APPLICATION_CONFIG_TYPE); agentx_config_init(); /* NOTIFY varlist */ pdu = pdu_create_opt_context(AGENTX_MSG_NOTIFY, context, contextLen); if (sysUpTime) snmp_add_var(pdu, sysuptime_oid, sysuptime_oid_len, 't', sysUpTime); if (arg == argc) { fprintf(stderr, "Missing trap-oid parameter\n"); usage(prognam); goto out; } if (snmp_add_var(pdu, snmptrap_oid, snmptrap_oid_len, 'o', argv[arg])) { snmp_perror(argv[arg]); goto out; } ++arg; while (arg < argc) { oid name[MAX_OID_LEN]; size_t name_length = MAX_OID_LEN; arg += 3; if (arg > argc) { fprintf(stderr, "%s: Missing type/value for variable\n", argv[arg - 3]); goto out; } if (!snmp_parse_oid(argv[arg - 3], name, &name_length)) { snmp_perror(argv[arg - 3]); goto out; } if (snmp_add_var(pdu, name, name_length, argv[arg - 2][0], argv[arg - 1]) != 0) { snmp_perror(argv[arg - 3]); goto out; } } packetid = 0; state = &Connecting; next_state = NULL; if(state->entry) state->entry(state); /* main loop here... */ for(;;) { int block = 1; int numfds = 0; int count; fd_set fdset; struct timeval timeout; while(next_state) { if(state->exit) state->exit(state); DEBUGMSGTL(("process", "State transition: %s -> %s\n", state->name, next_state->name)); state = next_state; next_state = NULL; if(state->entry) state->entry(state); } if(state == &Exit) break; FD_ZERO(&fdset); snmp_sess_select_info(sessp, &numfds, &fdset, &timeout, &block); count = select(numfds, &fdset, NULL, NULL, !block ? &timeout : NULL); if (count > 0) snmp_sess_read(sessp, &fdset); else if (count == 0) snmp_sess_timeout(sessp); else if (errno != EINTR) { snmp_log(LOG_ERR, "select error [%s]\n", strerror(errno)); change_state(&Exit); } } /* at shutdown time */ snmp_free_pdu(pdu); pdu = NULL; snmp_shutdown(NETSNMP_APPLICATION_CONFIG_TYPE); out: SOCK_CLEANUP; return result; }