View Javadoc

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   * Object representing all SNMP managers on the network to which SNMP traps should be sent.
16   * @author <a href="http://fr.linkedin.com/in/jpminetti/">Jean-Philippe MINETTI</a>
17   */
18  public class SnmpManagers {
19  
20  	/**
21  	 * CONSTANT: Connection duration after each sending of SNMP traps (in milliseconds).
22  	 */
23  	private static final long CONNECTION_DURATION = 30000;
24  
25  	/**
26  	 * {@link TimerTask} for close connections to all SNMP managers after a period of inactivity.
27  	 */
28  	final class ClosingTask
29  			extends TimerTask {
30  
31  		/*
32  		 * {@inheritDoc}
33  		 * @see java.util.TimerTask#run()
34  		 */
35  		@Override
36  		public void run () {
37  			SnmpManagers.this.close();
38  		}
39  
40  	}
41  
42  	/**
43  	 * Logger.
44  	 */
45  	protected final Logger logger = Logger.getLogger(SnmpManagers.class);
46  
47  	/**
48  	 * Connection duration after each sending of SNMP traps (in milliseconds).
49  	 */
50  	private final long connectionDuration;
51  
52  	/**
53  	 * List of SNMP trap sender.
54  	 */
55  	protected final List<SnmpTrapSender> senderList = new ArrayList<SnmpTrapSender>();
56  
57  	/**
58  	 * Factory used for instantiate senders of SNMP traps to SNMP managers.
59  	 */
60  	private final SnmpApiFactory senderFactory;
61  
62  	/**
63  	 * Timer used to close connections to all SNMP managers.
64  	 */
65  	protected Timer timer = null;
66  
67  	/**
68  	 * Closing time of connections to all SNMP managers (in milliseconds).
69  	 */
70  	protected long closingTime = Long.MAX_VALUE;
71  
72  	/**
73  	 * Constructor.
74  	 * @param senderFactory Factory used for instantiate senders of SNMP traps to SNMP managers (must not be <code>NULL</code>).
75  	 */
76  	public SnmpManagers (final SnmpApiFactory senderFactory) {
77  		this(senderFactory, SnmpManagers.CONNECTION_DURATION);
78  	}
79  
80  	/**
81  	 * Constructor (only for tests).
82  	 * @param senderFactory Factory used for instantiate senders of SNMP traps to SNMP managers (must not be <code>NULL</code>).
83  	 * @param connectionDuration Connection duration after each sending of SNMP traps (in milliseconds).
84  	 */
85  	protected SnmpManagers (final SnmpApiFactory senderFactory, final long connectionDuration) {
86  		super();
87  		this.senderFactory = senderFactory;
88  		this.connectionDuration = connectionDuration;
89  	}
90  
91  	/**
92  	 * Initializes the configuration for SNMP traps sending to SNMP managers.
93  	 * @param managerList Connection parameters to all SNMP managers (must not be <code>NULL</code>).
94  	 * @throws Exception Exception if an error has occurred.
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 	 * Sends a SNMP trap to all managers.
112 	 * @param trap Object representing a specific or generic SNMP trap (must not be <code>NULL</code>).
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 	 * Closes connections to all SNMP managers.
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 	 * {@inheritDoc}
171 	 * @see java.lang.Object#toString()
172 	 */
173 	@Override
174 	public final String toString () {
175 		return "SnmpManagers";
176 	}
177 
178 }