⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 subagent.c

📁 开发snmp的开发包有两个开放的SNMP开发库
💻 C
📖 第 1 页 / 共 3 页
字号:
                                    reg_parms->descr);    else        return agentx_remove_agentcaps(agentx_ss,                                       reg_parms->name,                                       reg_parms->namelen);}#endifstatic intsubagent_shutdown(int majorID, int minorID, void *serverarg, void *clientarg){    netsnmp_session *thesession = (netsnmp_session *)clientarg;    DEBUGMSGTL(("agentx/subagent", "shutting down session....\n"));    if (thesession == NULL) {	DEBUGMSGTL(("agentx/subagent", "Empty session to shutdown\n"));	main_session = NULL;	return 0;    }    agentx_close_session(thesession, AGENTX_CLOSE_SHUTDOWN);    snmp_close(thesession);    main_session = NULL;    DEBUGMSGTL(("agentx/subagent", "shut down finished.\n"));    return 1;}/* * Register all the "standard" AgentX callbacks for the given session.   */voidagentx_register_callbacks(netsnmp_session * s){    DEBUGMSGTL(("agentx/subagent",                "registering callbacks for session %p\n", 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_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_transport *t;    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;    sess.callback = handle_agentx_packet;    sess.authenticator = NULL;    t = netsnmp_transport_open_client(            "agentx", netsnmp_ds_get_string(NETSNMP_DS_APPLICATION_ID,                                            NETSNMP_DS_AGENT_X_SOCKET));    if (t == 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)) {            char buf[1024];            const char *socket =                netsnmp_ds_get_string(NETSNMP_DS_APPLICATION_ID,                                      NETSNMP_DS_AGENT_X_SOCKET);            snprintf(buf, sizeof(buf), "Warning: "                     "Failed to connect to the agentx master agent (%s)",                     socket ? socket : "[NIL]");            if (!netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,                                        NETSNMP_DS_AGENT_NO_ROOT_ACCESS)) {                netsnmp_sess_log_error(LOG_WARNING, buf, &sess);            } else {                snmp_sess_perror(buf, &sess);            }        }        return -1;    }    main_session =        snmp_add_full(&sess, t, NULL, agentx_parse, NULL, NULL,                      agentx_realloc_build, agentx_check_packet, NULL);    if (main_session == NULL) {        if (!netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,                                    NETSNMP_DS_AGENT_NO_CONNECTION_WARNINGS)) {            char buf[1024];            snprintf(buf, sizeof(buf), "Error: "                     "Failed to create the agentx master agent session (%s)",                     netsnmp_ds_get_string(NETSNMP_DS_APPLICATION_ID,                                           NETSNMP_DS_AGENT_X_SOCKET));            snmp_sess_perror(buf, &sess);        }        netsnmp_transport_free(t);        return -1;    }    /*     * 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);    snmp_log(LOG_INFO, "NET-SNMP version %s AgentX subagent connected\n",             netsnmp_get_version());    DEBUGMSGTL(("agentx/subagent", "opening session...  DONE (%p)\n",                main_session));    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);        register_mib_detach();        if (main_session != NULL) {            remove_trap_session(ss);        snmp_close(main_session);        main_session = NULL;        agentx_reopen_session(0, NULL);        }        else {            snmp_close(main_session);            main_session = NULL;        }    } else {        DEBUGMSGTL(("agentx/subagent", "session %p responded to ping\n",                    ss));    }}#endif /* USING_AGENTX_SUBAGENT_MODULE */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -