📄 subagent.c
字号:
subagent_register_ping_alarm, s); snmp_register_callback(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_SHUTDOWN, subagent_shutdown, s); snmp_register_callback(SNMP_CALLBACK_APPLICATION, SNMPD_CALLBACK_REGISTER_OID, agentx_registration_callback, s); snmp_register_callback(SNMP_CALLBACK_APPLICATION, SNMPD_CALLBACK_UNREGISTER_OID, agentx_registration_callback, s);#ifdef USING_MIBII_SYSORTABLE_MODULE snmp_register_callback(SNMP_CALLBACK_APPLICATION, SNMPD_CALLBACK_REG_SYSOR, agentx_sysOR_callback, s); snmp_register_callback(SNMP_CALLBACK_APPLICATION, SNMPD_CALLBACK_UNREG_SYSOR, agentx_sysOR_callback, s);#endif}/* * Unregister all the callbacks associated with this session. */voidagentx_unregister_callbacks(netsnmp_session * ss){ DEBUGMSGTL(("agentx/subagent", "unregistering callbacks for session %p\n", ss)); snmp_unregister_callback(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_POST_READ_CONFIG, subagent_register_ping_alarm, ss, 1); snmp_unregister_callback(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_SHUTDOWN, subagent_shutdown, ss, 1); snmp_unregister_callback(SNMP_CALLBACK_APPLICATION, SNMPD_CALLBACK_REGISTER_OID, agentx_registration_callback, ss, 1); snmp_unregister_callback(SNMP_CALLBACK_APPLICATION, SNMPD_CALLBACK_UNREGISTER_OID, agentx_registration_callback, ss, 1);#ifdef USING_MIBII_SYSORTABLE_MODULE snmp_unregister_callback(SNMP_CALLBACK_APPLICATION, SNMPD_CALLBACK_REG_SYSOR, agentx_sysOR_callback, ss, 1); snmp_unregister_callback(SNMP_CALLBACK_APPLICATION, SNMPD_CALLBACK_UNREG_SYSOR, agentx_sysOR_callback, ss, 1);#endif}/* * Open a session to the master agent. */intsubagent_open_master_session(void){ netsnmp_session sess; DEBUGMSGTL(("agentx/subagent", "opening session...\n")); if (main_session) { snmp_log(LOG_WARNING, "AgentX session to master agent attempted to be re-opened."); return -1; } snmp_sess_init(&sess); sess.version = AGENTX_VERSION_1; sess.retries = SNMP_DEFAULT_RETRIES; sess.timeout = SNMP_DEFAULT_TIMEOUT; sess.flags |= SNMP_FLAGS_STREAM_SOCKET; if (netsnmp_ds_get_string(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_X_SOCKET)) { sess.peername = strdup( netsnmp_ds_get_string(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_X_SOCKET)); } else { sess.peername = strdup(AGENTX_SOCKET); } sess.local_port = 0; /* client */ sess.remote_port = AGENTX_PORT; /* default port */ sess.callback = handle_agentx_packet; sess.authenticator = NULL; main_session = snmp_open_ex(&sess, NULL, agentx_parse, NULL, NULL, agentx_realloc_build, agentx_check_packet); if (main_session == NULL) { /* * Diagnose snmp_open errors with the input * netsnmp_session pointer. */ if (!netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_NO_CONNECTION_WARNINGS)) { if (!netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_NO_ROOT_ACCESS)) { netsnmp_sess_log_error(LOG_WARNING, "Error: Failed to connect to the agentx master agent", &sess); } else { snmp_sess_perror ("Error: Failed to connect to the agentx master agent", &sess); } } if (sess.peername) /* was memduped above and is no longer needed */ free(sess.peername); return -1; } if (sess.peername) /* was memduped above and is no longer needed */ free(sess.peername); /* * I don't know why 1 is success instead of the usual 0 = noerr, * but that's what the function returns. */ if (1 != agentx_open_session(main_session)) { snmp_close(main_session); main_session = NULL; return -1; } if (add_trap_session(main_session, AGENTX_MSG_NOTIFY, 1, AGENTX_VERSION_1)) { DEBUGMSGTL(("agentx/subagent", " trap session registered OK\n")); } else { DEBUGMSGTL(("agentx/subagent", "trap session registration failed\n")); snmp_close(main_session); main_session = NULL; return -1; } agentx_register_callbacks(main_session); snmp_call_callbacks(SNMP_CALLBACK_APPLICATION, SNMPD_CALLBACK_INDEX_START, (void *) main_session); DEBUGMSGTL(("agentx/subagent", "opening session... DONE (%p)\n", main_session)); return 0;}/* * returns non-zero on error */intsubagent_pre_init(void){ DEBUGMSGTL(("agentx/subagent", "initializing....\n")); /* * set up callbacks to initiate master agent pings for this session */ netsnmp_ds_register_config(ASN_INTEGER, netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_APPTYPE), "agentxPingInterval", NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_AGENTX_PING_INTERVAL); /* ping and/or reconnect by default every 15 seconds */ netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_AGENTX_PING_INTERVAL, 15); if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE) != SUB_AGENT) { return 0; } /* * if a valid ping interval has been defined, call agentx_reopen_session * to try to connect to master or setup a ping alarm if it couldn't * succeed */ if (netsnmp_ds_get_int(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_AGENTX_PING_INTERVAL) > 0) agentx_reopen_session(0, NULL); else /* if no ping interval was set up, just try to connect once */ subagent_open_master_session(); if (!main_session) return -1; DEBUGMSGTL(("agentx/subagent", "initializing.... DONE\n")); return 0;}/* * Alarm callback function to open a session to the master agent. If a * transport disconnection callback occurs, indicating that the master agent * has died (or there has been some strange communication problem), this * alarm is called repeatedly to try to re-open the connection. */voidagentx_reopen_session(unsigned int clientreg, void *clientarg){ DEBUGMSGTL(("agentx/subagent", "agentx_reopen_session(%d) called\n", clientreg)); if (subagent_open_master_session() == 0) { /* * Successful. Delete the alarm handle if one exists. */ if (clientreg != 0) { snmp_alarm_unregister(clientreg); } /* * Reregister all our nodes. */ register_mib_reattach(); /* * Register a ping alarm (if need be). */ subagent_register_ping_alarm(0, 0, 0, main_session); } else { if (clientreg == 0) { /* * Register a reattach alarm for later */ subagent_register_ping_alarm(0, 0, 0, main_session); } }}/* * If a valid session is passed in (through clientarg), register a * ping handler to ping it frequently, else register an attempt to try * and open it again later. */static intsubagent_register_ping_alarm(int majorID, int minorID, void *serverarg, void *clientarg){ netsnmp_session *ss = (netsnmp_session *) clientarg; int ping_interval = netsnmp_ds_get_int(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_AGENTX_PING_INTERVAL); if (!ping_interval) /* don't do anything if not setup properly */ return 0; /* * register a ping alarm, if desired */ if (ss) { if (ss->securityModel != SNMP_DEFAULT_SECMODEL) { DEBUGMSGTL(("agentx/subagent", "unregister existing alarm %d\n", ss->securityModel)); snmp_alarm_unregister(ss->securityModel); } DEBUGMSGTL(("agentx/subagent", "register ping alarm every %d seconds\n", ping_interval)); /* * we re-use the securityModel parameter for an alarm stash, * since agentx doesn't need it */ ss->securityModel = snmp_alarm_register(ping_interval, SA_REPEAT, agentx_check_session, ss); } else { /* * attempt to open it later instead */ DEBUGMSGTL(("agentx/subagent", "subagent not properly attached, postponing registration till later....\n")); snmp_alarm_register(ping_interval, SA_REPEAT, agentx_reopen_session, NULL); } return 0;}/* * check a session validity for connectivity to the master agent. If * not functioning, close and start attempts to reopen the session */voidagentx_check_session(unsigned int clientreg, void *clientarg){ netsnmp_session *ss = (netsnmp_session *) clientarg; if (!ss) { if (clientreg) snmp_alarm_unregister(clientreg); return; } DEBUGMSGTL(("agentx/subagent", "checking status of session %p\n", ss)); if (!agentx_send_ping(ss)) { snmp_log(LOG_WARNING, "AgentX master agent failed to respond to ping. Attempting to re-register.\n"); /* * master agent disappeared? Try and re-register. * close first, just to be sure . */ agentx_unregister_callbacks(ss); agentx_close_session(ss, AGENTX_CLOSE_TIMEOUT); snmp_alarm_unregister(clientreg); /* delete ping alarm timer */ snmp_call_callbacks(SNMP_CALLBACK_APPLICATION, SNMPD_CALLBACK_INDEX_STOP, (void *) ss); snmp_close(main_session); register_mib_detach(); main_session = NULL; agentx_reopen_session(0, NULL); } else { DEBUGMSGTL(("agentx/subagent", "session %p responded to ping\n", ss)); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -