SNMP Subagent Programmer's Guide

Programming Tutorials
SNMP Subagent Programmer’s Guide—119728 2-67
Indexed Trap Generator
/* register the basic group */
if (mgmt_new_instance(mgmt_env,
<-- 10
&SMI_GROUP_tblTrap,
(Void *) &master) == NULL)
{
fprintf(stderr, "%s: mgmt_new_instance failure\n", argv[0]);
return(2);
}
/* register the table */
if (mgmt_new_instance(mgmt_env,
<-- 11
&SMI_GROUP_tblTrapTableEntry,
(Void *)&master) == NULL)
{
fprintf(stderr, "%s: mgmt_new_instance failure\n", argv[0]);
return(2);
}
if (mgmt_poll(mgmt_env,(struct timeval *)NULL,&dummy) < 0)
<-- 12
{
fprintf(stderr, "%s: agent unavailable\n", argv[0]);
}
/* process management operations. Every time we wake up, either
* due to timeout or the processing of a management request or
* signal, test whether we need to send out a trap. */
for ( ; ; )
<-- 13
{
/* go to sleep until something interesting happens
* on one of the specified files */
/* extract the user-supplied card and port numbers
to use for the trap */
(void) fprintf(stdout, prompt);
<-- 14
count_read = scanf("%d %d",&card,&port);
/* We're awake! Handle any pending I/O
* if the user has given us some input, process it */
if ((count_read == 2) &&(card >= 1) && (card <= CARDS) &&
<-- 15
(port >= 1) && (port <= PORTS))
{
/* The input looks ok.
* Send the trap, based on the user's input */
master.current_card = card;
master.current_port = port;
mgmt_trap(mgmt_env, &TRAP_tableTrap);
}
else
{
(void) fprintf(stdout, "illegal input\n");
}
/* re-issue the prompt to the user */
/* If there are any management requests pending,
* process them.
* Check every time ::: */
wait.tv_sec = 0;
<-- 16
wait.tv_usec = 0;
if (mgmt_poll(mgmt_env, &wait, &dummy) < 0)
<-- 17
{
/* bail out if we lose contact with agent */
break;
}
}
/* clean up before exit */
if (mgmt_env)
<-- 18
{
mgmt_term_env(mgmt_env);
mgmt_env = NULL;
}
return(0);
Example 2-13. Indexed Trap Generator Main Program and Next and Locator
Functions (page 3 of 5)