-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new pipeline function "grok_exists" (#5699)
Prior to this change, a missing grok pattern would raise a error in the pipeline processor when using the "grok" function. But the user would like to able to make one rule which uses a grok pattern dynamically depending on if a grok pattern exists or not. This change adds a new function "grok_exists" which will return true or false depending if a grok pattern exists. Additionally it will make a entry to the graylog-server.log if the second argument of the function is true and the pattern was not found. Fixes #5689
- Loading branch information
Showing
7 changed files
with
117 additions
and
2 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
...log2-server/src/main/java/org/graylog/plugins/pipelineprocessor/functions/GrokExists.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** | ||
* This file is part of Graylog. | ||
* | ||
* Graylog is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Graylog 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 General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Graylog. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.graylog.plugins.pipelineprocessor.functions; | ||
|
||
import org.graylog.plugins.pipelineprocessor.EvaluationContext; | ||
import org.graylog.plugins.pipelineprocessor.ast.functions.AbstractFunction; | ||
import org.graylog.plugins.pipelineprocessor.ast.functions.FunctionArgs; | ||
import org.graylog.plugins.pipelineprocessor.ast.functions.FunctionDescriptor; | ||
import org.graylog.plugins.pipelineprocessor.ast.functions.ParameterDescriptor; | ||
import org.graylog2.grok.GrokPatternRegistry; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import static com.google.common.collect.ImmutableList.of; | ||
|
||
import javax.inject.Inject; | ||
|
||
public class GrokExists extends AbstractFunction<Boolean> { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(GrokExists.class); | ||
public static final String NAME = "grok_exists"; | ||
|
||
private final ParameterDescriptor<String, String> patternParam; | ||
private final ParameterDescriptor<Boolean, Boolean> doLog; | ||
|
||
private final GrokPatternRegistry grokPatternRegistry; | ||
|
||
@Inject | ||
public GrokExists(GrokPatternRegistry grokPatternRegistry) { | ||
this.grokPatternRegistry = grokPatternRegistry; | ||
|
||
patternParam = ParameterDescriptor.string("pattern") | ||
.description("The Grok Pattern which is to be tested for existance.").build(); | ||
doLog = ParameterDescriptor.bool("log_missing").optional() | ||
.description("Log if the Grok Pattern is missing. Warning: Switching on this flag can lead" + | ||
" to a high volume of logs.").build(); | ||
} | ||
|
||
@Override | ||
public Boolean evaluate(FunctionArgs args, EvaluationContext context) { | ||
final String pattern = patternParam.required(args, context); | ||
final boolean logWhenNotFound = doLog.optional(args, context).orElse(false); | ||
|
||
if (pattern == null) { | ||
return null; | ||
} | ||
|
||
final boolean patternExists = grokPatternRegistry.grokPatternExists(pattern); | ||
if (!patternExists && logWhenNotFound) { | ||
log.info("Grok Pattern " + pattern + " does not exists."); | ||
} | ||
|
||
return patternExists; | ||
} | ||
|
||
@Override | ||
public FunctionDescriptor<Boolean> descriptor() { | ||
return FunctionDescriptor.<Boolean>builder() | ||
.name(NAME) | ||
.returnType(Boolean.class) | ||
.params(of(patternParam, doLog)) | ||
.description("Checks if the given Grok pattern exists.") | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,6 +92,4 @@ public boolean isMatches() { | |
return captures.size() > 0; | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
...server/src/test/resources/org/graylog/plugins/pipelineprocessor/functions/grok_exists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
rule "grok_exists" | ||
when | ||
grok_exists("GREEDY") | ||
then | ||
trigger_test(); | ||
end |
6 changes: 6 additions & 0 deletions
6
...er/src/test/resources/org/graylog/plugins/pipelineprocessor/functions/grok_exists_not.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
rule "grok_exists_not" | ||
when | ||
grok_exists("DOESNOTEXISTS") | ||
then | ||
trigger_test(); | ||
end |