📄 baseagent.java
字号:
/*_############################################################################
_##
_## SNMP4J-Agent - BaseAgent.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.
_##
_##########################################################################*/
package org.snmp4j.agent;
import java.io.*;
import org.snmp4j.*;
import org.snmp4j.agent.io.*;
import org.snmp4j.agent.mo.snmp.*;
import org.snmp4j.agent.mo.snmp4j.*;
import org.snmp4j.log.*;
import org.snmp4j.mp.*;
import org.snmp4j.security.*;
import org.snmp4j.smi.*;
import org.snmp4j.transport.*;
/**
* The <code>BaseAgent</code> abstract class defines a framework for writing
* SNMP agents using the SNMP4J-Agent API. To implement your own SNMP agent,
* extend this class and implement the abstract methods defined by BaseAgent.
* The hook methods do not need any specific implementation. They only provide
* a defined mechanism to customize your agent.
*
* @author Frank Fock
* @version 1.0
*/
public abstract class BaseAgent implements Runnable, MOPersistenceProvider {
private static final LogAdapter logger =
LogFactory.getLogger(BaseAgent.class);
protected SNMPv2MIB snmpv2MIB;
protected SnmpFrameworkMIB snmpFrameworkMIB;
protected SnmpTargetMIB snmpTargetMIB;
protected SnmpNotificationMIB snmpNotificationMIB;
protected SnmpProxyMIB snmpProxyMIB;
protected SnmpCommunityMIB snmpCommunityMIB;
protected Snmp4jLogMib snmp4jLogMIB;
protected Snmp4jConfigMib snmp4jConfigMIB;
protected UsmMIB usmMIB;
protected VacmMIB vacmMIB;
protected DefaultMOServer server;
protected Snmp session;
protected TransportMapping[] transportMappings;
protected MessageDispatcherImpl dispatcher;
protected CommandProcessor agent;
protected MPv3 mpv3;
protected USM usm;
protected File configFile;
protected File bootCounterFile;
protected NotificationOriginator notificationOriginator;
protected ProxyForwarder defaultProxyForwarder;
protected OctetString sysDescr =
new OctetString("SNMP4J-Agent - "+
System.getProperty("os.name","")+
" - "+System.getProperty("os.arch")+
" - "+System.getProperty("os.version"));
protected OID sysOID = new OID("1.3.6.1.4.1.4976");
protected Integer32 sysServices = new Integer32(10);
/**
* Creates a base agent with a {@link DefaultMOServer} as {@link MOServer}.
* To use a different server implementation, modify the {@link #server} member
* after construction.
*/
protected BaseAgent() {
this.server = new DefaultMOServer();
}
/**
* Creates a base agent with boot-counter, config file, and a CommandProcessor
* for processing SNMP requests.
*
* @param bootCounterFile
* a file with serialized boot-counter information (read/write). If the
* file does not exist it is created on shutdown of the agent.
* @param configFile
* a file with serialized configuration information (read/write). If the
* file does not exist it is created on shutdown of the agent.
* @param commandProcessor
* the <code>CommandProcessor</code> instance that handles the SNMP
* requests.
*/
protected BaseAgent(File bootCounterFile,
File configFile,
CommandProcessor commandProcessor) {
this();
this.configFile = configFile;
this.bootCounterFile = bootCounterFile;
this.agent = commandProcessor;
}
/**
* Initialize transport mappings, message dispatcher, basic MIB modules,
* proxy forwarder, VACM and USM security, and custom MIB modules and objects
* provided by sub-classes.
*
* @throws IOException
* if initialization fails because transport initialization fails.
*/
public void init() throws IOException {
initTransportMappings();
initMessageDispatcher();
server.addContext(new OctetString());
snmpv2MIB = new SNMPv2MIB(sysDescr, sysOID, sysServices);
// register Snmp counters for updates
dispatcher.addCounterListener(snmpv2MIB);
agent.addCounterListener(snmpv2MIB);
snmpFrameworkMIB =
new SnmpFrameworkMIB((USM)
mpv3.getSecurityModel(SecurityModel.SECURITY_MODEL_USM),
dispatcher.getTransportMappings());
usmMIB = new UsmMIB(usm, SecurityProtocols.getInstance());
usm.addUsmUserListener(usmMIB);
vacmMIB = new VacmMIB(server);
snmpTargetMIB = new SnmpTargetMIB(session);
snmpNotificationMIB = new SnmpNotificationMIB();
snmpCommunityMIB = new SnmpCommunityMIB(snmpTargetMIB);
snmp4jLogMIB = new Snmp4jLogMib();
snmp4jConfigMIB = new Snmp4jConfigMib(snmpv2MIB.getSysUpTime());
snmp4jConfigMIB.setSnmpCommunityMIB(snmpCommunityMIB);
snmp4jConfigMIB.setDefaultConfigFile(configFile);
snmp4jConfigMIB.setPersistenceProvider(
Snmp4jConfigMib.Snmp4jCfgStorageFormatEnum.binary, this);
snmpProxyMIB = new SnmpProxyMIB();
notificationOriginator =
new NotificationOriginatorImpl(session, vacmMIB,
snmpv2MIB.getSysUpTime(),
snmpTargetMIB, snmpNotificationMIB);
setupDefaultProxyForwarder();
// add USM users
addUsmUser(usm);
// add SNMPv1/v2c community to SNMPv3 security name mappings
addCommunities(snmpCommunityMIB);
registerSnmpMIBs();
finishInit();
}
/**
* Register the basic MIB modules at the agent's <code>MOServer</code>.
*/
protected void registerSnmpMIBs() {
try {
snmpTargetMIB.registerMOs(server, null);
snmpNotificationMIB.registerMOs(server, null);
vacmMIB.registerMOs(server, null);
usmMIB.registerMOs(server, null);
snmpv2MIB.registerMOs(server, null);
snmpFrameworkMIB.registerMOs(server, null);
snmpCommunityMIB.registerMOs(server, null);
snmp4jLogMIB.registerMOs(server, null);
snmp4jConfigMIB.registerMOs(server, null);
snmpProxyMIB.registerMOs(server, null);
registerManagedObjects();
}
catch (DuplicateRegistrationException ex) {
ex.printStackTrace();
}
}
/**
* Unregister the basic MIB modules from the agent's <code>MOServer</code>.
*/
protected void unregisterSnmpMIBs() {
snmpTargetMIB.unregisterMOs(server, null);
snmpNotificationMIB.unregisterMOs(server, null);
vacmMIB.unregisterMOs(server, null);
usmMIB.unregisterMOs(server, null);
snmpv2MIB.unregisterMOs(server, null);
snmpFrameworkMIB.unregisterMOs(server, null);
snmpCommunityMIB.unregisterMOs(server, null);
snmp4jLogMIB.unregisterMOs(server, null);
snmp4jConfigMIB.unregisterMOs(server, null);
snmpProxyMIB.unregisterMOs(server, null);
unregisterManagedObjects();
}
/**
* Register additional managed objects at the agent's server.
*/
protected abstract void registerManagedObjects();
/**
* Unregister additional managed objects from the agent's server.
*/
protected abstract void unregisterManagedObjects();
/**
* Creates and registers the default proxy forwarder application
* ({@link ProxyForwarderImpl}).
*/
protected void setupDefaultProxyForwarder() {
defaultProxyForwarder = new ProxyForwarderImpl(session, snmpProxyMIB,
snmpTargetMIB);
agent.addProxyForwarder(defaultProxyForwarder,
null, ProxyForwarder.PROXY_TYPE_ALL);
((ProxyForwarderImpl)defaultProxyForwarder).addCounterListener(snmpv2MIB);
}
/**
* Loads the configuration using the specified import mode from the set
* config file.
* @param importMode
* one of the import modes defined by {@link ImportModes}.
*/
public void loadConfig(int importMode) {
try {
FileInputStream fis = null;
fis = new FileInputStream(configFile);
loadConfig(fis, importMode);
fis.close();
}
catch (FileNotFoundException ex) {
logger.error(ex);
}
catch (IOException ex) {
logger.error(ex);
}
}
/**
* Loads the configuration using the specified import mode from the set
* config file.
* @param configFile
* the config file (input stream) containing the serialized
* configuration data.
* @param importMode
* one of the import modes defined by {@link ImportModes}.
*/
public void loadConfig(InputStream configFile, int importMode)
throws IOException
{
ObjectInputStream ois = new ObjectInputStream(configFile);
DefaultMOInput is = new DefaultMOInput(ois);
is.setOverwriteMode(importMode);
MOServerPersistence p = new MOServerPersistence(server);
p.loadData(is);
ois.close();
}
/**
* Save the current (serializable) managed object configuration into
* the config file.
*/
public void saveConfig() {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(configFile);
saveConfig(fos);
fos.flush();
fos.close();
}
catch (FileNotFoundException ex) {
logger.error(ex);
}
catch (IOException ex) {
logger.error(ex);
}
}
/**
* Saves the current (serializable) managed object configuration into the
* specified config file (output stream).
* @param configFile
* an OutputStream to hold the serialized MIB data.
* @throws IOException
* if the supplied output stream throws an IOException during write
* operation.
*/
public void saveConfig(OutputStream configFile) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(configFile);
DefaultMOOutput os = new DefaultMOOutput(oos);
MOServerPersistence p = new MOServerPersistence(server);
p.saveData(os);
oos.flush();
}
/**
* Adds a shutdown hook that saves the internal config into the config file
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -