Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*_############################################################################
_##
_## SNMP4J-Agent 3 - SampleAgent.java
_##
_## Copyright (C) 2005-2021 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.example;
import java.io.*;
import java.security.NoSuchAlgorithmException;
import java.text.*;
import java.util.*;
import org.snmp4j.*;
import org.snmp4j.agent.*;
import org.snmp4j.agent.io.*;
import org.snmp4j.agent.io.prop.*;
import org.snmp4j.agent.mo.*;
import org.snmp4j.agent.mo.snmp.SNMPv2MIB;
import org.snmp4j.agent.mo.snmp.dh.DHKickstartParameters;
import org.snmp4j.agent.mo.snmp.dh.DHKickstartParametersImpl;
import org.snmp4j.cfg.EngineBootsCounterFile;
import org.snmp4j.cfg.EngineBootsProvider;
import org.snmp4j.log.*;
import org.snmp4j.mp.*;
import org.snmp4j.security.SecurityModels;
import org.snmp4j.smi.*;
import org.snmp4j.transport.*;
import org.snmp4j.util.*;
import org.snmp4j.security.SecurityProtocols;
import org.snmp4j.agent.mo.snmp.TimeStamp;
import javax.crypto.Cipher;
/**
* The SampleAgent uses an {@link AgentConfigManager} instance to create a minimal SNMP agent using the configuration
* defined by {@code SampleAgentConfig.properties} in this package. That properties file defines the initial content of
* the registered MIB objects of this agent which may differ from the hard coded defaults.
*
* In order to add a new MIB object, call {@code server.register(..)} or replace the {@code Modules.java} file in this
* package by the {@code Modules.java} generated by AgenPro for your MIB module(s).
*
* The agent uses the {@link ConsoleLogFactory} to log messages.
*
* @author Frank Fock
* @version 3.2.1
*/
public class SampleAgent {
static {
LogFactory.setLogFactory(new ConsoleLogFactory());
// This enables logging of user credentials (keys and passwords) in plain text to the DEBUG log
SNMP4JSettings.setSecretLoggingEnabled(true);
}
public static final String COMMAND_LINE_OPTIONS = "-c[s{=SampleAgent.cfg}] -bc[s{=SampleAgent.bc}] +dhks[s] +u[s] " +
"+tls-trust-ca[s] +tls-peer-id[s] +tls-local-id[s] +tls-version[s{=TLSv1}<(TLSv1|TLSv1.1|TLSv1.2|TLSv1.3)>] +dtls-version[s{=TLSv1.2}<(TLSv1.0|TLSv1.2)>]" +
"+Djavax.net.ssl.keyStore +Djavax.net.ssl.keyStorePassword " +
"+Djavax.net.ssl.trustStore +Djavax.net.ssl.trustStorePassword " +
"+ts[s] +cfg[s] +x ";
public static final String COMMAND_LINE_PARAMS = "#address[s<(udp|tcp|tls|dtls):.*[/[0-9]+]?>] ..";
private static final LogAdapter logger = LogFactory.getLogger(SampleAgent.class);
protected AgentConfigManager agent;
protected MOServer server;
private final String configFile;
private final File bootCounterFile;
// supported MIBs
protected Modules modules;
protected Properties tableSizeLimits;
@SuppressWarnings("unchecked")
public SampleAgent(Map> args) {
LogFactory.getLogFactory().getRootLogger().setLogLevel(LogLevel.ALL);
SNMP4JSettings.setReportSecurityLevelStrategy(SNMP4JSettings.ReportSecurityLevelStrategy.noAuthNoPrivIfNeeded);
//Security.setProperty("crypto.policy", "unlimited");
try {
logger.info("Max supported AES key length is "+ Cipher.getMaxAllowedKeyLength("AES"));
} catch (NoSuchAlgorithmException e) {
logger.error("AES privacy not supported by this VM: ", e);
}
server = new DefaultMOServer();
MOServer[] moServers = new MOServer[]{server};
String configFilename = null;
if (args.containsKey("cfg")) {
configFilename = (String) ArgumentParser.getValue(args, "cfg", 0);
}
configFile = (String) (args.get("c")).get(0);
bootCounterFile = new File((String) (args.get("bc")).get(0));
EngineBootsCounterFile engineBootsCounterFile = new EngineBootsCounterFile(bootCounterFile);
OctetString ownEngineId = engineBootsCounterFile.getEngineId(new OctetString(MPv3.createLocalEngineID()));
List> tlsVersions = args.get("tls-version");
if (tlsVersions != null && (tlsVersions.size() > 0)) {
System.setProperty(SnmpConfigurator.P_TLS_VERSION, (String) tlsVersions.get(0));
}
MOInputFactory configurationFactory = null;
// load initial configuration from properties file only if there is no persistent data for the default context:
configurationFactory = createMOInputFactory(configFilename, ImportMode.restoreChanges);
this.tableSizeLimits = getTableSizeLimitsProperties(args);
String dhKickstartInfoPath = (String) ArgumentParser.getFirstValue(args.get("dhks"));
DefaultMOPersistenceProvider persistenceProvider = new DefaultMOPersistenceProvider(moServers, configFile);
setupAgent(args, persistenceProvider, engineBootsCounterFile, ownEngineId,
moServers, configurationFactory, args.get("address"), dhKickstartInfoPath);
}
protected MOInputFactory createMOInputFactory(String configFilename, ImportMode importMode) {
MOInputFactory configurationFactory;
InputStream configInputStream =
SampleAgent.class.getResourceAsStream("SampleAgentConfig.properties");
final Properties props = new Properties();
if (configFilename != null) {
try {
configInputStream = new FileInputStream(configFilename);
} catch (FileNotFoundException ex1) {
logger.error("Config file '" + configFilename + "' not found: " + ex1.getMessage(), ex1);
throw new RuntimeException(ex1);
}
}
try {
props.load(configInputStream);
} catch (IOException ex) {
ex.printStackTrace();
}
configurationFactory = () -> new PropertyMOInput(props, this.agent, importMode);
return configurationFactory;
}
protected SampleAgent(Map> args,
MOServer[] moServers, MOPersistenceProvider persistenceProvider,
Properties tableSizeLimits, ImportMode importMode) {
this.server = new DefaultMOServer();
List> tlsVersions = args.get("tls-version");
if (tlsVersions != null && (tlsVersions.size() > 0)) {
System.setProperty(SnmpConfigurator.P_TLS_VERSION, (String) tlsVersions.get(0));
}
String configFilename = null;
if (args.containsKey("cfg")) {
configFilename = (String) ArgumentParser.getValue(args, "cfg", 0);
}
configFile = (String) (args.get("c")).get(0);
this.tableSizeLimits = tableSizeLimits;
String dhKickstartInfoPath = (String) ArgumentParser.getFirstValue(args.get("dhks"));
bootCounterFile = new File((String) (args.get("bc")).get(0));
EngineBootsCounterFile engineBootsCounterFile = new EngineBootsCounterFile(bootCounterFile);
OctetString ownEngineId = engineBootsCounterFile.getEngineId(new OctetString(MPv3.createLocalEngineID()));
setupAgent(args, persistenceProvider, engineBootsCounterFile, ownEngineId, moServers,
createMOInputFactory(configFilename, importMode), args.get("address"), dhKickstartInfoPath);
}
protected void setupAgent(Map> args,
MOPersistenceProvider persistenceProvider,
EngineBootsProvider engineBootsProvider,
OctetString engineID,
MOServer[] moServers, MOInputFactory configurationFactory,
List