Skip to content
This repository was archived by the owner on Mar 21, 2023. It is now read-only.

Commit 0b66cb6

Browse files
authored
Support for NetFlow Version 9 (#11)
Closes #1 Closes #6
1 parent 91e76b9 commit 0b66cb6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+2656
-952
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ This plugin provides a NetFlow UDP input to act as a Flow collector that receive
99

1010
## Supported NetFlow Versions
1111

12-
The plugin only supports NetFlow V5 at the moment.
12+
The version of the plugin now supports NetFlow V9. It can support IPv6 addresses without
13+
conversion and handles all of the fields from the fixed V5 format. In addition this plugin supports
14+
events from a CISCO ASA 5500, including firewall and routing events. Beware, there is significant
15+
duplication of typical syslog reporting in the v9 reporting.
1316

1417
## Installation
1518

pom.xml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,30 @@
6262
<version>${junit.version}</version>
6363
<scope>test</scope>
6464
</dependency>
65+
<dependency>
66+
<groupId>org.assertj</groupId>
67+
<artifactId>assertj-core</artifactId>
68+
<version>${assertj-core.version}</version>
69+
<scope>test</scope>
70+
</dependency>
71+
<dependency>
72+
<groupId>com.fasterxml.jackson.dataformat</groupId>
73+
<artifactId>jackson-dataformat-yaml</artifactId>
74+
<version>${jackson.version}</version>
75+
<scope>test</scope>
76+
</dependency>
77+
<dependency>
78+
<groupId>io.pkts</groupId>
79+
<artifactId>pkts-core</artifactId>
80+
<version>2.0.7</version>
81+
<scope>test</scope>
82+
</dependency>
83+
<dependency>
84+
<groupId>com.jayway.awaitility</groupId>
85+
<artifactId>awaitility</artifactId>
86+
<version>${awaitility.version}</version>
87+
<scope>test</scope>
88+
</dependency>
6589
</dependencies>
6690

6791
<build>
@@ -75,6 +99,15 @@
7599
</resource>
76100
</resources>
77101
<plugins>
102+
<plugin>
103+
<groupId>org.apache.maven.plugins</groupId>
104+
<artifactId>maven-compiler-plugin</artifactId>
105+
<configuration>
106+
<annotationProcessors>
107+
<annotationProcessor>com.google.auto.value.processor.AutoValueProcessor</annotationProcessor>
108+
</annotationProcessors>
109+
</configuration>
110+
</plugin>
78111
<plugin>
79112
<groupId>org.apache.maven.plugins</groupId>
80113
<artifactId>maven-shade-plugin</artifactId>

src/main/java/org/graylog/plugins/netflow/NetFlowPluginModule.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,33 @@
66
* you may not use this file except in compliance with the License.
77
* You may obtain a copy of the License at
88
*
9-
* http://www.apache.org/licenses/LICENSE-2.0
9+
* http://www.apache.org/licenses/LICENSE-2.0
1010
*
1111
* Unless required by applicable law or agreed to in writing, software
1212
* distributed under the License is distributed on an "AS IS" BASIS,
1313
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17+
/*
18+
* Modified by Benjamin H. Klimkowski, bhklimk@gmail.com
19+
*/
1720
package org.graylog.plugins.netflow;
1821

1922
import org.graylog.plugins.netflow.codecs.NetFlowCodec;
2023
import org.graylog.plugins.netflow.inputs.NetFlowUdpInput;
24+
import org.graylog2.plugin.PluginConfigBean;
2125
import org.graylog2.plugin.PluginModule;
2226

27+
import java.util.Collections;
28+
import java.util.Set;
29+
2330
public class NetFlowPluginModule extends PluginModule {
31+
@Override
32+
public Set<? extends PluginConfigBean> getConfigBeans() {
33+
return Collections.emptySet();
34+
}
35+
2436
@Override
2537
protected void configure() {
2638
addMessageInput(NetFlowUdpInput.class);

src/main/java/org/graylog/plugins/netflow/codecs/NetFlowCodec.java

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
22
* Copyright (C) 2012, 2013, 2014 wasted.io Ltd <really@wasted.io>
3-
* Copyright (C) 2015 Graylog, Inc. (hello@graylog.org)
3+
* Copyright (C) 2015-2017 Graylog, Inc. (hello@graylog.org)
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
77
* You may obtain a copy of the License at
88
*
9-
* http://www.apache.org/licenses/LICENSE-2.0
9+
* http://www.apache.org/licenses/LICENSE-2.0
1010
*
1111
* Unless required by applicable law or agreed to in writing, software
1212
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -16,15 +16,19 @@
1616
*/
1717
package org.graylog.plugins.netflow.codecs;
1818

19-
import com.google.common.collect.Lists;
19+
import com.fasterxml.jackson.databind.ObjectMapper;
20+
import com.google.common.annotations.VisibleForTesting;
2021
import com.google.inject.assistedinject.Assisted;
22+
import org.apache.commons.lang3.SystemUtils;
2123
import org.graylog.plugins.netflow.flows.FlowException;
2224
import org.graylog.plugins.netflow.flows.NetFlowParser;
23-
import org.graylog.plugins.netflow.flows.NetFlow;
24-
import org.graylog.plugins.netflow.flows.NetFlowPacket;
25+
import org.graylog.plugins.netflow.v9.NetFlowV9FieldTypeRegistry;
26+
import org.graylog.plugins.netflow.v9.NetFlowV9TemplateCache;
2527
import org.graylog2.plugin.Message;
2628
import org.graylog2.plugin.configuration.Configuration;
2729
import org.graylog2.plugin.configuration.ConfigurationRequest;
30+
import org.graylog2.plugin.configuration.fields.NumberField;
31+
import org.graylog2.plugin.configuration.fields.TextField;
2832
import org.graylog2.plugin.inputs.annotations.Codec;
2933
import org.graylog2.plugin.inputs.annotations.ConfigClass;
3034
import org.graylog2.plugin.inputs.annotations.FactoryClass;
@@ -38,16 +42,42 @@
3842
import javax.annotation.Nonnull;
3943
import javax.annotation.Nullable;
4044
import javax.inject.Inject;
45+
import javax.inject.Named;
46+
import java.nio.file.Path;
47+
import java.nio.file.Paths;
4148
import java.util.Collection;
42-
import java.util.List;
49+
import java.util.concurrent.ScheduledExecutorService;
4350

4451
@Codec(name = "netflow", displayName = "NetFlow")
4552
public class NetFlowCodec extends AbstractCodec implements MultiMessageCodec {
4653
private static final Logger LOG = LoggerFactory.getLogger(NetFlowCodec.class);
4754

55+
@VisibleForTesting
56+
static final String CK_CACHE_SIZE = "cache_size";
57+
@VisibleForTesting
58+
static final String CK_CACHE_PATH = "cache_path";
59+
@VisibleForTesting
60+
static final String CK_CACHE_SAVE_INTERVAL = "cache_save_interval";
61+
62+
private static final int DEFAULT_CACHE_SIZE = 1000;
63+
private static final String DEFAULT_CACHE_PATH = SystemUtils.getJavaIoTmpDir().toPath().resolve("netflow-templates.json").toString();
64+
private static final int DEFAULT_CACHE_SAVE_INTERVAL = 15 * 60;
65+
66+
private final NetFlowV9TemplateCache templateCache;
67+
private final NetFlowV9FieldTypeRegistry typeRegistry = new NetFlowV9FieldTypeRegistry();
68+
4869
@Inject
49-
protected NetFlowCodec(@Assisted Configuration configuration) {
70+
protected NetFlowCodec(@Assisted Configuration configuration,
71+
@Named("daemonScheduler") ScheduledExecutorService scheduler,
72+
ObjectMapper objectMapper) {
5073
super(configuration);
74+
75+
final int cacheSize = configuration.getInt(CK_CACHE_SIZE, DEFAULT_CACHE_SIZE);
76+
final int cacheSaveInterval = configuration.getInt(CK_CACHE_SAVE_INTERVAL, DEFAULT_CACHE_SAVE_INTERVAL);
77+
final String configCachePath = configuration.getString(CK_CACHE_PATH, DEFAULT_CACHE_PATH);
78+
final Path cachePath = Paths.get(configCachePath);
79+
80+
templateCache = new NetFlowV9TemplateCache(cacheSize, cachePath, cacheSaveInterval, scheduler, objectMapper);
5181
}
5282

5383
@Nullable
@@ -60,19 +90,7 @@ public Message decode(@Nonnull RawMessage rawMessage) {
6090
@Override
6191
public Collection<Message> decodeMessages(@Nonnull RawMessage rawMessage) {
6292
try {
63-
final NetFlowPacket packet = NetFlowParser.parse(rawMessage);
64-
65-
if (packet == null) {
66-
return null;
67-
}
68-
69-
final List<Message> messages = Lists.newArrayListWithCapacity(packet.getFlows().size());
70-
71-
for (NetFlow flow : packet.getFlows()) {
72-
messages.add(flow.toMessage());
73-
}
74-
75-
return messages;
93+
return NetFlowParser.parse(rawMessage, templateCache, typeRegistry);
7694
} catch (FlowException e) {
7795
LOG.error("Error parsing NetFlow packet", e);
7896
return null;
@@ -96,5 +114,16 @@ public void overrideDefaultValues(@Nonnull ConfigurationRequest cr) {
96114
cr.getField(NettyTransport.CK_PORT).setDefaultValue(2055);
97115
}
98116
}
117+
118+
@Override
119+
public ConfigurationRequest getRequestedConfiguration() {
120+
final ConfigurationRequest configuration = super.getRequestedConfiguration();
121+
122+
configuration.addField(new NumberField(CK_CACHE_SIZE, "Maximum cache size", DEFAULT_CACHE_SIZE, "Maximum number of elements in the NetFlow9 template cache"));
123+
configuration.addField(new TextField(CK_CACHE_PATH, "Cache file path", DEFAULT_CACHE_PATH, "Path to the file persisting the the NetFlow9 template cache"));
124+
configuration.addField(new NumberField(CK_CACHE_SAVE_INTERVAL, "Cache save interval (seconds)", DEFAULT_CACHE_SAVE_INTERVAL, "Interval in seconds for persisting the cache contents"));
125+
126+
return configuration;
127+
}
99128
}
100129
}

src/main/java/org/graylog/plugins/netflow/flows/CorruptFlowPacketException.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,11 @@
1717
package org.graylog.plugins.netflow.flows;
1818

1919
public class CorruptFlowPacketException extends FlowException {
20+
public CorruptFlowPacketException() {
21+
super();
22+
}
23+
24+
public CorruptFlowPacketException(String message) {
25+
super(message);
26+
}
2027
}

src/main/java/org/graylog/plugins/netflow/flows/NetFlowPacket.java renamed to src/main/java/org/graylog/plugins/netflow/flows/EmptyTemplateException.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package org.graylog.plugins.netflow.flows;
1817

19-
import java.util.Collection;
18+
/*
19+
* Created by Benjamin H. Klimkowski, bhklimk@gmail.com
20+
*/
21+
22+
package org.graylog.plugins.netflow.flows;
2023

21-
public interface NetFlowPacket {
22-
Collection<NetFlow> getFlows();
24+
public class EmptyTemplateException extends FlowException {
2325
}

src/main/java/org/graylog/plugins/netflow/flows/FlowException.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* you may not use this file except in compliance with the License.
77
* You may obtain a copy of the License at
88
*
9-
* http://www.apache.org/licenses/LICENSE-2.0
9+
* http://www.apache.org/licenses/LICENSE-2.0
1010
*
1111
* Unless required by applicable law or agreed to in writing, software
1212
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -16,5 +16,12 @@
1616
*/
1717
package org.graylog.plugins.netflow.flows;
1818

19-
public class FlowException extends Exception {
19+
public class FlowException extends RuntimeException {
20+
public FlowException() {
21+
super();
22+
}
23+
24+
public FlowException(String message) {
25+
super(message);
26+
}
2027
}

src/main/java/org/graylog/plugins/netflow/flows/InvalidFlowVersionException.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* you may not use this file except in compliance with the License.
77
* You may obtain a copy of the License at
88
*
9-
* http://www.apache.org/licenses/LICENSE-2.0
9+
* http://www.apache.org/licenses/LICENSE-2.0
1010
*
1111
* Unless required by applicable law or agreed to in writing, software
1212
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -18,5 +18,6 @@
1818

1919
public class InvalidFlowVersionException extends FlowException {
2020
public InvalidFlowVersionException(int version) {
21+
super("Invalid NetFlow version " + version);
2122
}
2223
}

src/main/java/org/graylog/plugins/netflow/flows/NetFlow.java

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)