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

📄 notification_log.c

📁 开发snmp的开发包有两个开放的SNMP开发库
💻 C
📖 第 1 页 / 共 2 页
字号:
    netsnmp_table_set_add_default_row(nlmLogTable,                                      COLUMN_NLMLOGENGINETADDRESS,                                      ASN_OCTET_STR, 0, NULL, 0);    /*     * adding column nlmLogEngineTDomain of type ASN_OBJECT_ID and access     * of ReadOnly      */    DEBUGMSGTL(("initialize_table_nlmLogTable",                "adding column nlmLogEngineTDomain (#6) of type ASN_OBJECT_ID to table nlmLogTable\n"));    netsnmp_table_set_add_default_row(nlmLogTable,                                      COLUMN_NLMLOGENGINETDOMAIN,                                      ASN_OBJECT_ID, 0, NULL, 0);    /*     * adding column nlmLogContextEngineID of type ASN_OCTET_STR and     * access of ReadOnly      */    DEBUGMSGTL(("initialize_table_nlmLogTable",                "adding column nlmLogContextEngineID (#7) of type ASN_OCTET_STR to table nlmLogTable\n"));    netsnmp_table_set_add_default_row(nlmLogTable,                                      COLUMN_NLMLOGCONTEXTENGINEID,                                      ASN_OCTET_STR, 0, NULL, 0);    /*     * adding column nlmLogContextName of type ASN_OCTET_STR and access of      * ReadOnly      */    DEBUGMSGTL(("initialize_table_nlmLogTable",                "adding column nlmLogContextName (#8) of type ASN_OCTET_STR to table nlmLogTable\n"));    netsnmp_table_set_add_default_row(nlmLogTable,                                      COLUMN_NLMLOGCONTEXTNAME,                                      ASN_OCTET_STR, 0, NULL, 0);    /*     * adding column nlmLogNotificationID of type ASN_OBJECT_ID and access      * of ReadOnly      */    DEBUGMSGTL(("initialize_table_nlmLogTable",                "adding column nlmLogNotificationID (#9) of type ASN_OBJECT_ID to table nlmLogTable\n"));    netsnmp_table_set_add_default_row(nlmLogTable,                                      COLUMN_NLMLOGNOTIFICATIONID,                                      ASN_OBJECT_ID, 0, NULL, 0);    /*     * registering the table with the master agent      */    /*     * note: if you don't need a subhandler to deal with any aspects of     * the request, change nlmLogTable_handler to "NULL"      */    reginfo =        netsnmp_create_handler_registration("nlmLogTable", nlmLogTable_handler,                                            nlmLogTable_oid,                                            nlmLogTable_oid_len,                                            HANDLER_CAN_RWRITE);    if (NULL != context)        reginfo->contextName = strdup(context);    netsnmp_register_table_data_set(reginfo, nlmLogTable, NULL);    /*     * hmm...  5 minutes seems like a reasonable time to check for out     * dated notification logs right?      */    snmp_alarm_register(300, SA_REPEAT, check_log_size, NULL);}intnotification_log_config_handler(netsnmp_mib_handler *handler,                                netsnmp_handler_registration *reginfo,                                netsnmp_agent_request_info *reqinfo,                                netsnmp_request_info *requests){    /*     *this handler exists only to act as a trigger when the     * configuration variables get set to a value and thus     * notifications must be possibly deleted from our archives.     */    if (reqinfo->mode == MODE_SET_COMMIT)        check_log_size(0, NULL);    return SNMP_ERR_NOERROR;}voidinit_notification_log(void){    static oid      my_nlmStatsGlobalNotificationsLogged_oid[] =        { 1, 3, 6, 1, 2, 1, 92, 1, 2, 1, 0 };    static oid      my_nlmStatsGlobalNotificationsBumped_oid[] =        { 1, 3, 6, 1, 2, 1, 92, 1, 2, 2, 0 };    static oid      my_nlmConfigGlobalEntryLimit_oid[] =        { 1, 3, 6, 1, 2, 1, 92, 1, 1, 1, 0 };    static oid      my_nlmConfigGlobalAgeOut_oid[] =        { 1, 3, 6, 1, 2, 1, 92, 1, 1, 2, 0 };    char * context;    char * apptype;    context = netsnmp_ds_get_string(NETSNMP_DS_APPLICATION_ID,                                     NETSNMP_DS_NOTIF_LOG_CTX);    DEBUGMSGTL(("notification_log", "registering with '%s' context\n",                   SNMP_STRORNULL(context)));    /*     * static variables      */    netsnmp_register_read_only_counter32_instance_context        ("nlmStatsGlobalNotificationsLogged",         my_nlmStatsGlobalNotificationsLogged_oid,         OID_LENGTH(my_nlmStatsGlobalNotificationsLogged_oid),         &num_received, NULL, context);    netsnmp_register_read_only_counter32_instance_context        ("nlmStatsGlobalNotificationsBumped",         my_nlmStatsGlobalNotificationsBumped_oid,         OID_LENGTH(my_nlmStatsGlobalNotificationsBumped_oid),         &num_deleted, NULL, context);    netsnmp_register_ulong_instance_context("nlmConfigGlobalEntryLimit",                                            my_nlmConfigGlobalEntryLimit_oid,                                            OID_LENGTH                                            (my_nlmConfigGlobalEntryLimit_oid),                                            &max_logged,                                            notification_log_config_handler,                                            context);    netsnmp_register_ulong_instance_context("nlmConfigGlobalAgeOut",                                            my_nlmConfigGlobalAgeOut_oid,                                            OID_LENGTH                                            (my_nlmConfigGlobalAgeOut_oid),                                            &max_age,                                            notification_log_config_handler,                                            context);    /*     * tables      */    initialize_table_nlmLogVariableTable(context);    initialize_table_nlmLogTable(context);    /*     * disable flag      */    apptype = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID,                                     NETSNMP_DS_LIB_APPTYPE);    netsnmp_ds_register_config(ASN_BOOLEAN, apptype, "dontRetainLogs",                               NETSNMP_DS_APPLICATION_ID,                               NETSNMP_DS_AGENT_DONT_RETAIN_NOTIFICATIONS);    netsnmp_ds_register_config(ASN_BOOLEAN, apptype, "doNotRetainNotificationLogs",                               NETSNMP_DS_APPLICATION_ID,                               NETSNMP_DS_AGENT_DONT_RETAIN_NOTIFICATIONS);#if 0    /* xxx-rks: config for max size; should be peristent too, & tied to mib */    netsnmp_ds_register_config(ASN_INTEGER,                               netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID,                                                      NETSNMP_DS_LIB_APPTYPE),                               "notificationLogMax",                               NETSNMP_DS_APPLICATION_ID,                               NETSNMP_DS_AGENT_NOTIF_LOG_MAX);#endif}voidshutdown_notification_log(void){    max_logged = 0;    check_log_size(0, NULL);}voidlog_notification(netsnmp_pdu *pdu, netsnmp_transport *transport){    long            tmpl;    struct timeval  now;    netsnmp_table_row *row;    static u_long   default_num = 0;    static oid      snmptrapoid[] = { 1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0 };    size_t          snmptrapoid_len = OID_LENGTH(snmptrapoid);    netsnmp_variable_list *vptr;    u_char         *logdate;    size_t          logdate_size;    time_t          timetnow;    u_long          vbcount = 0;    u_long          tmpul;    int             col;    if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,                               NETSNMP_DS_APP_DONT_LOG)) {        return;    }    DEBUGMSGTL(("notification_log", "logging something\n"));    row = netsnmp_create_table_data_row();    ++num_received;    default_num++;    /*     * indexes to the table      */    netsnmp_table_row_add_index(row, ASN_OCTET_STR, "default",                                strlen("default"));    netsnmp_table_row_add_index(row, ASN_UNSIGNED, &default_num,                                sizeof(default_num));    /*     * add the data      */    gettimeofday(&now, NULL);    tmpl = netsnmp_timeval_uptime(&now);    netsnmp_set_row_column(row, COLUMN_NLMLOGTIME, ASN_TIMETICKS,                           (u_char *) & tmpl, sizeof(tmpl));    time(&timetnow);    logdate = date_n_time(&timetnow, &logdate_size);    netsnmp_set_row_column(row, COLUMN_NLMLOGDATEANDTIME, ASN_OCTET_STR,                           logdate, logdate_size);    netsnmp_set_row_column(row, COLUMN_NLMLOGENGINEID, ASN_OCTET_STR,                           pdu->securityEngineID,                           pdu->securityEngineIDLen);    if (transport && transport->domain == netsnmpUDPDomain) {        /*         * check for the udp domain          */        struct sockaddr_in *addr =            (struct sockaddr_in *) pdu->transport_data;        if (addr) {            char            buf[sizeof(in_addr_t) +                                sizeof(addr->sin_port)];            in_addr_t       locaddr = htonl(addr->sin_addr.s_addr);            u_short         portnum = htons(addr->sin_port);            memcpy(buf, &locaddr, sizeof(in_addr_t));            memcpy(buf + sizeof(in_addr_t), &portnum,                   sizeof(addr->sin_port));            netsnmp_set_row_column(row, COLUMN_NLMLOGENGINETADDRESS,                                   ASN_OCTET_STR, buf,                                   sizeof(in_addr_t) +                                   sizeof(addr->sin_port));        }    }    if (transport)        netsnmp_set_row_column(row, COLUMN_NLMLOGENGINETDOMAIN,                                     ASN_OBJECT_ID,                                     (const u_char *) transport->domain,                                     sizeof(oid) * transport->domain_length);    netsnmp_set_row_column(row, COLUMN_NLMLOGCONTEXTENGINEID,                           ASN_OCTET_STR, pdu->contextEngineID,                           pdu->contextEngineIDLen);    netsnmp_set_row_column(row, COLUMN_NLMLOGCONTEXTNAME, ASN_OCTET_STR,                           pdu->contextName, pdu->contextNameLen);    for (vptr = pdu->variables; vptr; vptr = vptr->next_variable) {        if (snmp_oid_compare(snmptrapoid, snmptrapoid_len,                             vptr->name, vptr->name_length) == 0) {            netsnmp_set_row_column(row, COLUMN_NLMLOGNOTIFICATIONID,                                   ASN_OBJECT_ID, vptr->val.string,                                   vptr->val_len);        } else {            netsnmp_table_row *myrow;            myrow = netsnmp_create_table_data_row();            /*             * indexes to the table              */            netsnmp_table_row_add_index(myrow, ASN_OCTET_STR, "default",                                        strlen("default"));            netsnmp_table_row_add_index(myrow, ASN_UNSIGNED, &default_num,                                        sizeof(default_num));            vbcount++;            netsnmp_table_row_add_index(myrow, ASN_UNSIGNED, &vbcount,                                        sizeof(vbcount));            /*             * OID              */            netsnmp_set_row_column(myrow, COLUMN_NLMLOGVARIABLEID,                                   ASN_OBJECT_ID, (u_char *) vptr->name,                                   vptr->name_length * sizeof(oid));            /*             * value              */            switch (vptr->type) {            case ASN_OBJECT_ID:                tmpul = 7;                col = COLUMN_NLMLOGVARIABLEOIDVAL;                break;            case ASN_INTEGER:                tmpul = 4;                col = COLUMN_NLMLOGVARIABLEINTEGER32VAL;                break;            case ASN_UNSIGNED:                tmpul = 2;                col = COLUMN_NLMLOGVARIABLEUNSIGNED32VAL;                break;            case ASN_COUNTER:                tmpul = 1;                col = COLUMN_NLMLOGVARIABLECOUNTER32VAL;                break;            case ASN_TIMETICKS:                tmpul = 3;                col = COLUMN_NLMLOGVARIABLETIMETICKSVAL;                break;            case ASN_OCTET_STR:                tmpul = 6;                col = COLUMN_NLMLOGVARIABLEOCTETSTRINGVAL;                break;            default:                /*                 * unsupported                  */                DEBUGMSGTL(("notification_log",                            "skipping type %d\n", vptr->type));                continue;            }            netsnmp_set_row_column(myrow, COLUMN_NLMLOGVARIABLEVALUETYPE,                                   ASN_INTEGER, (u_char *) & tmpul,                                   sizeof(tmpul));            netsnmp_set_row_column(myrow, col, vptr->type,                                   vptr->val.string, vptr->val_len);            DEBUGMSGTL(("notification_log",                        "adding a row to the variables table\n"));            netsnmp_table_dataset_add_row(nlmLogVarTable, myrow);        }    }    /*     * store the row      */    netsnmp_table_dataset_add_row(nlmLogTable, row);    check_log_size(0, NULL);    DEBUGMSGTL(("notification_log", "done logging something\n"));}/** handles requests for the nlmLogTable table, if anything else needs to be done */intnlmLogTable_handler(netsnmp_mib_handler *handler,                    netsnmp_handler_registration *reginfo,                    netsnmp_agent_request_info *reqinfo,                    netsnmp_request_info *requests){    /*     * perform anything here that you need to do.  The requests have     * already been processed by the master table_dataset handler, but     * this gives you chance to act on the request in some other way if     * need be.      */    return SNMP_ERR_NOERROR;}/** handles requests for the nlmLogVariableTable table, if anything else needs to be done */intnlmLogVariableTable_handler(netsnmp_mib_handler *handler,                            netsnmp_handler_registration *reginfo,                            netsnmp_agent_request_info *reqinfo,                            netsnmp_request_info *requests){    /*     * perform anything here that you need to do.  The requests have     * already been processed by the master table_dataset handler, but     * this gives you chance to act on the request in some other way if     * need be.      */    return SNMP_ERR_NOERROR;}

⌨️ 快捷键说明

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