[SNMP4J] Patches to agent code

Matthias Wiesmann matthias.wiesmann at gmail.com
Tue Oct 11 02:52:54 CEST 2005


Franck,

thanks for the patch for the agent code.

Here are some modifications I did to the agent code. Basically, I
needed to be able to send periodical notifications only to certain
targets.The current implementation made it difficult to implement this
functionality. I wonder if it would be possible to add this code, or
similar functionality, to the agent code. This would avoid that I
maintain a separate branch.

First I added a new method to the NotificationOriginator interface,
basically, to send a notification to a given target.

public interface NotificationOriginator {

  /**
   * Sends notifications (traps) to all appropriate notification targets.
   * The targets to notify are determined through the SNMP-TARGET-MIB and
   * the SNMP-NOTIFICATION-MIB.
   *
   * @param context
   *    the context name of the context on whose behalf this notification has
   *    been generated.
   * @param notificationID
   *    the object ID that uniquely identifies this notification. For SNMPv1
   *    traps, the notification ID has to be build using the rules provided
   *    by RFC 2576.
   * @param vbs
   *    an array of <code>VariableBinding</code> instances representing the
   *    payload of the notification.
   * @return
   *    an array of ResponseEvent instances. Since the
   *    <code>NotificationOriginator</code> determines on behalf of the
   *    SNMP-NOTIFICTON-MIB contents whether a notification is sent as
   *    trap/notification or as inform request, the returned array contains
   *    an element for each addressed target, but only a response PDU for
   *    inform targets.
   */
  ResponseEvent[] notify(OctetString context,
                         OID notificationID, VariableBinding[] vbs);

   /** Sends a notification to a set of selected targets
      */

  ResponseEvent[] notifyTag(OctetString tag, OctetString context,
			      OID notificationID, VariableBinding[] vbs);


}

In the implementation class, I have split the implementation in three methods:

/**
   * Sends notifications (traps) to all appropriate notification targets.
   *
   * @param context the context name of the context on whose behalf this
   *   notification has been generated.
   * @param notificationID the object ID that uniquely identifies this
   *   notification. For SNMPv1 traps, the notification ID has to be build
   *   using the rules provided by RFC 2576.
   * @param vbs an array of <code>VariableBinding</code> instances
   *   representing the payload of the notification.
   * @return an array of ResponseEvent instances. Since the
   *   <code>NotificationOriginator</code> determines on behalf of the
   *   SNMP-NOTIFICTON-MIB contents whether a notification is sent as
   *   trap/notification or as inform request, the returned array contains an
   *   element for each addressed target, but only a response PDU for inform
   *   targets.
   */
  public ResponseEvent[] notify(OctetString context, OID notificationID,
                                VariableBinding[] vbs) {
      if (logger.isInfoEnabled()) {
	  logger.info("Notification " + notificationID + " issued with " +
		      Arrays.asList(vbs));
      }
      List responses = new LinkedList();
      for (Iterator it =
notificationMIB.getNotifyTable().getModel().iterator();it.hasNext(); )
{
	  MOTableRow notifyRow = (MOTableRow) it.next();
	  OctetString tag = (OctetString)
notifyRow.getValue(SnmpNotificationMIB.idxSnmpNotifyTag);
	  Integer32 type =(Integer32)
notifyRow.getValue(SnmpNotificationMIB.idxSnmpNotifyType);
	  responses.addAll(notifiyTag(tag,type,context,notificationID,vbs));
      }
      return (ResponseEvent[]) responses.toArray(new ResponseEvent[0]);
  } // notify

  /** Sends notifications to the destination with the given tag
    * @param tag_selected the target tag
    * @see notify
    */

  public ResponseEvent[] notifyTag(OctetString tag_selected,
OctetString context, OID notificationID, VariableBinding[] vbs) {
      if (logger.isInfoEnabled()) {
	  logger.info("Notification for tag " + tag_selected + " "+
notificationID + " issued with " + Arrays.asList(vbs));
      }
      List responses = new LinkedList();
      for (Iterator it =
notificationMIB.getNotifyTable().getModel().iterator();it.hasNext(); )
{
	  MOTableRow notifyRow = (MOTableRow) it.next();
	  final OctetString tag = (OctetString)
notifyRow.getValue(SnmpNotificationMIB.idxSnmpNotifyTag);
	  if (tag_selected.equals(tag)) {
	      final Integer32 type =(Integer32)
notifyRow.getValue(SnmpNotificationMIB.idxSnmpNotifyType);
	      responses.addAll(notifiyTag(tag,type,context,notificationID,vbs));
	  } // name matches
      } // for
      return (ResponseEvent[]) responses.toArray(new ResponseEvent[0]);
  } // notifyName

  /** Sends the notifications for a given tag
    * @param tag selected tag
    * @param type notification type
    * @param notificationID OID for notification
    * @param vbs variable bindings for notification
    */

  protected List notifiyTag(OctetString tag, Integer32 type,
OctetString context, OID notificationID, VariableBinding[] vbs) {
      List responses = new LinkedList();
      List addresses = targetMIB.getTargetAddrRowsForTag(new
String(tag.getValue()));
      MOTableRowFilter aFilter =  new
RowStatus.ActiveRowsFilter(SnmpTargetMIB.idxSnmpTargetAddrRowStatus);
      for (Iterator ait = addresses.iterator(); ait.hasNext(); ) {
	  MOTableRow addr = (MOTableRow) ait.next();
	  if (aFilter.passesFilter(addr)) {
	      OctetString params =
              (OctetString)addr.getValue(SnmpTargetMIB.idxSnmpTargetAddrParams);
	      MOTableRow paramsRow = targetMIB.getTargetParamsRow(params);
	      if (RowStatus.isRowActive(paramsRow,
					SnmpTargetMIB.idxSnmpTargetParamsRowStatus)) {
		  if (isAccessGranted(addr, paramsRow, context, notificationID, vbs)) {
		      ResponseEvent response =
		      sendNotification(addr, paramsRow, context,
				       notificationID, vbs, type.getValue());
		      responses.add(response);
		  }
		  else {
		      if (logger.isWarnEnabled()) {
			  logger.warn("Access denied by VACM for "+notificationID);
		      }
		  }
	      }
	      else {
		  logger.warn("Found active target address but corrsponding params"+
			      " are not active");
	      }
	  } // if
      } // for
      return responses ;
  } // NotifiyTag



More information about the SNMP4J mailing list