All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.snmp4j.agent.mo.snmp.SNMPv2MIB Maven / Gradle / Ivy

/*_############################################################################
  _##
  _##  SNMP4J-Agent - SNMPv2MIB.java
  _##
  _##  Copyright (C) 2005-2009  Frank Fock (SNMP4J.org)
  _##
  _##  Licensed under the Apache License, Version 2.0 (the "License");
  _##  you may not use this file except in compliance with the License.
  _##  You may obtain a copy of the License at
  _##
  _##      http://www.apache.org/licenses/LICENSE-2.0
  _##
  _##  Unless required by applicable law or agreed to in writing, software
  _##  distributed under the License is distributed on an "AS IS" BASIS,
  _##  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  _##  See the License for the specific language governing permissions and
  _##  limitations under the License.
  _##
  _##########################################################################*/


package org.snmp4j.agent.mo.snmp;

import org.snmp4j.agent.*;
import org.snmp4j.agent.mo.*;
import org.snmp4j.mp.*;
import org.snmp4j.smi.*;
import org.snmp4j.agent.mo.snmp.DisplayString;
import org.snmp4j.event.CounterListener;
import org.snmp4j.event.CounterEvent;
import java.util.HashMap;
import java.util.Map;
import java.util.Collections;
import org.snmp4j.log.LogAdapter;
import org.snmp4j.log.LogFactory;

/**
 * The SystemGroup implements the objects in the SNMPv2-MIB
 * specification.
 *
 * @author Frank Fock
 * @version 1.2
 */
public class SNMPv2MIB implements MOGroup, CounterListener, AgentCapabilityList {

  private static final LogAdapter LOGGER = LogFactory.getLogger(SNMPv2MIB.class);

  // Constants
  private static final OID snmpStatsPrefix =
      new OID(new int[] { 1,3,6,1,2,1,11 });
  private static final OID usmStatsPrefix =
      new OID(new int[] { 1,3,6,1,6,3,15,1,1 });


  private static final OID oidSnmpInPkts =
      SnmpConstants.snmpInPkts;
  private static final OID oidSnmpInBadVersions =
      SnmpConstants.snmpInBadVersions;
  private static final OID oidSnmpInBadCommunityNames =
      SnmpConstants.snmpInBadCommunityNames;
  private static final OID oidSnmpInBadCommunityUses =
      SnmpConstants.snmpInBadCommunityUses;
  private static final OID oidSnmpInASNParseErrs =
      SnmpConstants.snmpInASNParseErrs;
  private static final OID oidSnmpSilentDrops =
      SnmpConstants.snmpSilentDrops;
  private static final OID oidSnmpProxyDrops =
      SnmpConstants.snmpProxyDrops;

  private static final OID oidSysORLastChange =
    new OID(new int[] { 1,3,6,1,2,1,1,8,0 });

  private static final OID oidSnmpEnableAuthenTraps =
    new OID(new int[] { 1,3,6,1,2,1,11,30,0 });

  private static final OID oidSnmpSetSerialNo =
      new OID(new int[] { 1,3,6,1,6,3,1,1,6,1,0 });


  private MOScalar descr;
  private SysUpTimeImpl upTime;
  private MOScalar objectID;
  private MOScalar contact;
  private MOScalar name;
  private MOScalar location;
  private MOScalar services;
  private MOScalar snmpEnableAuthenTraps;
  private MOScalar snmpSetSerialNo;
  private MOScalar sysORLastChange;

  private static final OID[] snmpStatsOIDs = {
      oidSnmpInPkts, oidSnmpInBadVersions, oidSnmpInBadCommunityNames,
      oidSnmpInBadCommunityUses, oidSnmpInASNParseErrs,
      oidSnmpSilentDrops, oidSnmpProxyDrops
  };

  private MOScalar[] snmpStats;

  private static final OID oidSysOREntry =
      new OID(new int[] { 1,3,6,1,2,1,1,9,1 });

  // Column sub-identifer defintions for sysOREntry:
  private static final int colSysORID = 2;
  private static final int colSysORDescr = 3;
  private static final int colSysORUpTime = 4;

  // Column index defintions for sysOREntry:
  private static final int idxSysORID = 0;
  private static final int idxSysORDescr = 1;
  private static final int idxSysORUpTime = 2;
  private static MOTableSubIndex[] sysOREntryIndexes =
      new MOTableSubIndex[] {
      new MOTableSubIndex(SMIConstants.SYNTAX_INTEGER, 1, 1)  };

  private static MOTableIndex sysOREntryIndex =
      new MOTableIndex(sysOREntryIndexes, false);

  private DefaultMOTable             sysOREntry;
  private DefaultMOMutableTableModel sysOREntryModel;

  // Notification originator for authenticationFailure trap
  private NotificationOriginator notificationOriginator;

  // Context to sysUpTime Map
  private static Map sysUpTimes = Collections.synchronizedMap(new HashMap());

  public SNMPv2MIB(OctetString sysDescr,
                   OID sysOID,
                   Integer32 sysServices) {
    this.descr = new MOScalar(new OID(SnmpConstants.sysDescr),
                              MOAccessImpl.ACCESS_READ_ONLY,
                              sysDescr);
    this.upTime = new SysUpTimeImpl();
    this.objectID = new MOScalar(new OID(SnmpConstants.sysObjectID),
                                 MOAccessImpl.ACCESS_READ_ONLY,
                                 sysOID);
    this.contact =
        new DisplayStringScalar(new OID(SnmpConstants.sysContact),
                                MOAccessImpl.ACCESS_READ_WRITE,
                                new OctetString());
    this.name =
        new DisplayStringScalar(new OID(SnmpConstants.sysName),
                                MOAccessImpl.ACCESS_READ_WRITE,
                                new OctetString());
    this.location =
        new DisplayStringScalar(new OID(SnmpConstants.sysLocation),
                                MOAccessImpl.ACCESS_READ_WRITE,
                                new OctetString());
    this.services = new MOScalar(new OID(SnmpConstants.sysServices),
                                 MOAccessImpl.ACCESS_READ_ONLY,
                                 sysServices);
    snmpEnableAuthenTraps =
      new EnumeratedScalar(oidSnmpEnableAuthenTraps,
                           MOAccessImpl.ACCESS_READ_WRITE,
                           new Integer32(SnmpEnableAuthenTrapsEnum.enabled),
                           new int[] { SnmpEnableAuthenTrapsEnum.enabled,
                                       SnmpEnableAuthenTrapsEnum.disabled });
    snmpSetSerialNo = new TestAndIncr(oidSnmpSetSerialNo);
    sysORLastChange =
        new TimeStampScalar(oidSysORLastChange,
                            MOAccessImpl.ACCESS_READ_ONLY, upTime);
    createSysOREntry();
    createSnmpStats();
  }

  private void createSnmpStats() {
    snmpStats = new MOScalar[snmpStatsOIDs.length];
    for (int i=0; i snmpStatsPrefix.size())) {
      int suffix = event.getOid().get(snmpStatsPrefix.size());
      for (int i=0; inull for the default context.
   * @return
   *    the SysUpTime instance associated with the given context or
   *    null if such a sysUpTime instance has not been
   *    registered yet.
   */
  public static SysUpTime getSysUpTime(OctetString context) {
    return (SysUpTime) sysUpTimes.get(context);
  }

  public class SysOREntry extends DefaultMOTable implements AgentCapabilityList{

    public SysOREntry(OID oid, MOTableIndex index, MOColumn[] columns) {
      super(oid, index, columns);
    }

    public OID addSysOREntry(OID sysORID, OctetString sysORDescr) {
      return SNMPv2MIB.this.addSysOREntry(sysORID, sysORDescr);
    }

    public MOTableRow removeSysOREntry(OID index) {
      return SNMPv2MIB.this.removeSysOREntry(index);
    }

  }

  /**
   * Sets the NotificationOriginator to be used for sending the
   * authenticationFailure trap.
   * @param notificationOriginator
   *    a NotificationOriginator instance or null to disable
   *    authenticationFailure traps (default).
   */
  public void setNotificationOriginator(NotificationOriginator
                                        notificationOriginator) {
    this.notificationOriginator = notificationOriginator;
  }

  /**
   * Gets the notification originator used for sending authenticationFailure
   * traps.
   * @return
   *   a NotificationOriginator.
   * @since 1.2
   */
  public NotificationOriginator getNotificationOriginator() {
    return notificationOriginator;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy