notification_log.c
上传用户:wxp200602
上传日期:2007-10-30
资源大小:4028k
文件大小:28k
源码类别:

SNMP编程

开发平台:

Unix_Linux

  1. #include <net-snmp/net-snmp-config.h>
  2. #include <sys/types.h>
  3. #if HAVE_NETINET_IN_H
  4. #include <netinet/in.h>
  5. #endif
  6. #if HAVE_NETDB_H
  7. #include <netdb.h>
  8. #endif
  9. #include <net-snmp/net-snmp-includes.h>
  10. #include <net-snmp/agent/net-snmp-agent-includes.h>
  11. #if defined(USING_AGENTX_SUBAGENT_MODULE) && !defined(SNMPTRAPD_DISABLE_AGENTX)
  12. #include <net-snmp/agent/instance.h>
  13. #include <net-snmp/agent/table.h>
  14. #include <net-snmp/agent/table_data.h>
  15. #include <net-snmp/agent/table_dataset.h>
  16. #include "snmptrapd_handlers.h"
  17. #include "snmptrapd_log.h"
  18. #include "notification_log.h"
  19. #define SNMPTRAPD_CONTEXT "snmptrapd"
  20. extern u_long   num_received;
  21. u_long          num_deleted = 0;
  22. u_long          max_logged = 1000;      /* goes against the mib default of infinite */
  23. u_long          max_age = 1440; /* 24 hours, which is the mib default */
  24. netsnmp_table_data_set *nlmLogTable;
  25. netsnmp_table_data_set *nlmLogVarTable;
  26. void
  27. check_log_size(unsigned int clientreg, void *clientarg)
  28. {
  29.     netsnmp_table_row *row, *deleterow, *tmprow, *deletevarrow;
  30.     netsnmp_table_data_set_storage *data;
  31.     u_long          count = 0;
  32.     struct timeval  now;
  33.     long            tmpl;
  34.     gettimeofday(&now, NULL);
  35.     tmpl = netsnmp_timeval_uptime(&now);
  36.     if (!nlmLogTable || !nlmLogTable->table )  {
  37.         DEBUGMSGTL(("notification_log", "missing log tablen"));
  38.         return;
  39.     }
  40.     for (row = nlmLogTable->table->first_row; row; row = row->next) {
  41.         /*
  42.          * check max allowed count 
  43.          */
  44.         count++;
  45.         if (max_logged && count == max_logged)
  46.             break;
  47.         /*
  48.          * check max age 
  49.          */
  50.         data = (netsnmp_table_data_set_storage *) row->data;
  51.         data = netsnmp_table_data_set_find_column(data, COLUMN_NLMLOGTIME);
  52.         if (max_age && tmpl > (long)(*(data->data.integer) + max_age * 100 * 60))
  53.             break;
  54.     }
  55.     if (!row)
  56.         return;
  57.     /*
  58.      * we've reached the limit, so keep looping but start deleting
  59.      * from the beginning 
  60.      */
  61.     for (deleterow = nlmLogTable->table->first_row, row = row->next; row;
  62.          row = row->next) {
  63.         DEBUGMSGTL(("notification_log", "deleting a log entryn"));
  64.         /*
  65.          * delete contained varbinds 
  66.          */
  67.         for (deletevarrow = nlmLogVarTable->table->first_row;
  68.              deletevarrow; deletevarrow = tmprow) {
  69.             tmprow = deletevarrow->next;
  70.             if (deleterow->index_oid_len ==
  71.                 deletevarrow->index_oid_len - 1 &&
  72.                 snmp_oid_compare(deleterow->index_oid,
  73.                                  deleterow->index_oid_len,
  74.                                  deletevarrow->index_oid,
  75.                                  deleterow->index_oid_len) == 0) {
  76.                 netsnmp_table_dataset_remove_and_delete_row(nlmLogVarTable,
  77.                                                             deletevarrow);
  78.             }
  79.         }
  80.         /*
  81.          * delete the master row 
  82.          */
  83.         tmprow = deleterow->next;
  84.         netsnmp_table_dataset_remove_and_delete_row(nlmLogTable,
  85.                                                     deleterow);
  86.         deleterow = tmprow;
  87.         num_deleted++;
  88.         /*
  89.          * XXX: delete vars from its table 
  90.          */
  91.     }
  92. }
  93. /** Initialize the nlmLogVariableTable table by defining its contents and how it's structured */
  94. void
  95. initialize_table_nlmLogVariableTable(void)
  96. {
  97.     static oid      nlmLogVariableTable_oid[] =
  98.         { 1, 3, 6, 1, 2, 1, 92, 1, 3, 2 };
  99.     size_t          nlmLogVariableTable_oid_len =
  100.         OID_LENGTH(nlmLogVariableTable_oid);
  101.     netsnmp_table_data_set *table_set;
  102.     netsnmp_handler_registration *reginfo;
  103.     /*
  104.      * create the table structure itself 
  105.      */
  106.     table_set = netsnmp_create_table_data_set("nlmLogVariableTable");
  107.     nlmLogVarTable = table_set;
  108.     nlmLogVarTable->table->store_indexes = 1;
  109.     /***************************************************
  110.      * Adding indexes
  111.      */
  112.     /*
  113.      * declaring the nlmLogName index
  114.      */
  115.     DEBUGMSGTL(("initialize_table_nlmLogVariableTable",
  116.                 "adding index nlmLogName of type ASN_OCTET_STR to table nlmLogVariableTablen"));
  117.     netsnmp_table_dataset_add_index(table_set, ASN_OCTET_STR);
  118.     /*
  119.      * declaring the nlmLogIndex index
  120.      */
  121.     DEBUGMSGTL(("initialize_table_nlmLogVariableTable",
  122.                 "adding index nlmLogIndex of type ASN_UNSIGNED to table nlmLogVariableTablen"));
  123.     netsnmp_table_dataset_add_index(table_set, ASN_UNSIGNED);
  124.     /*
  125.      * declaring the nlmLogVariableIndex index
  126.      */
  127.     DEBUGMSGTL(("initialize_table_nlmLogVariableTable",
  128.                 "adding index nlmLogVariableIndex of type ASN_UNSIGNED to table nlmLogVariableTablen"));
  129.     netsnmp_table_dataset_add_index(table_set, ASN_UNSIGNED);
  130.     /*
  131.      * adding column nlmLogVariableIndex of type ASN_UNSIGNED and access
  132.      * of NoAccess 
  133.      */
  134.     DEBUGMSGTL(("initialize_table_nlmLogVariableTable",
  135.                 "adding column nlmLogVariableIndex (#1) of type ASN_UNSIGNED to table nlmLogVariableTablen"));
  136.     netsnmp_table_set_add_default_row(table_set,
  137.                                       COLUMN_NLMLOGVARIABLEINDEX,
  138.                                       ASN_UNSIGNED, 0, NULL, 0);
  139.     /*
  140.      * adding column nlmLogVariableID of type ASN_OBJECT_ID and access of
  141.      * ReadOnly 
  142.      */
  143.     DEBUGMSGTL(("initialize_table_nlmLogVariableTable",
  144.                 "adding column nlmLogVariableID (#2) of type ASN_OBJECT_ID to table nlmLogVariableTablen"));
  145.     netsnmp_table_set_add_default_row(table_set, COLUMN_NLMLOGVARIABLEID,
  146.                                       ASN_OBJECT_ID, 0, NULL, 0);
  147.     /*
  148.      * adding column nlmLogVariableValueType of type ASN_INTEGER and
  149.      * access of ReadOnly 
  150.      */
  151.     DEBUGMSGTL(("initialize_table_nlmLogVariableTable",
  152.                 "adding column nlmLogVariableValueType (#3) of type ASN_INTEGER to table nlmLogVariableTablen"));
  153.     netsnmp_table_set_add_default_row(table_set,
  154.                                       COLUMN_NLMLOGVARIABLEVALUETYPE,
  155.                                       ASN_INTEGER, 0, NULL, 0);
  156.     /*
  157.      * adding column nlmLogVariableCounter32Val of type ASN_COUNTER and
  158.      * access of ReadOnly 
  159.      */
  160.     DEBUGMSGTL(("initialize_table_nlmLogVariableTable",
  161.                 "adding column nlmLogVariableCounter32Val (#4) of type ASN_COUNTER to table nlmLogVariableTablen"));
  162.     netsnmp_table_set_add_default_row(table_set,
  163.                                       COLUMN_NLMLOGVARIABLECOUNTER32VAL,
  164.                                       ASN_COUNTER, 0, NULL, 0);
  165.     /*
  166.      * adding column nlmLogVariableUnsigned32Val of type ASN_UNSIGNED and
  167.      * access of ReadOnly 
  168.      */
  169.     DEBUGMSGTL(("initialize_table_nlmLogVariableTable",
  170.                 "adding column nlmLogVariableUnsigned32Val (#5) of type ASN_UNSIGNED to table nlmLogVariableTablen"));
  171.     netsnmp_table_set_add_default_row(table_set,
  172.                                       COLUMN_NLMLOGVARIABLEUNSIGNED32VAL,
  173.                                       ASN_UNSIGNED, 0, NULL, 0);
  174.     /*
  175.      * adding column nlmLogVariableTimeTicksVal of type ASN_TIMETICKS and
  176.      * access of ReadOnly 
  177.      */
  178.     DEBUGMSGTL(("initialize_table_nlmLogVariableTable",
  179.                 "adding column nlmLogVariableTimeTicksVal (#6) of type ASN_TIMETICKS to table nlmLogVariableTablen"));
  180.     netsnmp_table_set_add_default_row(table_set,
  181.                                       COLUMN_NLMLOGVARIABLETIMETICKSVAL,
  182.                                       ASN_TIMETICKS, 0, NULL, 0);
  183.     /*
  184.      * adding column nlmLogVariableInteger32Val of type ASN_INTEGER and
  185.      * access of ReadOnly 
  186.      */
  187.     DEBUGMSGTL(("initialize_table_nlmLogVariableTable",
  188.                 "adding column nlmLogVariableInteger32Val (#7) of type ASN_INTEGER to table nlmLogVariableTablen"));
  189.     netsnmp_table_set_add_default_row(table_set,
  190.                                       COLUMN_NLMLOGVARIABLEINTEGER32VAL,
  191.                                       ASN_INTEGER, 0, NULL, 0);
  192.     /*
  193.      * adding column nlmLogVariableOctetStringVal of type ASN_OCTET_STR
  194.      * and access of ReadOnly 
  195.      */
  196.     DEBUGMSGTL(("initialize_table_nlmLogVariableTable",
  197.                 "adding column nlmLogVariableOctetStringVal (#8) of type ASN_OCTET_STR to table nlmLogVariableTablen"));
  198.     netsnmp_table_set_add_default_row(table_set,
  199.                                       COLUMN_NLMLOGVARIABLEOCTETSTRINGVAL,
  200.                                       ASN_OCTET_STR, 0, NULL, 0);
  201.     /*
  202.      * adding column nlmLogVariableIpAddressVal of type ASN_IPADDRESS and
  203.      * access of ReadOnly 
  204.      */
  205.     DEBUGMSGTL(("initialize_table_nlmLogVariableTable",
  206.                 "adding column nlmLogVariableIpAddressVal (#9) of type ASN_IPADDRESS to table nlmLogVariableTablen"));
  207.     netsnmp_table_set_add_default_row(table_set,
  208.                                       COLUMN_NLMLOGVARIABLEIPADDRESSVAL,
  209.                                       ASN_IPADDRESS, 0, NULL, 0);
  210.     /*
  211.      * adding column nlmLogVariableOidVal of type ASN_OBJECT_ID and access 
  212.      * of ReadOnly 
  213.      */
  214.     DEBUGMSGTL(("initialize_table_nlmLogVariableTable",
  215.                 "adding column nlmLogVariableOidVal (#10) of type ASN_OBJECT_ID to table nlmLogVariableTablen"));
  216.     netsnmp_table_set_add_default_row(table_set,
  217.                                       COLUMN_NLMLOGVARIABLEOIDVAL,
  218.                                       ASN_OBJECT_ID, 0, NULL, 0);
  219.     /*
  220.      * adding column nlmLogVariableCounter64Val of type ASN_COUNTER64 and
  221.      * access of ReadOnly 
  222.      */
  223.     DEBUGMSGTL(("initialize_table_nlmLogVariableTable",
  224.                 "adding column nlmLogVariableCounter64Val (#11) of type ASN_COUNTER64 to table nlmLogVariableTablen"));
  225.     netsnmp_table_set_add_default_row(table_set,
  226.                                       COLUMN_NLMLOGVARIABLECOUNTER64VAL,
  227.                                       ASN_COUNTER64, 0, NULL, 0);
  228.     /*
  229.      * adding column nlmLogVariableOpaqueVal of type ASN_OPAQUE and access 
  230.      * of ReadOnly 
  231.      */
  232.     DEBUGMSGTL(("initialize_table_nlmLogVariableTable",
  233.                 "adding column nlmLogVariableOpaqueVal (#12) of type ASN_OPAQUE to table nlmLogVariableTablen"));
  234.     netsnmp_table_set_add_default_row(table_set,
  235.                                       COLUMN_NLMLOGVARIABLEOPAQUEVAL,
  236.                                       ASN_OPAQUE, 0, NULL, 0);
  237.     /*
  238.      * registering the table with the master agent 
  239.      */
  240.     /*
  241.      * note: if you don't need a subhandler to deal with any aspects of
  242.      * the request, change nlmLogVariableTable_handler to "NULL" 
  243.      */
  244.     reginfo =
  245.         netsnmp_create_handler_registration ("nlmLogVariableTable",
  246.                                              nlmLogVariableTable_handler,
  247.                                              nlmLogVariableTable_oid,
  248.                                              nlmLogVariableTable_oid_len,
  249.                                              HANDLER_CAN_RWRITE);
  250.     reginfo->contextName = strdup(SNMPTRAPD_CONTEXT);
  251.     netsnmp_register_table_data_set(reginfo, table_set, NULL);
  252. }
  253. /** Initialize the nlmLogTable table by defining its contents and how it's structured */
  254. void
  255. initialize_table_nlmLogTable(void)
  256. {
  257.     static oid      nlmLogTable_oid[] = { 1, 3, 6, 1, 2, 1, 92, 1, 3, 1 };
  258.     size_t          nlmLogTable_oid_len = OID_LENGTH(nlmLogTable_oid);
  259.     netsnmp_handler_registration *reginfo;
  260.     /*
  261.      * create the table structure itself 
  262.      */
  263.     nlmLogTable = netsnmp_create_table_data_set("nlmLogTable");
  264.     /***************************************************
  265.      * Adding indexes
  266.      */
  267.     /*
  268.      * declaring the nlmLogIndex index
  269.      */
  270.     DEBUGMSGTL(("initialize_table_nlmLogTable",
  271.                 "adding index nlmLogName of type ASN_OCTET_STR to table nlmLogTablen"));
  272.     netsnmp_table_dataset_add_index(nlmLogTable, ASN_OCTET_STR);
  273.     DEBUGMSGTL(("initialize_table_nlmLogTable",
  274.                 "adding index nlmLogIndex of type ASN_UNSIGNED to table nlmLogTablen"));
  275.     netsnmp_table_dataset_add_index(nlmLogTable, ASN_UNSIGNED);
  276.     /*
  277.      * adding column nlmLogTime of type ASN_TIMETICKS and access of
  278.      * ReadOnly 
  279.      */
  280.     DEBUGMSGTL(("initialize_table_nlmLogTable",
  281.                 "adding column nlmLogTime (#2) of type ASN_TIMETICKS to table nlmLogTablen"));
  282.     netsnmp_table_set_add_default_row(nlmLogTable, COLUMN_NLMLOGTIME,
  283.                                       ASN_TIMETICKS, 0, NULL, 0);
  284.     /*
  285.      * adding column nlmLogDateAndTime of type ASN_OCTET_STR and access of 
  286.      * ReadOnly 
  287.      */
  288.     DEBUGMSGTL(("initialize_table_nlmLogTable",
  289.                 "adding column nlmLogDateAndTime (#3) of type ASN_OCTET_STR to table nlmLogTablen"));
  290.     netsnmp_table_set_add_default_row(nlmLogTable,
  291.                                       COLUMN_NLMLOGDATEANDTIME,
  292.                                       ASN_OCTET_STR, 0, NULL, 0);
  293.     /*
  294.      * adding column nlmLogEngineID of type ASN_OCTET_STR and access of
  295.      * ReadOnly 
  296.      */
  297.     DEBUGMSGTL(("initialize_table_nlmLogTable",
  298.                 "adding column nlmLogEngineID (#4) of type ASN_OCTET_STR to table nlmLogTablen"));
  299.     netsnmp_table_set_add_default_row(nlmLogTable, COLUMN_NLMLOGENGINEID,
  300.                                       ASN_OCTET_STR, 0, NULL, 0);
  301.     /*
  302.      * adding column nlmLogEngineTAddress of type ASN_OCTET_STR and access 
  303.      * of ReadOnly 
  304.      */
  305.     DEBUGMSGTL(("initialize_table_nlmLogTable",
  306.                 "adding column nlmLogEngineTAddress (#5) of type ASN_OCTET_STR to table nlmLogTablen"));
  307.     netsnmp_table_set_add_default_row(nlmLogTable,
  308.                                       COLUMN_NLMLOGENGINETADDRESS,
  309.                                       ASN_OCTET_STR, 0, NULL, 0);
  310.     /*
  311.      * adding column nlmLogEngineTDomain of type ASN_OBJECT_ID and access
  312.      * of ReadOnly 
  313.      */
  314.     DEBUGMSGTL(("initialize_table_nlmLogTable",
  315.                 "adding column nlmLogEngineTDomain (#6) of type ASN_OBJECT_ID to table nlmLogTablen"));
  316.     netsnmp_table_set_add_default_row(nlmLogTable,
  317.                                       COLUMN_NLMLOGENGINETDOMAIN,
  318.                                       ASN_OBJECT_ID, 0, NULL, 0);
  319.     /*
  320.      * adding column nlmLogContextEngineID of type ASN_OCTET_STR and
  321.      * access of ReadOnly 
  322.      */
  323.     DEBUGMSGTL(("initialize_table_nlmLogTable",
  324.                 "adding column nlmLogContextEngineID (#7) of type ASN_OCTET_STR to table nlmLogTablen"));
  325.     netsnmp_table_set_add_default_row(nlmLogTable,
  326.                                       COLUMN_NLMLOGCONTEXTENGINEID,
  327.                                       ASN_OCTET_STR, 0, NULL, 0);
  328.     /*
  329.      * adding column nlmLogContextName of type ASN_OCTET_STR and access of 
  330.      * ReadOnly 
  331.      */
  332.     DEBUGMSGTL(("initialize_table_nlmLogTable",
  333.                 "adding column nlmLogContextName (#8) of type ASN_OCTET_STR to table nlmLogTablen"));
  334.     netsnmp_table_set_add_default_row(nlmLogTable,
  335.                                       COLUMN_NLMLOGCONTEXTNAME,
  336.                                       ASN_OCTET_STR, 0, NULL, 0);
  337.     /*
  338.      * adding column nlmLogNotificationID of type ASN_OBJECT_ID and access 
  339.      * of ReadOnly 
  340.      */
  341.     DEBUGMSGTL(("initialize_table_nlmLogTable",
  342.                 "adding column nlmLogNotificationID (#9) of type ASN_OBJECT_ID to table nlmLogTablen"));
  343.     netsnmp_table_set_add_default_row(nlmLogTable,
  344.                                       COLUMN_NLMLOGNOTIFICATIONID,
  345.                                       ASN_OBJECT_ID, 0, NULL, 0);
  346.     /*
  347.      * registering the table with the master agent 
  348.      */
  349.     /*
  350.      * note: if you don't need a subhandler to deal with any aspects of
  351.      * the request, change nlmLogTable_handler to "NULL" 
  352.      */
  353.     reginfo =
  354.         netsnmp_create_handler_registration("nlmLogTable", nlmLogTable_handler,
  355.                                             nlmLogTable_oid,
  356.                                             nlmLogTable_oid_len,
  357.                                             HANDLER_CAN_RWRITE);
  358.     reginfo->contextName = strdup(SNMPTRAPD_CONTEXT);
  359.     netsnmp_register_table_data_set(reginfo, nlmLogTable, NULL);
  360.     /*
  361.      * hmm...  5 minutes seems like a reasonable time to check for out
  362.      * dated notification logs right? 
  363.      */
  364.     snmp_alarm_register(300, SA_REPEAT, check_log_size, NULL);
  365. }
  366. int
  367. notification_log_config_handler(netsnmp_mib_handler *handler,
  368.                                 netsnmp_handler_registration *reginfo,
  369.                                 netsnmp_agent_request_info *reqinfo,
  370.                                 netsnmp_request_info *requests)
  371. {
  372.     /*
  373.      *this handler exists only to act as a trigger when the
  374.      * configuration variables get set to a value and thus
  375.      * notifications must be possibly deleted from our archives.
  376.      */
  377.     if (reqinfo->mode == MODE_SET_COMMIT)
  378.         check_log_size(0, NULL);
  379.     return SNMP_ERR_NOERROR;
  380. }
  381. void
  382. init_notification_log(void)
  383. {
  384.     static oid      my_nlmStatsGlobalNotificationsLogged_oid[] =
  385.         { 1, 3, 6, 1, 2, 1, 92, 1, 2, 1, 0 };
  386.     static oid      my_nlmStatsGlobalNotificationsBumped_oid[] =
  387.         { 1, 3, 6, 1, 2, 1, 92, 1, 2, 2, 0 };
  388.     static oid      my_nlmConfigGlobalEntryLimit_oid[] =
  389.         { 1, 3, 6, 1, 2, 1, 92, 1, 1, 1, 0 };
  390.     static oid      my_nlmConfigGlobalAgeOut_oid[] =
  391.         { 1, 3, 6, 1, 2, 1, 92, 1, 1, 2, 0 };
  392.     /*
  393.      * static variables 
  394.      */
  395. #ifdef USING_AGENTX_SUBAGENT_MODULE
  396.     netsnmp_register_read_only_counter32_instance_context
  397.         ("nlmStatsGlobalNotificationsLogged",
  398.          my_nlmStatsGlobalNotificationsLogged_oid,
  399.          OID_LENGTH(my_nlmStatsGlobalNotificationsLogged_oid),
  400.          &num_received, NULL, SNMPTRAPD_CONTEXT);
  401.     netsnmp_register_read_only_counter32_instance_context
  402.         ("nlmStatsGlobalNotificationsBumped",
  403.          my_nlmStatsGlobalNotificationsBumped_oid,
  404.          OID_LENGTH(my_nlmStatsGlobalNotificationsBumped_oid),
  405.          &num_deleted, NULL, SNMPTRAPD_CONTEXT);
  406.     netsnmp_register_ulong_instance_context("nlmConfigGlobalEntryLimit",
  407.                                             my_nlmConfigGlobalEntryLimit_oid,
  408.                                             OID_LENGTH
  409.                                             (my_nlmConfigGlobalEntryLimit_oid),
  410.                                             &max_logged,
  411.                                             notification_log_config_handler,
  412.                                             SNMPTRAPD_CONTEXT);
  413.     netsnmp_register_ulong_instance_context("nlmConfigGlobalAgeOut",
  414.                                             my_nlmConfigGlobalAgeOut_oid,
  415.                                             OID_LENGTH
  416.                                             (my_nlmConfigGlobalAgeOut_oid),
  417.                                             &max_age,
  418.                                             notification_log_config_handler,
  419.                                             SNMPTRAPD_CONTEXT);
  420.     /*
  421.      * tables 
  422.      */
  423.     initialize_table_nlmLogVariableTable();
  424.     initialize_table_nlmLogTable();
  425.     /*
  426.      * disable flag 
  427.      */
  428.     netsnmp_ds_register_config(ASN_BOOLEAN, "snmptrapd", "dontRetainLogs",
  429.    NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_APP_DONT_LOG);
  430. #endif /* USING_AGENTX_SUBAGENT_MODULE */
  431. }
  432. u_long          default_num = 0;
  433. void
  434. log_notification(struct hostent *host, netsnmp_pdu *pdu,
  435.                  netsnmp_transport *transport)
  436. {
  437.     long            tmpl;
  438.     struct timeval  now;
  439.     netsnmp_table_row *row;
  440.     static oid      snmptrapoid[] = { 1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0 };
  441.     size_t          snmptrapoid_len = OID_LENGTH(snmptrapoid);
  442.     netsnmp_variable_list *vptr;
  443.     u_char         *logdate;
  444.     size_t          logdate_size;
  445.     time_t          timetnow;
  446.     u_long          vbcount = 0;
  447.     u_long          tmpul;
  448.     int             col;
  449.     if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, 
  450.        NETSNMP_DS_APP_DONT_LOG)) {
  451.         return;
  452.     }
  453.     DEBUGMSGTL(("log_notification", "logging somethingn"));
  454.     row = netsnmp_create_table_data_row();
  455.     default_num++;
  456.     /*
  457.      * indexes to the table 
  458.      */
  459.     netsnmp_table_row_add_index(row, ASN_OCTET_STR, "default",
  460.                                 strlen("default"));
  461.     netsnmp_table_row_add_index(row, ASN_UNSIGNED, &default_num,
  462.                                 sizeof(default_num));
  463.     /*
  464.      * add the data 
  465.      */
  466.     gettimeofday(&now, NULL);
  467.     tmpl = netsnmp_timeval_uptime(&now);
  468.     netsnmp_set_row_column(row, COLUMN_NLMLOGTIME, ASN_TIMETICKS,
  469.                            (u_char *) & tmpl, sizeof(tmpl));
  470.     time(&timetnow);
  471.     logdate = date_n_time(&timetnow, &logdate_size);
  472.     netsnmp_set_row_column(row, COLUMN_NLMLOGDATEANDTIME, ASN_OCTET_STR,
  473.                            logdate, logdate_size);
  474.     netsnmp_set_row_column(row, COLUMN_NLMLOGENGINEID, ASN_OCTET_STR,
  475.                            pdu->securityEngineID,
  476.                            pdu->securityEngineIDLen);
  477.     if (transport && transport->domain == netsnmpUDPDomain) {
  478.         /*
  479.          * lame way to check for the udp domain 
  480.          */
  481.         /*
  482.          * no, it is the correct way to do it -- jbpn 
  483.          */
  484.         struct sockaddr_in *addr =
  485.             (struct sockaddr_in *) pdu->transport_data;
  486.         if (addr) {
  487.             char            buf[sizeof(in_addr_t) +
  488.                                 sizeof(addr->sin_port)];
  489.             in_addr_t       locaddr = htonl(addr->sin_addr.s_addr);
  490.             u_short         portnum = htons(addr->sin_port);
  491.             memcpy(buf, &locaddr, sizeof(in_addr_t));
  492.             memcpy(buf + sizeof(in_addr_t), &portnum,
  493.                    sizeof(addr->sin_port));
  494.             netsnmp_set_row_column(row, COLUMN_NLMLOGENGINETADDRESS,
  495.                                    ASN_OCTET_STR, buf,
  496.                                    sizeof(in_addr_t) +
  497.                                    sizeof(addr->sin_port));
  498.         }
  499.     }
  500.     netsnmp_set_row_column(row, COLUMN_NLMLOGENGINETDOMAIN, ASN_OBJECT_ID,
  501.                            (const u_char *) transport->domain,
  502.                            sizeof(oid) * transport->domain_length);
  503.     netsnmp_set_row_column(row, COLUMN_NLMLOGCONTEXTENGINEID,
  504.                            ASN_OCTET_STR, pdu->contextEngineID,
  505.                            pdu->contextEngineIDLen);
  506.     netsnmp_set_row_column(row, COLUMN_NLMLOGCONTEXTNAME, ASN_OCTET_STR,
  507.                            pdu->contextName, pdu->contextNameLen);
  508.     for (vptr = pdu->variables; vptr; vptr = vptr->next_variable) {
  509.         if (snmp_oid_compare(snmptrapoid, snmptrapoid_len,
  510.                              vptr->name, vptr->name_length) == 0) {
  511.             netsnmp_set_row_column(row, COLUMN_NLMLOGNOTIFICATIONID,
  512.                                    ASN_OBJECT_ID, vptr->val.string,
  513.                                    vptr->val_len);
  514.         } else {
  515.             netsnmp_table_row *myrow;
  516.             myrow = netsnmp_create_table_data_row();
  517.             /*
  518.              * indexes to the table 
  519.              */
  520.             netsnmp_table_row_add_index(myrow, ASN_OCTET_STR, "default",
  521.                                         strlen("default"));
  522.             netsnmp_table_row_add_index(myrow, ASN_UNSIGNED, &default_num,
  523.                                         sizeof(default_num));
  524.             vbcount++;
  525.             netsnmp_table_row_add_index(myrow, ASN_UNSIGNED, &vbcount,
  526.                                         sizeof(vbcount));
  527.             /*
  528.              * OID 
  529.              */
  530.             netsnmp_set_row_column(myrow, COLUMN_NLMLOGVARIABLEID,
  531.                                    ASN_OBJECT_ID, (u_char *) vptr->name,
  532.                                    vptr->name_length * sizeof(oid));
  533.             /*
  534.              * value 
  535.              */
  536.             switch (vptr->type) {
  537.             case ASN_OBJECT_ID:
  538.                 tmpul = 7;
  539.                 col = COLUMN_NLMLOGVARIABLEOIDVAL;
  540.                 break;
  541.             case ASN_INTEGER:
  542.                 tmpul = 4;
  543.                 col = COLUMN_NLMLOGVARIABLEINTEGER32VAL;
  544.                 break;
  545.             case ASN_UNSIGNED:
  546.                 tmpul = 2;
  547.                 col = COLUMN_NLMLOGVARIABLEUNSIGNED32VAL;
  548.                 break;
  549.             case ASN_COUNTER:
  550.                 tmpul = 1;
  551.                 col = COLUMN_NLMLOGVARIABLECOUNTER32VAL;
  552.                 break;
  553.             case ASN_TIMETICKS:
  554.                 tmpul = 3;
  555.                 col = COLUMN_NLMLOGVARIABLETIMETICKSVAL;
  556.                 break;
  557.             case ASN_OCTET_STR:
  558.                 tmpul = 6;
  559.                 col = COLUMN_NLMLOGVARIABLEOCTETSTRINGVAL;
  560.                 break;
  561.             default:
  562.                 /*
  563.                  * unsupported 
  564.                  */
  565.                 DEBUGMSGTL(("log_notification",
  566.                             "skipping type %dn", vptr->type));
  567.                 continue;
  568.             }
  569.             netsnmp_set_row_column(myrow, COLUMN_NLMLOGVARIABLEVALUETYPE,
  570.                                    ASN_INTEGER, (u_char *) & tmpul,
  571.                                    sizeof(tmpul));
  572.             netsnmp_set_row_column(myrow, col, vptr->type,
  573.                                    vptr->val.string, vptr->val_len);
  574.             DEBUGMSGTL(("log_notification",
  575.                         "adding a row to the variables tablen"));
  576.             netsnmp_table_dataset_add_row(nlmLogVarTable, myrow);
  577.         }
  578.     }
  579.     /*
  580.      * store the row 
  581.      */
  582.     netsnmp_table_dataset_add_row(nlmLogTable, row);
  583.     check_log_size(0, NULL);
  584.     DEBUGMSGTL(("log_notification", "done logging somethingn"));
  585. }
  586. /** handles requests for the nlmLogTable table, if anything else needs to be done */
  587. int
  588. nlmLogTable_handler(netsnmp_mib_handler *handler,
  589.                     netsnmp_handler_registration *reginfo,
  590.                     netsnmp_agent_request_info *reqinfo,
  591.                     netsnmp_request_info *requests)
  592. {
  593.     /*
  594.      * perform anything here that you need to do.  The requests have
  595.      * already been processed by the master table_dataset handler, but
  596.      * this gives you chance to act on the request in some other way if
  597.      * need be. 
  598.      */
  599.     return SNMP_ERR_NOERROR;
  600. }
  601. /** handles requests for the nlmLogVariableTable table, if anything else needs to be done */
  602. int
  603. nlmLogVariableTable_handler(netsnmp_mib_handler *handler,
  604.                             netsnmp_handler_registration *reginfo,
  605.                             netsnmp_agent_request_info *reqinfo,
  606.                             netsnmp_request_info *requests)
  607. {
  608.     /*
  609.      * perform anything here that you need to do.  The requests have
  610.      * already been processed by the master table_dataset handler, but
  611.      * this gives you chance to act on the request in some other way if
  612.      * need be. 
  613.      */
  614.     return SNMP_ERR_NOERROR;
  615. }
  616. /*
  617.  *  "Notification" handler for implementing NOTIFICATION-MIB
  618.  *   (presumably)
  619.  */
  620. int   notification_handler(netsnmp_pdu           *pdu,
  621.                            netsnmp_transport     *transport,
  622.                            netsnmp_trapd_handler *handler)
  623. {
  624.     struct hostent *host = NULL;
  625.     DEBUGMSGTL(( "snmptrapd", "notification_handlern"));
  626.         if (!netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, 
  627. NETSNMP_DS_APP_NUMERIC_IP)) {
  628.             /*
  629.              * Right, apparently a name lookup is wanted.  This is only
  630.              * reasonable for the UDP and TCP transport domains (we
  631.              * don't want to try to be too clever here).  
  632.              */
  633.             if (transport != NULL
  634.                 && (transport->domain == netsnmpUDPDomain
  635. #ifdef SNMP_TRANSPORT_TCP_DOMAIN
  636.                     || transport->domain == netsnmp_snmpTCPDomain
  637. #endif
  638. )) {
  639.                 /*
  640.                  * This is kind of bletcherous -- it breaks the opacity of
  641.                  * transport_data but never mind -- the alternative is a
  642.                  * lot of munging strings from f_fmtaddr.
  643.                  */
  644.                 struct sockaddr_in *addr =
  645.                     (struct sockaddr_in *) pdu->transport_data;
  646.                 if (addr != NULL && 
  647.     pdu->transport_data_length == sizeof(struct sockaddr_in)) {
  648.                     host = gethostbyaddr((char *) &(addr->sin_addr),
  649.      sizeof(struct in_addr), AF_INET);
  650.                 }
  651.             }
  652.         }
  653.     log_notification(host, pdu, transport);
  654.     return NETSNMPTRAPD_HANDLER_OK;
  655. }
  656. #endif /* USING_AGENTX_SUBAGENT_MODULE && !SNMPTRAPD_DISABLE_AGENTX */