You can also use the option string for SNMP requests. You can specify certain options that you want the SNMP proxy agent to use when sending an SNMP request to a target device. The optional arguments are:
na.snmp-get-list (used only for data reports)--list of object identifiers in standard dot notation. Separate each object ID with a space. For each object ID in the list, the SNMP proxy agent will get the value of the instance specified by the object ID. na.snmp-get-next-list (used only for data reports)--list of object identifiers in standard dot notation. Separate each object ID with a space. For each object ID in the list, the SNMP proxy agent will get the value of the lexicographic next instance specified by the object ID. na.snmp-timeout (number of seconds the SNMP proxy agent waits for a response from the target device)
You should register your application as a temporary callback service by calling netmgt_register_callback (3n). This function registers a temporary (or transient) RPC service with the local portmapper which is described by portmap(8). Note, before exiting, the manager should unregister the RPC service by calling netmgt_unregister_callback (3n).
#include <sys/param.h>
#include <sys/types.h>
#include <netmgt/netmgt.h>
#include <rpc/rpcent.h>
#include <signal.h>
/* static functions */
static bool_t register_results();
static void display_results();
static void unregister_results();
/* static data - global so exit handler can unregister RPC program */
u_long callback_prog; /* callback RPC program number */
char agentname[NETMGT_NAMESIZ]; /* name of agent */
char target[MAXHOSTNAMELEN]; /* name of target system */
char agent_host[MAXHOSTNAMELEN]; /* agent hostname */
char event_host[MAXHOSTNAMELEN]; /* host where the event dispatcher runs*/
/*---------------------------------------------------------------------
* main routine
*---------------------------------------------------------------------
*/
int
main (argc, argv)
int argc;
char ** argv;
{
char local_host[MAXHOSTNAMELEN]; /* local host name */
struct timeval timeout; /* request RPC timeout */
Netmgt_setval setval; /* attribute buffer*/
char *optstring= (char *) NULL; /* optional string */
u_int flags = 0; /* request option flags */
char *location = "1st floor, room 101";
/* sets the debug mode to print NETMGT_DBG messages */
netmgt_debug = 1;
timeout.tv_sec = NETMGT_TIMEOUT;
timeout.tv_usec = 0;
if (gethostname(local_host, sizeof(local_host)) == -1) {
perror("gethostname");
exit(1);
}
(void) strcpy(event_host, local_host);
(void) strcpy(agent_host, local_host);
(void) strcpy(target, local_host);
(void) strcpy(agentname, "snmp");
/* build set request argument */
(void) strcpy(setval.group, "system");
(void) strcpy(setval.key,"" );
(void) strcpy(setval.name, "sysLocation");
setval.type = NETMGT_STRING;
setval.length = strlen(location) + 1;
setval.value = location;
/* send a set request - if it succeeds, run as a callback
* RPC server waiting for the results
if (!do_set_request(target, &setval, &optstring, flags, timeout))
/* request was verified */
svc_run();
* do_set_request - send a set request and get the request verification
* returns TRUE if successful; otherwise returns FALSE
bool_t
do_set_request(target, setvalp, optstring, flags, timeout)
char *target; /* target system name */
Netmgt_setval *setvalp;/* set request argument */
char *optstring; /* optional argument string */
u_int flags; /* request flags */
struct timeval timeout; /* RPC timeout */
u_long agent_prog; /* agent's RPC program number */
struct rpcent *agent_rpc; /* RPC database entry */
Netmgt_arg option; /* option argument */
/* get the agent's RPC info */
agent_rpc = getrpcbyname(agentname);
if (!agent_rpc) {
NETMGT_DBG("can't get RPC program number for %s\n", agentname);
return (FALSE);
agent_prog = agent_rpc->r_number;
/* declare managed object instance */
if (!netmgt_set_instance(target, (char *) NULL, (char *) NULL)) {
NETMGT_DBG("set_instance failed for %s: %s\n",
target, netmgt_sperror());
#ifdef SNMP_AGENT
/* set SNMP arguments */
set_snmp_args();
#endif
/* specifies the attribute to be set */
if (!netmgt_set_value(setvalp)) {
NETMGT_DBG("set_value failed : %s\n", netmgt_sperror());
/* pass optional arguments */
if (optstring) {
(void) strcpy(option.name, NETMGT_OPTSTRING);
option.type = NETMGT_STRING;
option.length = strlen(optstring) + 1;
option.value = optstring;
if (!netmgt_set_argument(&option)) {
NETMGT_DBG ("netmgt_set_argument failed: %s \n",
netmgt_sperror());
/* register a calback RPC program number to get results */
if (!register_results(display_results))
return(FALSE);
/* and send the request ... */
if (!netmgt_request_set2(target, agent_prog, NETMGT_VERS,
event_host, callback_prog, NETMGT_VERS,
timeout, flags)) {
NETMGT_DBG("set_request failed : %s\n", netmgt_sperror());
return(TRUE);
/*
* chapter 4.2: Optional Arguments
set_snmp_args()
/* argument for setting options */
Netmgt_arg option;
/* write community string ( = default set during snmpd installation)*/
char *community= "private";
NETMGT_DBG("setting snmp arguments \n");
/* send SNMP write community */
strcpy(option.name, "na.snmp-write-community");
option.length = strlen(community) + 1;
option.value = community;
* register_results - register callback function
static bool_t
register_results(callback)
void (*callback) (); /* callback function pointer */
int udpSock; /* UDP/IP socket descriptor */
int tcpSock; /* TCP/IP socket descriptor */
u_long proto; /* RPC transport protocol */
NETMGT_DBG("register_results\n");
udpSock = RPC_ANYSOCK;
tcpSock = RPC_ANYSOCK;
proto = (u_long) IPPROTO_UDP;
if (! (callback_prog = netmgt_register_callback(callback, &udpSock,
&tcpSock, NETMGT_VERS, proto)) ) {
NETMGT_DBG("can't register RPC callback: %s\n",
/* declare exit handler to clean up */
(void) signal(SIGINT, unregister_results);
(void) signal(SIGQUIT, unregister_results);
(void) signal(SIGTERM, unregister_results);
NETMGT_DBG( "registered callback: prog == %d\n", callback_prog );
return (TRUE);
* displays_results - displays results of set request and exit
* no return
static void
display_results(type, system, group, key, count, interval, flags)
u_int type; /* request type */
char *system; /* target system name */
char *group; /* object group name */
char *key; /* object group key */
u_int count; /* report count */
struct timeval interval; /* report interval */
u_int flags; /* report flags */
Netmgt_error error; /* error report argument */
NETMGT_DBG("display_results\n");
/* get error status */
if (!netmgt_fetch_error(&error)) {
NETMGT_DBG("can't get set report: %s\n",
/* display confirmation if successful */
if (error.service_error == NETMGT_SUCCESS) {
NETMGT_DBG("request succeeded. \n");
exit(0);
/* otherwise display error */
NETMGT_DBG("request failed \n");
NETMGT_DBG("service error code: %d\n", error.service_error);
NETMGT_DBG("agent error code: %d ", error.agent_error);
NETMGT_DBG("error message: %s \n", error.message);
* unregister_results - unregister callback function before exiting
* no return value
unregister_results()
NETMGT_DBG("unregister_mycallback\n");
/* tell the local portmapper to unregister the callback RPC service */
if (!netmgt_unregister_callback(callback_prog, NETMGT_VERS))
NETMGT_DBG("can't unregister callback\n");