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

SNMP编程

开发平台:

Unix_Linux

  1. /*
  2.  * snmpbulkwalk.c - send SNMPv2 Bulk requests to a network entity, walking a
  3.  * subtree.
  4.  *
  5.  */
  6. /*********************************************************************
  7. Copyright 1988, 1989, 1991, 1992 by Carnegie Mellon University
  8.                       All Rights Reserved
  9. Permission to use, copy, modify, and distribute this software and its 
  10. documentation for any purpose and without fee is hereby granted, 
  11. provided that the above copyright notice appear in all copies and that
  12. both that copyright notice and this permission notice appear in 
  13. supporting documentation, and that the name of CMU not be
  14. used in advertising or publicity pertaining to distribution of the
  15. software without specific, written prior permission.  
  16. CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  17. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  18. CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  19. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  21. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  22. SOFTWARE.
  23. **********************************************************************/
  24. #include <net-snmp/net-snmp-config.h>
  25. #if HAVE_STDLIB_H
  26. #include <stdlib.h>
  27. #endif
  28. #if HAVE_UNISTD_H
  29. #include <unistd.h>
  30. #endif
  31. #if HAVE_STRING_H
  32. #include <string.h>
  33. #else
  34. #include <strings.h>
  35. #endif
  36. #include <sys/types.h>
  37. #if HAVE_NETINET_IN_H
  38. #include <netinet/in.h>
  39. #endif
  40. #if TIME_WITH_SYS_TIME
  41. # ifdef WIN32
  42. #  include <sys/timeb.h>
  43. # else
  44. #  include <sys/time.h>
  45. # endif
  46. # include <time.h>
  47. #else
  48. # if HAVE_SYS_TIME_H
  49. #  include <sys/time.h>
  50. # else
  51. #  include <time.h>
  52. # endif
  53. #endif
  54. #if HAVE_SYS_SELECT_H
  55. #include <sys/select.h>
  56. #endif
  57. #include <stdio.h>
  58. #include <ctype.h>
  59. #if HAVE_WINSOCK_H
  60. #include <winsock.h>
  61. #endif
  62. #if HAVE_NETDB_H
  63. #include <netdb.h>
  64. #endif
  65. #if HAVE_ARPA_INET_H
  66. #include <arpa/inet.h>
  67. #endif
  68. #include <net-snmp/net-snmp-includes.h>
  69. #define NETSNMP_DS_WALK_INCLUDE_REQUESTED 1
  70. #define NETSNMP_DS_WALK_PRINT_STATISTICS 2
  71. #define NETSNMP_DS_WALK_DONT_CHECK_LEXICOGRAPHIC 3
  72. oid             objid_mib[] = { 1, 3, 6, 1, 2, 1 };
  73. int             numprinted = 0;
  74. int             reps = 10, non_reps = 0;
  75. void
  76. usage(void)
  77. {
  78.     fprintf(stderr, "USAGE: snmpbulkwalk ");
  79.     snmp_parse_args_usage(stderr);
  80.     fprintf(stderr, " [OID]nn");
  81.     snmp_parse_args_descriptions(stderr);
  82.     fprintf(stderr,
  83.             "  -C APPOPTSttSet various application specific behaviours:n");
  84.     fprintf(stderr,
  85.             "ttt  c:       do not check returned OIDs are increasingn");
  86.     fprintf(stderr,
  87.             "ttt  i:       include given OIDs in the search rangen");
  88.     fprintf(stderr, "ttt  n<NUM>:  set non-repeaters to <NUM>n");
  89.     fprintf(stderr,
  90.             "ttt  p:       print the number of variables foundn");
  91.     fprintf(stderr, "ttt  r<NUM>:  set max-repeaters to <NUM>n");
  92. }
  93. static void
  94. snmp_get_and_print(netsnmp_session * ss, oid * theoid, size_t theoid_len)
  95. {
  96.     netsnmp_pdu    *pdu, *response;
  97.     netsnmp_variable_list *vars;
  98.     int             status;
  99.     pdu = snmp_pdu_create(SNMP_MSG_GET);
  100.     snmp_add_null_var(pdu, theoid, theoid_len);
  101.     status = snmp_synch_response(ss, pdu, &response);
  102.     if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR) {
  103.         for (vars = response->variables; vars; vars = vars->next_variable) {
  104.             numprinted++;
  105.             print_variable(vars->name, vars->name_length, vars);
  106.         }
  107.     }
  108.     if (response) {
  109.         snmp_free_pdu(response);
  110.     }
  111. }
  112. static
  113.     void
  114. optProc(int argc, char *const *argv, int opt)
  115. {
  116.     char           *endptr = NULL;
  117.     switch (opt) {
  118.     case 'C':
  119.         while (*optarg) {
  120.             switch (*optarg++) {
  121.             case 'c':
  122.                 netsnmp_ds_toggle_boolean(NETSNMP_DS_APPLICATION_ID,
  123.      NETSNMP_DS_WALK_DONT_CHECK_LEXICOGRAPHIC);
  124.                 break;
  125.             case 'i':
  126.                 netsnmp_ds_toggle_boolean(NETSNMP_DS_APPLICATION_ID,
  127.   NETSNMP_DS_WALK_INCLUDE_REQUESTED);
  128.                 break;
  129.             case 'n':
  130.             case 'r':
  131.                 if (*(optarg - 1) == 'r') {
  132.                     reps = strtol(optarg, &endptr, 0);
  133.                 } else {
  134.                     non_reps = strtol(optarg, &endptr, 0);
  135.                 }
  136.                 if (endptr == optarg) {
  137.                     /*
  138.                      * No number given -- error.  
  139.                      */
  140.                     usage();
  141.                     exit(1);
  142.                 } else {
  143.                     optarg = endptr;
  144.                     if (isspace(*optarg)) {
  145.                         return;
  146.                     }
  147.                 }
  148.                 break;
  149.             case 'p':
  150.                 netsnmp_ds_toggle_boolean(NETSNMP_DS_APPLICATION_ID,
  151.   NETSNMP_DS_WALK_PRINT_STATISTICS);
  152.                 break;
  153.             default:
  154.                 fprintf(stderr, "Unknown flag passed to -C: %cn",
  155.                         optarg[-1]);
  156.                 exit(1);
  157.             }
  158.         }
  159.         break;
  160.     }
  161. }
  162. int
  163. main(int argc, char *argv[])
  164. {
  165.     netsnmp_session session, *ss;
  166.     netsnmp_pdu    *pdu, *response;
  167.     netsnmp_variable_list *vars;
  168.     int             arg;
  169.     oid             name[MAX_OID_LEN];
  170.     size_t          name_length;
  171.     oid             root[MAX_OID_LEN];
  172.     size_t          rootlen;
  173.     int             count;
  174.     int             running;
  175.     int             status;
  176.     int             check;
  177.     int             exitval = 0;
  178.     netsnmp_ds_register_config(ASN_BOOLEAN, "snmpwalk", "includeRequested",
  179.        NETSNMP_DS_APPLICATION_ID, 
  180.        NETSNMP_DS_WALK_INCLUDE_REQUESTED);
  181.     netsnmp_ds_register_config(ASN_BOOLEAN, "snmpwalk", "printStatistics",
  182.        NETSNMP_DS_APPLICATION_ID, 
  183.        NETSNMP_DS_WALK_PRINT_STATISTICS);
  184.     netsnmp_ds_register_config(ASN_BOOLEAN, "snmpwalk", "dontCheckOrdering",
  185.        NETSNMP_DS_APPLICATION_ID,
  186.        NETSNMP_DS_WALK_DONT_CHECK_LEXICOGRAPHIC);
  187.     /*
  188.      * get the common command line arguments 
  189.      */
  190.     switch (arg = snmp_parse_args(argc, argv, &session, "C:", optProc)) {
  191.     case -2:
  192.         exit(0);
  193.     case -1:
  194.         usage();
  195.         exit(1);
  196.     default:
  197.         break;
  198.     }
  199.     /*
  200.      * get the initial object and subtree 
  201.      */
  202.     if (arg < argc) {
  203.         /*
  204.          * specified on the command line 
  205.          */
  206.         rootlen = MAX_OID_LEN;
  207.         if (snmp_parse_oid(argv[arg], root, &rootlen) == NULL) {
  208.             snmp_perror(argv[arg]);
  209.             exit(1);
  210.         }
  211.     } else {
  212.         /*
  213.          * use default value 
  214.          */
  215.         memmove(root, objid_mib, sizeof(objid_mib));
  216.         rootlen = sizeof(objid_mib) / sizeof(oid);
  217.     }
  218.     SOCK_STARTUP;
  219.     /*
  220.      * open an SNMP session 
  221.      */
  222.     ss = snmp_open(&session);
  223.     if (ss == NULL) {
  224.         /*
  225.          * diagnose snmp_open errors with the input netsnmp_session pointer 
  226.          */
  227.         snmp_sess_perror("snmpbulkwalk", &session);
  228.         SOCK_CLEANUP;
  229.         exit(1);
  230.     }
  231.     /*
  232.      * setup initial object name 
  233.      */
  234.     memmove(name, root, rootlen * sizeof(oid));
  235.     name_length = rootlen;
  236.     running = 1;
  237.     check = !netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
  238.     NETSNMP_DS_WALK_DONT_CHECK_LEXICOGRAPHIC);
  239.     if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
  240.        NETSNMP_DS_WALK_INCLUDE_REQUESTED)) {
  241.         snmp_get_and_print(ss, root, rootlen);
  242.     }
  243.     while (running) {
  244.         /*
  245.          * create PDU for GETBULK request and add object name to request 
  246.          */
  247.         pdu = snmp_pdu_create(SNMP_MSG_GETBULK);
  248.         pdu->non_repeaters = non_reps;
  249.         pdu->max_repetitions = reps;    /* fill the packet */
  250.         snmp_add_null_var(pdu, name, name_length);
  251.         /*
  252.          * do the request 
  253.          */
  254.         status = snmp_synch_response(ss, pdu, &response);
  255.         if (status == STAT_SUCCESS) {
  256.             if (response->errstat == SNMP_ERR_NOERROR) {
  257.                 /*
  258.                  * check resulting variables 
  259.                  */
  260.                 for (vars = response->variables; vars;
  261.                      vars = vars->next_variable) {
  262.                     if ((vars->name_length < rootlen)
  263.                         || (memcmp(root, vars->name, rootlen * sizeof(oid))
  264.                             != 0)) {
  265.                         /*
  266.                          * not part of this subtree 
  267.                          */
  268.                         running = 0;
  269.                         continue;
  270.                     }
  271.                     numprinted++;
  272.                     print_variable(vars->name, vars->name_length, vars);
  273.                     if ((vars->type != SNMP_ENDOFMIBVIEW) &&
  274.                         (vars->type != SNMP_NOSUCHOBJECT) &&
  275.                         (vars->type != SNMP_NOSUCHINSTANCE)) {
  276.                         /*
  277.                          * not an exception value 
  278.                          */
  279.                         if (check
  280.                             && snmp_oid_compare(name, name_length,
  281.                                                 vars->name,
  282.                                                 vars->name_length) >= 0) {
  283.                             fprintf(stderr, "Error: OID not increasing: ");
  284.                             fprint_objid(stderr, name, name_length);
  285.                             fprintf(stderr, " >= ");
  286.                             fprint_objid(stderr, vars->name,
  287.                                          vars->name_length);
  288.                             fprintf(stderr, "n");
  289.                             running = 0;
  290.                             exitval = 1;
  291.                         }
  292.                         /*
  293.                          * Check if last variable, and if so, save for next request.  
  294.                          */
  295.                         if (vars->next_variable == NULL) {
  296.                             memmove(name, vars->name,
  297.                                     vars->name_length * sizeof(oid));
  298.                             name_length = vars->name_length;
  299.                         }
  300.                     } else {
  301.                         /*
  302.                          * an exception value, so stop 
  303.                          */
  304.                         running = 0;
  305.                     }
  306.                 }
  307.             } else {
  308.                 /*
  309.                  * error in response, print it 
  310.                  */
  311.                 running = 0;
  312.                 if (response->errstat == SNMP_ERR_NOSUCHNAME) {
  313.                     printf("End of MIBn");
  314.                 } else {
  315.                     fprintf(stderr, "Error in packet.nReason: %sn",
  316.                             snmp_errstring(response->errstat));
  317.                     if (response->errindex != 0) {
  318.                         fprintf(stderr, "Failed object: ");
  319.                         for (count = 1, vars = response->variables;
  320.                              vars && count != response->errindex;
  321.                              vars = vars->next_variable, count++)
  322.                             /*EMPTY*/;
  323.                         if (vars)
  324.                             fprint_objid(stderr, vars->name,
  325.                                          vars->name_length);
  326.                         fprintf(stderr, "n");
  327.                     }
  328.                     exitval = 2;
  329.                 }
  330.             }
  331.         } else if (status == STAT_TIMEOUT) {
  332.             fprintf(stderr, "Timeout: No Response from %sn",
  333.                     session.peername);
  334.             running = 0;
  335.             exitval = 1;
  336.         } else {                /* status == STAT_ERROR */
  337.             snmp_sess_perror("snmpbulkwalk", ss);
  338.             running = 0;
  339.             exitval = 1;
  340.         }
  341.         if (response)
  342.             snmp_free_pdu(response);
  343.     }
  344.     if (numprinted == 0 && status == STAT_SUCCESS) {
  345.         /*
  346.          * no printed successful results, which may mean we were
  347.          * pointed at an only existing instance.  Attempt a GET, just
  348.          * for get measure. 
  349.          */
  350.         snmp_get_and_print(ss, root, rootlen);
  351.     }
  352.     snmp_close(ss);
  353.     if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, 
  354.        NETSNMP_DS_WALK_PRINT_STATISTICS)) {
  355.         printf("Variables found: %dn", numprinted);
  356.     }
  357.     SOCK_CLEANUP;
  358.     return exitval;
  359. }