From: tim@boxhill.com (Timothy Jones)
Subject: SunNet Manager 2.0 -- management API / lousy examples in manual
Organization: Box Hill Systems Corporation, New York, NY
Date: Thu, 17 Dec 1992 21:31:55 GMT

I'm trying to write a very simple management application that gets the value
of the system.sysDescr SNMP variable from the Sun-supplied agent.  I've glued
together some sample code from the Programmer's Guide, which has some
confusing inconsistencies.  For example, on page 4-2, the option to specify
the community for read requests is named "na.snmp-read-community".  On page
4-4, the sample code useds "na.snmp.read-community".  There are other errors
like this.

I've included the code below.  The problem is that the callback routine is
never invoked.

Has anyone got a skeleton program that correctly uses the management API
they're willing to share?  Sun's is a little less than useful...  I've looked
at the Sun mail server and the ftp area at arizona, but can't find any working
examples...

Thanks,
Tim

--- cut here ---

#include <stdio.h>
#include <string.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/time.h>

#include "netmgt.h"
#include "netmgt_db.h"

#define AGENT "snmp"
#define HOST "jake"
#define GROUP "system"

void report_handler (u_int, char *, char *, char *, u_int, struct timeval, u_int);

void main (int argc, char *argv[])
{
    char myname[MAXHOSTNAMELEN];
    int sock_udp, sock_tcp;
    u_long rendez;
    static struct timeval timeout = { NETMGT_TIMEOUT, 0 };
    static struct timeval interval = { 0, 0 };
    struct rpcent *agent_rpc;
    struct timeval *timestamp;
    Netmgt_arg option;
    char *community = "public";
    char *get_list = "1.3.6.1.2.1.1.1.0";

    if (gethostname (myname, MAXHOSTNAMELEN) == -1)
    {
	perror ("gethostname");
	exit (1);
    }

    /* initialize RPC stuff to talk to SNM */
    sock_udp = RPC_ANYSOCK;
    sock_tcp = RPC_ANYSOCK;
    if (! (rendez = netmgt_register_callback (report_handler, &sock_udp,
					       &sock_tcp, NETMGT_VERS,
					       IPPROTO_UDP | IPPROTO_TCP)))
    {
	(void) fprintf (stderr, "Can't register callback: %s\n",
			netmgt_sperror ());
	exit (1);
    }

    /* register handler for reports received */
    if (! netmgt_register_rendez (myname, myname, rendez, NETMGT_VERS,
				  NETMGT_ANY_AGENT, NETMGT_LOW_PRIORITY,
				  timeout))
    {
	(void) fprintf (stderr, "Can't register with the event dispatcher: %s\n",
			netmgt_sperror ());
	(void) netmgt_unregister_callback (rendez, NETMGT_VERS);
	exit (1);
    }

    /* get RPC info for agent */
    if (! (agent_rpc = getrpcbyname (AGENT)))
    {
	(void) fprintf (stderr, "Can't get RPC info for agent \"%s\"\n",
			AGENT);
	(void) netmgt_unregister_callback (rendez, NETMGT_VERS);
	exit (1);
    }

    /* tell agent what we're interested in */
    if (! netmgt_set_instance (HOST, GROUP, 0))
    {
	(void) fprintf (stderr, "set_instance failed for %s: %s\n",
			HOST, netmgt_sperror ());
	(void) netmgt_unregister_callback (rendez, NETMGT_VERS);
	exit (1);
    }

    /* specify community */
    strcpy (option.name, "na.snmp-read-community");
    option.type = NETMGT_STRING;
    option.length = strlen (community) + 1;
    option.value = community;
    if (! netmgt_set_argument (&option))
    {
	NETMGT_DBG ("netmgt_set_argument failed: %s\n", netmgt_sperror ());
	(void) netmgt_unregister_callback (rendez, NETMGT_VERS);
	exit (1);
    }		    

    /* specify variable list */
    strcpy (option.name, "na.snmp-get-list");
    option.type = NETMGT_STRING;
    option.length = strlen (get_list) + 1;
    option.value = get_list;
    if (! netmgt_set_argument (&option))
    {
	NETMGT_DBG ("netmgt_set_argument failed: %s\n", netmgt_sperror ());
	(void) netmgt_unregister_callback (rendez, NETMGT_VERS);
	exit (1);
    }

    if (! (timestamp = netmgt_request_data (HOST,
					     (u_long) agent_rpc->r_number,
					     NETMGT_VERS, myname, 
					     NETMGT_EVENT_PROG, NETMGT_VERS,
					     1, interval, timeout, 0)))
    {
	(void) fprintf (stderr, "Can't request event report: %s\n", 
			netmgt_sperror ());
	(void) netmgt_unregister_callback (rendez, NETMGT_VERS);
	exit (1);
    }

    /* enter RPC loop */
    svc_run ();

    (void) fprintf (stderr, "Returned from svc_run() !\n");
    exit (1);
}

void report_handler (u_int type, char *target, char *group, char *key, 
		     u_int count, struct timeval interval, u_int flags)
{
    switch ( type )
    {
    case NETMGT_DATA_REPORT:
	(void) printf ("got data report from %s, group %s", target, group);
	break;
    case NETMGT_EVENT_REPORT:
	(void) printf ("got event report from %s, group %s", target, group);
	break;
    case NETMGT_TRAP_REPORT:
	(void) printf ("got trap report from %s, group %s", target, group);
	break;
    case NETMGT_ERROR_REPORT:
	(void) printf ("got error report from %s, group %s", target, group);
	break;
    }	

    if (key && key[0])
    {
	(void) printf (", key = %s", key);
    }

    (void) printf (", count = %u, interval = %d.%d, flags = %d\n",
		   count, interval.tv_sec, interval.tv_usec, flags);

    if ( type == NETMGT_ERROR_REPORT )
    {
	Netmgt_error error;
	int x;

	if (! netmgt_fetch_error ( &error ) )
	{
	    (void) fprintf (stderr, 
			    "Couldn't get detailed error information.\n");
	    exit (1);
	}

	x = 0;
    }

} /* report_handler */

--- cut here ---
--
Timothy Jones                                         internet: tim@boxhill.com
Box Hill Systems Corporation                                voice: 212-989-4455
161 Avenue of the Americas, New York, NY, 10013               fax: 212-989-6817
Newsgroups: comp.sys.sun.apps