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

SNMP编程

开发平台:

Unix_Linux

  1. /**************************************************************
  2.  * Copyright (C) 2001 Alex Rozin, Optical Access
  3.  *
  4.  *                     All Rights Reserved
  5.  *
  6.  * Permission to use, copy, modify and distribute this software and its
  7.  * documentation for any purpose and without fee is hereby granted,
  8.  * provided that the above copyright notice appear in all copies and that
  9.  * both that copyright notice and this permission notice appear in
  10.  * supporting documentation.
  11.  *
  12.  * ALEX ROZIN DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  13.  * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  14.  * ALEX ROZIN BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  15.  * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  16.  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  17.  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  18.  * SOFTWARE.
  19.  ******************************************************************/
  20. #include <net-snmp/net-snmp-config.h>
  21. #if HAVE_STDLIB_H
  22. #include <stdlib.h>
  23. #endif
  24. #if TIME_WITH_SYS_TIME
  25. # ifdef WIN32
  26. #  include <sys/timeb.h>
  27. # else
  28. #  include <sys/time.h>
  29. # endif
  30. # include <time.h>
  31. #else
  32. # if HAVE_SYS_TIME_H
  33. #  include <sys/time.h>
  34. # else
  35. #  include <time.h>
  36. # endif
  37. #endif
  38. #if HAVE_UNISTD_H
  39. #include <unistd.h>
  40. #endif
  41. #include <ctype.h>
  42. #include <net-snmp/net-snmp-includes.h>
  43. #include <net-snmp/agent/net-snmp-agent-includes.h>
  44. #include "util_funcs.h"
  45. #include "event.h"
  46. /*
  47.  * Implementation headers 
  48.  */
  49. #include "agutil_api.h"
  50. #include "row_api.h"
  51. /*
  52.  * File scope definitions section 
  53.  */
  54. /*
  55.  * from MIB compilation 
  56.  */
  57. #define eventEntryFirstIndexBegin       11
  58. #define EVENTINDEX            3
  59. #define EVENTDESCRIPTION      4
  60. #define EVENTTYPE             5
  61. #define EVENTCOMMUNITY        6
  62. #define EVENTLASTTIMESENT     7
  63. #define EVENTOWNER            8
  64. #define EVENTSTATUS           9
  65. #define Leaf_event_description  2
  66. #define MIN_event_description   0
  67. #define MAX_event_description   127
  68. #define Leaf_event_type         3
  69. #define Leaf_event_community    4
  70. #define MIN_event_community     0
  71. #define MAX_event_community     127
  72. #define Leaf_event_last_time_sent 5
  73. #define Leaf_eventOwner        6
  74. #define Leaf_eventStatus       7
  75. #define LOGEVENTINDEX         3
  76. #define LOGINDEX              4
  77. #define LOGTIME               5
  78. #define LOGDESCRIPTION        6
  79. /*
  80.  * defaults & limitations 
  81.  */
  82. #define MAX_LOG_ENTRIES_PER_CTRL 200
  83. typedef struct data_struct_t {
  84.     struct data_struct_t *next;
  85.     u_long          data_index;
  86.     u_long          log_time;
  87.     char           *log_description;
  88. } DATA_ENTRY_T;
  89. typedef enum {
  90.     EVENT_NONE = 1,
  91.     EVENT_LOG,
  92.     EVENT_TRAP,
  93.     EVENT_LOG_AND_TRAP
  94. } EVENT_TYPE_T;
  95. typedef struct {
  96.     char           *event_description;
  97.     char           *event_community;
  98.     EVENT_TYPE_T    event_type;
  99.     u_long          event_last_time_sent;
  100.     SCROLLER_T      scrlr;
  101. #if 0
  102.     u_long          event_last_logged_index;
  103.     u_long          event_number_of_log_entries;
  104.     DATA_ENTRY_T   *log_list;
  105.     DATA_ENTRY_T   *last_log_ptr;
  106. #endif
  107. } CRTL_ENTRY_T;
  108. /*
  109.  * Main section 
  110.  */
  111. static TABLE_DEFINTION_T EventCtrlTable;
  112. static TABLE_DEFINTION_T *table_ptr = &EventCtrlTable;
  113. /*
  114.  * Control Table RowApi Callbacks 
  115.  */
  116. static int
  117. data_destructor(SCROLLER_T * scrlr, void *free_me)
  118. {
  119.     DATA_ENTRY_T   *lptr = free_me;
  120.     if (lptr->log_description)
  121.         AGFREE(lptr->log_description);
  122.     return 0;
  123. }
  124. int
  125. event_Create(RMON_ENTRY_T * eptr)
  126. {                               /* create the body: alloc it and set defaults */
  127.     CRTL_ENTRY_T   *body;
  128.     eptr->body = AGMALLOC(sizeof(CRTL_ENTRY_T));
  129.     if (!eptr->body)
  130.         return -3;
  131.     body = (CRTL_ENTRY_T *) eptr->body;
  132.     /*
  133.      * set defaults 
  134.      */
  135.     body->event_description = NULL;
  136.     body->event_community = AGSTRDUP("public");
  137.     /*
  138.      * ag_trace ("Dbg: created event_community=<%s>", body->event_community); 
  139.      */
  140.     body->event_type = EVENT_NONE;
  141.     ROWDATAAPI_init(&body->scrlr,
  142.                     MAX_LOG_ENTRIES_PER_CTRL,
  143.                     MAX_LOG_ENTRIES_PER_CTRL,
  144.                     sizeof(DATA_ENTRY_T), &data_destructor);
  145.     return 0;
  146. }
  147. int
  148. event_Clone(RMON_ENTRY_T * eptr)
  149. {                               /* copy entry_bod -> clone */
  150.     CRTL_ENTRY_T   *body = (CRTL_ENTRY_T *) eptr->body;
  151.     CRTL_ENTRY_T   *clone = (CRTL_ENTRY_T *) eptr->tmp;
  152.     if (body->event_description)
  153.         clone->event_description = AGSTRDUP(body->event_description);
  154.     if (body->event_community)
  155.         clone->event_community = AGSTRDUP(body->event_community);
  156.     return 0;
  157. }
  158. int
  159. event_Copy(RMON_ENTRY_T * eptr)
  160. {
  161.     CRTL_ENTRY_T   *body = (CRTL_ENTRY_T *) eptr->body;
  162.     CRTL_ENTRY_T   *clone = (CRTL_ENTRY_T *) eptr->tmp;
  163.     if (body->event_type != clone->event_type) {
  164.         body->event_type = clone->event_type;
  165.     }
  166.     if (clone->event_description) {
  167.         if (body->event_description)
  168.             AGFREE(body->event_description);
  169.         body->event_description = AGSTRDUP(clone->event_description);
  170.     }
  171.     if (clone->event_community) {
  172.         if (body->event_community)
  173.             AGFREE(body->event_community);
  174.         body->event_community = AGSTRDUP(clone->event_community);
  175.     }
  176.     return 0;
  177. }
  178. int
  179. event_Delete(RMON_ENTRY_T * eptr)
  180. {
  181.     CRTL_ENTRY_T   *body = (CRTL_ENTRY_T *) eptr;
  182.     if (body->event_description)
  183.         AGFREE(body->event_description);
  184.     if (body->event_community)
  185.         AGFREE(body->event_community);
  186.     return 0;
  187. }
  188. int
  189. event_Activate(RMON_ENTRY_T * eptr)
  190. {                               /* init logTable */
  191.     CRTL_ENTRY_T   *body = (CRTL_ENTRY_T *) eptr->body;
  192.     ROWDATAAPI_set_size(&body->scrlr,
  193.                         body->scrlr.data_requested,
  194.                         (u_char)(RMON1_ENTRY_VALID == eptr->status) );
  195.     return 0;
  196. }
  197. int
  198. event_Deactivate(RMON_ENTRY_T * eptr)
  199. {                               /* free logTable */
  200.     CRTL_ENTRY_T   *body = (CRTL_ENTRY_T *) eptr->body;
  201.     /*
  202.      * free data list 
  203.      */
  204.     ROWDATAAPI_descructor(&body->scrlr);
  205.     return 0;
  206. }
  207. static int
  208. write_eventControl(int action, u_char * var_val, u_char var_val_type,
  209.                    size_t var_val_len, u_char * statP,
  210.                    oid * name, size_t name_len)
  211. {
  212.     long            long_temp;
  213.     char           *char_temp;
  214.     int             leaf_id, snmp_status;
  215.     static int      prev_action = COMMIT;
  216.     RMON_ENTRY_T   *hdr;
  217.     CRTL_ENTRY_T   *cloned_body;
  218.     CRTL_ENTRY_T   *body;
  219.     switch (action) {
  220.     case RESERVE1:
  221.     case FREE:
  222.     case UNDO:
  223.     case ACTION:
  224.     case COMMIT:
  225.     default:
  226.         return ROWAPI_do_another_action(name, eventEntryFirstIndexBegin,
  227.                                         action, &prev_action,
  228.                                         table_ptr, sizeof(CRTL_ENTRY_T));
  229.     case RESERVE2:
  230.         /*
  231.          * get values from PDU, check them and save them in the cloned entry 
  232.          */
  233.         long_temp = name[eventEntryFirstIndexBegin];
  234.         leaf_id = (int) name[eventEntryFirstIndexBegin - 1];
  235.         hdr = ROWAPI_find(table_ptr, long_temp);        /* it MUST be OK */
  236.         cloned_body = (CRTL_ENTRY_T *) hdr->tmp;
  237.         body = (CRTL_ENTRY_T *) hdr->body;
  238.         switch (leaf_id) {
  239.         case Leaf_event_description:
  240.             char_temp = AGMALLOC(1 + MAX_event_description);
  241.             if (!char_temp)
  242.                 return SNMP_ERR_TOOBIG;
  243.             snmp_status = AGUTIL_get_string_value(var_val, var_val_type,
  244.                                                   var_val_len,
  245.                                                   MAX_event_description,
  246.                                                   1, NULL, char_temp);
  247.             if (SNMP_ERR_NOERROR != snmp_status) {
  248.                 AGFREE(char_temp);
  249.                 return snmp_status;
  250.             }
  251.             if (cloned_body->event_description)
  252.                 AGFREE(cloned_body->event_description);
  253.             cloned_body->event_description = AGSTRDUP(char_temp);
  254.             /*
  255.              * ag_trace ("rx: event_description=<%s>", cloned_body->event_description); 
  256.              */
  257.             AGFREE(char_temp);
  258.             break;
  259.         case Leaf_event_type:
  260.             snmp_status = AGUTIL_get_int_value(var_val, var_val_type,
  261.                                                var_val_len,
  262.                                                EVENT_NONE,
  263.                                                EVENT_LOG_AND_TRAP,
  264.                                                &long_temp);
  265.             if (SNMP_ERR_NOERROR != snmp_status) {
  266.                 return snmp_status;
  267.             }
  268.             cloned_body->event_type = long_temp;
  269.             break;
  270.         case Leaf_event_community:
  271.             char_temp = AGMALLOC(1 + MAX_event_community);
  272.             if (!char_temp)
  273.                 return SNMP_ERR_TOOBIG;
  274.             snmp_status = AGUTIL_get_string_value(var_val, var_val_type,
  275.                                                   var_val_len,
  276.                                                   MAX_event_community,
  277.                                                   1, NULL, char_temp);
  278.             if (SNMP_ERR_NOERROR != snmp_status) {
  279.                 AGFREE(char_temp);
  280.                 return snmp_status;
  281.             }
  282.             if (cloned_body->event_community)
  283.                 AGFREE(cloned_body->event_community);
  284.             cloned_body->event_community = AGSTRDUP(char_temp);
  285.             AGFREE(char_temp);
  286.             break;
  287.         case Leaf_eventOwner:
  288.             if (hdr->new_owner)
  289.                 AGFREE(hdr->new_owner);
  290.             hdr->new_owner = AGMALLOC(MAX_OWNERSTRING);;
  291.             if (!hdr->new_owner)
  292.                 return SNMP_ERR_TOOBIG;
  293.             snmp_status = AGUTIL_get_string_value(var_val, var_val_type,
  294.                                                   var_val_len,
  295.                                                   MAX_OWNERSTRING,
  296.                                                   1, NULL, hdr->new_owner);
  297.             if (SNMP_ERR_NOERROR != snmp_status) {
  298.                 return snmp_status;
  299.             }
  300.             break;
  301.         case Leaf_eventStatus:
  302.             snmp_status = AGUTIL_get_int_value(var_val, var_val_type,
  303.                                                var_val_len,
  304.                                                RMON1_ENTRY_VALID,
  305.                                                RMON1_ENTRY_INVALID,
  306.                                                &long_temp);
  307.             if (SNMP_ERR_NOERROR != snmp_status) {
  308.                 return snmp_status;
  309.             }
  310.             hdr->new_status = long_temp;
  311.             break;
  312.         default:
  313.             ag_trace("%s:unknown leaf_id=%dn", table_ptr->name,
  314.                      (int) leaf_id);
  315.             return SNMP_ERR_NOSUCHNAME;
  316.         }                       /* of switch by 'leaf_id' */
  317.         break;
  318.     }                           /* of switch by actions */
  319.     prev_action = action;
  320.     return SNMP_ERR_NOERROR;
  321. }
  322. unsigned char  *
  323. var_eventTable(struct variable *vp,
  324.                oid * name,
  325.                size_t * length,
  326.                int exact, size_t * var_len, WriteMethod ** write_method)
  327. {
  328.     static long     long_ret;
  329.     static CRTL_ENTRY_T theEntry;
  330.     RMON_ENTRY_T   *hdr;
  331.     *write_method = write_eventControl;
  332.     hdr = ROWAPI_header_ControlEntry(vp, name, length, exact, var_len,
  333.                                      table_ptr,
  334.                                      &theEntry, sizeof(CRTL_ENTRY_T));
  335.     if (!hdr)
  336.         return NULL;
  337.     *var_len = sizeof(long);    /* default */
  338.     switch (vp->magic) {
  339.     case EVENTINDEX:
  340.         long_ret = hdr->ctrl_index;
  341.         return (unsigned char *) &long_ret;
  342.     case EVENTDESCRIPTION:
  343.         if (theEntry.event_description) {
  344.             *var_len = strlen(theEntry.event_description);
  345.             return (unsigned char *) theEntry.event_description;
  346.         } else {
  347.             *var_len = 0;
  348.             return (unsigned char *) "";
  349.         }
  350.     case EVENTTYPE:
  351.         long_ret = theEntry.event_type;
  352.         return (unsigned char *) &long_ret;
  353.     case EVENTCOMMUNITY:
  354.         if (theEntry.event_community) {
  355.             *var_len = strlen(theEntry.event_community);
  356.             return (unsigned char *) theEntry.event_community;
  357.         } else {
  358.             *var_len = 0;
  359.             return (unsigned char *) "";
  360.         }
  361.     case EVENTLASTTIMESENT:
  362.         long_ret = theEntry.event_last_time_sent;
  363.         return (unsigned char *) &long_ret;
  364.     case EVENTOWNER:
  365.         if (hdr->owner) {
  366.             *var_len = strlen(hdr->owner);
  367.             return (unsigned char *) hdr->owner;
  368.         } else {
  369.             *var_len = 0;
  370.             return (unsigned char *) "";
  371.         }
  372.     case EVENTSTATUS:
  373.         long_ret = hdr->status;
  374.         return (unsigned char *) &long_ret;
  375.     default:
  376.         ag_trace("EventControlTable: unknown vp->magic=%d",
  377.                  (int) vp->magic);
  378.         ERROR_MSG("");
  379.     }
  380.     return NULL;
  381. }
  382. static SCROLLER_T *
  383. event_extract_scroller(void *v_body)
  384. {
  385.     CRTL_ENTRY_T   *body = (CRTL_ENTRY_T *) v_body;
  386.     return &body->scrlr;
  387. }
  388. unsigned char  *
  389. var_logTable(struct variable *vp,
  390.              oid * name,
  391.              size_t * length,
  392.              int exact, size_t * var_len, WriteMethod ** write_method)
  393. {
  394.     static long     long_ret;
  395.     static DATA_ENTRY_T theEntry;
  396.     RMON_ENTRY_T   *hdr;
  397.     CRTL_ENTRY_T   *ctrl;
  398.     *write_method = NULL;
  399.     hdr = ROWDATAAPI_header_DataEntry(vp, name, length, exact, var_len,
  400.                                       table_ptr,
  401.                                       &event_extract_scroller,
  402.                                       sizeof(DATA_ENTRY_T), &theEntry);
  403.     if (!hdr)
  404.         return NULL;
  405.     ctrl = (CRTL_ENTRY_T *) hdr->body;
  406.     *var_len = sizeof(long);    /* default */
  407.     switch (vp->magic) {
  408.     case LOGEVENTINDEX:
  409.         long_ret = hdr->ctrl_index;
  410.         return (unsigned char *) &long_ret;
  411.     case LOGINDEX:
  412.         long_ret = theEntry.data_index;
  413.         return (unsigned char *) &long_ret;
  414.     case LOGTIME:
  415.         long_ret = theEntry.log_time;
  416.         return (unsigned char *) &long_ret;
  417.     case LOGDESCRIPTION:
  418.         if (theEntry.log_description) {
  419.             *var_len = strlen(theEntry.log_description);
  420.             return (unsigned char *) theEntry.log_description;
  421.         } else {
  422.             *var_len = 0;
  423.             return (unsigned char *) "";
  424.         }
  425.     default:
  426.         ERROR_MSG("");
  427.     }
  428.     return NULL;
  429. }
  430. /*
  431.  * External API section 
  432.  */
  433. static char    *
  434. create_explanaition(CRTL_ENTRY_T * evptr, u_char is_rising,
  435.                     u_long alarm_index, u_long event_index,
  436.                     oid * alarmed_var,
  437.                     size_t alarmed_var_length,
  438.                     u_long value, u_long the_threshold,
  439.                     u_long sample_type, char *alarm_descr)
  440. {
  441. #define UNEQ_LENGTH (1 + 11 + 4 + 11 + 1 + 20)
  442.     char            expl[UNEQ_LENGTH];
  443.     static char     c_oid[SPRINT_MAX_LEN];
  444.     size_t          sz;
  445.     char           *descr;
  446.     register char  *pch;
  447.     register char  *tmp;
  448.     snprint_objid(c_oid, sizeof(c_oid)-1, alarmed_var, alarmed_var_length);
  449.     c_oid[sizeof(c_oid)-1] = '';
  450.     for (pch = c_oid;;) {
  451.         tmp = strchr(pch, '.');
  452.         if (!tmp)
  453.             break;
  454.         if (isdigit(tmp[1]) || '"' == tmp[1])
  455.             break;
  456.         pch = tmp + 1;
  457.     }
  458.     snprintf(expl, UNEQ_LENGTH, "=%ld %s= %ld :%ld, %ld",
  459.              (unsigned long) value,
  460.              is_rising ? ">" : "<",
  461.              (unsigned long) the_threshold,
  462.              (long) alarm_index, (long) event_index);
  463.     sz = 3 + strlen(expl) + strlen(pch);
  464.     if (alarm_descr)
  465.         sz += strlen(alarm_descr);
  466.     descr = AGMALLOC(sz);
  467.     if (!descr) {
  468.         ag_trace("Can't allocate event description");
  469.         return NULL;
  470.     }
  471.     if (alarm_descr) {
  472.         strcpy(descr, alarm_descr);
  473.         strcat(descr, ":");
  474.     } else
  475.         *descr = '';
  476.     strcat(descr, pch);
  477.     strcat(descr, expl);
  478.     return descr;
  479. }
  480. extern void     send_enterprise_trap_vars(int, int, oid *, int,
  481.                                           netsnmp_variable_list *);
  482. static netsnmp_variable_list *
  483. oa_bind_var(netsnmp_variable_list * prev,
  484.             void *value, int type, size_t sz_val, oid * oid, size_t sz_oid)
  485. {
  486.     netsnmp_variable_list *var;
  487.     var = (netsnmp_variable_list *) malloc(sizeof(netsnmp_variable_list));
  488.     if (!var) {
  489.         ag_trace("FATAL: cannot malloc in oa_bind_varn");
  490.         exit(-1);               /* Sorry :( */
  491.     }
  492.     memset(var, 0, sizeof(netsnmp_variable_list));
  493.     var->next_variable = prev;
  494.     snmp_set_var_objid(var, oid, sz_oid);
  495.     snmp_set_var_value(var, (u_char *) value, sz_val);
  496.     var->type = type;
  497.     return var;
  498. }
  499. static void
  500. event_send_trap(CRTL_ENTRY_T * evptr, u_char is_rising,
  501.                 u_int alarm_index,
  502.                 u_int value, u_int the_threshold,
  503.                 oid * alarmed_var, size_t alarmed_var_length,
  504.                 u_int sample_type)
  505. {
  506.     static oid      rmon1_trap_oid[] = { 1, 3, 6, 1, 2, 1, 16, 0, 0 };
  507.     static oid      alarm_index_oid[] =
  508.         { 1, 3, 6, 1, 2, 1, 16, 3, 1, 1, 1 };
  509.     static oid      alarmed_var_oid[] =
  510.         { 1, 3, 6, 1, 2, 1, 16, 3, 1, 1, 3 };
  511.     static oid      sample_type_oid[] =
  512.         { 1, 3, 6, 1, 2, 1, 16, 3, 1, 1, 4 };
  513.     static oid      value_oid[] = { 1, 3, 6, 1, 2, 1, 16, 3, 1, 1, 5 };
  514.     static oid      threshold_oid[] = { 1, 3, 6, 1, 2, 1, 16, 3, 1, 1, 7 };     /* rising case */
  515.     netsnmp_variable_list *top = NULL;
  516.     register int    iii;
  517.     /*
  518.      * set the last 'oid' : risingAlarm or fallingAlarm 
  519.      */
  520.     if (is_rising) {
  521.         iii = OID_LENGTH(rmon1_trap_oid);
  522.         rmon1_trap_oid[iii - 1] = 1;
  523.         iii = OID_LENGTH(threshold_oid);
  524.         threshold_oid[iii - 1] = 7;
  525.     } else {
  526.         iii = OID_LENGTH(rmon1_trap_oid);
  527.         rmon1_trap_oid[iii - 1] = 0;
  528.         iii = OID_LENGTH(threshold_oid);
  529.         threshold_oid[iii - 1] = 8;
  530.     }
  531.     /*
  532.      * build the var list 
  533.      */
  534.     top = oa_bind_var(top, &alarm_index, ASN_INTEGER, sizeof(u_int),
  535.                       alarm_index_oid, OID_LENGTH(alarm_index_oid));
  536.     top =
  537.         oa_bind_var(top, alarmed_var, ASN_OBJECT_ID,
  538.                     sizeof(oid) * alarmed_var_length, alarmed_var_oid,
  539.                     OID_LENGTH(alarmed_var_oid));
  540.     top = oa_bind_var(top, &sample_type, ASN_INTEGER, sizeof(u_int),
  541.                       sample_type_oid, OID_LENGTH(sample_type_oid));
  542.     top = oa_bind_var(top, &value, ASN_INTEGER, sizeof(u_int),
  543.                       value_oid, OID_LENGTH(value_oid));
  544.     top = oa_bind_var(top, &the_threshold, ASN_INTEGER, sizeof(u_int),
  545.                       threshold_oid, OID_LENGTH(threshold_oid));
  546.     send_enterprise_trap_vars(SNMP_TRAP_ENTERPRISESPECIFIC, 0,
  547.                               rmon1_trap_oid,
  548.                               OID_LENGTH(rmon1_trap_oid), top);
  549.     ag_trace("rmon trap has been sent");
  550.     snmp_free_varbind(top);
  551. }
  552. static void
  553. event_save_log(CRTL_ENTRY_T * body, char *event_descr)
  554. {
  555.     register DATA_ENTRY_T *lptr;
  556.     lptr = ROWDATAAPI_locate_new_data(&body->scrlr);
  557.     if (!lptr) {
  558.         ag_trace("Err: event_save_log:cannot locate ?");
  559.         return;
  560.     }
  561.     lptr->log_time = body->event_last_time_sent;
  562.     if (lptr->log_description)
  563.         AGFREE(lptr->log_description);
  564.     lptr->log_description = AGSTRDUP(event_descr);
  565.     lptr->data_index = ROWDATAAPI_get_total_number(&body->scrlr);
  566.     /*
  567.      * ag_trace ("log has been saved, data_index=%d", (int) lptr->data_index); 
  568.      */
  569. }
  570. int
  571. event_api_send_alarm(u_char is_rising,
  572.                      u_long alarm_index,
  573.                      u_long event_index,
  574.                      oid * alarmed_var,
  575.                      size_t alarmed_var_length,
  576.                      u_long sample_type,
  577.                      u_long value, u_long the_threshold, char *alarm_descr)
  578. {
  579.     RMON_ENTRY_T   *eptr;
  580.     CRTL_ENTRY_T   *evptr;
  581.     if (!event_index)
  582.         return SNMP_ERR_NOSUCHNAME;
  583. #if 0
  584.     ag_trace("event_api_send_alarm(%d,%d,%d,'%s')",
  585.              (int) is_rising, (int) alarm_index, (int) event_index,
  586.              alarm_descr);
  587. #endif
  588.     eptr = ROWAPI_find(table_ptr, event_index);
  589.     if (!eptr) {
  590.         /*
  591.          * ag_trace ("event cannot find entry %ld", event_index); 
  592.          */
  593.         return SNMP_ERR_NOSUCHNAME;
  594.     }
  595.     evptr = (CRTL_ENTRY_T *) eptr->body;
  596.     evptr->event_last_time_sent = AGUTIL_sys_up_time();
  597.     if (EVENT_TRAP == evptr->event_type
  598.         || EVENT_LOG_AND_TRAP == evptr->event_type) {
  599.         event_send_trap(evptr, is_rising, alarm_index, value,
  600.                         the_threshold, alarmed_var, alarmed_var_length,
  601.                         sample_type);
  602.     }
  603.     if (EVENT_LOG == evptr->event_type
  604.         || EVENT_LOG_AND_TRAP == evptr->event_type) {
  605.         register char  *explain;
  606.         explain = create_explanaition(evptr, is_rising,
  607.                                       alarm_index, event_index,
  608.                                       alarmed_var, alarmed_var_length,
  609.                                       value, the_threshold,
  610.                                       sample_type, alarm_descr);
  611.         /*
  612.          * if (explain) ag_trace ("Dbg:'%s'", explain); 
  613.          */
  614.         event_save_log(evptr, explain);
  615.         if (explain)
  616.             AGFREE(explain);
  617.     }
  618.     return SNMP_ERR_NOERROR;
  619. }
  620. #if 1                           /* debug, but may be used for init. TBD: may be token snmpd.conf ? */
  621. int
  622. add_event_entry(int ctrl_index,
  623.                 char *event_description,
  624.                 EVENT_TYPE_T event_type, char *event_community)
  625. {
  626.     register RMON_ENTRY_T *eptr;
  627.     register CRTL_ENTRY_T *body;
  628.     int             ierr;
  629.     ierr = ROWAPI_new(table_ptr, ctrl_index);
  630.     if (ierr) {
  631.         ag_trace("ROWAPI_new failed with %d", ierr);
  632.         return ierr;
  633.     }
  634.     eptr = ROWAPI_find(table_ptr, ctrl_index);
  635.     if (!eptr) {
  636.         ag_trace("ROWAPI_find failed");
  637.         return -4;
  638.     }
  639.     body = (CRTL_ENTRY_T *) eptr->body;
  640.     /*
  641.      * set parameters 
  642.      */
  643.     if (event_description) {
  644.         if (body->event_description)
  645.             AGFREE(body->event_description);
  646.         body->event_description = AGSTRDUP(event_description);
  647.     }
  648.     if (event_community) {
  649.         if (body->event_community)
  650.             AGFREE(body->event_community);
  651.         body->event_community = AGSTRDUP(event_community);
  652.     }
  653.     body->event_type = event_type;
  654.     eptr->new_status = RMON1_ENTRY_VALID;
  655.     ierr = ROWAPI_commit(table_ptr, ctrl_index);
  656.     if (ierr) {
  657.         ag_trace("ROWAPI_commit failed with %d", ierr);
  658.     }
  659.     return ierr;
  660. }
  661. #endif
  662. /*
  663.  * Registration & Initializatio section 
  664.  */
  665. oid             eventTable_variables_oid[] =
  666.     { 1, 3, 6, 1, 2, 1, 16, 9, 1 };
  667. oid             logTable_variables_oid[] = { 1, 3, 6, 1, 2, 1, 16, 9, 2 };
  668. struct variable2 eventTable_variables[] = {
  669.     /*
  670.      * magic number        , variable type, ro/rw , callback fn  ,           L, oidsuffix 
  671.      */
  672.     {EVENTINDEX, ASN_INTEGER, RONLY, var_eventTable, 2, {1, 1}},
  673.     {EVENTDESCRIPTION, ASN_OCTET_STR, RWRITE, var_eventTable, 2, {1, 2}},
  674.     {EVENTTYPE, ASN_INTEGER, RWRITE, var_eventTable, 2, {1, 3}},
  675.     {EVENTCOMMUNITY, ASN_OCTET_STR, RWRITE, var_eventTable, 2, {1, 4}},
  676.     {EVENTLASTTIMESENT, ASN_TIMETICKS, RONLY, var_eventTable, 2, {1, 5}},
  677.     {EVENTOWNER, ASN_OCTET_STR, RWRITE, var_eventTable, 2, {1, 6}},
  678.     {EVENTSTATUS, ASN_INTEGER, RWRITE, var_eventTable, 2, {1, 7}}
  679. };
  680. struct variable2 logTable_variables[] = {
  681.     /*
  682.      * magic number        , variable type, ro/rw , callback fn  ,           L, oidsuffix 
  683.      */
  684.     {LOGEVENTINDEX, ASN_INTEGER, RONLY, var_logTable, 2, {1, 1}},
  685.     {LOGINDEX, ASN_INTEGER, RONLY, var_logTable, 2, {1, 2}},
  686.     {LOGTIME, ASN_TIMETICKS, RONLY, var_logTable, 2, {1, 3}},
  687.     {LOGDESCRIPTION, ASN_OCTET_STR, RONLY, var_logTable, 2, {1, 4}}
  688. };
  689. void
  690. init_event(void)
  691. {
  692.     REGISTER_MIB("eventTable", eventTable_variables, variable2,
  693.                  eventTable_variables_oid);
  694.     REGISTER_MIB("logTable", logTable_variables, variable2,
  695.                  logTable_variables_oid);
  696.     ROWAPI_init_table(&EventCtrlTable, "Event", 0, &event_Create, &event_Clone, &event_Delete, NULL,    /* &event_Validate, */
  697.                       &event_Activate, &event_Deactivate, &event_Copy);
  698. #if 0
  699.     add_event_entry(3, "Alarm", EVENT_LOG_AND_TRAP, NULL);
  700.     /*
  701.      * add_event_entry (5, ">=", EVENT_LOG_AND_TRAP, NULL); 
  702.      */
  703. #endif
  704. }