home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / sys / sun / apps / 2805 < prev    next >
Encoding:
Text File  |  1992-12-18  |  5.7 KB  |  199 lines

  1. Newsgroups: comp.sys.sun.apps
  2. Path: sparky!uunet!destroyer!sol.ctr.columbia.edu!news.cs.columbia.edu!boxhill.com!tim
  3. From: tim@boxhill.com (Timothy Jones)
  4. Subject: SunNet Manager 2.0 -- management API / lousy examples in manual
  5. Message-ID: <TIM.92Dec17163153@jake.boxhill.com>
  6. Lines: 188
  7. Sender: news@boxhill.com
  8. Organization: Box Hill Systems Corporation, New York, NY
  9. Date: Thu, 17 Dec 1992 21:31:55 GMT
  10.  
  11. I'm trying to write a very simple management application that gets the value
  12. of the system.sysDescr SNMP variable from the Sun-supplied agent.  I've glued
  13. together some sample code from the Programmer's Guide, which has some
  14. confusing inconsistencies.  For example, on page 4-2, the option to specify
  15. the community for read requests is named "na.snmp-read-community".  On page
  16. 4-4, the sample code useds "na.snmp.read-community".  There are other errors
  17. like this.
  18.  
  19. I've included the code below.  The problem is that the callback routine is
  20. never invoked.
  21.  
  22. Has anyone got a skeleton program that correctly uses the management API
  23. they're willing to share?  Sun's is a little less than useful...  I've looked
  24. at the Sun mail server and the ftp area at arizona, but can't find any working
  25. examples...
  26.  
  27. Thanks,
  28. Tim
  29.  
  30. --- cut here ---
  31.  
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <sys/param.h>
  35. #include <sys/types.h>
  36. #include <sys/time.h>
  37.  
  38. #include "netmgt.h"
  39. #include "netmgt_db.h"
  40.  
  41. #define AGENT "snmp"
  42. #define HOST "jake"
  43. #define GROUP "system"
  44.  
  45. void report_handler (u_int, char *, char *, char *, u_int, struct timeval, u_int);
  46.  
  47. void main (int argc, char *argv[])
  48. {
  49.     char myname[MAXHOSTNAMELEN];
  50.     int sock_udp, sock_tcp;
  51.     u_long rendez;
  52.     static struct timeval timeout = { NETMGT_TIMEOUT, 0 };
  53.     static struct timeval interval = { 0, 0 };
  54.     struct rpcent *agent_rpc;
  55.     struct timeval *timestamp;
  56.     Netmgt_arg option;
  57.     char *community = "public";
  58.     char *get_list = "1.3.6.1.2.1.1.1.0";
  59.  
  60.     if (gethostname (myname, MAXHOSTNAMELEN) == -1)
  61.     {
  62.     perror ("gethostname");
  63.     exit (1);
  64.     }
  65.  
  66.     /* initialize RPC stuff to talk to SNM */
  67.     sock_udp = RPC_ANYSOCK;
  68.     sock_tcp = RPC_ANYSOCK;
  69.     if (! (rendez = netmgt_register_callback (report_handler, &sock_udp,
  70.                            &sock_tcp, NETMGT_VERS,
  71.                            IPPROTO_UDP | IPPROTO_TCP)))
  72.     {
  73.     (void) fprintf (stderr, "Can't register callback: %s\n",
  74.             netmgt_sperror ());
  75.     exit (1);
  76.     }
  77.  
  78.     /* register handler for reports received */
  79.     if (! netmgt_register_rendez (myname, myname, rendez, NETMGT_VERS,
  80.                   NETMGT_ANY_AGENT, NETMGT_LOW_PRIORITY,
  81.                   timeout))
  82.     {
  83.     (void) fprintf (stderr, "Can't register with the event dispatcher: %s\n",
  84.             netmgt_sperror ());
  85.     (void) netmgt_unregister_callback (rendez, NETMGT_VERS);
  86.     exit (1);
  87.     }
  88.  
  89.     /* get RPC info for agent */
  90.     if (! (agent_rpc = getrpcbyname (AGENT)))
  91.     {
  92.     (void) fprintf (stderr, "Can't get RPC info for agent \"%s\"\n",
  93.             AGENT);
  94.     (void) netmgt_unregister_callback (rendez, NETMGT_VERS);
  95.     exit (1);
  96.     }
  97.  
  98.     /* tell agent what we're interested in */
  99.     if (! netmgt_set_instance (HOST, GROUP, 0))
  100.     {
  101.     (void) fprintf (stderr, "set_instance failed for %s: %s\n",
  102.             HOST, netmgt_sperror ());
  103.     (void) netmgt_unregister_callback (rendez, NETMGT_VERS);
  104.     exit (1);
  105.     }
  106.  
  107.     /* specify community */
  108.     strcpy (option.name, "na.snmp-read-community");
  109.     option.type = NETMGT_STRING;
  110.     option.length = strlen (community) + 1;
  111.     option.value = community;
  112.     if (! netmgt_set_argument (&option))
  113.     {
  114.     NETMGT_DBG ("netmgt_set_argument failed: %s\n", netmgt_sperror ());
  115.     (void) netmgt_unregister_callback (rendez, NETMGT_VERS);
  116.     exit (1);
  117.     }            
  118.  
  119.     /* specify variable list */
  120.     strcpy (option.name, "na.snmp-get-list");
  121.     option.type = NETMGT_STRING;
  122.     option.length = strlen (get_list) + 1;
  123.     option.value = get_list;
  124.     if (! netmgt_set_argument (&option))
  125.     {
  126.     NETMGT_DBG ("netmgt_set_argument failed: %s\n", netmgt_sperror ());
  127.     (void) netmgt_unregister_callback (rendez, NETMGT_VERS);
  128.     exit (1);
  129.     }
  130.  
  131.     if (! (timestamp = netmgt_request_data (HOST,
  132.                          (u_long) agent_rpc->r_number,
  133.                          NETMGT_VERS, myname, 
  134.                          NETMGT_EVENT_PROG, NETMGT_VERS,
  135.                          1, interval, timeout, 0)))
  136.     {
  137.     (void) fprintf (stderr, "Can't request event report: %s\n", 
  138.             netmgt_sperror ());
  139.     (void) netmgt_unregister_callback (rendez, NETMGT_VERS);
  140.     exit (1);
  141.     }
  142.  
  143.     /* enter RPC loop */
  144.     svc_run ();
  145.  
  146.     (void) fprintf (stderr, "Returned from svc_run() !\n");
  147.     exit (1);
  148. }
  149.  
  150. void report_handler (u_int type, char *target, char *group, char *key, 
  151.              u_int count, struct timeval interval, u_int flags)
  152. {
  153.     switch ( type )
  154.     {
  155.     case NETMGT_DATA_REPORT:
  156.     (void) printf ("got data report from %s, group %s", target, group);
  157.     break;
  158.     case NETMGT_EVENT_REPORT:
  159.     (void) printf ("got event report from %s, group %s", target, group);
  160.     break;
  161.     case NETMGT_TRAP_REPORT:
  162.     (void) printf ("got trap report from %s, group %s", target, group);
  163.     break;
  164.     case NETMGT_ERROR_REPORT:
  165.     (void) printf ("got error report from %s, group %s", target, group);
  166.     break;
  167.     }    
  168.  
  169.     if (key && key[0])
  170.     {
  171.     (void) printf (", key = %s", key);
  172.     }
  173.  
  174.     (void) printf (", count = %u, interval = %d.%d, flags = %d\n",
  175.            count, interval.tv_sec, interval.tv_usec, flags);
  176.  
  177.     if ( type == NETMGT_ERROR_REPORT )
  178.     {
  179.     Netmgt_error error;
  180.     int x;
  181.  
  182.     if (! netmgt_fetch_error ( &error ) )
  183.     {
  184.         (void) fprintf (stderr, 
  185.                 "Couldn't get detailed error information.\n");
  186.         exit (1);
  187.     }
  188.  
  189.     x = 0;
  190.     }
  191.  
  192. } /* report_handler */
  193.  
  194. --- cut here ---
  195. --
  196. Timothy Jones                                         internet: tim@boxhill.com
  197. Box Hill Systems Corporation                                voice: 212-989-4455
  198. 161 Avenue of the Americas, New York, NY, 10013               fax: 212-989-6817
  199.