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