/
Main.java
367 lines (317 loc) · 12.4 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2006-2014 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2014 The OpenNMS Group, Inc.
*
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
*
* OpenNMS(R) is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* OpenNMS(R) is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with OpenNMS(R). If not, see:
* http://www.gnu.org/licenses/
*
* For more information contact:
* OpenNMS(R) Licensing <license@opennms.org>
* http://www.opennms.org/
* http://www.opennms.com/
*******************************************************************************/
package org.opennms.poller.remote;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.JOptionPane;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import org.opennms.netmgt.icmp.NullPinger;
import org.opennms.netmgt.icmp.Pinger;
import org.opennms.netmgt.icmp.PingerFactory;
import org.opennms.netmgt.poller.remote.PollerFrontEnd;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
/**
* <p>Main class.</p>
*
* @author <a href="mailto:ranger@opennms.org">Benjamin Reed</a>
* @author <a href="mailto:brozow@opennms.org">Mathew Brozowski</a>
*/
public class Main implements Runnable {
private static final Logger LOG = LoggerFactory.getLogger(Main.class);
protected final String[] m_args;
protected URI m_uri = null;
protected String m_locationName;
protected String m_username = null;
protected String m_password = null;
protected static boolean m_shuttingDown = false;
protected boolean m_gui = false;
protected boolean m_disableIcmp = false;
private Main(String[] args) {
// Give us some time to attach a debugger if necessary
//try { Thread.sleep(20000); } catch (InterruptedException e) {}
m_args = Arrays.copyOf(args, args.length);
}
public void initializePinger() {
if (m_disableIcmp) {
LOG.info("Disabling ICMP by user request.");
System.setProperty("org.opennms.netmgt.icmp.pingerClass", "org.opennms.netmgt.icmp.NullPinger");
PingerFactory.setInstance(new NullPinger());
return;
}
final String pingerClass = System.getProperty("org.opennms.netmgt.icmp.pingerClass");
if (pingerClass == null) {
LOG.info("System property org.opennms.netmgt.icmp.pingerClass is not set; using JnaPinger by default");
System.setProperty("org.opennms.netmgt.icmp.pingerClass", "org.opennms.netmgt.icmp.jna.JnaPinger");
}
LOG.info("Pinger class: {}", System.getProperty("org.opennms.netmgt.icmp.pingerClass"));
try {
final Pinger pinger = PingerFactory.getInstance();
pinger.ping(InetAddress.getLoopbackAddress());
} catch (final Throwable t) {
LOG.warn("Unable to get pinger instance. Setting pingerClass to NullPinger. For details, see: http://www.opennms.org/wiki/ICMP_could_not_be_initialized");
System.setProperty("org.opennms.netmgt.icmp.pingerClass", "org.opennms.netmgt.icmp.NullPinger");
PingerFactory.setInstance(new NullPinger());
if (m_gui) {
final String message = "ICMP (ping) could not be initialized: " + t.getMessage()
+ "\nDisabling ICMP and using the NullPinger instead."
+ "\nFor details, see: http://www.opennms.org/wiki/ICMP_could_not_be_initialized";
JOptionPane.showMessageDialog(null, message, "ICMP Not Available", JOptionPane.WARNING_MESSAGE);
}
}
}
private void getAuthenticationInfo() {
if (m_uri == null || m_uri.getScheme() == null) {
throw new RuntimeException("no URI specified!");
}
if (m_uri.getScheme().equals("rmi")) {
// RMI doesn't have authentication
return;
}
if (m_username == null) {
GroovyGui gui = createGui();
gui.createAndShowGui();
AuthenticationBean auth = gui.getAuthenticationBean();
m_username = auth.getUsername();
m_password = auth.getPassword();
}
if (m_username != null) {
SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_GLOBAL);
SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(m_username, m_password));
}
}
private static GroovyGui createGui() {
try {
return (GroovyGui)Class.forName("org.opennms.groovy.poller.remote.ConfigurationGui").newInstance();
} catch (Throwable e) {
throw new RuntimeException("Unable to find Configuration GUI!", e);
}
}
@Override
public void run() {
try {
parseArguments(m_args);
initializePinger();
getAuthenticationInfo();
AbstractApplicationContext context = createAppContext();
PollerFrontEnd frontEnd = getPollerFrontEnd(context);
if (!m_gui) {
if (!frontEnd.isRegistered()) {
if (m_locationName == null) {
LOG.error("No location name provided. You must pass a location name the first time you start the remote poller!");
System.exit(27);
} else {
frontEnd.register(m_locationName);
}
}
}
} catch(Throwable e) {
// a fatal exception occurred
LOG.error("Exception occurred during registration!", e);
System.exit(27);
}
}
private void parseArguments(String[] args) throws ParseException {
Options options = new Options();
options.addOption("h", "help", false, "this help");
options.addOption("d", "debug", false, "write debug messages to the log");
options.addOption("g", "gui", false, "start a GUI (default: false)");
options.addOption("i", "disable-icmp", false, "disable ICMP/ping (overrides -Dorg.opennms.netmgt.icmp.pingerClass=)");
options.addOption("l", "location", true, "the location name of this remote poller");
options.addOption("u", "url", true, "the URL for OpenNMS (example: https://server-name/opennms-remoting)");
options.addOption("n", "name", true, "the name of the user to connect as");
options.addOption("p", "password", true, "the password to use when connecting");
CommandLineParser parser = new PosixParser();
CommandLine cl = parser.parse(options, args);
if (cl.hasOption("h")) {
usage(options);
System.exit(1);
}
if (cl.hasOption("d")) {
}
if (cl.hasOption("i")) {
m_disableIcmp = true;
}
if (cl.hasOption("l")) {
m_locationName = cl.getOptionValue("l");
}
if (cl.hasOption("u")) {
String arg = cl.getOptionValue("u").toLowerCase();
try {
m_uri = new URI(arg);
} catch (URISyntaxException e) {
usage(options);
e.printStackTrace();
System.exit(2);
}
} else {
usage(options);
System.exit(3);
}
if (cl.hasOption("g")) {
m_gui = true;
}
if (cl.hasOption("n")) {
m_username = cl.getOptionValue("n");
m_password = cl.getOptionValue("p");
if (m_password == null) {
m_password = "";
}
}
// If we cannot obtain the username/password from the command line, attempt
// to optionally get it from system properties
if (m_username == null) {
m_username = System.getProperty("opennms.poller.server.username");
if (m_username != null) {
m_password = System.getProperty("opennms.poller.server.password");
if (m_password == null) {
m_password = "";
}
}
}
}
private static void usage(Options o) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp(Main.class.getName() + " -u [URL] [options]", o);
}
private AbstractApplicationContext createAppContext() {
/*
* Set a system property called user.home.url so that the
* Spring contexts can reference resources that are stored
* in the user's home directory.
*/
File homeDir = new File(System.getProperty("user.home"));
String homeUrl = homeDir.toURI().toString();
// Trim the trailing file separator off of the end of the URI
if (homeUrl.endsWith("/")) {
homeUrl = homeUrl.substring(0, homeUrl.length()-1);
}
LOG.info("user.home.url = {}", homeUrl);
System.setProperty("user.home.url", homeUrl);
String serverURI = m_uri.toString().replaceAll("/*$", "");
LOG.info("opennms.poller.server.url = {}", serverURI);
System.setProperty("opennms.poller.server.url", serverURI);
LOG.info("location name = {}", m_locationName);
List<String> configs = new ArrayList<String>();
configs.add("classpath:/META-INF/opennms/applicationContext-remotePollerBackEnd-" + m_uri.getScheme() + ".xml");
configs.add("classpath:/META-INF/opennms/applicationContext-pollerFrontEnd.xml");
if (m_gui) {
configs.add("classpath:/META-INF/opennms/applicationContext-ws-gui.xml");
}
final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(configs.toArray(new String[0]));
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
m_shuttingDown = true;
// MVR: gracefully shutdown scheduler, otherwise context.close will raise an exception
// See #NMS-6966 for more details.
try {
((Scheduler)context.getBean("scheduler")).shutdown(true);
} catch (SchedulerException e) {
LOG.warn("Scheduler shutdown failed.", e);
}
// now close the application context
context.close();
}
});
return context;
}
private static PollerFrontEnd getPollerFrontEnd(AbstractApplicationContext context) {
PollerFrontEnd frontEnd = (PollerFrontEnd) context.getBean("pollerFrontEnd");
frontEnd.addPropertyChangeListener(new PropertyChangeListener() {
private boolean shouldExit(PropertyChangeEvent e) {
LOG.info("shouldExit: received property change event for property: {}; oldvalue: {}; newvalue: {}", e.getPropertyName(), e.getOldValue(), e.getNewValue());
String propName = e.getPropertyName();
Object newValue = e.getNewValue();
// if exitNecessary becomes true.. then return true
if ("exitNecessary".equals(propName) && Boolean.TRUE.equals(newValue)) {
LOG.info("shouldExit: Exiting because exitNecessary is TRUE");
return true;
}
// if started becomes false the we should exit
if ("started".equals(propName) && Boolean.FALSE.equals(newValue)) {
LOG.info("shouldExit: Exiting because started is now false");
return true;
}
LOG.info("shouldExit: not exiting");
return false;
}
@Override
public void propertyChange(PropertyChangeEvent e) {
if (!m_shuttingDown && shouldExit(e)) {
System.exit(10);
}
}
});
return frontEnd;
}
/**
* <p>main</p>
*
* @param args an array of {@link java.lang.String} objects.
* @throws java.lang.Exception if any.
*/
public static void main(String[] args) {
try {
String killSwitchFileName = System.getProperty("opennms.poller.killSwitch.resource");
File killSwitch = null;
if (! "".equals(killSwitchFileName) && killSwitchFileName != null) {
killSwitch = new File(System.getProperty("opennms.poller.killSwitch.resource"));
if (!killSwitch.exists()) {
try {
killSwitch.createNewFile();
} catch (IOException ioe) {
// We'll just do without one
}
}
}
new Main(args).run();
} catch (Throwable e) {
e.printStackTrace();
}
}
}