Skip to content

Commit 74247ce

Browse files
committed
add timzone config to CiscoSyslogServerEvent
1 parent 48e4c61 commit 74247ce

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

src/main/java/org/graylog2/syslog4j/server/impl/event/CiscoSyslogServerEvent.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package org.graylog2.syslog4j.server.impl.event;
22

3+
import org.joda.time.DateTimeZone;
4+
35
import java.net.InetAddress;
6+
import java.time.ZoneId;
47
import java.time.ZoneOffset;
58
import java.time.ZonedDateTime;
69
import java.time.format.DateTimeFormatter;
710
import java.time.format.DateTimeParseException;
811
import java.util.Calendar;
912
import java.util.Date;
1013
import java.util.Locale;
14+
import java.util.Objects;
1115

1216
/**
1317
* CiscoSyslogServerEvent provides an implementation of the
@@ -19,8 +23,7 @@
1923
public class CiscoSyslogServerEvent extends SyslogServerEvent {
2024
private static final DateTimeFormatter DEFAULT_FORMATTER =
2125
DateTimeFormatter
22-
.ofPattern("yyyy MMM ppd HH:mm:ss[.SSS][ zzz]", Locale.ROOT)
23-
.withZone(ZoneOffset.UTC);
26+
.ofPattern("yyyy MMM ppd HH:mm:ss[.SSS][ zzz]", Locale.ROOT);
2427
private int sequenceNumber = 0;
2528

2629
public CiscoSyslogServerEvent(final byte[] message, int length, InetAddress inetAddress) {
@@ -29,6 +32,12 @@ public CiscoSyslogServerEvent(final byte[] message, int length, InetAddress inet
2932
initialize(message, length, inetAddress);
3033
parse();
3134
}
35+
public CiscoSyslogServerEvent(final byte[] message, int length, InetAddress inetAddress, DateTimeZone sysLogServerTimeZone) {
36+
super();
37+
38+
initialize(message, length, inetAddress, sysLogServerTimeZone);
39+
parse();
40+
}
3241

3342
public CiscoSyslogServerEvent(final String message, InetAddress inetAddress) {
3443
super();
@@ -37,6 +46,13 @@ public CiscoSyslogServerEvent(final String message, InetAddress inetAddress) {
3746
parse();
3847
}
3948

49+
public CiscoSyslogServerEvent(final String message, InetAddress inetAddress, DateTimeZone sysLogServerTimeZone) {
50+
super();
51+
52+
initialize(message, inetAddress, sysLogServerTimeZone);
53+
parse();
54+
}
55+
4056
@Override
4157
protected void parsePriority() {
4258
if (this.message.charAt(0) == '<') {
@@ -117,7 +133,7 @@ protected void parseDate() {
117133
if (this.message.length() > dateLength) {
118134
boolean isYearMissing = Character.isLetter(message.charAt(0));
119135
String originalDate = this.message.substring(0, dateLength);
120-
DateTimeFormatter formatter = DEFAULT_FORMATTER;
136+
DateTimeFormatter formatter = DEFAULT_FORMATTER.withZone(getDefaultServerZoneId());
121137

122138
// Hacky override for: "Mar 06 2016 12:53:10 DEVICENAME :"
123139
if (Character.isDigit(message.charAt(7))
@@ -129,7 +145,7 @@ protected void parseDate() {
129145
originalDate = this.message.substring(0, dateLength);
130146
formatter = DateTimeFormatter
131147
.ofPattern("MMM ppd yyyy HH:mm:ss", Locale.ROOT)
132-
.withZone(ZoneOffset.UTC);
148+
.withZone(getDefaultServerZoneId());
133149
}
134150

135151
try {
@@ -154,4 +170,8 @@ protected void parseDate() {
154170
public int getSequenceNumber() {
155171
return sequenceNumber;
156172
}
173+
174+
private ZoneId getDefaultServerZoneId() {
175+
return Objects.isNull(sysLogServerTimeZone) ? ZoneOffset.UTC : sysLogServerTimeZone.toTimeZone().toZoneId();
176+
}
157177
}

src/test/java/org/graylog2/syslog4j/server/impl/event/CiscoSyslogServerEventTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
package org.graylog2.syslog4j.server.impl.event;
22

3+
import org.joda.time.DateTimeZone;
34
import org.junit.Test;
45

56
import java.net.InetAddress;
67
import java.net.InetSocketAddress;
78
import java.time.ZoneId;
9+
import java.time.ZoneOffset;
810
import java.time.ZonedDateTime;
11+
import java.time.format.DateTimeFormatter;
912
import java.util.Date;
13+
import java.util.Locale;
1014

1115
import static java.time.ZoneOffset.UTC;
1216
import static org.assertj.core.api.Assertions.assertThat;
1317
import static org.junit.Assert.assertEquals;
1418

1519

1620
public class CiscoSyslogServerEventTest {
21+
public static final DateTimeZone MST = DateTimeZone.forID("MST");
22+
public static final ZoneId MST_ZONE_ID = MST.toTimeZone().toZoneId();
1723
private static final InetAddress INET_ADDR = new InetSocketAddress(514).getAddress();
1824
private static final ZoneId CET = ZoneId.of("CET");
1925
private static final int YEAR = ZonedDateTime.now().getYear();
@@ -122,6 +128,31 @@ public void testCisco7() throws Exception {
122128
assertThat(event.getMessage()).isEqualTo("%ASA-6-302015: Built inbound UDP connection 23631055 for inside:192.168.19.91/44764 (192.168.19.91/44764) to identity:192.168.249.33/161 (192.168.249.33/161)");
123129
}
124130

131+
@Test
132+
public void testDefaultTimeZoneUtcIfNotConfigured() throws Exception {
133+
final String message = "<190>: 2016 Mar 06 09:22:34: %AUTHPRIV-6-SYSTEM_MSG: START: rsync pid=4311 from=::ffff:IP - xinetd[6219]";
134+
final CiscoSyslogServerEvent event = buildEvent(message);
135+
136+
assertThat(toZonedDateTime(event.getDate(), UTC)).isEqualTo(ZonedDateTime.of(2016, 3, 6, 9, 22, 34, 0, UTC));
137+
}
138+
139+
@Test
140+
public void testDefaultTimeZoneConfigured() throws Exception {
141+
final String message = "<190>: 2016 Mar 06 09:22:34: %AUTHPRIV-6-SYSTEM_MSG: START: rsync pid=4311 from=::ffff:IP - xinetd[6219]";
142+
final CiscoSyslogServerEvent event = new CiscoSyslogServerEvent(message, INET_ADDR, MST);
143+
144+
assertThat(toZonedDateTime(event.getDate(), MST_ZONE_ID)).isEqualTo(ZonedDateTime.of(2016, 3, 6, 9, 22, 34, 0, MST_ZONE_ID));
145+
}
146+
147+
@Test
148+
public void testDefaultTimeZoneIgnoredSinceZoneDetected() throws Exception {
149+
final String message = "<190>: 2016 Mar 06 09:22:34 CET: %AUTHPRIV-6-SYSTEM_MSG: START: rsync pid=4311 from=::ffff:IP - xinetd[6219]";
150+
DateTimeZone mst = DateTimeZone.forID("MST");
151+
final CiscoSyslogServerEvent event = new CiscoSyslogServerEvent(message, INET_ADDR, mst);
152+
153+
assertThat(toZonedDateTime(event.getDate(), CET)).isEqualTo(ZonedDateTime.of(2016, 3, 6, 9, 22, 34, 0, CET));
154+
}
155+
125156
private ZonedDateTime toZonedDateTime(Date date, ZoneId zoneId) {
126157
return ZonedDateTime.ofInstant(date.toInstant(), zoneId);
127158
}

0 commit comments

Comments
 (0)