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

SNMP编程

开发平台:

Unix_Linux

  1. /*
  2.  * snmpwalk.c - send snmp GETNEXT 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. #if HAVE_WINSOCK_H
  59. #include <winsock.h>
  60. #endif
  61. #if HAVE_NETDB_H
  62. #include <netdb.h>
  63. #endif
  64. #if HAVE_ARPA_INET_H
  65. #include <arpa/inet.h>
  66. #endif
  67. #include <net-snmp/net-snmp-includes.h>
  68. #define NETSNMP_DS_WALK_INCLUDE_REQUESTED         1
  69. #define NETSNMP_DS_WALK_PRINT_STATISTICS         2
  70. #define NETSNMP_DS_WALK_DONT_CHECK_LEXICOGRAPHIC 3
  71. #define NETSNMP_DS_WALK_TIME_RESULTS              4
  72. #define NETSNMP_DS_WALK_DONT_GET_REQUESTED         5
  73. oid             objid_mib[] = { 1, 3, 6, 1, 2, 1 };
  74. int             numprinted = 0;
  75. void
  76. usage(void)
  77. {
  78.     fprintf(stderr, "USAGE: snmpwalk ");
  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, "ttt  p:  print the number of variables foundn");
  85.     fprintf(stderr, "ttt  i:  include given OID in the search rangen");
  86.     fprintf(stderr, "ttt  I:  don't include the given OID, even if no results are returnedn");
  87.     fprintf(stderr,
  88.             "ttt  c:  do not check returned OIDs are increasingn");
  89.     fprintf(stderr,
  90.             "ttt  t:  Display wall-clock time to complete the requestn");
  91. }
  92. void
  93. snmp_get_and_print(netsnmp_session * ss, oid * theoid, size_t theoid_len)
  94. {
  95.     netsnmp_pdu    *pdu, *response;
  96.     netsnmp_variable_list *vars;
  97.     int             status;
  98.     pdu = snmp_pdu_create(SNMP_MSG_GET);
  99.     snmp_add_null_var(pdu, theoid, theoid_len);
  100.     status = snmp_synch_response(ss, pdu, &response);
  101.     if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR) {
  102.         for (vars = response->variables; vars; vars = vars->next_variable) {
  103.             numprinted++;
  104.             print_variable(vars->name, vars->name_length, vars);
  105.         }
  106.     }
  107.     if (response) {
  108.         snmp_free_pdu(response);
  109.     }
  110. }
  111. static void
  112. optProc(int argc, char *const *argv, int opt)
  113. {
  114.     switch (opt) {
  115.     case 'C':
  116.         while (*optarg) {
  117.             switch (*optarg++) {
  118.             case 'i':
  119.                 netsnmp_ds_toggle_boolean(NETSNMP_DS_APPLICATION_ID,
  120.   NETSNMP_DS_WALK_INCLUDE_REQUESTED);
  121.                 break;
  122.             case 'I':
  123.                 netsnmp_ds_toggle_boolean(NETSNMP_DS_APPLICATION_ID,
  124.   NETSNMP_DS_WALK_DONT_GET_REQUESTED);
  125.                 break;
  126.             case 'p':
  127.                 netsnmp_ds_toggle_boolean(NETSNMP_DS_APPLICATION_ID,
  128.   NETSNMP_DS_WALK_PRINT_STATISTICS);
  129.                 break;
  130.             case 'c':
  131.                 netsnmp_ds_toggle_boolean(NETSNMP_DS_APPLICATION_ID,
  132.     NETSNMP_DS_WALK_DONT_CHECK_LEXICOGRAPHIC);
  133.                 break;
  134.             case 't':
  135.                 netsnmp_ds_toggle_boolean(NETSNMP_DS_APPLICATION_ID,
  136.                                           NETSNMP_DS_WALK_TIME_RESULTS);
  137.                 break;
  138.                 
  139.             default:
  140.                 fprintf(stderr, "Unknown flag passed to -C: %cn",
  141.                         optarg[-1]);
  142.                 exit(1);
  143.             }
  144.         }
  145.         break;
  146.     }
  147. }
  148. int
  149. main(int argc, char *argv[])
  150. {
  151.     netsnmp_session session, *ss;
  152.     netsnmp_pdu    *pdu, *response;
  153.     netsnmp_variable_list *vars;
  154.     int             arg;
  155.     oid             name[MAX_OID_LEN];
  156.     size_t          name_length;
  157.     oid             root[MAX_OID_LEN];
  158.     size_t          rootlen;
  159.     int             count;
  160.     int             running;
  161.     int             status;
  162.     int             check;
  163.     int             exitval = 0;
  164.     struct timeval  tv1, tv2;
  165.     netsnmp_ds_register_config(ASN_BOOLEAN, "snmpwalk", "includeRequested",
  166.        NETSNMP_DS_APPLICATION_ID, 
  167.        NETSNMP_DS_WALK_INCLUDE_REQUESTED);
  168.     netsnmp_ds_register_config(ASN_BOOLEAN, "snmpwalk", "excludeRequested",
  169.        NETSNMP_DS_APPLICATION_ID, 
  170.        NETSNMP_DS_WALK_DONT_GET_REQUESTED);
  171.     netsnmp_ds_register_config(ASN_BOOLEAN, "snmpwalk", "printStatistics",
  172.        NETSNMP_DS_APPLICATION_ID, 
  173.        NETSNMP_DS_WALK_PRINT_STATISTICS);
  174.     netsnmp_ds_register_config(ASN_BOOLEAN, "snmpwalk", "dontCheckOrdering",
  175.        NETSNMP_DS_APPLICATION_ID,
  176.        NETSNMP_DS_WALK_DONT_CHECK_LEXICOGRAPHIC);
  177.     netsnmp_ds_register_config(ASN_BOOLEAN, "snmpwalk", "timeResults",
  178.                                NETSNMP_DS_APPLICATION_ID,
  179.        NETSNMP_DS_WALK_TIME_RESULTS);
  180.     /*
  181.      * get the common command line arguments 
  182.      */
  183.     switch (arg = snmp_parse_args(argc, argv, &session, "C:", optProc)) {
  184.     case -2:
  185.         exit(0);
  186.     case -1:
  187.         usage();
  188.         exit(1);
  189.     default:
  190.         break;
  191.     }
  192.     /*
  193.      * get the initial object and subtree 
  194.      */
  195.     if (arg < argc) {
  196.         /*
  197.          * specified on the command line 
  198.          */
  199.         rootlen = MAX_OID_LEN;
  200.         if (snmp_parse_oid(argv[arg], root, &rootlen) == NULL) {
  201.             snmp_perror(argv[arg]);
  202.             exit(1);
  203.         }
  204.     } else {
  205.         /*
  206.          * use default value 
  207.          */
  208.         memmove(root, objid_mib, sizeof(objid_mib));
  209.         rootlen = sizeof(objid_mib) / sizeof(oid);
  210.     }
  211.     SOCK_STARTUP;
  212.     /*
  213.      * open an SNMP session 
  214.      */
  215.     ss = snmp_open(&session);
  216.     if (ss == NULL) {
  217.         /*
  218.          * diagnose snmp_open errors with the input netsnmp_session pointer 
  219.          */
  220.         snmp_sess_perror("snmpwalk", &session);
  221.         SOCK_CLEANUP;
  222.         exit(1);
  223.     }
  224.     /*
  225.      * get first object to start walk 
  226.      */
  227.     memmove(name, root, rootlen * sizeof(oid));
  228.     name_length = rootlen;
  229.     running = 1;
  230.     check =
  231.         !netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
  232.                         NETSNMP_DS_WALK_DONT_CHECK_LEXICOGRAPHIC);
  233.     if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_WALK_INCLUDE_REQUESTED)) {
  234.         snmp_get_and_print(ss, root, rootlen);
  235.     }
  236.     if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
  237.                                NETSNMP_DS_WALK_TIME_RESULTS))
  238.         gettimeofday(&tv1, NULL);
  239.     while (running) {
  240.         /*
  241.          * create PDU for GETNEXT request and add object name to request 
  242.          */
  243.         pdu = snmp_pdu_create(SNMP_MSG_GETNEXT);
  244.         snmp_add_null_var(pdu, name, name_length);
  245.         /*
  246.          * do the request 
  247.          */
  248.         status = snmp_synch_response(ss, pdu, &response);
  249.         if (status == STAT_SUCCESS) {
  250.             if (response->errstat == SNMP_ERR_NOERROR) {
  251.                 /*
  252.                  * check resulting variables 
  253.                  */
  254.                 for (vars = response->variables; vars;
  255.                      vars = vars->next_variable) {
  256.                     if ((vars->name_length < rootlen)
  257.                         || (memcmp(root, vars->name, rootlen * sizeof(oid))
  258.                             != 0)) {
  259.                         /*
  260.                          * not part of this subtree 
  261.                          */
  262.                         running = 0;
  263.                         continue;
  264.                     }
  265.                     numprinted++;
  266.                     print_variable(vars->name, vars->name_length, vars);
  267.                     if ((vars->type != SNMP_ENDOFMIBVIEW) &&
  268.                         (vars->type != SNMP_NOSUCHOBJECT) &&
  269.                         (vars->type != SNMP_NOSUCHINSTANCE)) {
  270.                         /*
  271.                          * not an exception value 
  272.                          */
  273.                         if (check
  274.                             && snmp_oid_compare(name, name_length,
  275.                                                 vars->name,
  276.                                                 vars->name_length) >= 0) {
  277.                             fprintf(stderr, "Error: OID not increasing: ");
  278.                             fprint_objid(stderr, name, name_length);
  279.                             fprintf(stderr, " >= ");
  280.                             fprint_objid(stderr, vars->name,
  281.                                          vars->name_length);
  282.                             fprintf(stderr, "n");
  283.                             running = 0;
  284.                             exitval = 1;
  285.                         }
  286.                         memmove((char *) name, (char *) vars->name,
  287.                                 vars->name_length * sizeof(oid));
  288.                         name_length = vars->name_length;
  289.                     } else
  290.                         /*
  291.                          * an exception value, so stop 
  292.                          */
  293.                         running = 0;
  294.                 }
  295.             } else {
  296.                 /*
  297.                  * error in response, print it 
  298.                  */
  299.                 running = 0;
  300.                 if (response->errstat == SNMP_ERR_NOSUCHNAME) {
  301.                     printf("End of MIBn");
  302.                 } else {
  303.                     fprintf(stderr, "Error in packet.nReason: %sn",
  304.                             snmp_errstring(response->errstat));
  305.                     if (response->errindex != 0) {
  306.                         fprintf(stderr, "Failed object: ");
  307.                         for (count = 1, vars = response->variables;
  308.                              vars && count != response->errindex;
  309.                              vars = vars->next_variable, count++)
  310.                             /*EMPTY*/;
  311.                         if (vars)
  312.                             fprint_objid(stderr, vars->name,
  313.                                          vars->name_length);
  314.                         fprintf(stderr, "n");
  315.                     }
  316.                     exitval = 2;
  317.                 }
  318.             }
  319.         } else if (status == STAT_TIMEOUT) {
  320.             fprintf(stderr, "Timeout: No Response from %sn",
  321.                     session.peername);
  322.             running = 0;
  323.             exitval = 1;
  324.         } else {                /* status == STAT_ERROR */
  325.             snmp_sess_perror("snmpwalk", ss);
  326.             running = 0;
  327.             exitval = 1;
  328.         }
  329.         if (response)
  330.             snmp_free_pdu(response);
  331.     }
  332.     if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
  333.                                NETSNMP_DS_WALK_TIME_RESULTS))
  334.         gettimeofday(&tv2, NULL);
  335.     if (numprinted == 0 && status == STAT_SUCCESS) {
  336.         /*
  337.          * no printed successful results, which may mean we were
  338.          * pointed at an only existing instance.  Attempt a GET, just
  339.          * for get measure. 
  340.          */
  341.         if (!netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_WALK_DONT_GET_REQUESTED)) {
  342.             snmp_get_and_print(ss, root, rootlen);
  343.         }
  344.     }
  345.     snmp_close(ss);
  346.     if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
  347.                                NETSNMP_DS_WALK_PRINT_STATISTICS)) {
  348.         printf("Variables found: %dn", numprinted);
  349.     }
  350.     if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
  351.                                NETSNMP_DS_WALK_TIME_RESULTS)) {
  352.         fprintf (stderr, "Total traversal time = %f secondsn",
  353.                  (double) (tv2.tv_usec - tv1.tv_usec)/1000000 +
  354.                  (double) (tv2.tv_sec - tv1.tv_sec));
  355.     }
  356.     SOCK_CLEANUP;
  357.     return exitval;
  358. }