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

📄 snmp4jheartbeatmib.java

📁 你个snmp的源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*_############################################################################
  _##
  _##  SNMP4J-Agent - Snmp4jHeartbeatMib.java
  _##
  _##  Copyright 2005-2006  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.
  _##
  _##########################################################################*/

//--AgentGen BEGIN=_BEGIN
package org.snmp4j.agent.mo.snmp4j.example;
//--AgentGen END

import 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 java.util.Timer;
import java.util.GregorianCalendar;
import java.util.Calendar;
import java.util.TimerTask;
import org.snmp4j.agent.mo.snmp4j.example.Snmp4jHeartbeatMib.
    Snmp4jAgentHBCtrlEntryRow;
import org.snmp4j.agent.mo.snmp4j.example.Snmp4jHeartbeatMib.HeartbeatTask;
import org.snmp4j.PDU;
import java.util.Date;

//--AgentGen BEGIN=_IMPORT
//--AgentGen END

public class Snmp4jHeartbeatMib
//--AgentGen BEGIN=_EXTENDS
//--AgentGen END
    implements MOGroup
//--AgentGen BEGIN=_IMPLEMENTS
    , RowStatusListener,
    MOTableRowListener
//--AgentGen END
{

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

//--AgentGen BEGIN=_STATIC
//--AgentGen END

  // Factory
  private static MOFactory moFactory = DefaultMOFactory.getInstance();

  // Constants
  public static final OID oidSnmp4jAgentHBRefTime =
      new OID(new int[] {1, 3, 6, 1, 4, 1, 4976, 10, 1, 1, 42, 2, 1, 1, 0});
  public static final OID oidSnmp4jAgentHBEvent =
      new OID(new int[] {1, 3, 6, 1, 4, 1, 4976, 10, 1, 1, 42, 2, 2, 0, 1});
  public static final OID oidTrapVarSnmp4jAgentHBCtrlEvents =
      new OID(new int[] {1, 3, 6, 1, 4, 1, 4976, 10, 1, 1, 42, 2, 1, 2, 1, 6});


  // Enumerations

  public static final class Snmp4jAgentHBCtrlStorageTypeEnum {
    /* -- eh? */
    public static final int other = 1;
    /* -- e.g., in RAM */
    public static final int _volatile = 2;
    /* -- e.g., in NVRAM */
    public static final int nonVolatile = 3;
    /* -- e.g., partially in ROM */
    public static final int permanent = 4;
    /* -- e.g., completely in ROM */
    public static final int readOnly = 5;
  }
  public static final class Snmp4jAgentHBCtrlRowStatusEnum {
    public static final int active = 1;
    /* -- the following value is a state:
-- this value may be read, but not written */
    public static final int notInService = 2;
    /* -- the following three values are
-- actions: these values may be written,
--   but are never read */
    public static final int notReady = 3;
    public static final int createAndGo = 4;
    public static final int createAndWait = 5;
    public static final int destroy = 6;
  }

  // TextualConventions
  private static final String TC_MODULE_SNMPV2_TC = "SNMPv2-TC";
  private static final String TC_DATEANDTIME = "DateAndTime";

  // Scalars
  private MOScalar snmp4jAgentHBRefTime;

  // Tables
  public static final OID oidSnmp4jAgentHBCtrlEntry =
      new OID(new int[] {1, 3, 6, 1, 4, 1, 4976, 10, 1, 1, 42, 2, 1, 2, 1});

  // Column sub-identifer defintions for snmp4jAgentHBCtrlEntry:
  public static final int colSnmp4jAgentHBCtrlStartTime = 2;
  public static final int colSnmp4jAgentHBCtrlDelay = 3;
  public static final int colSnmp4jAgentHBCtrlPeriod = 4;
  public static final int colSnmp4jAgentHBCtrlMaxEvents = 5;
  public static final int colSnmp4jAgentHBCtrlEvents = 6;
  public static final int colSnmp4jAgentHBCtrlLastChange = 7;
  public static final int colSnmp4jAgentHBCtrlStorageType = 8;
  public static final int colSnmp4jAgentHBCtrlRowStatus = 9;

  // Column index defintions for snmp4jAgentHBCtrlEntry:
  public static final int idxSnmp4jAgentHBCtrlStartTime = 0;
  public static final int idxSnmp4jAgentHBCtrlDelay = 1;
  public static final int idxSnmp4jAgentHBCtrlPeriod = 2;
  public static final int idxSnmp4jAgentHBCtrlMaxEvents = 3;
  public static final int idxSnmp4jAgentHBCtrlEvents = 4;
  public static final int idxSnmp4jAgentHBCtrlLastChange = 5;
  public static final int idxSnmp4jAgentHBCtrlStorageType = 6;
  public static final int idxSnmp4jAgentHBCtrlRowStatus = 7;

  private static final MOTableSubIndex[] snmp4jAgentHBCtrlEntryIndexes =
      new MOTableSubIndex[] {
      moFactory.createSubIndex(SMIConstants.SYNTAX_OCTET_STRING, 1, 32)
  };

  private static final MOTableIndex snmp4jAgentHBCtrlEntryIndex =
      moFactory.createIndex(snmp4jAgentHBCtrlEntryIndexes,
                            true,
                            new MOTableIndexValidator() {
    public boolean isValidIndex(OID index) {
      boolean isValidIndex = true;
      //--AgentGen BEGIN=snmp4jAgentHBCtrlEntry::isValidIndex
      //--AgentGen END
      return isValidIndex;
    }
  });

  private MOTable snmp4jAgentHBCtrlEntry;
  private MOMutableTableModel snmp4jAgentHBCtrlEntryModel;

//--AgentGen BEGIN=_MEMBERS
  private static final int[] PROTECTED_COLS =
      {
      idxSnmp4jAgentHBCtrlStartTime,
      idxSnmp4jAgentHBCtrlDelay,
      idxSnmp4jAgentHBCtrlPeriod
  };

  private Timer heartbeatTimer = new Timer();
  private int heartbeatOffset = 0;
  private NotificationOriginator notificationOriginator;
  private OctetString context;
  private SysUpTime sysUpTime;
//--AgentGen END

  private Snmp4jHeartbeatMib() {
    snmp4jAgentHBRefTime =
        new Snmp4jAgentHBRefTime(oidSnmp4jAgentHBRefTime,
                                 MOAccessImpl.ACCESS_READ_WRITE);
    snmp4jAgentHBRefTime.addMOValueValidationListener(new
        Snmp4jAgentHBRefTimeValidator());
    createSnmp4jAgentHBCtrlEntry();
//--AgentGen BEGIN=_DEFAULTCONSTRUCTOR
    ((RowStatus) snmp4jAgentHBCtrlEntry.getColumn(idxSnmp4jAgentHBCtrlRowStatus)).
        addRowStatusListener(this);
    snmp4jAgentHBCtrlEntry.addMOTableRowListener(this);
//--AgentGen END
  }

//--AgentGen BEGIN=_CONSTRUCTORS
  public Snmp4jHeartbeatMib(NotificationOriginator notificationOriginator,
                            OctetString context, SysUpTime upTime) {
    this();

    this.notificationOriginator = notificationOriginator;
    this.context = context;
    this.sysUpTime = upTime;
    // make sure that the heartbeat timer related objects are not modified
    // while row is active:
    for (int i=0; i<PROTECTED_COLS.length; i++) {
      ((MOMutableColumn)
       snmp4jAgentHBCtrlEntry.getColumn(i)).setMutableInService(false);
    }
  }

//--AgentGen END


  public MOTable getSnmp4jAgentHBCtrlEntry() {
    return snmp4jAgentHBCtrlEntry;
  }

  private void createSnmp4jAgentHBCtrlEntry() {
    MOColumn[] snmp4jAgentHBCtrlEntryColumns = new MOColumn[8];
    snmp4jAgentHBCtrlEntryColumns[idxSnmp4jAgentHBCtrlStartTime] =
        new DateAndTime(colSnmp4jAgentHBCtrlStartTime,
                        MOAccessImpl.ACCESS_READ_CREATE,
                        null);
    ValueConstraint snmp4jAgentHBCtrlStartTimeVC = new ConstraintsImpl();
    ((ConstraintsImpl) snmp4jAgentHBCtrlStartTimeVC).add(new Constraint(8, 8));
    ((ConstraintsImpl) snmp4jAgentHBCtrlStartTimeVC).add(new Constraint(11, 11));
    ((MOMutableColumn) snmp4jAgentHBCtrlEntryColumns[
     idxSnmp4jAgentHBCtrlStartTime]).
        addMOValueValidationListener(new ValueConstraintValidator(
        snmp4jAgentHBCtrlStartTimeVC));
    ((MOMutableColumn) snmp4jAgentHBCtrlEntryColumns[
     idxSnmp4jAgentHBCtrlStartTime]).
        addMOValueValidationListener(new Snmp4jAgentHBCtrlStartTimeValidator());
    snmp4jAgentHBCtrlEntryColumns[idxSnmp4jAgentHBCtrlDelay] =
        new MOMutableColumn(colSnmp4jAgentHBCtrlDelay,
                            SMIConstants.SYNTAX_GAUGE32,
                            MOAccessImpl.ACCESS_READ_CREATE,
                            new UnsignedInteger32(1000));
    ((MOMutableColumn) snmp4jAgentHBCtrlEntryColumns[idxSnmp4jAgentHBCtrlDelay]).
        addMOValueValidationListener(new Snmp4jAgentHBCtrlDelayValidator());
    snmp4jAgentHBCtrlEntryColumns[idxSnmp4jAgentHBCtrlPeriod] =
        new MOMutableColumn(colSnmp4jAgentHBCtrlPeriod,
                            SMIConstants.SYNTAX_GAUGE32,
                            MOAccessImpl.ACCESS_READ_CREATE,
                            new UnsignedInteger32(60000));
    ((MOMutableColumn) snmp4jAgentHBCtrlEntryColumns[idxSnmp4jAgentHBCtrlPeriod]).
        addMOValueValidationListener(new Snmp4jAgentHBCtrlPeriodValidator());
    snmp4jAgentHBCtrlEntryColumns[idxSnmp4jAgentHBCtrlMaxEvents] =
      new MOMutableColumn(colSnmp4jAgentHBCtrlMaxEvents,
                          SMIConstants.SYNTAX_GAUGE32,
                          MOAccessImpl.ACCESS_READ_CREATE,
                          new UnsignedInteger32(0));
    snmp4jAgentHBCtrlEntryColumns[idxSnmp4jAgentHBCtrlEvents] =
      moFactory.createColumn(colSnmp4jAgentHBCtrlEvents,
                             SMIConstants.SYNTAX_COUNTER64,
                             MOAccessImpl.ACCESS_READ_ONLY);
    snmp4jAgentHBCtrlEntryColumns[idxSnmp4jAgentHBCtrlLastChange] =
        moFactory.createColumn(colSnmp4jAgentHBCtrlLastChange,
                               SMIConstants.SYNTAX_TIMETICKS,
                               MOAccessImpl.ACCESS_READ_ONLY);
    snmp4jAgentHBCtrlEntryColumns[idxSnmp4jAgentHBCtrlEvents] =
        moFactory.createColumn(colSnmp4jAgentHBCtrlEvents,
                               SMIConstants.SYNTAX_COUNTER64,
                               MOAccessImpl.ACCESS_READ_ONLY);
    snmp4jAgentHBCtrlEntryColumns[idxSnmp4jAgentHBCtrlStorageType] =
        new StorageType(colSnmp4jAgentHBCtrlStorageType,
                        MOAccessImpl.ACCESS_READ_CREATE,
                        new Integer32(3));
    ValueConstraint snmp4jAgentHBCtrlStorageTypeVC = new EnumerationConstraint(
        new int[] {Snmp4jAgentHBCtrlStorageTypeEnum.other,
        Snmp4jAgentHBCtrlStorageTypeEnum._volatile,
        Snmp4jAgentHBCtrlStorageTypeEnum.nonVolatile,
        Snmp4jAgentHBCtrlStorageTypeEnum.permanent,
        Snmp4jAgentHBCtrlStorageTypeEnum.readOnly});
    ((MOMutableColumn)snmp4jAgentHBCtrlEntryColumns[idxSnmp4jAgentHBCtrlStorageType]).
      addMOValueValidationListener(new ValueConstraintValidator(snmp4jAgentHBCtrlStorageTypeVC));
    snmp4jAgentHBCtrlEntryColumns[idxSnmp4jAgentHBCtrlRowStatus] =
        new RowStatus(colSnmp4jAgentHBCtrlRowStatus);
    ValueConstraint snmp4jAgentHBCtrlRowStatusVC = new EnumerationConstraint(
        new int[] {Snmp4jAgentHBCtrlRowStatusEnum.active,
        Snmp4jAgentHBCtrlRowStatusEnum.notInService,
        Snmp4jAgentHBCtrlRowStatusEnum.notReady,
        Snmp4jAgentHBCtrlRowStatusEnum.createAndGo,
        Snmp4jAgentHBCtrlRowStatusEnum.createAndWait,
        Snmp4jAgentHBCtrlRowStatusEnum.destroy});
    ((MOMutableColumn)snmp4jAgentHBCtrlEntryColumns[idxSnmp4jAgentHBCtrlRowStatus]).
      addMOValueValidationListener(new ValueConstraintValidator(snmp4jAgentHBCtrlRowStatusVC));

    snmp4jAgentHBCtrlEntryModel = new DefaultMOMutableTableModel();
    snmp4jAgentHBCtrlEntryModel.setRowFactory(new Snmp4jAgentHBCtrlEntryRowFactory());
    snmp4jAgentHBCtrlEntry =
        moFactory.createTable(oidSnmp4jAgentHBCtrlEntry,
                              snmp4jAgentHBCtrlEntryIndex,
                              snmp4jAgentHBCtrlEntryColumns,
                              snmp4jAgentHBCtrlEntryModel);
  }

  public void registerMOs(MOServer server, OctetString context) throws
      DuplicateRegistrationException {
    // Scalar Objects
    server.register(this.snmp4jAgentHBRefTime, context);
    server.register(this.snmp4jAgentHBCtrlEntry, context);
//--AgentGen BEGIN=_registerMOs
//--AgentGen END
  }

  public void unregisterMOs(MOServer server, OctetString context) {
    // Scalar Objects
    server.unregister(this.snmp4jAgentHBRefTime, context);
    server.unregister(this.snmp4jAgentHBCtrlEntry, context);
//--AgentGen BEGIN=_unregisterMOs
//--AgentGen END
  }

  // Notifications
  public void snmp4jAgentHBEvent(NotificationOriginator notificationOriginator,
                                 OctetString context, VariableBinding[] vbs) {
    if (vbs.length < 1) {
      throw new IllegalArgumentException("Too few notification objects: " +
                                         vbs.length + "<1");
    }
    if (!(vbs[0].getOid().startsWith(oidTrapVarSnmp4jAgentHBCtrlEvents))) {
      throw new IllegalArgumentException("Variable 0 has wrong OID: " +
                                         vbs[0].getOid() +
                                         " does not start with " +
                                         oidTrapVarSnmp4jAgentHBCtrlEvents);
    }
    if (!snmp4jAgentHBCtrlEntryIndex.isValidIndex(snmp4jAgentHBCtrlEntry.
                                                  getIndexPart(vbs[0].getOid()))) {
      throw new IllegalArgumentException(
          "Illegal index for variable 0 specified: " +
          snmp4jAgentHBCtrlEntry.getIndexPart(vbs[0].getOid()));

⌨️ 快捷键说明

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