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

SNMP编程

开发平台:

Unix_Linux

  1. /*
  2.  * Note: this file originally auto-generated by mib2c using
  3.  *        : mib2c.create-dataset.conf,v 5.2 2002/07/17 14:41:53 dts12 Exp $
  4.  */
  5. #include <net-snmp/net-snmp-config.h>
  6. #include <net-snmp/net-snmp-includes.h>
  7. #include <net-snmp/agent/net-snmp-agent-includes.h>
  8. #include "mteTriggerTable.h"
  9. #include "mteEventTable.h"
  10. #include "mteEventNotificationTable.h"
  11. #include "mteObjectsTable.h"
  12. static netsnmp_table_data_set *table_set = NULL;
  13. /** Initialize the mteEventTable table by defining its contents and how it's structured */
  14. void
  15. initialize_table_mteEventTable(void)
  16. {
  17.     static oid      mteEventTable_oid[] =
  18.         { 1, 3, 6, 1, 2, 1, 88, 1, 4, 2 };
  19.     size_t          mteEventTable_oid_len = OID_LENGTH(mteEventTable_oid);
  20.     /*
  21.      * create the table structure itself 
  22.      */
  23.     table_set = netsnmp_create_table_data_set("mteEventTable");
  24.     /*
  25.      * comment this out or delete if you don't support creation of new rows 
  26.      */
  27.     table_set->allow_creation = 1;
  28.     /* mark the row status column */
  29.     table_set->rowstatus_column = COLUMN_MTEEVENTENTRYSTATUS;
  30.     /***************************************************
  31.      * Adding indexes
  32.      */
  33.     DEBUGMSGTL(("initialize_table_mteEventTable",
  34.                 "adding indexes to table mteEventTablen"));
  35.     netsnmp_table_set_add_indexes(table_set,
  36.                                   ASN_OCTET_STR,   /* index: mteOwner */
  37.                                   ASN_PRIV_IMPLIED_OCTET_STR, /* index: mteEventName */
  38.                                   0);
  39.     DEBUGMSGTL(("initialize_table_mteEventTable",
  40.                 "adding column types to table mteEventTablen"));
  41.     netsnmp_table_set_multi_add_default_row(table_set,
  42.                                             COLUMN_MTEEVENTNAME,
  43.                                             ASN_OCTET_STR, 0, NULL, 0,
  44.                                             COLUMN_MTEEVENTCOMMENT,
  45.                                             ASN_OCTET_STR, 1, NULL, 0,
  46.                                             COLUMN_MTEEVENTACTIONS,
  47.                                             ASN_OCTET_STR, 1, NULL, 0,
  48.                                             COLUMN_MTEEVENTENABLED,
  49.                                             ASN_INTEGER, 1, NULL, 0,
  50.                                             COLUMN_MTEEVENTENTRYSTATUS,
  51.                                             ASN_INTEGER, 1, NULL, 0, 0);
  52.     /* keep index values around for comparisons later */
  53.     table_set->table->store_indexes = 1;
  54.     /*
  55.      * registering the table with the master agent 
  56.      */
  57.     /*
  58.      * note: if you don't need a subhandler to deal with any aspects
  59.      * of the request, change mteEventTable_handler to "NULL" 
  60.      */
  61.     netsnmp_register_table_data_set(netsnmp_create_handler_registration
  62.                                     ("mteEventTable",
  63.                                      mteEventTable_handler,
  64.                                      mteEventTable_oid,
  65.                                      mteEventTable_oid_len,
  66.                                      HANDLER_CAN_RWRITE), table_set, NULL);
  67. }
  68. /** Initializes the mteEventTable module */
  69. void
  70. init_mteEventTable(void)
  71. {
  72.     /*
  73.      * here we initialize all the tables we're planning on supporting 
  74.      */
  75.     initialize_table_mteEventTable();
  76.     snmpd_register_config_handler("notificationEvent", parse_notificationEvent,
  77.                                   NULL,
  78.                                   "notificationEvent NAME TRAP_OID [[-w] EXTRA_OID ...]");
  79.     snmpd_register_config_handler("linkUpDownNotifications",
  80.                                   parse_linkUpDownNotifications,
  81.                                   NULL,
  82.                                   "linkUpDownNotifications (yes|no)");
  83. }
  84. /** handles requests for the mteEventTable table, if anything else needs to be done */
  85. int
  86. mteEventTable_handler(netsnmp_mib_handler *handler,
  87.                       netsnmp_handler_registration *reginfo,
  88.                       netsnmp_agent_request_info *reqinfo,
  89.                       netsnmp_request_info *requests)
  90. {
  91.     /*
  92.      * perform anything here that you need to do.  The requests have
  93.      * already been processed by the master table_dataset handler, but
  94.      * this gives you chance to act on the request in some other way
  95.      * if need be. 
  96.      */
  97.     /* XXX: on rowstatus = destroy, remove the corresponding rows from the
  98.        other tables: snmpEventNotificationTable and the set table */
  99.     return SNMP_ERR_NOERROR;
  100. }
  101. void
  102. parse_linkUpDownNotifications(const char *token, char *line) {
  103.     if (strncmp(line, "y", 1) == 0) {
  104.         parse_notificationEvent("notificationEvent", "linkUpTrap     linkUp     ifIndex ifAdminStatus ifOperStatus");
  105.         parse_notificationEvent("notificationEvent", "linkDownTrap   linkDown   ifIndex ifAdminStatus ifOperStatus");
  106.         parse_simple_monitor("monitor", "-r 60 -e linkUpTrap "Generate linkUp" ifOperStatus != 2");
  107.         parse_simple_monitor("monitor", "-r 60 -e linkDownTrap "Generate linkDown" ifOperStatus == 2");
  108.     }
  109. }
  110. void
  111. parse_notificationEvent(const char *token, char *line) {
  112.     char            name_buf[64];
  113.     char            oid_name_buf[SPRINT_MAX_LEN];
  114.     oid             oid_buf[MAX_OID_LEN];
  115.     size_t          oid_buf_len = sizeof(oid_buf);
  116.     int             wild = 1;
  117.     netsnmp_table_row *row;
  118.     long tlong;
  119.     char tc;
  120.     /* get the owner */
  121.     const char *owner = "snmpd.conf";
  122.     /* get the name */
  123.     char *cp = copy_nword(line, name_buf, SPRINT_MAX_LEN);
  124.     if (!cp || name_buf[0] == '') {
  125.         config_perror("syntax error.");
  126.         return;
  127.     }
  128.     for(row = table_set->table->first_row; row; row = row->next) {
  129.         if (strcmp(row->indexes->val.string, owner) == 0 &&
  130.             strcmp(row->indexes->next_variable->val.string,
  131.                    name_buf) == 0) {
  132.             config_perror("An eventd by that name has already been defined.");
  133.             return;
  134.         }
  135.     }
  136.     /* now, get all the trap oid */
  137.     cp = copy_nword(cp, oid_name_buf, SPRINT_MAX_LEN);
  138.     if (oid_name_buf[0] == '') {
  139.         config_perror("syntax error.");
  140.         return;
  141.     }
  142.     if (!snmp_parse_oid(oid_name_buf, oid_buf, &oid_buf_len)) {
  143.         snmp_log(LOG_ERR,"namebuf: %sn",oid_name_buf);
  144.         config_perror("unable to parse trap oid");
  145.         return;
  146.     }
  147.     /*
  148.      * add to the mteEventNotificationtable to point to the
  149.      * notification and the objects.
  150.      */
  151.     row = netsnmp_create_table_data_row();
  152.     /* indexes */
  153.     netsnmp_table_row_add_index(row, ASN_OCTET_STR, owner, strlen(owner));
  154.     netsnmp_table_row_add_index(row, ASN_PRIV_IMPLIED_OCTET_STR,
  155.                                 name_buf, strlen(name_buf));
  156.     /* columns */
  157.     netsnmp_set_row_column(row, COLUMN_MTEEVENTNOTIFICATION, ASN_OBJECT_ID,
  158.                            (char *) oid_buf, oid_buf_len * sizeof(oid));
  159.     netsnmp_set_row_column(row, COLUMN_MTEEVENTNOTIFICATIONOBJECTSOWNER,
  160.                            ASN_OCTET_STR, owner, strlen(owner));
  161.     netsnmp_set_row_column(row, COLUMN_MTEEVENTNOTIFICATIONOBJECTS,
  162.                            ASN_OCTET_STR, name_buf, strlen(name_buf));
  163.     netsnmp_table_data_add_row(mteEventNotif_table_set->table, row);
  164.     /*
  165.      * add to the mteEventTable to make it a notification to trigger
  166.      * notification and the objects.
  167.      */
  168.     row = netsnmp_create_table_data_row();
  169.     /* indexes */
  170.     netsnmp_table_row_add_index(row, ASN_OCTET_STR, owner, strlen(owner));
  171.     netsnmp_table_row_add_index(row, ASN_PRIV_IMPLIED_OCTET_STR,
  172.                                 name_buf, strlen(name_buf));
  173.     /* columns */
  174.     tc = (u_char)0x80;
  175.     netsnmp_set_row_column(row, COLUMN_MTEEVENTACTIONS, ASN_OCTET_STR,
  176.                            &tc, 1);
  177.     tlong = MTETRIGGERENABLED_TRUE;
  178.     netsnmp_set_row_column(row, COLUMN_MTEEVENTENABLED,
  179.                            ASN_INTEGER, (char *) &tlong, sizeof(tlong));
  180.     tlong = RS_ACTIVE;
  181.     netsnmp_set_row_column(row, COLUMN_MTEEVENTENTRYSTATUS,
  182.                            ASN_INTEGER, (char *) &tlong, sizeof(tlong));
  183.     netsnmp_table_data_add_row(table_set->table, row);
  184.     
  185.     /*
  186.      * now all the objects to put into the trap's object row
  187.      */
  188.     while(cp) {
  189.         cp = copy_nword(cp, oid_name_buf, SPRINT_MAX_LEN);
  190.         if (strcmp(oid_name_buf, "-w") == 0) {
  191.             wild = 0;
  192.             continue;
  193.         }
  194.         if (!snmp_parse_oid(oid_name_buf, oid_buf, &oid_buf_len)) {
  195.             config_perror("unable to parse an object oid");
  196.             return;
  197.         }
  198.         mte_add_object_to_table("snmpd.conf", name_buf,
  199.                                 oid_buf, oid_buf_len, wild);
  200.         wild = 1;
  201.     }
  202. }
  203. /*
  204.  * send trap 
  205.  */
  206. void
  207. run_mte_events(struct mteTriggerTable_data *item,
  208.                oid * name_oid, size_t name_oid_len,
  209.                const char *eventobjowner, const char *eventobjname)
  210. {
  211.     static oid      objid_snmptrap[] = { 1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0 };     /* snmpTrapIOD.0 */
  212.     netsnmp_variable_list *var_list = NULL;
  213.     netsnmp_table_row *row, *notif_row;
  214.     netsnmp_table_data_set_storage *col1, *tc, *no, *noo;
  215.     for(row = table_set->table->first_row; row; row = row->next) {
  216.         if (strcmp(row->indexes->val.string, eventobjowner) == 0 &&
  217.             strcmp(row->indexes->next_variable->val.string,
  218.                    eventobjname) == 0) {
  219.             /* run this event */
  220.             col1 = (netsnmp_table_data_set_storage *) row->data;
  221.             
  222.             tc = netsnmp_table_data_set_find_column(col1,
  223.                                                     COLUMN_MTEEVENTACTIONS);
  224.             if (!tc->data.bitstring[0] & 0x80) {
  225.                 /* not a notification.  next! (XXX: do sets) */
  226.                 continue;
  227.             }
  228.             tc = netsnmp_table_data_set_find_column(col1,
  229.                                                     COLUMN_MTEEVENTENABLED);
  230.             if (*(tc->data.integer) != 1) {
  231.                 /* not enabled.  next! */
  232.                 continue;
  233.             }
  234.             if (!mteEventNotif_table_set) {
  235.                 /* no notification info */
  236.                 continue;
  237.             }
  238.             /* send the notification */
  239.             var_list = NULL;
  240.             /* XXX: get notif information */
  241.             for(notif_row = mteEventNotif_table_set->table->first_row;
  242.                 notif_row; notif_row = notif_row->next) {
  243.                 if (strcmp(notif_row->indexes->val.string,
  244.                            eventobjowner) == 0 &&
  245.                     strcmp(notif_row->indexes->next_variable->val.string,
  246.                            eventobjname) == 0) {
  247.                     /* run this event */
  248.                     col1 = (netsnmp_table_data_set_storage *) notif_row->data;
  249.             
  250.                     tc = netsnmp_table_data_set_find_column(col1, COLUMN_MTEEVENTNOTIFICATION);
  251.                     no = netsnmp_table_data_set_find_column(col1, COLUMN_MTEEVENTNOTIFICATIONOBJECTS);
  252.                     noo = netsnmp_table_data_set_find_column(col1, COLUMN_MTEEVENTNOTIFICATIONOBJECTSOWNER);
  253.                     if (!tc)
  254.                         continue; /* no notification to be had. XXX: return? */
  255.                     
  256.                     /*
  257.                      * snmpTrap oid 
  258.                      */
  259.                     snmp_varlist_add_variable(&var_list, objid_snmptrap,
  260.                                               sizeof(objid_snmptrap) /
  261.                                               sizeof(oid),
  262.                                               ASN_OBJECT_ID,
  263.                                               (u_char *) tc->data.objid,
  264.                                               tc->data_len);
  265.                     /* XXX: add objects from the mteObjectsTable */
  266.                     DEBUGMSGTL(("mteEventTable:send_events", "no: %x, no->data: %s", no, no->data.string));
  267.                     DEBUGMSGTL(("mteEventTable:send_events", "noo: %x, noo->data: %s", noo, noo->data.string));
  268.                     DEBUGMSGTL(("mteEventTable:send_events", "name_oid: %x",name_oid));
  269.                     if (no && no->data.string &&
  270.                         noo && noo->data.string && name_oid) {
  271.                         char *tmpowner =
  272.                             netsnmp_strdup_and_null(noo->data.string,
  273.                                                     noo->data_len);
  274.                         char *tmpname =
  275.                             netsnmp_strdup_and_null(no->data.string,
  276.                                                     no->data_len);
  277.                         DEBUGMSGTL(("mteEventTable:send_events", "Adding objects for owner=%s name=%s", tmpowner, tmpname));
  278.                         mte_add_objects(var_list, item,
  279.                                         tmpowner, tmpname, 
  280.                                        name_oid + item->mteTriggerValueIDLen,
  281.                                         name_oid_len - item->mteTriggerValueIDLen);
  282.                         free(tmpowner);
  283.                         free(tmpname);
  284.                     }
  285.                     DEBUGMSGTL(("mteEventTable:send_events", "sending an event "));
  286.                     DEBUGMSGOID(("mteEventTable:send_events", tc->data.objid, tc->data_len / sizeof(oid)));
  287.                     DEBUGMSG(("mteEventTable:send_events", "n"));
  288.                     
  289.                     send_v2trap(var_list);
  290.                     snmp_free_varbind(var_list);
  291.                 }
  292.             }
  293.         }
  294.     }
  295. }