📄 baseagent.java
字号:
* when a SIGTERM (Ctrl-C) is terminating the agent.
*/
protected void addShutdownHook() {
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
saveConfig();
}
});
}
/**
* Finishes intialization of the agent by connecting server and command
* processor, setting up USM, VACM, notification targets, and finally sending
* a coldStart notification to configured targets.
*/
protected void finishInit() {
agent.addMOServer(server);
agent.setCoexistenceProvider(snmpCommunityMIB);
addViews(vacmMIB);
addNotificationTargets(snmpTargetMIB, snmpNotificationMIB);
agent.setVacm(vacmMIB);
dispatcher.addCommandResponder(agent);
notificationOriginator.notify(new OctetString(), SnmpConstants.coldStart,
new VariableBinding[0]);
}
/**
* Starts the agent by let it listen on the configured SNMP agent ports
* (transpot mappings).
*/
public void run() {
try {
session.listen();
}
catch (IOException iox) {
logger.error(iox);
}
}
/**
* Initializes the message dispatcher ({@link MessageDispatcherImpl}) with
* the transport mappings.
*/
protected void initMessageDispatcher() {
dispatcher = new MessageDispatcherImpl();
session = new Snmp(dispatcher);
mpv3 = new MPv3(agent.getContextEngineID().getValue());
usm = new USM(SecurityProtocols.getInstance(),
agent.getContextEngineID(),
updateEngineBoots());
SecurityModels.getInstance().addSecurityModel(usm);
SecurityProtocols.getInstance().addDefaultProtocols();
dispatcher.addMessageProcessingModel(new MPv1());
dispatcher.addMessageProcessingModel(new MPv2c());
dispatcher.addMessageProcessingModel(mpv3);
for (int i=0; i<transportMappings.length; i++) {
session.addTransportMapping(transportMappings[i]);
}
}
/**
* Updates the engine boots counter and returns the actual value.
* @return
* the actual boots counter value.
*/
protected int updateEngineBoots() {
int boots = getEngineBoots();
boots++;
if (boots <= 0) {
boots = 1;
}
setEngineBoots(boots);
return boots;
}
/**
* Reads the engine boots counter from the corresponding input stream (file).
* @return
* the boots counter value read or zero if it could not be read.
*/
protected int getEngineBoots() {
FileInputStream fis = null;
try {
fis = new FileInputStream(bootCounterFile);
ObjectInputStream ois = new ObjectInputStream(fis);
int boots = ois.readInt();
if (logger.isInfoEnabled()) {
logger.info("Engine boots is: "+boots);
}
return boots;
}
catch (FileNotFoundException ex) {
logger.warn("Could not find conig file: "+configFile);
}
catch (IOException iox) {
if (logger.isDebugEnabled()) {
iox.printStackTrace();
}
logger.error("Failed to read boot counter: "+iox.getMessage());
}
finally {
if (fis != null) {
try {
fis.close();
}
catch (IOException ex1) {
logger.warn(ex1);
}
}
}
return 0;
}
protected void setEngineBoots(int engineBoots) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(bootCounterFile);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeInt(engineBoots);
oos.close();
if (logger.isInfoEnabled()) {
logger.info("Wrote boot counter: " + engineBoots);
}
}
catch (FileNotFoundException fnfex) {
logger.error("Boot counter configuration file not found: "+
fnfex.getMessage());
}
catch (IOException iox) {
logger.error("Failed to write boot counter: "+iox.getMessage());
}
finally {
if (fos != null) {
try {
fos.close();
}
catch (IOException ex1) {
logger.warn(ex1);
}
}
}
}
/**
* Adds all the necessary initial users to the USM.
* @param usm
* the USM instance used by this agent.
*/
protected abstract void addUsmUser(USM usm);
/**
* Adds initial notification targets and filters.
* @param targetMIB
* the SnmpTargetMIB holding the target configuration.
* @param notificationMIB
* the SnmpNotificationMIB holding the notification (filter)
* configuration.
*/
protected abstract void addNotificationTargets(SnmpTargetMIB targetMIB,
SnmpNotificationMIB notificationMIB);
/**
* Adds initial VACM configuration.
* @param vacmMIB
* the VacmMIB holding the agent's view configuration.
*/
protected abstract void addViews(VacmMIB vacmMIB);
/**
* Adds community to security name mappings needed for SNMPv1 and SNMPv2c.
* @param communityMIB
* the SnmpCommunityMIB holding coexistence configuration for community
* based security models.
*/
protected abstract void addCommunities(SnmpCommunityMIB communityMIB);
/**
* Initializes the transport mappings (ports) to be used by the agent.
* @throws IOException
*/
protected void initTransportMappings() throws IOException {
transportMappings = new TransportMapping[1];
transportMappings[0] =
new DefaultUdpTransportMapping(new UdpAddress("0.0.0.0/161"));
}
public NotificationOriginator getNotificationOriginator() {
return notificationOriginator;
}
public void setDefaultProxyForwarder(ProxyForwarder defaultProxyForwarder) {
this.defaultProxyForwarder = defaultProxyForwarder;
}
public void setSysDescr(OctetString sysDescr) {
this.sysDescr = sysDescr;
}
public void setSysOID(OID sysOID) {
this.sysOID = sysOID;
}
public void setSysServices(Integer32 sysServices) {
this.sysServices = sysServices;
}
public void setAgent(CommandProcessor agent) {
this.agent = agent;
}
public void setBootCounterFile(File bootCounterFile) {
this.bootCounterFile = bootCounterFile;
}
public void setConfigFile(File configFile) {
this.configFile = configFile;
}
public ProxyForwarder getDefaultProxyForwarder() {
return defaultProxyForwarder;
}
public OctetString getSysDescr() {
return sysDescr;
}
public OID getSysOID() {
return sysOID;
}
public Integer32 getSysServices() {
return sysServices;
}
public CommandProcessor getAgent() {
return agent;
}
public File getBootCounterFile() {
return bootCounterFile;
}
public File getConfigFile() {
return configFile;
}
public Snmp4jConfigMib getSnmp4jConfigMIB() {
return snmp4jConfigMIB;
}
public Snmp4jLogMib getSnmp4jLogMIB() {
return snmp4jLogMIB;
}
public SnmpCommunityMIB getSnmpCommunityMIB() {
return snmpCommunityMIB;
}
public SnmpFrameworkMIB getSnmpFrameworkMIB() {
return snmpFrameworkMIB;
}
public SnmpNotificationMIB getSnmpNotificationMIB() {
return snmpNotificationMIB;
}
public SnmpProxyMIB getSnmpProxyMIB() {
return snmpProxyMIB;
}
public SnmpTargetMIB getSnmpTargetMIB() {
return snmpTargetMIB;
}
public SNMPv2MIB getSnmpv2MIB() {
return snmpv2MIB;
}
public UsmMIB getUsmMIB() {
return usmMIB;
}
public VacmMIB getVacmMIB() {
return vacmMIB;
}
public Snmp getSession() {
return session;
}
public DefaultMOServer getServer() {
return server;
}
public MPv3 getMPv3() {
return mpv3;
}
public USM getUsm() {
return usm;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -