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

SNMP编程

开发平台:

Unix_Linux

  1. /*
  2.  *Copyright(c)2004,Cisco URP imburses and Network Information Center in Beijing University of Posts and Telecommunications researches.
  3.  *
  4.  *All right reserved
  5.  *
  6.  *File Name:pingProbeHistoryTable.c
  7.  *File Description:Rows of pingProbeHistoryTable MIB read.
  8.  *              
  9.  *
  10.  *Current Version:1.0
  11.  *Author:ChenJing
  12.  *Date:2004.8.20
  13.  */
  14. /*
  15.  * This should always be included first before anything else 
  16.  */
  17. #if HAVE_STDLIB_H
  18. #include <stdlib.h>
  19. #endif
  20. #if HAVE_STRING_H
  21. #include <string.h>
  22. #else
  23. #include <strings.h>
  24. #endif
  25. #ifdef HAVE_LIMITS_H
  26. #include <limits.h>
  27. #endif
  28. #include <net-snmp/net-snmp-config.h>
  29. #include <net-snmp/net-snmp-includes.h>
  30. #include <net-snmp/agent/net-snmp-agent-includes.h>
  31. #include "pingCtlTable.h"
  32. #include "pingResultsTable.h"
  33. #include "pingProbeHistoryTable.h"
  34. #include "header_complex.h"
  35. /*
  36.  *pingProbeHistoryTable_variables_oid:
  37.  *
  38.  */
  39. oid             pingProbeHistoryTable_variables_oid[] =
  40.     { 1, 3, 6, 1, 2, 1, 80, 1, 4 };
  41. struct variable2 pingProbeHistoryTable_variables[] = {
  42.     {COLUMN_PINGPROBEHISTORYRESPONSE, ASN_UNSIGNED, RONLY, var_pingProbeHistoryTable, 2, {1, 2}},
  43.     {COLUMN_PINGPROBEHISTORYSTATUS,    ASN_INTEGER, RONLY, var_pingProbeHistoryTable, 2, {1, 3}},
  44.     {COLUMN_PINGPROBEHISTORYLASTRC,    ASN_INTEGER, RONLY, var_pingProbeHistoryTable, 2, {1, 4}},
  45.     {COLUMN_PINGPROBEHISTORYTIME,    ASN_OCTET_STR, RONLY, var_pingProbeHistoryTable, 2, {1, 5}}
  46. };
  47. /*
  48.  * global storage of our data, saved in and configured by header_complex() 
  49.  */
  50. extern struct header_complex_index *pingCtlTableStorage;
  51. extern struct header_complex_index *pingProbeHistoryTableStorage;
  52. void
  53. pingProbeHistoryTable_cleaner(struct header_complex_index *thestuff)
  54. {
  55.     struct header_complex_index *hciptr = NULL;
  56.     struct pingProbeHistoryTable_data *StorageDel = NULL;
  57.     DEBUGMSGTL(("pingProbeHistoryTable", "cleanerout  "));
  58.     for (hciptr = thestuff; hciptr != NULL; hciptr = hciptr->next) {
  59.         StorageDel =
  60.             header_complex_extract_entry(&pingProbeHistoryTableStorage,
  61.                                          hciptr);
  62.         DEBUGMSGTL(("pingProbeHistoryTable", "cleaner  "));
  63.     }
  64. }
  65. void
  66. init_pingProbeHistoryTable(void)
  67. {
  68.     DEBUGMSGTL(("pingProbeHistoryTable", "initializing...  "));
  69.     /*
  70.      * register ourselves with the agent to handle our mib tree 
  71.      */
  72.     REGISTER_MIB("pingProbeHistoryTable", pingProbeHistoryTable_variables,
  73.                  variable2, pingProbeHistoryTable_variables_oid);
  74.     /*
  75.      * register our config handler(s) to deal with registrations 
  76.      */
  77.     snmpd_register_config_handler("pingProbeHistoryTable",
  78.                                   parse_pingProbeHistoryTable, NULL, NULL);
  79.     /*
  80.      * we need to be called back later to store our data 
  81.      */
  82.     snmp_register_callback(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_STORE_DATA,
  83.                            store_pingProbeHistoryTable, NULL);
  84.     DEBUGMSGTL(("pingProbeHistoryTable", "done.n"));
  85. }
  86. /*
  87.  * parse_mteObjectsTable():
  88.  *   parses .conf file entries needed to configure the mib.
  89.  */
  90. void
  91. parse_pingProbeHistoryTable(const char *token, char *line)
  92. {
  93.     size_t          tmpint;
  94.     struct pingProbeHistoryTable_data *StorageTmp =
  95.         SNMP_MALLOC_STRUCT(pingProbeHistoryTable_data);
  96.     DEBUGMSGTL(("pingProbeHistoryTable", "parsing config...  "));
  97.     if (StorageTmp == NULL) {
  98.         config_perror("malloc failure");
  99.         return;
  100.     }
  101.     line =
  102.         read_config_read_data(ASN_OCTET_STR, line,
  103.                               &StorageTmp->pingCtlOwnerIndex,
  104.                               &StorageTmp->pingCtlOwnerIndexLen);
  105.     if (StorageTmp->pingCtlOwnerIndex == NULL) {
  106.         config_perror("invalid specification for pingCtlOwnerIndex");
  107.         return;
  108.     }
  109.     line =
  110.         read_config_read_data(ASN_OCTET_STR, line,
  111.                               &StorageTmp->pingCtlTestName,
  112.                               &StorageTmp->pingCtlTestNameLen);
  113.     if (StorageTmp->pingCtlTestName == NULL) {
  114.         config_perror("invalid specification for pingCtlTestName");
  115.         return;
  116.     }
  117.     line =
  118.         read_config_read_data(ASN_UNSIGNED, line,
  119.                               &StorageTmp->pingProbeHistoryIndex, &tmpint);
  120.     line =
  121.         read_config_read_data(ASN_UNSIGNED, line,
  122.                               &StorageTmp->pingProbeHistoryResponse,
  123.                               &tmpint);
  124.     line =
  125.         read_config_read_data(ASN_INTEGER, line,
  126.                               &StorageTmp->pingProbeHistoryStatus,
  127.                               &tmpint);
  128.     line =
  129.         read_config_read_data(ASN_INTEGER, line,
  130.                               &StorageTmp->pingProbeHistoryLastRC,
  131.                               &tmpint);
  132.     line =
  133.         read_config_read_data(ASN_OCTET_STR, line,
  134.                               &StorageTmp->pingProbeHistoryTime,
  135.                               &StorageTmp->pingProbeHistoryTimeLen);
  136.     if (StorageTmp->pingProbeHistoryTime == NULL) {
  137.         config_perror("invalid specification for pingProbeHistoryTime");
  138.         return;
  139.     }
  140.     pingProbeHistoryTable_inadd(StorageTmp);
  141.     /* pingProbeHistoryTable_cleaner(pingProbeHistoryTableStorage); */
  142.     DEBUGMSGTL(("pingProbeHistoryTable", "done.n"));
  143. }
  144. /*
  145.  * store_pingProbeHistoryTable():
  146.  *   stores .conf file entries needed to configure the mib.
  147.  */
  148. int
  149. store_pingProbeHistoryTable(int majorID, int minorID, void *serverarg,
  150.                             void *clientarg)
  151. {
  152.     char            line[SNMP_MAXBUF];
  153.     char           *cptr;
  154.     size_t          tmpint;
  155.     struct pingProbeHistoryTable_data *StorageTmp;
  156.     struct header_complex_index *hcindex;
  157.     DEBUGMSGTL(("pingProbeHistoryTable", "storing data...  "));
  158.     for (hcindex = pingProbeHistoryTableStorage; hcindex != NULL;
  159.          hcindex = hcindex->next) {
  160.         StorageTmp = (struct pingProbeHistoryTable_data *) hcindex->data;
  161.         if (StorageTmp->storageType != ST_READONLY) {
  162.             memset(line, 0, sizeof(line));
  163.             strcat(line, "pingProbeHistoryTable ");
  164.             cptr = line + strlen(line);
  165.             cptr =
  166.                 read_config_store_data(ASN_OCTET_STR, cptr,
  167.                                        &StorageTmp->pingCtlOwnerIndex,
  168.                                        &StorageTmp->pingCtlOwnerIndexLen);
  169.             cptr =
  170.                 read_config_store_data(ASN_OCTET_STR, cptr,
  171.                                        &StorageTmp->pingCtlTestName,
  172.                                        &StorageTmp->pingCtlTestNameLen);
  173.             cptr =
  174.                 read_config_store_data(ASN_UNSIGNED, cptr,
  175.                                        &StorageTmp->pingProbeHistoryIndex,
  176.                                        &tmpint);
  177.             cptr =
  178.                 read_config_store_data(ASN_UNSIGNED, cptr,
  179.                                        &StorageTmp->
  180.                                        pingProbeHistoryResponse, &tmpint);
  181.             cptr =
  182.                 read_config_store_data(ASN_INTEGER, cptr,
  183.                                        &StorageTmp->pingProbeHistoryStatus,
  184.                                        &tmpint);
  185.             cptr =
  186.                 read_config_store_data(ASN_INTEGER, cptr,
  187.                                        &StorageTmp->pingProbeHistoryLastRC,
  188.                                        &tmpint);
  189.             cptr =
  190.                 read_config_store_data(ASN_OCTET_STR, cptr,
  191.                                        &StorageTmp->pingProbeHistoryTime,
  192.                                        &StorageTmp->
  193.                                        pingProbeHistoryTimeLen);
  194.             snmpd_store_config(line);
  195.         }
  196.     }
  197.     DEBUGMSGTL(("pingProbeHistoryTable", "done.n"));
  198.     return SNMPERR_SUCCESS;
  199. }
  200. int
  201. pingProbeHistoryTable_inadd(struct pingProbeHistoryTable_data *thedata)
  202. {
  203.     netsnmp_variable_list *vars_list;
  204.     vars_list = NULL;
  205.     snmp_varlist_add_variable(&vars_list, NULL, 0, ASN_OCTET_STR, (char *) thedata->pingCtlOwnerIndex, thedata->pingCtlOwnerIndexLen);  /* pingCtlOwnerIndex */
  206.     snmp_varlist_add_variable(&vars_list, NULL, 0, ASN_OCTET_STR, (char *) thedata->pingCtlTestName, thedata->pingCtlTestNameLen);      /* pingCtlTestName */
  207.     snmp_varlist_add_variable(&vars_list, NULL, 0, ASN_UNSIGNED, (char *) &thedata->pingProbeHistoryIndex, sizeof(thedata->pingProbeHistoryIndex));     /* pingProbeHistoryIndex */
  208.     /*
  209.      * XXX: fill in default row values here into StorageNew 
  210.      * 
  211.      */
  212.     DEBUGMSGTL(("pingProbeHistoryTable", "adding data...  "));
  213.     /*
  214.      * add the index variables to the varbind list, which is 
  215.      * used by header_complex to index the data 
  216.      */
  217.     header_complex_add_data(&pingProbeHistoryTableStorage, vars_list,
  218.                             thedata);
  219.     DEBUGMSGTL(("pingProbeHistoryTable", "registered an entryn"));
  220.     DEBUGMSGTL(("pingProbeHistoryTable", "done.n"));
  221.     return SNMPERR_SUCCESS;
  222. }
  223. /*
  224.  * var_pingProbeHistoryTable():
  225.  *   Handle this table separately from the scalar value case.
  226.  *   The workings of this are basically the same as for var_pingProbeHistoryTable above.
  227.  */
  228. unsigned char  *
  229. var_pingProbeHistoryTable(struct variable *vp,
  230.                           oid * name,
  231.                           size_t *length,
  232.                           int exact,
  233.                           size_t *var_len, WriteMethod ** write_method)
  234. {
  235.     struct pingProbeHistoryTable_data *StorageTmp = NULL;
  236.     *write_method = NULL;
  237.     /*
  238.      * this assumes you have registered all your data properly
  239.      */
  240.     if ((StorageTmp =
  241.          header_complex(pingProbeHistoryTableStorage, vp, name, length,
  242.                         exact, var_len, write_method)) == NULL) {
  243.         return NULL;
  244.     }
  245.     /*
  246.      * this is where we do the value assignments for the mib results.
  247.      */
  248.     switch (vp->magic) {
  249.     case COLUMN_PINGPROBEHISTORYRESPONSE:
  250.         *var_len = sizeof(StorageTmp->pingProbeHistoryResponse);
  251.         return (u_char *) & StorageTmp->pingProbeHistoryResponse;
  252.     case COLUMN_PINGPROBEHISTORYSTATUS:
  253.         *var_len = sizeof(StorageTmp->pingProbeHistoryStatus);
  254.         return (u_char *) & StorageTmp->pingProbeHistoryStatus;
  255.     case COLUMN_PINGPROBEHISTORYLASTRC:
  256.         *var_len = sizeof(StorageTmp->pingProbeHistoryLastRC);
  257.         return (u_char *) & StorageTmp->pingProbeHistoryLastRC;
  258.     case COLUMN_PINGPROBEHISTORYTIME:
  259.         *var_len = (StorageTmp->pingProbeHistoryTimeLen);
  260.         return (u_char *) StorageTmp->pingProbeHistoryTime;
  261.     default:
  262.         ERROR_MSG("");
  263.     }
  264.     return NULL;
  265. }