| 1 | |
package net.sf.snmpadaptor4j.core.trap; |
| 2 | |
|
| 3 | |
import java.net.InetAddress; |
| 4 | |
import java.util.ArrayList; |
| 5 | |
import java.util.List; |
| 6 | |
import java.util.Timer; |
| 7 | |
import java.util.TimerTask; |
| 8 | |
import org.apache.log4j.Logger; |
| 9 | |
import net.sf.snmpadaptor4j.SnmpManagerConfiguration; |
| 10 | |
import net.sf.snmpadaptor4j.api.SnmpApiFactory; |
| 11 | |
import net.sf.snmpadaptor4j.api.SnmpTrapSender; |
| 12 | |
import net.sf.snmpadaptor4j.object.SnmpTrap; |
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
public class SnmpManagers { |
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
private static final long CONNECTION_DURATION = 30000; |
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | 2 | final class ClosingTask |
| 29 | |
extends TimerTask { |
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
@Override |
| 36 | |
public void run () { |
| 37 | 4 | SnmpManagers.this.close(); |
| 38 | 4 | } |
| 39 | |
|
| 40 | |
} |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | 14 | protected final Logger logger = Logger.getLogger(SnmpManagers.class); |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
private final long connectionDuration; |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | 14 | protected final List<SnmpTrapSender> senderList = new ArrayList<SnmpTrapSender>(); |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
private final SnmpApiFactory senderFactory; |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | 14 | protected Timer timer = null; |
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | 14 | protected long closingTime = Long.MAX_VALUE; |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
public SnmpManagers (final SnmpApiFactory senderFactory) { |
| 77 | 11 | this(senderFactory, SnmpManagers.CONNECTION_DURATION); |
| 78 | 11 | } |
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
protected SnmpManagers (final SnmpApiFactory senderFactory, final long connectionDuration) { |
| 86 | 14 | super(); |
| 87 | 14 | this.senderFactory = senderFactory; |
| 88 | 14 | this.connectionDuration = connectionDuration; |
| 89 | 14 | } |
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
public final void initialize (final List<SnmpManagerConfiguration> managerList) throws Exception { |
| 97 | 4 | synchronized (this.senderList) { |
| 98 | 4 | this.senderList.clear(); |
| 99 | 4 | final InetAddress agentAddress = InetAddress.getLocalHost(); |
| 100 | 4 | this.logger.info("Local IP address (SNMP agent) = " + agentAddress); |
| 101 | 4 | for (final SnmpManagerConfiguration manager : managerList) { |
| 102 | 8 | this.logger.info("SNMP manager at " + manager.getAddress() + ":" + manager.getPort() + " (v" + manager.getVersion() + " - community " |
| 103 | |
+ manager.getCommunity() + ")"); |
| 104 | 8 | this.senderList.add(this.senderFactory.newSnmpTrapSender(agentAddress, manager.getAddress(), manager.getPort(), manager.getVersion(), |
| 105 | |
manager.getCommunity())); |
| 106 | |
} |
| 107 | 4 | } |
| 108 | 4 | } |
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
public void send (final SnmpTrap trap) { |
| 115 | 5 | synchronized (this.senderList) { |
| 116 | 5 | if (this.logger.isDebugEnabled()) { |
| 117 | 3 | this.logger.debug("New " + trap + " to send"); |
| 118 | |
} |
| 119 | 5 | if (this.logger.isTraceEnabled()) { |
| 120 | 3 | trap.traceTo(this.logger); |
| 121 | |
} |
| 122 | 5 | for (final SnmpTrapSender sender : this.senderList) { |
| 123 | |
try { |
| 124 | 10 | if (!sender.isConnected()) { |
| 125 | 4 | sender.open(); |
| 126 | |
} |
| 127 | 10 | sender.send(trap); |
| 128 | |
} |
| 129 | 2 | catch (final Throwable e) { |
| 130 | 2 | this.logger.error("Unable to send a trap to the SNMP manager at " + sender.getName(), e); |
| 131 | 18 | } |
| 132 | |
} |
| 133 | 5 | this.closingTime = System.currentTimeMillis() + this.connectionDuration; |
| 134 | 5 | if (this.timer == null) { |
| 135 | 2 | this.timer = new Timer(); |
| 136 | 2 | this.timer.schedule(new ClosingTask(), this.connectionDuration, this.connectionDuration); |
| 137 | |
} |
| 138 | 5 | } |
| 139 | 5 | } |
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
final void close () { |
| 145 | 6 | this.logger.trace("Checking Connections to the SNMP managers..."); |
| 146 | 6 | if (this.closingTime < System.currentTimeMillis()) { |
| 147 | 4 | synchronized (this.senderList) { |
| 148 | 4 | if (this.closingTime < System.currentTimeMillis()) { |
| 149 | 3 | this.logger.trace("Closing connections to the SNMP managers..."); |
| 150 | 3 | this.timer.cancel(); |
| 151 | 3 | this.timer = null; |
| 152 | 3 | this.closingTime = Long.MAX_VALUE; |
| 153 | 3 | for (final SnmpTrapSender sender : this.senderList) { |
| 154 | |
try { |
| 155 | 7 | if (sender.isConnected()) { |
| 156 | 6 | sender.close(); |
| 157 | |
} |
| 158 | |
} |
| 159 | 2 | catch (final Throwable e) { |
| 160 | 2 | this.logger.error("Unable to close the connection to the SNMP manager at " + sender.getName(), e); |
| 161 | 12 | } |
| 162 | |
} |
| 163 | 3 | this.logger.debug("Connections closed to the SNMP managers"); |
| 164 | |
} |
| 165 | 4 | } |
| 166 | |
} |
| 167 | 6 | } |
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
@Override |
| 174 | |
public final String toString () { |
| 175 | 1 | return "SnmpManagers"; |
| 176 | |
} |
| 177 | |
|
| 178 | |
} |