/*! \fn void snmp_spine_init() * \brief wrapper function for init_snmp * * Initializes snmp for the given application ID * */ void snmp_spine_init(void) { #ifdef USE_NET_SNMP /* Only do numeric output */ #ifdef NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM, 1); #endif /* Prevent update of the snmpapp.conf file */ #ifdef NETSNMP_DS_LIB_DONT_PERSIST_STATE netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DONT_PERSIST_STATE, 1); #endif /* Prevent update of the snmpapp.conf file */ #ifdef NETSNMP_DS_LIB_DISABLE_PERSISTENT_LOAD netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DISABLE_PERSISTENT_LOAD, 1); #endif #ifdef NETSNMP_DS_LIB_DONT_PRINT_UNITS netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DONT_PRINT_UNITS, 1); #endif netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT, 1); netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICKE_PRINT, 1); netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_BARE_VALUE, 1); netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_NUMERIC_TIMETICKS, 1); #ifdef PACKAGE_VERSION /* check that the headers we compiled with match the library we linked with - apparently not defined in UCD-SNMP... */ SPINE_LOG_DEBUG(("DEBUG: SNMP Header Version is %s\n", PACKAGE_VERSION)); SPINE_LOG_DEBUG(("DEBUG: SNMP Library Version is %s\n", netsnmp_get_version())); if(STRMATCH(PACKAGE_VERSION,netsnmp_get_version())) { init_snmp("spine"); }else{ /* report the error and quit spine */ die("ERROR: SNMP Library Version Mismatch (%s vs %s)",PACKAGE_VERSION,netsnmp_get_version()); } #else SPINE_LOG_DEBUG(("DEBUG: Issues with SNMP Header Version information, assuming old version of Net-SNMP.\n")); init_snmp("spine"); #endif #else ds_set_boolean(DS_LIBRARY_ID, DS_LIB_QUICK_PRINT, 1); ds_set_boolean(DS_LIBRARY_ID, DS_LIB_PRINT_BARE_VALUE, 1); ds_set_boolean(DS_LIBRARY_ID, DS_LIB_NUMERIC_TIMETICKS, 1); init_snmp("spine"); #endif }
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; }
// Set up the SNMP handling threads. Returns 0 if it succeeds. int init_snmp_handler_threads(const char* name) { init_snmp(name); int ret = pthread_create(&snmp_thread_var, NULL, snmp_thread, NULL); return ret; }
void snmp_init(void) { console_log("snmp: init\n"); init_snmp(APPNAME); conv_utf16_utf8 = iconv_open("UTF-8","UTF-16LE"); oid_rssi_ts1_length = MAX_OID_LEN; if (!read_objid(OID_RSSI_TS1, oid_rssi_ts1, &oid_rssi_ts1_length)) console_log("snmp error: can't parse ts1 rssi oid (%s)\n", OID_RSSI_TS1); oid_rssi_ts2_length = MAX_OID_LEN; if (!read_objid(OID_RSSI_TS2, oid_rssi_ts2, &oid_rssi_ts2_length)) console_log("snmp error: can't parse ts2 rssi oid (%s)\n", OID_RSSI_TS2); oid_id_length = MAX_OID_LEN; if (!read_objid(OID_ID, oid_id, &oid_id_length)) console_log("snmp error: can't parse id oid (%s)\n", OID_ID); oid_repeatertype_length = MAX_OID_LEN; if (!read_objid(OID_REPEATERTYPE, oid_repeatertype, &oid_repeatertype_length)) console_log("snmp error: can't parse repeatertype oid (%s)\n", OID_REPEATERTYPE); oid_fwversion_length = MAX_OID_LEN; if (!read_objid(OID_FWVERSION, oid_fwversion, &oid_fwversion_length)) console_log("snmp error: can't parse fwversion oid (%s)\n", OID_FWVERSION); oid_callsign_length = MAX_OID_LEN; if (!read_objid(OID_CALLSIGN, oid_callsign, &oid_callsign_length)) console_log("snmp error: can't parse callsign oid (%s)\n", OID_CALLSIGN); oid_dlfreq_length = MAX_OID_LEN; if (!read_objid(OID_DLFREQ, oid_dlfreq, &oid_dlfreq_length)) console_log("snmp error: can't parse callsign oid (%s)\n", OID_DLFREQ); oid_ulfreq_length = MAX_OID_LEN; if (!read_objid(OID_ULFREQ, oid_ulfreq, &oid_ulfreq_length)) console_log("snmp error: can't parse callsign oid (%s)\n", OID_ULFREQ); }
QtNetSNMP::QSNMPCore::QSNMPCore(unsigned short port, unsigned short retries, long timeout) : _port(port), _retries(retries), _timeout(timeout) { init_snmp(QTNETSNMP_LIBRARY_NAME); snmp_set_mib_warnings(0); snmp_set_mib_errors(0); snmp_set_save_descriptions(1); }
void netsnmp_subagent_init (evContext netsnmp_ctx) { /* setup isc context */ netsnmp_set_evcontext(netsnmp_ctx); /* set netsnmp parameters */ netsnmp_ds_set_init(); /* initialize agent */ init_agent(DNAME_JNX_EXAMPLED); /* register mib modules */ init_jnxExampleMIB(); init_jnxExampleNetSnmpVersion(); /* * start library */ init_snmp(DNAME_JNX_EXAMPLED); /* send out startup trap */ exampled_trap_send_startup(); }
lagopus_result_t snmpmgr_start(void) { lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES; (void)lagopus_mutex_lock(&snmp_state_lock); if (state == SNMPMGR_RUNNABLE) { if (is_thread == false) { state = SNMPMGR_RUNNING; (void)lagopus_mutex_unlock(&snmp_state_lock); /* open the session to AgentX master agent here. */ init_snmp(SNMP_TYPE); /* send_*_trap always return SNMP_ERR_NOERROR */ (void)send_coldStart_trap(); ret = LAGOPUS_RESULT_OK; lagopus_msg_info("SNMP manager started.\n"); } else { /* Note: * If is_thread is true, init_snmp and send_coldStart_trap * are called inside snmpmgr_thread_loop */ (void)lagopus_mutex_unlock(&snmp_state_lock); ret = lagopus_thread_start(&snmpmgr, false); } } else { (void)lagopus_mutex_unlock(&snmp_state_lock); ret = LAGOPUS_RESULT_NOT_STARTED; } return ret; }
/* * initialize */ void snmpInitialize(tInterfaceList *interfaceList) { tOidList *oidList; /* initialize library */ init_snmp("snmppoller"); netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT, TRUE); unsigned int i = 0; while (i < interfaceList->count) { syslog(LOG_DEBUG, "snmpInitialize: %s", interfaceList->interface[i].ip); oidList = &interfaceList->interface[i].oidList; unsigned int o = 0; while (o < oidList->count) { syslog(LOG_DEBUG, "snmpInitialize: * %s", oidList->oid[o].oidName); oidList->oid[o].OidLen = sizeof(oidList->oid[o].Oid)/sizeof(oidList->oid[o].Oid[0]); if (!read_objid(oidList->oid[o].oidName, oidList->oid[o].Oid, &oidList->oid[o].OidLen)) { syslog(LOG_ERR, "snmpInitialize: read_objid failed for: %d %s", interfaceList->interface[i].id_interface, oidList->oid[o].oidName); exit(1); } o++; } i++; } }
int init_ilo_snmp_session(struct ilo_snmp_priv * priv_ptr) { int ret = NAGIOS_ILO_SUCCESS_STATUS; netsnmp_session session; init_snmp("Nagios_hpilo_snmp"); snmp_sess_init(&session); session.peername = strdup(priv_ptr->options.host); session.community = strdup(priv_ptr->options.community); session.community_len = strlen((char *) session.community); session.version = priv_ptr->options.version; session.timeout = priv_ptr->options.timeout; session.retries = priv_ptr->options.retries; priv_ptr->session = snmp_open(&session); if (priv_ptr->session == NULL) { snmp_error(&session, &session.s_errno, &session.s_snmp_errno, &priv_ptr->err_str); ret = NAGIOS_ILO_FAIL_STATUS; } return ret; }
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; }
/* * Callgraph for the config stuff: * csnmp_config * +-> call_snmp_init_once * +-> csnmp_config_add_data * ! +-> csnmp_config_add_data_instance * ! +-> csnmp_config_add_data_instance_prefix * ! +-> csnmp_config_add_data_values * +-> csnmp_config_add_host * +-> csnmp_config_add_host_version * +-> csnmp_config_add_host_collect * +-> csnmp_config_add_host_auth_protocol * +-> csnmp_config_add_host_priv_protocol * +-> csnmp_config_add_host_security_level */ static void call_snmp_init_once (void) { static int have_init = 0; if (have_init == 0) init_snmp (PACKAGE_NAME); have_init = 1; } /* void call_snmp_init_once */
static int nl_init_mib(lua_State *L) { register_mib_handlers(); read_premib_configs(); read_configs(); init_snmp("lua_snmp"); lua_pushnil(L); return 1; }
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; }
int main(int argc, char *argv[]) { int ch; int config = 1; extern char *optarg; extern int optind, opterr; struct qtbl_t qtbl[MAX_IF]; char config_file[64] = CONFIG_FILE; int nif; while((ch = getopt(argc, argv, "nc:")) != -1){ switch(ch){ case 'n': config = 0; break; case 'c': strncpy(config_file, optarg, 64); break; default: usage(); break; } } fd = socket(PF_INET, SOCK_DGRAM, 0); if(config == 0){ int i; if(strncmp(CONFIG_FILE, config_file, strlen(CONFIG_FILE))){ usage(); } if(setsockopt(fd, IPPROTO_IP, IPSIRENS_SRFIL, &i, sizeof(i)) < 0){ printf("failed in IPSIRENS_SRFIL\n"); } return(0); } nif = qtbl_init(config_file, qtbl); if(nif == 0){ printf("No valid interface is founded\n"); exit(1); } init_snmp("vifset"); while(1){ vid_query(qtbl, nif); sleep(interval); } /* */ return (0); }
// initialize connection to snmp agent void init(char* ip , char* community) { init_snmp("cs158b"); /* * Initialize a "session" that defines who we're going to talk to */ snmp_sess_init( &session ); /* set up defaults */ session.peername = strdup(ip); // ip session.version = SNMP_VERSION_2c; session.community = community; // community session.community_len = strlen(session.community); }
int snmp_init(selector_t *sel) { struct snmp_session session; #ifdef HAVE_NETSNMP netsnmp_transport *transport = NULL; static char *snmp_default_port = "udp:162"; netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIB_ERRORS, 0); init_snmp("ipmi_ui"); transport = netsnmp_tdomain_transport(snmp_default_port, 1, "udp"); if (!transport) { snmp_sess_perror("ipmi_ui", &session); return -1; } #else void *transport = NULL; #endif snmp_sess_init(&session); session.peername = SNMP_DEFAULT_PEERNAME; session.version = SNMP_DEFAULT_VERSION; session.community_len = SNMP_DEFAULT_COMMUNITY_LEN; session.retries = SNMP_DEFAULT_RETRIES; session.timeout = SNMP_DEFAULT_TIMEOUT; session.local_port = SNMP_TRAP_PORT; session.callback = snmp_input; session.callback_magic = transport; session.authenticator = NULL; session.isAuthoritative = SNMP_SESS_UNKNOWNAUTH; #ifdef HAVE_NETSNMP snmp_session = snmp_add(&session, transport, snmp_pre_parse, NULL); #else snmp_session = snmp_open_ex(&session, snmp_pre_parse, NULL, NULL, NULL, NULL); #endif if (snmp_session == NULL) { snmp_sess_perror("ipmi_ui", &session); return -1; } ipmi_sel_set_read_fds_handler(sel, snmp_add_read_fds, snmp_check_read_fds, snmp_check_timeout, NULL); return 0; }
/* Main program Queries various SNMP values on cisco devices. */ int main(int argc, char *argv[]) { char* output; struct snmp_session session; char arg; // int i; // long snmpVer; /* Initialize and build snmp session, add version, community, host, etc */ init_snmp("Linux_checks"); snmp_sess_init( &session ); switch (arg = snmp_parse_args(argc, argv, &session, "C:", optProc)) { case -3: exit(1); case -2: exit(0); case -1: usage(); exit(1); default: break; } /* Check warning/critical test values to verify they are within the appropriate ranges */ if ((crit > 100) && (strcmp(mode, "sessions"))) { printf("Critical threshold should be less than 100!\n"); usage(); exit(RESULT_UNKNOWN); } else if (crit < 0) { printf("Critical threshould must be greater than 0!\n"); usage(); exit(RESULT_UNKNOWN); } else if ((warn < 0) && (strcmp(mode, "sessions"))) { printf("Warning threshold must be greater than or equal to 0!\n"); usage(); exit(RESULT_UNKNOWN); } else if (warn > crit) { printf("Warning threshold must not be greater than critical threshold!\n"); usage(); exit(RESULT_UNKNOWN); } output = checkCPU(&session); printf("%s", output); return exitVal; }
static lagopus_result_t snmpmgr_thread_loop(const lagopus_thread_t *selfptr, void *arg) { lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES; lagopus_chrono_t interval = DEFAULT_SNMPMGR_LOOP_INTERVAL_NSEC; global_state_t global_state; shutdown_grace_level_t l; (void)selfptr; (void)arg; /* open the session to AgentX master agent here. */ init_snmp(SNMP_TYPE); /* wait all modules start */ if ((ret = global_state_wait_for(GLOBAL_STATE_STARTED, &global_state, &l, -1 /* forever! */)) != LAGOPUS_RESULT_OK) { lagopus_perror(ret); } else { if (global_state != GLOBAL_STATE_STARTED) { lagopus_exit_fatal("must not happen. die!\n"); } } /* all modules have started, then send a coldStart trap */ (void)send_coldStart_trap(); lagopus_msg_info("SNMP manager started (as a thread).\n"); /* main loop */ while (keep_running) { (void)lagopus_mutex_lock(&snmp_lock); ret = internal_snmpmgr_poll(interval); (void)lagopus_mutex_unlock(&snmp_lock); if (ret != LAGOPUS_RESULT_OK && ret != LAGOPUS_RESULT_TIMEDOUT) { lagopus_msg_warning("failed to poll SNMP AgentX request: %s", lagopus_error_get_string(ret)); } check_status_and_send_traps(); } /* stop SNMP */ snmp_shutdown(SNMP_TYPE); (void)lagopus_mutex_lock(&snmp_state_lock); if (state != SNMPMGR_RUNNABLE) { state = SNMPMGR_NONE; } (void)lagopus_mutex_unlock(&snmp_state_lock); return LAGOPUS_RESULT_OK; }
/** * Initialize SNMP library */ void snmp_init(struct snmp_sessions *ss) { // Initialize SNMP library init_snmp("scm_daemon"); // Initialize local sessions for reading and writing init_session_helper(&ss->read_local, "public"); init_session_helper(&ss->write_local, "private"); // Open the sessions to get a remote connection open_session_helper(&ss->read_local, &ss->read); open_session_helper(&ss->write_local, &ss->write); }
/* int main (int argc, char **argv) */ int init_ifs_agent() { 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 */ std::cout << ".......1........." << std::endl; /* print log errors to syslog or stderr */ if (syslog) snmp_enable_calllog(); else snmp_enable_stderrlog(); std::cout << ".......1........." << std::endl; /* 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("infinetfs_agent"); /* initialize mib code here */ /* mib code: init_nstAgentSubagentObject from nstAgentSubagentObject.C */ /* init_nstAgentSubagentObject(); */ init_infinetfs_mib_module(); /* example-demon will be used to read example-demon.conf files. */ init_snmp("infinetfs_agent"); /* 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,"infinetfs_agent is up and running.\n"); return 0; }
struct snmp_mtp_session *snmp_mtp_session_create() { struct snmp_mtp_session *session = talloc_zero(NULL, struct snmp_mtp_session); if (!session) return NULL; init_snmp("cellmgr_ng"); snmp_sess_init(&session->session); session->session.version = SNMP_VERSION_1; session->session.community = (unsigned char *) "private"; session->session.community_len = strlen((const char *) session->session.community); session->session.myvoid = session; return session; }
int snmp_printer_info(char *hostname, char *community, char **manufacturer, char **model, char **description, char **serial_no, char ***command_set, char **uri) { char *tmp = NULL; init_snmp("network-printer-probe"); init_mib(); if (snmp_brother_printer_info(hostname, community, manufacturer, model, description, serial_no, command_set) == 0) { return (0); } else if (snmp_ricoh_printer_info(hostname, community, manufacturer, model, description, serial_no, command_set) == 0) { return (0); } else if (snmp_lexmark_printer_info(hostname, community, manufacturer, model, description, serial_no, command_set) == 0) { return (0); } else if (snmp_xerox_phaser_printer_info(hostname, community, manufacturer, model, description, serial_no, command_set, uri) == 0) { return (0); } else if (snmp_qms_printer_info(hostname, community, manufacturer, model, description, serial_no, command_set, uri) == 0) { return (0); } else if (snmp_oki_printer_info(hostname, community, manufacturer, model, description, serial_no, command_set) == 0) { return (0); } else if (snmp_hp_printer_info(hostname, community, manufacturer, model, description, serial_no, command_set) == 0) { return (0); } else if (snmp_ppm_printer_info(hostname, community, manufacturer, model, description, serial_no, command_set) == 0) { return (0); } else if (snmp_prt_printer_info(hostname, community, manufacturer, model, description, serial_no, command_set) == 0) { return (0); } else if (snmp_host_resource_printer_info(hostname, community, manufacturer, model, description, serial_no, command_set) == 0) { return (0); } return (-1); }
QSnmp::QSnmp(const QString &peername, const QByteArray &community) { init_snmp("door_control"); snmp_sess_init(&session); session.peername = strdup(peername.toUtf8().constData()); //"192.168.0.100"; session.version = SNMP_VERSION_1; session.community = (unsigned char*)strdup(community.constData()); //(unsigned char*)"private"; session.community_len = strlen((char*)session.community); ss = snmp_open(&session); if (!ss) { snmp_perror("ack"); snmp_log(LOG_ERR, "something horrible happened!!!\n"); return; } qDebug("QSnmp: session established"); }
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; }
/*! This function opens up a connection with the master agent specified in * the snmpstats modules configuration file */ void register_with_master_agent(char *name_to_register_under) { /* Set ourselves up as an AgentX Client. */ netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE, 1); /* Initialize TCP if necessary. (Its here for WIN32) compatibility. */ SOCK_STARTUP; LM_DBG("Connecting to SNMPD MasterX\n"); /* Read in our configuration file to determine master agent ping times * what port communication is to take place over, etc. */ init_agent("snmpstats"); /* Use a name we can register our agent under. */ init_snmp(name_to_register_under); LM_DBG("** Connected to SNMPD MasterX\n"); }
static void init_snmp_subagent(const char *agentx_host, int agentx_port) { char agentx_socket[128]; int background = 1; /* change this if you want to run in the background */ int syslog = 0; /* change this if you want to use syslog */ snmp_enable_filelog("/var/log/exa_agentx.log", 1); snmp_log(LOG_INFO, "exa_agentx configured to connect to %s:%d\n", agentx_host, agentx_port); /* we're in an agentx subagent */ netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE, 1); sprintf(agentx_socket, "tcp:%s:%d", agentx_host, agentx_port); netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_X_SOCKET, agentx_socket); /* run in background, if requested */ if (background && netsnmp_daemonize(1, !syslog)) exit(1); snmp_log(LOG_INFO, "exa_agentx daemonized\n"); /* initialize tcpip, if necessary */ SOCK_STARTUP; /* initialize the agent library */ init_agent("exa_agentx"); /* initialize mib code here */ /* mib code: init_nstAgentSubagentObject from nstAgentSubagentObject.C */ /* not necessary to just send TRAPs !! */ /* init_mib_int_watch(); */ /* example-demon will be used to read example-demon.conf files. */ init_snmp("exa_agentx"); /* In case we recevie a request to stop (kill -TERM or kill -INT) */ stop = false; signal(SIGTERM, stop_server); signal(SIGINT, stop_server); snmp_log(LOG_INFO, "exa_agentx is up and running.\n"); }
void snmp::session::open(const std::string & peername, const std::string & community) { if (!_initialized) { init_snmp("snmpapp"); _initialized = true; } ::snmp_session session; snmp_sess_init(&session); session.peername = const_cast<char *>(peername.c_str()); session.version = SNMP_VERSION_1; session.community = reinterpret_cast<u_char *>(const_cast<char *>(community.c_str())); session.community_len = community.size(); _ss = snmp_open(&session); if (!_ss) throw std::runtime_error("Failed to open SNMP session"); }
/* * initialize */ void initialize (void) { struct oid *op = oids; /* Win32: init winsock */ SOCK_STARTUP; /* initialize library */ init_snmp("asynchapp"); /* parse the oids */ while (op->Name) { op->OidLen = sizeof(op->Oid)/sizeof(op->Oid[0]); if (!read_objid(op->Name, op->Oid, &op->OidLen)) { snmp_perror("read_objid"); exit(1); } op++; } }
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"); }
struct snmp_session *session_open(const char *chost, const char *ccommunity) { struct snmp_session *ss, session; char *host, *community; host=calloc(strlen(chost)+1, sizeof(char)); strcpy(host, chost); community=calloc(strlen(ccommunity)+1, sizeof(char)); strcpy(community, ccommunity); /* Initialize the SNMP library */ init_snmp("snmpapp"); /* Initialize the session */ snmp_sess_init( &session ); session.version = SNMP_VERSION_2c; session.peername = host; session.community = (u_char*)community; session.community_len = strlen(community); netsnmp_ds_set_int( NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, NETSNMP_OID_OUTPUT_UCD ); // NETSNMP_OID_OUTPUT_NONE); SOCK_STARTUP /* open the SNMP session */ ss = snmp_open(&session); if (ss == NULL) { perror("Cant snmp_open"); exit(-1); } netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, NETSNMP_OID_OUTPUT_NUMERIC); return ss; }