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

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:pingResultsTable.c
  7.  *File Description:Rows of lookupResultsTable MIB add and delete.
  8.  *
  9.  *Current Version:1.0
  10.  *Author:ChenJing
  11.  *Date:2004.8.20
  12.  */
  13. /*
  14.  * This should always be included first before anything else 
  15.  */
  16. #if HAVE_STDLIB_H
  17. #include <stdlib.h>
  18. #endif
  19. #if HAVE_STRING_H
  20. #include <string.h>
  21. #else
  22. #include <strings.h>
  23. #endif
  24. #ifdef HAVE_LIMITS_H
  25. #include <limits.h>
  26. #endif
  27. #include <net-snmp/net-snmp-config.h>
  28. #include <net-snmp/net-snmp-includes.h>
  29. #include <net-snmp/agent/net-snmp-agent-includes.h>
  30. #include "pingCtlTable.h"
  31. #include "pingResultsTable.h"
  32. #include "pingProbeHistoryTable.h"
  33. #include "header_complex.h"
  34. /*
  35.  *pingResultsTable_variables_oid:
  36.  *
  37.  */
  38. oid             pingResultsTable_variables_oid[] =
  39.     { 1, 3, 6, 1, 2, 1, 80, 1, 3 };
  40. struct variable2 pingResultsTable_variables[] = {
  41.     {COLUMN_PINGRESULTSOPERSTATUS,       ASN_INTEGER, RONLY, var_pingResultsTable, 2, {1, 1}},
  42.     {COLUMN_PINGRESULTSIPTARGETADDRESSTYPE, ASN_INTEGER, RONLY, var_pingResultsTable, 2, {1, 2}},
  43.     {COLUMN_PINGRESULTSIPTARGETADDRESS, ASN_OCTET_STR, RONLY, var_pingResultsTable, 2, {1, 3}},
  44.     {COLUMN_PINGRESULTSMINRTT,          ASN_UNSIGNED, RONLY, var_pingResultsTable, 2, {1, 4}},
  45.     {COLUMN_PINGRESULTSMAXRTT,          ASN_UNSIGNED, RONLY, var_pingResultsTable, 2, {1, 5}},
  46.     {COLUMN_PINGRESULTSAVERAGERTT,      ASN_UNSIGNED, RONLY, var_pingResultsTable, 2, {1, 6}},
  47.     {COLUMN_PINGRESULTSPROBERESPONSES,  ASN_UNSIGNED, RONLY, var_pingResultsTable, 2, {1, 7}},
  48.     {COLUMN_PINGRESULTSSENTPROBES,      ASN_UNSIGNED, RONLY, var_pingResultsTable, 2, {1, 8}},
  49.     {COLUMN_PINGRESULTSRTTSUMOFSQUARES, ASN_UNSIGNED, RONLY, var_pingResultsTable, 2, {1, 9}},
  50.     {COLUMN_PINGRESULTSLASTGOODPROBE,  ASN_OCTET_STR, RONLY, var_pingResultsTable, 2, {1, 10}}
  51. };
  52. /*
  53.  * global storage of our data, saved in and configured by header_complex() 
  54.  */
  55. extern struct header_complex_index *pingCtlTableStorage;
  56. extern struct header_complex_index *pingResultsTableStorage;
  57. void
  58. pingResultsTable_cleaner(struct header_complex_index *thestuff)
  59. {
  60.     struct header_complex_index *hciptr = NULL;
  61.     struct pingResultsTable_data *StorageDel = NULL;
  62.     DEBUGMSGTL(("pingResultsTable", "cleanerout  "));
  63.     for (hciptr = thestuff; hciptr != NULL; hciptr = hciptr->next) {
  64.         StorageDel =
  65.             header_complex_extract_entry(&pingResultsTableStorage, hciptr);
  66.         DEBUGMSGTL(("pingResultsTable", "cleaner  "));
  67.     }
  68. }
  69. void
  70. init_pingResultsTable(void)
  71. {
  72.     DEBUGMSGTL(("pingResultsTable", "initializing...  "));
  73.     /*
  74.      * register ourselves with the agent to handle our mib tree 
  75.      */
  76.     REGISTER_MIB("pingResultsTable", pingResultsTable_variables, variable2,
  77.                  pingResultsTable_variables_oid);
  78.     /*
  79.      * register our config handler(s) to deal with registrations 
  80.      */
  81.     snmpd_register_config_handler("pingResultsTable",
  82.                                   parse_pingResultsTable, NULL, NULL);
  83.     /*
  84.      * we need to be called back later to store our data 
  85.      */
  86.     snmp_register_callback(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_STORE_DATA,
  87.                            store_pingResultsTable, NULL);
  88.     DEBUGMSGTL(("pingResultsTable", "done.n"));
  89. }
  90. /*
  91.  * parse_mteObjectsTable():
  92.  *   parses .conf file entries needed to configure the mib.
  93.  */
  94. void
  95. parse_pingResultsTable(const char *token, char *line)
  96. {
  97.     size_t          tmpint;
  98.     struct pingResultsTable_data *StorageTmp =
  99.         SNMP_MALLOC_STRUCT(pingResultsTable_data);
  100.     DEBUGMSGTL(("pingResultsTable", "parsing config...  "));
  101.     if (StorageTmp == NULL) {
  102.         config_perror("malloc failure");
  103.         return;
  104.     }
  105.     line =
  106.         read_config_read_data(ASN_OCTET_STR, line,
  107.                               &StorageTmp->pingCtlOwnerIndex,
  108.                               &StorageTmp->pingCtlOwnerIndexLen);
  109.     if (StorageTmp->pingCtlOwnerIndex == NULL) {
  110.         config_perror("invalid specification for pingCtlOwnerIndex");
  111.         return;
  112.     }
  113.     line =
  114.         read_config_read_data(ASN_OCTET_STR, line,
  115.                               &StorageTmp->pingCtlTestName,
  116.                               &StorageTmp->pingCtlTestNameLen);
  117.     if (StorageTmp->pingCtlTestName == NULL) {
  118.         config_perror("invalid specification for pingCtlTestName");
  119.         return;
  120.     }
  121.     line =
  122.         read_config_read_data(ASN_INTEGER, line,
  123.                               &StorageTmp->pingResultsOperStatus, &tmpint);
  124.     line =
  125.         read_config_read_data(ASN_INTEGER, line,
  126.                               &StorageTmp->pingResultsIpTargetAddressType,
  127.                               &tmpint);
  128.     line =
  129.         read_config_read_data(ASN_OCTET_STR, line,
  130.                               &StorageTmp->pingResultsIpTargetAddress,
  131.                               &StorageTmp->pingResultsIpTargetAddressLen);
  132.     if (StorageTmp->pingResultsIpTargetAddress == NULL) {
  133.         config_perror
  134.             ("invalid specification for pingResultsIpTargetAddress");
  135.         return;
  136.     }
  137.     line =
  138.         read_config_read_data(ASN_UNSIGNED, line,
  139.                               &StorageTmp->pingResultsMinRtt, &tmpint);
  140.     line =
  141.         read_config_read_data(ASN_UNSIGNED, line,
  142.                               &StorageTmp->pingResultsMaxRtt, &tmpint);
  143.     line =
  144.         read_config_read_data(ASN_UNSIGNED, line,
  145.                               &StorageTmp->pingResultsAverageRtt, &tmpint);
  146.     line =
  147.         read_config_read_data(ASN_UNSIGNED, line,
  148.                               &StorageTmp->pingResultsProbeResponses,
  149.                               &tmpint);
  150.     line =
  151.         read_config_read_data(ASN_UNSIGNED, line,
  152.                               &StorageTmp->pingResultsSendProbes, &tmpint);
  153.     line =
  154.         read_config_read_data(ASN_UNSIGNED, line,
  155.                               &StorageTmp->pingResultsRttSumOfSquares,
  156.                               &tmpint);
  157.     line =
  158.         read_config_read_data(ASN_OCTET_STR, line,
  159.                               &StorageTmp->pingResultsLastGoodProbe,
  160.                               &StorageTmp->pingResultsLastGoodProbeLen);
  161.     if (StorageTmp->pingResultsLastGoodProbe == NULL) {
  162.         config_perror
  163.             ("invalid specification for pingResultsLastGoodProbe!");
  164.         return;
  165.     }
  166.     pingResultsTable_inadd(StorageTmp);
  167.     /* pingResultsTable_cleaner(pingResultsTableStorage); */
  168.     DEBUGMSGTL(("pingResultsTable", "done.n"));
  169. }
  170. /*
  171.  * store_pingResultsTable():
  172.  *   stores .conf file entries needed to configure the mib.
  173.  */
  174. int
  175. store_pingResultsTable(int majorID, int minorID, void *serverarg,
  176.                        void *clientarg)
  177. {
  178.     char            line[SNMP_MAXBUF];
  179.     char           *cptr;
  180.     size_t          tmpint;
  181.     struct pingResultsTable_data *StorageTmp;
  182.     struct header_complex_index *hcindex;
  183.     DEBUGMSGTL(("pingResultsTable", "storing data...  "));
  184.     for (hcindex = pingResultsTableStorage; hcindex != NULL;
  185.          hcindex = hcindex->next) {
  186.         StorageTmp = (struct pingResultsTable_data *) hcindex->data;
  187.         if (StorageTmp->storageType != ST_READONLY) {
  188.             memset(line, 0, sizeof(line));
  189.             strcat(line, "pingResultsTable ");
  190.             cptr = line + strlen(line);
  191.             cptr =
  192.                 read_config_store_data(ASN_OCTET_STR, cptr,
  193.                                        &StorageTmp->pingCtlOwnerIndex,
  194.                                        &StorageTmp->pingCtlOwnerIndexLen);
  195.             cptr =
  196.                 read_config_store_data(ASN_OCTET_STR, cptr,
  197.                                        &StorageTmp->pingCtlTestName,
  198.                                        &StorageTmp->pingCtlTestNameLen);
  199.             cptr =
  200.                 read_config_store_data(ASN_INTEGER, cptr,
  201.                                        &StorageTmp->pingResultsOperStatus,
  202.                                        &tmpint);
  203.             cptr =
  204.                 read_config_store_data(ASN_INTEGER, cptr,
  205.                                        &StorageTmp->
  206.                                        pingResultsIpTargetAddressType,
  207.                                        &tmpint);
  208.             cptr =
  209.                 read_config_store_data(ASN_OCTET_STR, cptr,
  210.                                        &StorageTmp->
  211.                                        pingResultsIpTargetAddress,
  212.                                        &StorageTmp->
  213.                                        pingResultsIpTargetAddressLen);
  214.             cptr =
  215.                 read_config_store_data(ASN_UNSIGNED, cptr,
  216.                                        &StorageTmp->pingResultsMinRtt,
  217.                                        &tmpint);
  218.             cptr =
  219.                 read_config_store_data(ASN_UNSIGNED, cptr,
  220.                                        &StorageTmp->pingResultsMaxRtt,
  221.                                        &tmpint);
  222.             cptr =
  223.                 read_config_store_data(ASN_UNSIGNED, cptr,
  224.                                        &StorageTmp->pingResultsAverageRtt,
  225.                                        &tmpint);
  226.             cptr =
  227.                 read_config_store_data(ASN_UNSIGNED, cptr,
  228.                                        &StorageTmp->
  229.                                        pingResultsProbeResponses, &tmpint);
  230.             cptr =
  231.                 read_config_store_data(ASN_UNSIGNED, cptr,
  232.                                        &StorageTmp->pingResultsSendProbes,
  233.                                        &tmpint);
  234.             cptr =
  235.                 read_config_store_data(ASN_UNSIGNED, cptr,
  236.                                        &StorageTmp->
  237.                                        pingResultsRttSumOfSquares,
  238.                                        &tmpint);
  239.             cptr =
  240.                 read_config_store_data(ASN_OCTET_STR, cptr,
  241.                                        &StorageTmp->
  242.                                        pingResultsLastGoodProbe,
  243.                                        &StorageTmp->
  244.                                        pingResultsLastGoodProbeLen);
  245.             snmpd_store_config(line);
  246.         }
  247.     }
  248.     DEBUGMSGTL(("pingResultsTable", "done.n"));
  249.     return SNMPERR_SUCCESS;
  250. }
  251. int
  252. pingResultsTable_inadd(struct pingResultsTable_data *thedata)
  253. {
  254.     netsnmp_variable_list *vars_list;
  255.     vars_list = NULL;
  256.     snmp_varlist_add_variable(&vars_list, NULL, 0, ASN_OCTET_STR, (char *) thedata->pingCtlOwnerIndex, thedata->pingCtlOwnerIndexLen);  /* pingCtlOwnerIndex */
  257.     snmp_varlist_add_variable(&vars_list, NULL, 0, ASN_OCTET_STR, (char *) thedata->pingCtlTestName, thedata->pingCtlTestNameLen);      /* pingCtlTestName */
  258.     /*
  259.      * XXX: fill in default row values here into StorageNew 
  260.      * 
  261.      */
  262.     DEBUGMSGTL(("pingResultsTable", "adding data...  "));
  263.     /*
  264.      * add the index variables to the varbind list, which is 
  265.      * used by header_complex to index the data 
  266.      */
  267.     header_complex_add_data(&pingResultsTableStorage, vars_list, thedata);
  268.     DEBUGMSGTL(("pingResultsTable", "registered an entryn"));
  269.     DEBUGMSGTL(("pingResultsTable", "done.n"));
  270.     return SNMPERR_SUCCESS;
  271. }
  272. /*
  273.  * var_pingResultsTable():
  274.  *   Handle this table separately from the scalar value case.
  275.  *   The workings of this are basically the same as for var_pingResultsTable above.
  276.  */
  277. unsigned char  *
  278. var_pingResultsTable(struct variable *vp,
  279.                      oid * name,
  280.                      size_t *length,
  281.                      int exact,
  282.                      size_t *var_len, WriteMethod ** write_method)
  283. {
  284.     struct pingResultsTable_data *StorageTmp = NULL;
  285.     *write_method = NULL;
  286.     /*
  287.      * this assumes you have registered all your data properly
  288.      */
  289.     if ((StorageTmp =
  290.          header_complex(pingResultsTableStorage, vp, name, length, exact,
  291.                         var_len, write_method)) == NULL) {
  292.         return NULL;
  293.     }
  294.     /*
  295.      * this is where we do the value assignments for the mib results.
  296.      */
  297.     switch (vp->magic) {
  298.     case COLUMN_PINGRESULTSOPERSTATUS:
  299.         *var_len = sizeof(StorageTmp->pingResultsOperStatus);
  300.         return (u_char *) & StorageTmp->pingResultsOperStatus;
  301.     case COLUMN_PINGRESULTSIPTARGETADDRESSTYPE:
  302.         *var_len = sizeof(StorageTmp->pingResultsIpTargetAddressType);
  303.         return (u_char *) & StorageTmp->pingResultsIpTargetAddressType;
  304.     case COLUMN_PINGRESULTSIPTARGETADDRESS:
  305.         *var_len = (StorageTmp->pingResultsIpTargetAddressLen);
  306.         return (u_char *) StorageTmp->pingResultsIpTargetAddress;
  307.     case COLUMN_PINGRESULTSMINRTT:
  308.         *var_len = sizeof(StorageTmp->pingResultsMinRtt);
  309.         return (u_char *) & StorageTmp->pingResultsMinRtt;
  310.     case COLUMN_PINGRESULTSMAXRTT:
  311.         *var_len = sizeof(StorageTmp->pingResultsMaxRtt);
  312.         return (u_char *) & StorageTmp->pingResultsMaxRtt;
  313.     case COLUMN_PINGRESULTSAVERAGERTT:
  314.         *var_len = sizeof(StorageTmp->pingResultsAverageRtt);
  315.         return (u_char *) & StorageTmp->pingResultsAverageRtt;
  316.     case COLUMN_PINGRESULTSPROBERESPONSES:
  317.         *var_len = sizeof(StorageTmp->pingResultsProbeResponses);
  318.         return (u_char *) & StorageTmp->pingResultsProbeResponses;
  319.     case COLUMN_PINGRESULTSSENTPROBES:
  320.         *var_len = sizeof(StorageTmp->pingResultsSendProbes);
  321.         return (u_char *) & StorageTmp->pingResultsSendProbes;
  322.     case COLUMN_PINGRESULTSRTTSUMOFSQUARES:
  323.         *var_len = sizeof(StorageTmp->pingResultsRttSumOfSquares);
  324.         return (u_char *) & StorageTmp->pingResultsRttSumOfSquares;
  325.     case COLUMN_PINGRESULTSLASTGOODPROBE:
  326.         *var_len = (StorageTmp->pingResultsLastGoodProbeLen);
  327.         return (u_char *) StorageTmp->pingResultsLastGoodProbe;
  328.     default:
  329.         ERROR_MSG("");
  330.     }
  331.     return NULL;
  332. }