-
Notifications
You must be signed in to change notification settings - Fork 562
/
SyslogdIT.java
476 lines (392 loc) · 20.7 KB
/
SyslogdIT.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2005-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.netmgt.syslogd;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.opennms.core.utils.InetAddressUtils.addr;
import java.io.InputStream;
import java.lang.reflect.UndeclaredThrowableException;
import java.net.BindException;
import java.net.DatagramPacket;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.commons.io.IOUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.opennms.core.concurrent.WaterfallExecutor;
import org.opennms.core.spring.BeanUtils;
import org.opennms.core.test.ConfigurationTestUtils;
import org.opennms.core.test.MockLogAppender;
import org.opennms.core.test.OpenNMSJUnit4ClassRunner;
import org.opennms.core.test.db.MockDatabase;
import org.opennms.core.test.db.annotations.JUnitTemporaryDatabase;
import org.opennms.netmgt.config.SyslogdConfig;
import org.opennms.netmgt.config.SyslogdConfigFactory;
import org.opennms.netmgt.config.syslogd.UeiMatch;
import org.opennms.netmgt.dao.mock.EventAnticipator;
import org.opennms.netmgt.dao.mock.MockEventIpcManager;
import org.opennms.netmgt.model.events.EventBuilder;
import org.opennms.netmgt.xml.event.Event;
import org.opennms.netmgt.xml.event.Parm;
import org.opennms.test.JUnitConfigurationEnvironment;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.transaction.annotation.Transactional;
@RunWith(OpenNMSJUnit4ClassRunner.class)
@ContextConfiguration(locations={
"classpath:/META-INF/opennms/applicationContext-commonConfigs.xml",
"classpath:/META-INF/opennms/applicationContext-minimal-conf.xml",
"classpath:/META-INF/opennms/applicationContext-soa.xml",
"classpath:/META-INF/opennms/applicationContext-dao.xml",
"classpath*:/META-INF/opennms/component-dao.xml",
"classpath:/META-INF/opennms/applicationContext-daemon.xml",
"classpath:/META-INF/opennms/mockEventIpcManager.xml",
"classpath:/META-INF/opennms/applicationContext-setupIpLike-enabled.xml",
"classpath:/META-INF/opennms/applicationContext-databasePopulator.xml"
})
@JUnitConfigurationEnvironment
@JUnitTemporaryDatabase(dirtiesContext=false,tempDbClass=MockDatabase.class)
@Transactional
public class SyslogdIT implements InitializingBean {
final String m_localhost = "127.0.0.1";
private Syslogd m_syslogd;
private final ExecutorService m_executorService = Executors.newFixedThreadPool(3);
@Autowired
private MockEventIpcManager m_eventIpcManager;
@Override
public void afterPropertiesSet() throws Exception {
BeanUtils.assertAutowiring(this);
}
@Before
public void setUp() throws Exception {
MockLogAppender.setupLogging();
MockLogAppender.resetEvents();
InputStream stream = null;
try {
stream = ConfigurationTestUtils.getInputStreamForResource(this, "/etc/syslogd-configuration.xml");
SyslogdConfigFactory.setInstance(new SyslogdConfigFactory(stream));
} finally {
if (stream != null) {
IOUtils.closeQuietly(stream);
}
}
m_syslogd = new Syslogd();
m_syslogd.init();
// Verify that the test syslogd-configuration.xml file was loaded
boolean foundBeer = false;
boolean foundMalt = false;
assertEquals(10514, SyslogdConfigFactory.getInstance().getSyslogPort());
for (final UeiMatch match : SyslogdConfigFactory.getInstance().getUeiList().getUeiMatch()) {
if (match.getProcessMatch() != null) {
if (!foundBeer && "beerd".equals(match.getProcessMatch().getExpression())) {
foundBeer = true;
} else if (!foundMalt && "maltd".equals(match.getProcessMatch().getExpression())) {
foundMalt = true;
}
}
}
assertTrue(foundBeer);
assertTrue(foundMalt);
}
@After
public void tearDown() throws Exception {
MockLogAppender.assertNoErrorOrGreater();
}
@After
public void stopSyslogd() throws Exception {
m_syslogd.stop();
}
/**
* Send a raw syslog message and expect a given event as a result
*
* @param testPDU The raw syslog message as it would appear on the wire (just the UDP payload)
* @param expectedHost The host from which the event should be resolved as originating
* @param expectedUEI The expected UEI of the resulting event
* @param expectedLogMsg The expected contents of the logmsg for the resulting event
*
* @throws UnknownHostException
* @throws InterruptedException
* @throws ExecutionException
*/
private List<Event> doMessageTest(String testPDU, String expectedHost, String expectedUEI, String expectedLogMsg) throws UnknownHostException, InterruptedException, ExecutionException {
startSyslogdGracefully();
final EventBuilder expectedEventBldr = new EventBuilder(expectedUEI, "syslogd");
expectedEventBldr.setInterface(addr(expectedHost));
expectedEventBldr.setLogDest("logndisplay");
expectedEventBldr.setLogMessage(expectedLogMsg);
final EventAnticipator ea = new EventAnticipator();
m_eventIpcManager.addEventListener(ea);
ea.anticipateEvent(expectedEventBldr.getEvent());
final SyslogClient sc = new SyslogClient(null, 10, SyslogClient.LOG_DAEMON, addr(m_localhost));
final DatagramPacket pkt = sc.getPacket(SyslogClient.LOG_DEBUG, testPDU);
final SyslogdConfig config = SyslogdConfigFactory.getInstance();
WaterfallExecutor.waterfall(m_executorService, new SyslogConnection(pkt, config.getForwardingRegexp(), config.getMatchingGroupHost(), config.getMatchingGroupMessage(), config.getUeiList(), config.getHideMessages(), config.getDiscardUei()));
ea.verifyAnticipated(5000,0,0,0,0);
final Event receivedEvent = ea.getAnticipatedEventsRecieved().get(0);
assertEquals("Log messages do not match", expectedLogMsg, receivedEvent.getLogmsg().getContent());
return ea.getAnticipatedEventsRecieved();
}
private List<Event> doMessageTest(String testPDU, String expectedHost, String expectedUEI, String expectedLogMsg, Map<String,String> expectedParams) throws UnknownHostException, InterruptedException, ExecutionException {
final List<Event> receivedEvents = doMessageTest(testPDU, expectedHost, expectedUEI, expectedLogMsg);
final Map<String,String> actualParms = new HashMap<String,String>();
for (final Parm actualParm : receivedEvents.get(0).getParmCollection()) {
actualParms.put(actualParm.getParmName(), actualParm.getValue().getContent());
}
for (final String expectedKey : expectedParams.keySet()) {
final String expectedValue = expectedParams.get(expectedKey);
assertTrue("Actual event does not have a parameter called " + expectedKey, actualParms.containsKey(expectedKey));
assertEquals("Actual event has a parameter called " + expectedKey + " but its value does not match", expectedValue, actualParms.get(expectedKey));
}
return receivedEvents;
}
@Test
public void testMessaging() throws UnknownHostException {
// More of an integrations test
// relies on you reading some of the logging....
final SyslogClient s = new SyslogClient(null, 0, SyslogClient.LOG_DAEMON, addr(m_localhost));
s.syslog(SyslogClient.LOG_ERR, "Hello.");
}
@Test
public void testMyPatternsSyslogNG() throws UnknownHostException {
final SyslogClient s = new SyslogClient(null, 10, SyslogClient.LOG_DAEMON, addr(m_localhost));
s.syslog(SyslogClient.LOG_DEBUG, "2007-01-01 host.domain.com A SyslogNG style message");
}
@Test
public void testRegexSeverityMatch() throws Exception {
startSyslogdGracefully();
MockLogAppender.setupLogging(true, "TRACE");
String localhost = m_localhost;
final String testPDU = "2007-01-01 127.0.0.1 beer - Not just for dinner anymore";
final String testUEI = "uei.opennms.org/tests/syslogd/nonMessageMatch/severityOnly";
final String testMsg = "beer - Not just for dinner anymore";
final EventBuilder expectedEventBldr = new EventBuilder(testUEI, "syslogd");
expectedEventBldr.setInterface(addr(localhost));
expectedEventBldr.setLogDest("logndisplay");
expectedEventBldr.setLogMessage(testMsg);
final EventAnticipator ea = new EventAnticipator();
m_eventIpcManager.addEventListener(ea);
ea.anticipateEvent(expectedEventBldr.getEvent());
final SyslogClient s = new SyslogClient(null, 10, SyslogClient.LOG_DAEMON, addr(m_localhost));
s.syslog(SyslogClient.LOG_CRIT, testPDU);
ea.verifyAnticipated(10000, 0, 0, 0, 0);
}
@Test
public void testRegexFacilitySeverityProcessMatch() throws Exception {
startSyslogdGracefully();
MockLogAppender.setupLogging(true, "TRACE");
String localhost = m_localhost;
final String testPDU = "2007-01-01 127.0.0.1 maltd: beer - Not just for lunch anymore";
final String testUEI = "uei.opennms.org/tests/syslogd/nonMessageMatch/facilitySeverityProcess";
final String testMsg = "beer - Not just for lunch anymore";
final EventBuilder expectedEventBldr = new EventBuilder(testUEI, "syslogd");
expectedEventBldr.setInterface(addr(localhost));
expectedEventBldr.setLogDest("logndisplay");
expectedEventBldr.setLogMessage(testMsg);
expectedEventBldr.addParam("process", "maltd");
expectedEventBldr.addParam("service", "local1");
expectedEventBldr.addParam("severity", "Warning");
final EventAnticipator ea = new EventAnticipator();
m_eventIpcManager.addEventListener(ea);
ea.anticipateEvent(expectedEventBldr.getEvent());
final SyslogClient s = new SyslogClient("maltd", 10, SyslogClient.LOG_LOCAL1, addr(m_localhost));
s.syslog(SyslogClient.LOG_WARNING, testPDU);
ea.verifyAnticipated(5000, 0, 0, 0, 0);
}
@Test
public void testRegexFacilitySeverityMatch() throws Exception {
startSyslogdGracefully();
MockLogAppender.setupLogging(true, "TRACE");
final String localhost = m_localhost;
final String testPDU = "2007-01-01 127.0.0.1 beer - Not just for lunch anymore";
final String testUEI = "uei.opennms.org/tests/syslogd/nonMessageMatch/facilitySeverity";
final String testMsg = "beer - Not just for lunch anymore";
final EventBuilder expectedEventBldr = new EventBuilder(testUEI, "syslogd");
expectedEventBldr.setInterface(addr(localhost));
expectedEventBldr.setLogDest("logndisplay");
expectedEventBldr.setLogMessage(testMsg);
expectedEventBldr.addParam("service", "local1");
expectedEventBldr.addParam("severity", "Warning");
final EventAnticipator ea = new EventAnticipator();
m_eventIpcManager.addEventListener(ea);
ea.anticipateEvent(expectedEventBldr.getEvent());
final SyslogClient s = new SyslogClient(null, 10, SyslogClient.LOG_LOCAL1, addr(m_localhost));
s.syslog(SyslogClient.LOG_WARNING, testPDU);
ea.verifyAnticipated(5000, 0, 0, 0, 0);
}
@Test
public void testRegexFacilityMatch() throws Exception {
startSyslogdGracefully();
MockLogAppender.setupLogging(true, "TRACE");
final String localhost = m_localhost;
final String testPDU = "2007-01-01 127.0.0.1 beer - Not just for lunch anymore";
final String testUEI = "uei.opennms.org/tests/syslogd/nonMessageMatch/facilityOnly";
final String testMsg = "beer - Not just for lunch anymore";
final EventBuilder expectedEventBldr = new EventBuilder(testUEI, "syslogd");
expectedEventBldr.setInterface(addr(localhost));
expectedEventBldr.setLogDest("logndisplay");
expectedEventBldr.setLogMessage(testMsg);
expectedEventBldr.addParam("service", "local0");
final EventAnticipator ea = new EventAnticipator();
m_eventIpcManager.addEventListener(ea);
ea.anticipateEvent(expectedEventBldr.getEvent());
final SyslogClient s = new SyslogClient(null, 10, SyslogClient.LOG_LOCAL0, addr(localhost));
s.syslog(SyslogClient.LOG_DEBUG, testPDU);
ea.verifyAnticipated(5000, 0, 0, 0, 0);
}
@Test
public void testRegexProcessMatch() throws Exception {
startSyslogdGracefully();
MockLogAppender.setupLogging(true, "TRACE");
final String localhost = m_localhost;
final String testPDU = "2007-01-01 127.0.0.1 beerd: beer - Not just for breakfast anymore";
final String testUEI = "uei.opennms.org/tests/syslogd/nonMessageMatch/processOnly";
final String testMsg = "beer - Not just for breakfast anymore";
final EventBuilder expectedEventBldr = new EventBuilder(testUEI, "syslogd");
expectedEventBldr.setInterface(addr(localhost));
expectedEventBldr.setLogDest("logndisplay");
expectedEventBldr.setLogMessage(testMsg);
expectedEventBldr.addParam("process", "beerd");
final EventAnticipator ea = new EventAnticipator();
m_eventIpcManager.addEventListener(ea);
ea.anticipateEvent(expectedEventBldr.getEvent());
final SyslogClient s = new SyslogClient("beerd", 10, SyslogClient.LOG_DAEMON, addr(m_localhost));
s.syslog(SyslogClient.LOG_DEBUG, testPDU);
ea.verifyAnticipated(5000, 0, 0, 0, 0);
}
@Test
public void testIPPatternsSyslogNG() throws UnknownHostException {
final SyslogClient s = new SyslogClient(null, 10, SyslogClient.LOG_DAEMON, addr(m_localhost));
s.syslog(SyslogClient.LOG_DEBUG, "2007-01-01 127.0.0.1 A SyslogNG style message");
}
@Test
public void testResolvePatternsSyslogNG() throws UnknownHostException {
final SyslogClient s = new SyslogClient(null, 10, SyslogClient.LOG_DAEMON, addr(m_localhost));
s.syslog(SyslogClient.LOG_DEBUG, "2007-01-01 www.opennms.org A SyslogNG style message");
}
private void startSyslogdGracefully() {
try {
m_syslogd.start();
} catch (UndeclaredThrowableException ute) {
if (ute.getCause() instanceof BindException) {
// continue, this was expected
} else {
throw ute;
}
}
}
@Test
public void testSubstrUEIRewrite() throws Exception {
doMessageTest("2007-01-01 localhost A CRISCO message",
m_localhost, "uei.opennms.org/tests/syslogd/substrUeiRewriteTest",
"A CRISCO message");
}
@Test
public void testRegexUEIRewrite() throws Exception {
MockLogAppender.setupLogging(true, "TRACE");
doMessageTest("2007-01-01 localhost foo: 100 out of 666 tests failed for bar",
m_localhost, "uei.opennms.org/tests/syslogd/regexUeiRewriteTest",
"100 out of 666 tests failed for bar");
}
@Test
public void testSubstrTESTTestThatRemovesATESTString() throws Exception {
doMessageTest("2007-01-01 localhost A CRISCO message that is also a TESTHIDING message -- hide me!",
m_localhost, "uei.opennms.org/tests/syslogd/substrUeiRewriteTest",
ConvertToEvent.HIDDEN_MESSAGE);
}
@Test
public void testRegexTESTTestThatRemovesADoubleSecretString() throws Exception {
doMessageTest("2007-01-01 localhost foo: 100 out of 666 tests failed for doubleSecret",
m_localhost, "uei.opennms.org/tests/syslogd/regexUeiRewriteTest",
ConvertToEvent.HIDDEN_MESSAGE);
}
@Test
public void testSubstrDiscard() throws Exception {
startSyslogdGracefully();
final String testPDU = "2007-01-01 127.0.0.1 A JUNK message";
final EventAnticipator ea = new EventAnticipator();
m_eventIpcManager.addEventListener(ea);
final SyslogClient sc = new SyslogClient(null, 10, SyslogClient.LOG_DAEMON, addr(m_localhost));
sc.syslog(SyslogClient.LOG_DEBUG, testPDU);
ea.verifyAnticipated(5000, 0, 0, 0, 0);
}
@Test
public void testRegexDiscard() throws Exception {
startSyslogdGracefully();
final String testPDU = "2007-01-01 127.0.0.1 A TrAsH message";
final EventAnticipator ea = new EventAnticipator();
m_eventIpcManager.addEventListener(ea);
final SyslogClient sc = new SyslogClient(null, 10, SyslogClient.LOG_DAEMON, addr(m_localhost));
sc.syslog(SyslogClient.LOG_DEBUG, testPDU);
ea.verifyAnticipated(5000, 0, 0, 0, 0);
}
@Test
public void testRegexUEIWithBothKindsOfParameterAssignments() throws Exception {
final String testPDU = "2007-01-01 127.0.0.1 coffee: Secretly replaced rangerrick's coffee with 42 wombats";
final String expectedUEI = "uei.opennms.org/tests/syslogd/regexParameterAssignmentTest/bothKinds";
final String expectedLogMsg = "Secretly replaced rangerrick's coffee with 42 wombats";
final String[] testGroups = { "rangerrick's", "42", "wombats" };
final Map<String,String> expectedParms = new HashMap<String,String>();
expectedParms.put("group1", testGroups[0]);
expectedParms.put("whoseBeverage", testGroups[0]);
expectedParms.put("group2", testGroups[1]);
expectedParms.put("count", testGroups[1]);
expectedParms.put("group3", testGroups[2]);
expectedParms.put("replacementItem", testGroups[2]);
doMessageTest(testPDU, m_localhost, expectedUEI, expectedLogMsg, expectedParms);
}
@Test
public void testRegexUEIWithOnlyUserSpecifiedParameterAssignments() throws InterruptedException, UnknownHostException {
startSyslogdGracefully();
final String localhost = m_localhost;
final String testPDU = "2007-01-01 127.0.0.1 tea: Secretly replaced cmiskell's tea with 666 ferrets";
final String testUEI = "uei.opennms.org/tests/syslogd/regexParameterAssignmentTest/userSpecifiedOnly";
final String testMsg = "Secretly replaced cmiskell's tea with 666 ferrets";
final String[] testGroups = { "cmiskell's", "666", "ferrets" };
final EventBuilder expectedEventBldr = new EventBuilder(testUEI, "syslogd");
expectedEventBldr.setInterface(addr(localhost));
expectedEventBldr.setLogDest("logndisplay");
expectedEventBldr.setLogMessage(testMsg);
expectedEventBldr.addParam("whoseBeverage", testGroups[0]);
expectedEventBldr.addParam("count", testGroups[1]);
expectedEventBldr.addParam("replacementItem", testGroups[2]);
final EventAnticipator ea = new EventAnticipator();
m_eventIpcManager.addEventListener(ea);
ea.anticipateEvent(expectedEventBldr.getEvent());
final SyslogClient s = new SyslogClient(null, 10, SyslogClient.LOG_DAEMON, addr(m_localhost));
s.syslog(SyslogClient.LOG_DEBUG, testPDU);
ea.verifyAnticipated(5000, 0, 0, 0, 0);
}
}