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

Commit 2055a87

Browse files
joschikroepke
authored andcommitted
Add syslog-related functions (#19)
1 parent 1ba3c63 commit 2055a87

File tree

11 files changed

+524
-0
lines changed

11 files changed

+524
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* This file is part of Graylog Pipeline Processor.
3+
*
4+
* Graylog Pipeline Processor is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Graylog Pipeline Processor is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with Graylog Pipeline Processor. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
package org.graylog.plugins.pipelineprocessor.functions.syslog;
18+
19+
import com.google.common.primitives.Ints;
20+
import org.graylog.plugins.pipelineprocessor.EvaluationContext;
21+
import org.graylog.plugins.pipelineprocessor.ast.functions.AbstractFunction;
22+
import org.graylog.plugins.pipelineprocessor.ast.functions.FunctionArgs;
23+
import org.graylog.plugins.pipelineprocessor.ast.functions.FunctionDescriptor;
24+
import org.graylog.plugins.pipelineprocessor.ast.functions.ParameterDescriptor;
25+
26+
import static com.google.common.base.MoreObjects.firstNonNull;
27+
import static org.graylog.plugins.pipelineprocessor.ast.functions.ParameterDescriptor.object;
28+
29+
public class SyslogFacilityConversion extends AbstractFunction<String> {
30+
public static final String NAME = "syslog_facility";
31+
32+
private final ParameterDescriptor<Object, Object> valueParam = object("value").build();
33+
34+
@Override
35+
public String evaluate(FunctionArgs args, EvaluationContext context) {
36+
final String s = String.valueOf(valueParam.required(args, context));
37+
final Integer facility = firstNonNull(Ints.tryParse(s), -1);
38+
39+
return SyslogUtils.facilityToString(facility);
40+
}
41+
42+
@Override
43+
public FunctionDescriptor<String> descriptor() {
44+
return FunctionDescriptor.<String>builder()
45+
.name(NAME)
46+
.returnType(String.class)
47+
.params(valueParam)
48+
.build();
49+
}
50+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* This file is part of Graylog Pipeline Processor.
3+
*
4+
* Graylog Pipeline Processor is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Graylog Pipeline Processor is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with Graylog Pipeline Processor. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
package org.graylog.plugins.pipelineprocessor.functions.syslog;
18+
19+
import com.google.common.primitives.Ints;
20+
import org.graylog.plugins.pipelineprocessor.EvaluationContext;
21+
import org.graylog.plugins.pipelineprocessor.ast.functions.AbstractFunction;
22+
import org.graylog.plugins.pipelineprocessor.ast.functions.FunctionArgs;
23+
import org.graylog.plugins.pipelineprocessor.ast.functions.FunctionDescriptor;
24+
import org.graylog.plugins.pipelineprocessor.ast.functions.ParameterDescriptor;
25+
26+
import static com.google.common.base.MoreObjects.firstNonNull;
27+
import static org.graylog.plugins.pipelineprocessor.ast.functions.ParameterDescriptor.object;
28+
29+
public class SyslogLevelConversion extends AbstractFunction<String> {
30+
public static final String NAME = "syslog_level";
31+
32+
private final ParameterDescriptor<Object, Object> valueParam = object("value").build();
33+
34+
@Override
35+
public String evaluate(FunctionArgs args, EvaluationContext context) {
36+
final String s = String.valueOf(valueParam.required(args, context));
37+
final Integer level = firstNonNull(Ints.tryParse(s), -1);
38+
39+
return SyslogUtils.levelToString(level);
40+
}
41+
42+
@Override
43+
public FunctionDescriptor<String> descriptor() {
44+
return FunctionDescriptor.<String>builder()
45+
.name(NAME)
46+
.returnType(String.class)
47+
.params(valueParam)
48+
.build();
49+
}
50+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* This file is part of Graylog Pipeline Processor.
3+
*
4+
* Graylog Pipeline Processor is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Graylog Pipeline Processor is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with Graylog Pipeline Processor. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
package org.graylog.plugins.pipelineprocessor.functions.syslog;
18+
19+
import com.google.auto.value.AutoValue;
20+
21+
@AutoValue
22+
public abstract class SyslogPriority {
23+
public abstract int getLevel();
24+
25+
public abstract int getFacility();
26+
27+
public static SyslogPriority create(int level, int facility) {
28+
return new AutoValue_SyslogPriority(level, facility);
29+
}
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* This file is part of Graylog Pipeline Processor.
3+
*
4+
* Graylog Pipeline Processor is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Graylog Pipeline Processor is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with Graylog Pipeline Processor. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
package org.graylog.plugins.pipelineprocessor.functions.syslog;
18+
19+
import com.google.auto.value.AutoValue;
20+
21+
@AutoValue
22+
public abstract class SyslogPriorityAsString {
23+
public abstract String getLevel();
24+
25+
public abstract String getFacility();
26+
27+
public static SyslogPriorityAsString create(String level, String facility) {
28+
return new AutoValue_SyslogPriorityAsString(level, facility);
29+
}
30+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* This file is part of Graylog Pipeline Processor.
3+
*
4+
* Graylog Pipeline Processor is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Graylog Pipeline Processor is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with Graylog Pipeline Processor. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
package org.graylog.plugins.pipelineprocessor.functions.syslog;
18+
19+
import org.graylog.plugins.pipelineprocessor.EvaluationContext;
20+
import org.graylog.plugins.pipelineprocessor.ast.functions.AbstractFunction;
21+
import org.graylog.plugins.pipelineprocessor.ast.functions.FunctionArgs;
22+
import org.graylog.plugins.pipelineprocessor.ast.functions.FunctionDescriptor;
23+
import org.graylog.plugins.pipelineprocessor.ast.functions.ParameterDescriptor;
24+
25+
import static org.graylog.plugins.pipelineprocessor.ast.functions.ParameterDescriptor.object;
26+
27+
public class SyslogPriorityConversion extends AbstractFunction<SyslogPriority> {
28+
public static final String NAME = "expand_syslog_priority";
29+
30+
private final ParameterDescriptor<Object, Object> valueParam = object("value").build();
31+
32+
@Override
33+
public SyslogPriority evaluate(FunctionArgs args, EvaluationContext context) {
34+
final String s = String.valueOf(valueParam.required(args, context));
35+
final int priority = Integer.parseInt(s);
36+
final int facility = SyslogUtils.facilityFromPriority(priority);
37+
final int level = SyslogUtils.levelFromPriority(priority);
38+
39+
return SyslogPriority.create(level, facility);
40+
}
41+
42+
@Override
43+
public FunctionDescriptor<SyslogPriority> descriptor() {
44+
return FunctionDescriptor.<SyslogPriority>builder()
45+
.name(NAME)
46+
.returnType(SyslogPriority.class)
47+
.params(valueParam)
48+
.build();
49+
}
50+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* This file is part of Graylog Pipeline Processor.
3+
*
4+
* Graylog Pipeline Processor is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Graylog Pipeline Processor is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with Graylog Pipeline Processor. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
package org.graylog.plugins.pipelineprocessor.functions.syslog;
18+
19+
import org.graylog.plugins.pipelineprocessor.EvaluationContext;
20+
import org.graylog.plugins.pipelineprocessor.ast.functions.AbstractFunction;
21+
import org.graylog.plugins.pipelineprocessor.ast.functions.FunctionArgs;
22+
import org.graylog.plugins.pipelineprocessor.ast.functions.FunctionDescriptor;
23+
import org.graylog.plugins.pipelineprocessor.ast.functions.ParameterDescriptor;
24+
25+
import static org.graylog.plugins.pipelineprocessor.ast.functions.ParameterDescriptor.object;
26+
27+
public class SyslogPriorityToStringConversion extends AbstractFunction<SyslogPriorityAsString> {
28+
public static final String NAME = "expand_syslog_priority_as_string";
29+
30+
private final ParameterDescriptor<Object, Object> valueParam = object("value").build();
31+
32+
@Override
33+
public SyslogPriorityAsString evaluate(FunctionArgs args, EvaluationContext context) {
34+
final String s = String.valueOf(valueParam.required(args, context));
35+
final int priority = Integer.parseInt(s);
36+
final int facility = SyslogUtils.facilityFromPriority(priority);
37+
final String facilityString = SyslogUtils.facilityToString(facility);
38+
final int level = SyslogUtils.levelFromPriority(priority);
39+
final String levelString = SyslogUtils.levelToString(level);
40+
41+
return SyslogPriorityAsString.create(levelString, facilityString);
42+
}
43+
44+
@Override
45+
public FunctionDescriptor<SyslogPriorityAsString> descriptor() {
46+
return FunctionDescriptor.<SyslogPriorityAsString>builder()
47+
.name(NAME)
48+
.returnType(SyslogPriorityAsString.class)
49+
.params(valueParam)
50+
.build();
51+
}
52+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/**
2+
* This file is part of Graylog Pipeline Processor.
3+
*
4+
* Graylog Pipeline Processor is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Graylog Pipeline Processor is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with Graylog Pipeline Processor. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
package org.graylog.plugins.pipelineprocessor.functions.syslog;
18+
19+
public final class SyslogUtils {
20+
/**
21+
* Converts integer syslog loglevel to human readable string
22+
*
23+
* @param level The level to convert
24+
* @return The human readable level
25+
* @see <a href="https://tools.ietf.org/html/rfc5424#section-6.2.1">RFC 5424, Section 6.2.1</a>
26+
*/
27+
public static String levelToString(int level) {
28+
switch (level) {
29+
case 0:
30+
return "Emergency";
31+
case 1:
32+
return "Alert";
33+
case 2:
34+
return "Critical";
35+
case 3:
36+
return "Error";
37+
case 4:
38+
return "Warning";
39+
case 5:
40+
return "Notice";
41+
case 6:
42+
return "Informational";
43+
case 7:
44+
return "Debug";
45+
}
46+
47+
return "Unknown";
48+
}
49+
50+
/**
51+
* Converts integer syslog facility to human readable string
52+
*
53+
* @param facility The facility to convert
54+
* @return The human readable facility
55+
* @see <a href="https://tools.ietf.org/html/rfc5424#section-6.2.1">RFC 5424, Section 6.2.1</a>
56+
*/
57+
public static String facilityToString(int facility) {
58+
switch (facility) {
59+
case 0:
60+
return "kern";
61+
case 1:
62+
return "user";
63+
case 2:
64+
return "mail";
65+
case 3:
66+
return "daemon";
67+
case 4:
68+
return "auth";
69+
case 5:
70+
return "syslog";
71+
case 6:
72+
return "lpr";
73+
case 7:
74+
return "news";
75+
case 8:
76+
return "uucp";
77+
case 9:
78+
return "clock";
79+
case 10:
80+
return "authpriv";
81+
case 11:
82+
return "ftp";
83+
case 12:
84+
return "ntp";
85+
case 13:
86+
return "log audit";
87+
case 14:
88+
return "log alert";
89+
case 15:
90+
return "cron";
91+
case 16:
92+
return "local0";
93+
case 17:
94+
return "local1";
95+
case 18:
96+
return "local2";
97+
case 19:
98+
return "local3";
99+
case 20:
100+
return "local4";
101+
case 21:
102+
return "local5";
103+
case 22:
104+
return "local6";
105+
case 23:
106+
return "local7";
107+
default:
108+
return "Unknown";
109+
}
110+
}
111+
112+
public static int levelFromPriority(int priority) {
113+
return priority - (facilityFromPriority(priority) << 3);
114+
}
115+
116+
public static int facilityFromPriority(int priority) {
117+
return priority >> 3;
118+
}
119+
}

0 commit comments

Comments
 (0)