⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snmp4jdemomib.java

📁 用snmp4j实现的agent,代码比较多,但是很值得一看,尤其是对于要用SNMP监控信息的编程者,可以仔细研究一下里面的代码.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package org.snmp4j.agent.example;//--AgentGen BEGIN=_BEGIN//--AgentGen ENDimport org.snmp4j.smi.*;import org.snmp4j.mp.SnmpConstants;import org.snmp4j.agent.*;import org.snmp4j.agent.mo.*;import org.snmp4j.agent.mo.snmp.*;import org.snmp4j.agent.mo.snmp.smi.*;import org.snmp4j.agent.request.*;import org.snmp4j.log.LogFactory;import org.snmp4j.log.LogAdapter;import org.snmp4j.agent.mo.snmp.tc.*;//--AgentGen BEGIN=_IMPORT//--AgentGen ENDpublic class Snmp4jDemoMib//--AgentGen BEGIN=_EXTENDS//--AgentGen ENDimplements MOGroup//--AgentGen BEGIN=_IMPLEMENTS//--AgentGen END{  private static final LogAdapter LOGGER =      LogFactory.getLogger(Snmp4jDemoMib.class);//--AgentGen BEGIN=_STATIC//--AgentGen END  // Factory  private MOFactory moFactory =    DefaultMOFactory.getInstance();  // Constants  public static final OID oidSnmp4jDemoScalar =    new OID(new int[] { 1,3,6,1,4,1,4976,10,1,1,20,1,1,0 });  public static final OID oidSnmp4jDemoEvent =    new OID(new int[] { 1,3,6,1,4,1,4976,10,1,1,20,2,0,1 });  public static final OID oidTrapVarSnmp4jDemoEntryCol3 =    new OID(new int[] { 1,3,6,1,4,1,4976,10,1,1,20,1,2,1,5 });  public static final OID oidTrapVarSnmp4jDemoTableRowModification =    new OID(new int[] { 1,3,6,1,4,1,4976,10,1,1,20,1,2,1,9 });  // Enumerations  public static final class Snmp4jDemoTableRowModificationEnum {    public static final int created = 1;    public static final int updated = 2;    public static final int deleted = 3;  }  // TextualConventions  private static final String TC_MODULE_SNMPV2_TC = "SNMPv2-TC";  private static final String TC_DISPLAYSTRING = "DisplayString";  private static final String TC_ROWSTATUS = "RowStatus";  private static final String TC_STORAGETYPE = "StorageType";  private static final String TC_TIMESTAMP = "TimeStamp";  // Scalars  private MOScalar snmp4jDemoScalar;  // Tables  public static final OID oidSnmp4jDemoEntry =    new OID(new int[] { 1,3,6,1,4,1,4976,10,1,1,20,1,2,1 });  // Index OID definitions  public static final OID oidSnmp4jDemoEntryIndex1 =    new OID(new int[] { 1,3,6,1,4,1,4976,10,1,1,20,1,2,1,1 });  public static final OID oidSnmp4jDemoEntryIndex2 =    new OID(new int[] { 1,3,6,1,4,1,4976,10,1,1,20,1,2,1,2 });  // Column TC definitions for snmp4jDemoEntry:  public static final String tcModuleSNMPv2Tc = "SNMPv2-TC";  public static final String tcDefTimeStamp = "TimeStamp";  public static final String tcDefStorageType = "StorageType";  public static final String tcDefRowStatus = "RowStatus";  // Column sub-identifer definitions for snmp4jDemoEntry:  public static final int colSnmp4jDemoEntryCol1 = 3;  public static final int colSnmp4jDemoEntryCol2 = 4;  public static final int colSnmp4jDemoEntryCol3 = 5;  public static final int colSnmp4jDemoEntryCol4 = 6;  public static final int colSnmp4jDemoEntryCol5 = 7;  public static final int colSnmp4jDemoEntryCol6 = 8;  public static final int colSnmp4jDemoTableRowModification = 9;  // Column index definitions for snmp4jDemoEntry:  public static final int idxSnmp4jDemoEntryCol1 = 0;  public static final int idxSnmp4jDemoEntryCol2 = 1;  public static final int idxSnmp4jDemoEntryCol3 = 2;  public static final int idxSnmp4jDemoEntryCol4 = 3;  public static final int idxSnmp4jDemoEntryCol5 = 4;  public static final int idxSnmp4jDemoEntryCol6 = 5;  public static final int idxSnmp4jDemoTableRowModification = 6;  private MOTableSubIndex[] snmp4jDemoEntryIndexes;  private MOTableIndex snmp4jDemoEntryIndex;  private MOTable      snmp4jDemoEntry;  private MOTableModel snmp4jDemoEntryModel;//--AgentGen BEGIN=_MEMBERS//--AgentGen END  /**   * Constructs a Snmp4jDemoMib instance without actually creating its   * <code>ManagedObject</code> instances. This has to be done in a   * sub-class constructor or after construction by calling   * {@link #createMO(MOFactory moFactory)}.   */  protected Snmp4jDemoMib() {//--AgentGen BEGIN=_DEFAULTCONSTRUCTOR//--AgentGen END  }  /**   * Constructs a Snmp4jDemoMib instance and actually creates its   * <code>ManagedObject</code> instances using the supplied   * <code>MOFactory</code> (by calling   * {@link #createMO(MOFactory moFactory)}).   * @param moFactory   *    the <code>MOFactory</code> to be used to create the   *    managed objects for this module.   */  public Snmp4jDemoMib(MOFactory moFactory) {    createMO(moFactory);//--AgentGen BEGIN=_FACTORYCONSTRUCTOR//--AgentGen END  }//--AgentGen BEGIN=_CONSTRUCTORS//--AgentGen END  /**   * Create the ManagedObjects defined for this MIB module   * using the specified {@link MOFactory}.   * @param moFactory   *    the <code>MOFactory</code> instance to use for object   *    creation.   */  protected void createMO(MOFactory moFactory) {    addTCsToFactory(moFactory);    snmp4jDemoScalar =      new Snmp4jDemoScalar(oidSnmp4jDemoScalar,                           moFactory.createAccess(MOAccessImpl.ACCESSIBLE_FOR_READ_WRITE));    snmp4jDemoScalar.addMOValueValidationListener(new Snmp4jDemoScalarValidator());    createSnmp4jDemoEntry(moFactory);  }  public MOScalar getSnmp4jDemoScalar() {    return snmp4jDemoScalar;  }  public MOTable getSnmp4jDemoEntry() {    return snmp4jDemoEntry;  }  private void createSnmp4jDemoEntry(MOFactory moFactory) {    // Index definition    snmp4jDemoEntryIndexes =      new MOTableSubIndex[] {      moFactory.createSubIndex(oidSnmp4jDemoEntryIndex1,                               SMIConstants.SYNTAX_INTEGER, 1, 1),      moFactory.createSubIndex(oidSnmp4jDemoEntryIndex2,                               SMIConstants.SYNTAX_OCTET_STRING, 1, 32)    };    snmp4jDemoEntryIndex =      moFactory.createIndex(snmp4jDemoEntryIndexes, true);    // Columns    MOColumn[] snmp4jDemoEntryColumns = new MOColumn[7];    snmp4jDemoEntryColumns[idxSnmp4jDemoEntryCol1] =      new MOMutableColumn(colSnmp4jDemoEntryCol1,                          SMIConstants.SYNTAX_INTEGER32,                          moFactory.createAccess(MOAccessImpl.ACCESSIBLE_FOR_READ_CREATE),                          new Integer32(1));    ((MOMutableColumn)snmp4jDemoEntryColumns[idxSnmp4jDemoEntryCol1]).      addMOValueValidationListener(new Snmp4jDemoEntryCol1Validator());    snmp4jDemoEntryColumns[idxSnmp4jDemoEntryCol2] =      new MOMutableColumn(colSnmp4jDemoEntryCol2,                          SMIConstants.SYNTAX_OCTET_STRING,                          moFactory.createAccess(MOAccessImpl.ACCESSIBLE_FOR_READ_CREATE),                          new OctetString(new byte[] {  }));    ValueConstraint snmp4jDemoEntryCol2VC = new ConstraintsImpl();    ((ConstraintsImpl)snmp4jDemoEntryCol2VC).add(new Constraint(0L, 128L));    ((MOMutableColumn)snmp4jDemoEntryColumns[idxSnmp4jDemoEntryCol2]).      addMOValueValidationListener(new ValueConstraintValidator(snmp4jDemoEntryCol2VC));    ((MOMutableColumn)snmp4jDemoEntryColumns[idxSnmp4jDemoEntryCol2]).      addMOValueValidationListener(new Snmp4jDemoEntryCol2Validator());    snmp4jDemoEntryColumns[idxSnmp4jDemoEntryCol3] =      moFactory.createColumn(colSnmp4jDemoEntryCol3,                             SMIConstants.SYNTAX_COUNTER32,                             moFactory.createAccess(MOAccessImpl.ACCESSIBLE_FOR_READ_ONLY));    snmp4jDemoEntryColumns[idxSnmp4jDemoEntryCol4] =      moFactory.createColumn(colSnmp4jDemoEntryCol4,                             SMIConstants.SYNTAX_TIMETICKS,                             moFactory.createAccess(MOAccessImpl.ACCESSIBLE_FOR_READ_ONLY),                             tcModuleSNMPv2Tc,                             tcDefTimeStamp);    snmp4jDemoEntryColumns[idxSnmp4jDemoEntryCol5] =      new StorageType(colSnmp4jDemoEntryCol5,                      moFactory.createAccess(MOAccessImpl.ACCESSIBLE_FOR_READ_CREATE),                      new Integer32(2));    ValueConstraint snmp4jDemoEntryCol5VC = new EnumerationConstraint(      new int[] { 1,                  2,                  3,                  4,                  5 });    ((MOMutableColumn)snmp4jDemoEntryColumns[idxSnmp4jDemoEntryCol5]).      addMOValueValidationListener(new ValueConstraintValidator(snmp4jDemoEntryCol5VC));    ((MOMutableColumn)snmp4jDemoEntryColumns[idxSnmp4jDemoEntryCol5]).      addMOValueValidationListener(new Snmp4jDemoEntryCol5Validator());    snmp4jDemoEntryColumns[idxSnmp4jDemoEntryCol6] =      new RowStatus(colSnmp4jDemoEntryCol6);    ValueConstraint snmp4jDemoEntryCol6VC = new EnumerationConstraint(      new int[] { 1,                  2,                  3,                  4,                  5,                  6 });    ((MOMutableColumn)snmp4jDemoEntryColumns[idxSnmp4jDemoEntryCol6]).      addMOValueValidationListener(new ValueConstraintValidator(snmp4jDemoEntryCol6VC));    ((MOMutableColumn)snmp4jDemoEntryColumns[idxSnmp4jDemoEntryCol6]).      addMOValueValidationListener(new Snmp4jDemoEntryCol6Validator());    snmp4jDemoEntryColumns[idxSnmp4jDemoTableRowModification] =      moFactory.createColumn(colSnmp4jDemoTableRowModification,                             SMIConstants.SYNTAX_INTEGER,                             moFactory.createAccess(MOAccessImpl.ACCESSIBLE_FOR_NOTIFY));    // Table model    snmp4jDemoEntryModel =      moFactory.createTableModel(oidSnmp4jDemoEntry,                                 snmp4jDemoEntryIndex,                                 snmp4jDemoEntryColumns);    ((MOMutableTableModel)snmp4jDemoEntryModel).setRowFactory(      new Snmp4jDemoEntryRowFactory());    snmp4jDemoEntry =      moFactory.createTable(oidSnmp4jDemoEntry,                            snmp4jDemoEntryIndex,                            snmp4jDemoEntryColumns,                            snmp4jDemoEntryModel);  }  public void registerMOs(MOServer server, OctetString context)    throws DuplicateRegistrationException  {    // Scalar Objects    server.register(this.snmp4jDemoScalar, context);    server.register(this.snmp4jDemoEntry, context);//--AgentGen BEGIN=_registerMOs//--AgentGen END  }  public void unregisterMOs(MOServer server, OctetString context) {    // Scalar Objects    server.unregister(this.snmp4jDemoScalar, context);    server.unregister(this.snmp4jDemoEntry, context);//--AgentGen BEGIN=_unregisterMOs//--AgentGen END  }  // Notifications  public void snmp4jDemoEvent(NotificationOriginator notificationOriginator,                              OctetString context, VariableBinding[] vbs) {    if (vbs.length < 2) {      throw new IllegalArgumentException("Too few notification objects: "+                                         vbs.length+"<2");    }    if (!(vbs[0].getOid().startsWith(oidTrapVarSnmp4jDemoEntryCol3))) {      throw new IllegalArgumentException("Variable 0 has wrong OID: "+vbs[0].getOid()+                                         " does not start with "+oidTrapVarSnmp4jDemoEntryCol3);    }    if (!snmp4jDemoEntryIndex.isValidIndex(snmp4jDemoEntry.getIndexPart(vbs[0].getOid()))) {      throw new IllegalArgumentException("Illegal index for variable 0 specified: "+                                         snmp4jDemoEntry.getIndexPart(vbs[0].getOid()));    }    if (!(vbs[1].getOid().startsWith(oidTrapVarSnmp4jDemoTableRowModification))) {      throw new IllegalArgumentException("Variable 1 has wrong OID: "+vbs[1].getOid()+                                         " does not start with "+oidTrapVarSnmp4jDemoTableRowModification);    }    if (!snmp4jDemoEntryIndex.isValidIndex(snmp4jDemoEntry.getIndexPart(vbs[1].getOid()))) {      throw new IllegalArgumentException("Illegal index for variable 1 specified: "+                                         snmp4jDemoEntry.getIndexPart(vbs[1].getOid()));    }    notificationOriginator.notify(context, oidSnmp4jDemoEvent, vbs);  }  // Scalars  public class Snmp4jDemoScalar extends DisplayStringScalar {    Snmp4jDemoScalar(OID oid, MOAccess access) {      super(oid, access, new OctetString(),            0,            255);//--AgentGen BEGIN=snmp4jDemoScalar//--AgentGen END    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -