From 5e1481e770222e98f598868417bba2edf89ef911 Mon Sep 17 00:00:00 2001 From: Mark Nellemann Date: Wed, 17 Mar 2021 14:30:37 +0100 Subject: [PATCH] Minor version incremented as command line parameters and functionality has changed. Severity and facility are now in lowercase. LokiClient http timeouts set more aggressively. Cleanup various places. --- README.md | 3 ++ doc/syslogd.service | 2 +- gradle.properties | 2 +- .../biz/nellemann/syslogd/Application.java | 27 +---------- .../biz/nellemann/syslogd/SyslogPrinter.java | 7 --- .../biz/nellemann/syslogd/msg/Facility.java | 48 +++++++++---------- .../biz/nellemann/syslogd/msg/Severity.java | 16 +++---- .../biz/nellemann/syslogd/net/LokiClient.java | 4 +- .../nellemann/syslogd/SyslogParserTest.groovy | 4 +- .../syslogd/SyslogPrinterTest.groovy | 2 +- 10 files changed, 45 insertions(+), 70 deletions(-) diff --git a/README.md b/README.md index b50f43f..6f5349e 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,9 @@ If you don't want any output locally (only forwarding), you can use the ```--no- Syslog messages from AIX (and IBM Power Virtual I/O Servers) can be troublesome with some logging solutions. These can be received with syslogd and then forwarded on to your preferred logging solution. +### Forwarding to Grafana Loki + +Forwarding is currently done by making HTTP connections to the Loki API, which works fine for low volume messages, but might cause issues for large volume of messages. ## Development Notes diff --git a/doc/syslogd.service b/doc/syslogd.service index 6b4598d..e199406 100644 --- a/doc/syslogd.service +++ b/doc/syslogd.service @@ -4,7 +4,7 @@ Description=Simple Syslog Service [Service] TimeoutStartSec=0 Restart=always -ExecStart=/opt/syslogd/bin/syslogd --no-stdout --forward=localhost:1514 +ExecStart=/opt/syslogd/bin/syslogd --no-stdout --syslog=udp://localhost:1514 [Install] WantedBy=default.target diff --git a/gradle.properties b/gradle.properties index 3887c25..fe73369 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ id = syslogd group = biz.nellemann.syslogd -version = 1.0.14 +version = 1.2.1 diff --git a/src/main/java/biz/nellemann/syslogd/Application.java b/src/main/java/biz/nellemann/syslogd/Application.java index 8f1ff14..71f4145 100644 --- a/src/main/java/biz/nellemann/syslogd/Application.java +++ b/src/main/java/biz/nellemann/syslogd/Application.java @@ -98,17 +98,16 @@ public class Application implements Callable, LogListener { udpClient = new UdpClient(getInetSocketAddress(syslog)); doForward = true; } else { - throw new UnsupportedOperationException("Syslog protocol not implemented: " + syslog.getScheme()); + throw new UnsupportedOperationException("Forward protocol not implemented: " + syslog.getScheme()); } } if(gelf != null) { - System.err.println(gelf.getScheme()); if(gelf.getScheme().toLowerCase(Locale.ROOT).equals("udp")) { gelfClient = new UdpClient(getInetSocketAddress(gelf)); doForward = true; } else { - throw new UnsupportedOperationException("GELF protocol not implemented: " + gelf.getScheme()); + throw new UnsupportedOperationException("Forward protocol not implemented: " + gelf.getScheme()); } } @@ -185,28 +184,6 @@ public class Application implements Callable, LogListener { } - private InetSocketAddress getInetSocketAddress(String input) { - - String dstHost; - int dstPort; - InetSocketAddress inetSocketAddress = null; - - Pattern pattern = Pattern.compile("^([^:]+)(?::([0-9]+))?$", Pattern.CASE_INSENSITIVE); - Matcher matcher = pattern.matcher(input); - if(matcher.find()) { - dstHost = matcher.group(1); - if(matcher.groupCount() == 2 && matcher.group(2) != null) { - dstPort = Integer.parseInt(matcher.group(2)); - } else { - dstPort = 514; - } - inetSocketAddress = new InetSocketAddress(dstHost, dstPort); - } - - return inetSocketAddress; - } - - public static void main(String... args) { int exitCode = new CommandLine(new Application()).execute(args); System.exit(exitCode); diff --git a/src/main/java/biz/nellemann/syslogd/SyslogPrinter.java b/src/main/java/biz/nellemann/syslogd/SyslogPrinter.java index 0efb53c..0fdb83d 100644 --- a/src/main/java/biz/nellemann/syslogd/SyslogPrinter.java +++ b/src/main/java/biz/nellemann/syslogd/SyslogPrinter.java @@ -110,13 +110,6 @@ public class SyslogPrinter { * @param msg * @return */ - -/* -{ "streams": [ { "stream": { "label": "value" }, "values": [ [ "", "" ], [ "", "" ] ] } ] } -{ "streams": [ { "stream": { "host": "hyperion", "facility": "USER", "severity": "NOTICE", "application": "mark"}, "values": [ [ "1615823598000000000", "Test 2345534343434" ] ] } ] } -{ "streams": [ { "stream": { "host": "hyperion", "facility": "USER", "severity": "NOTICE", "application": "mark"}, "values": [ [ "1615842165000000000", "Test" ] ] } ] } -*/ - public static String toLoki(SyslogMessage msg) { StringBuilder sb = new StringBuilder("{ \"streams\": [ { \"stream\": {"); sb.append(String.format(" \"host\": \"%s\",", msg.hostname)); diff --git a/src/main/java/biz/nellemann/syslogd/msg/Facility.java b/src/main/java/biz/nellemann/syslogd/msg/Facility.java index 79b675b..388da34 100644 --- a/src/main/java/biz/nellemann/syslogd/msg/Facility.java +++ b/src/main/java/biz/nellemann/syslogd/msg/Facility.java @@ -31,30 +31,30 @@ import java.util.Map; */ public enum Facility { - KERNEL(0), - USER(1), - MAIL(2), - DAEMON(3), - AUTH(4), - SYSLOG(5), - PRINT(6), - NEWS(7), - UUCP(8), - CRON(9), - AUTHPRIV(10), - FTP(11), - NTP(12), - AUDIT(13), - ALERT(14), - TIME(15), - LOCAL0(16), - LOCAL1(17), - LOCAL2(18), - LOCAL3(19), - LOCAL4(20), - LOCAL5(21), - LOCAL6(22), - LOCAL7(23); + kernel(0), + user(1), + mail(2), + daemon(3), + auth(4), + syslog(5), + print(6), + news(7), + uucp(8), + cron(9), + authpriv(10), + ftp(11), + ntp(12), + audit(13), + alert(14), + time(15), + local0(16), + local1(17), + local2(18), + local3(19), + local4(20), + local5(21), + local6(22), + local7(23); // Cache lookups private static final Map BY_NUMBER = new HashMap<>(); diff --git a/src/main/java/biz/nellemann/syslogd/msg/Severity.java b/src/main/java/biz/nellemann/syslogd/msg/Severity.java index 3f8a1df..5a12114 100644 --- a/src/main/java/biz/nellemann/syslogd/msg/Severity.java +++ b/src/main/java/biz/nellemann/syslogd/msg/Severity.java @@ -15,14 +15,14 @@ import java.util.Map; */ public enum Severity { - EMERG(0), - ALERT(1), - CRIT(2), - ERROR(3), - WARN(4), - NOTICE(5), - INFO(6), - DEBUG(7); + emerg(0), + alert(1), + crit(2), + error(3), + warn(4), + notice(5), + info(6), + debug(7); // Cache lookups private static final Map BY_NUMBER = new HashMap<>(); diff --git a/src/main/java/biz/nellemann/syslogd/net/LokiClient.java b/src/main/java/biz/nellemann/syslogd/net/LokiClient.java index 7eda2b9..d500fb1 100644 --- a/src/main/java/biz/nellemann/syslogd/net/LokiClient.java +++ b/src/main/java/biz/nellemann/syslogd/net/LokiClient.java @@ -28,6 +28,8 @@ public class LokiClient { con = (HttpURLConnection)pushUrl.openConnection(); con.setRequestMethod("POST"); con.setRequestProperty("Content-Type", "application/json"); + con.setConnectTimeout(500); + con.setReadTimeout(100); con.setDoOutput(true); byte[] input = msg.getBytes(StandardCharsets.UTF_8); @@ -43,7 +45,7 @@ public class LokiClient { } } catch (IOException e) { - log.warn("send() - " + e.getMessage()); + log.error("send() - " + e.getMessage()); } finally { if(con != null) { con.disconnect(); diff --git a/src/test/groovy/biz/nellemann/syslogd/SyslogParserTest.groovy b/src/test/groovy/biz/nellemann/syslogd/SyslogParserTest.groovy index 55e72eb..65ab701 100644 --- a/src/test/groovy/biz/nellemann/syslogd/SyslogParserTest.groovy +++ b/src/test/groovy/biz/nellemann/syslogd/SyslogParserTest.groovy @@ -19,7 +19,7 @@ class SyslogParserTest extends Specification { int code = syslogParser.getFacility("132") then: - code == Facility.LOCAL0.toNumber() + code == Facility.local0.toNumber() } void "test severity WARN"() { @@ -27,7 +27,7 @@ class SyslogParserTest extends Specification { int code = syslogParser.getSeverity("132") then: - code == Severity.WARN.toNumber() + code == Severity.warn.toNumber() } } diff --git a/src/test/groovy/biz/nellemann/syslogd/SyslogPrinterTest.groovy b/src/test/groovy/biz/nellemann/syslogd/SyslogPrinterTest.groovy index adc03fc..44f9694 100644 --- a/src/test/groovy/biz/nellemann/syslogd/SyslogPrinterTest.groovy +++ b/src/test/groovy/biz/nellemann/syslogd/SyslogPrinterTest.groovy @@ -34,7 +34,7 @@ class SyslogPrinterTest extends Specification { String output = SyslogPrinter.toLoki(msg) then: - output == '{ "streams": [ { "stream": { "host": "xps13", "facility": "USER", "severity": "NOTICE", "application": "mark"}, "values": [ [ "1600845200000000000", "adfdfdf3432434565656" ] ] } ] }' + output == '{ "streams": [ { "stream": { "host": "xps13", "facility": "user", "severity": "notice", "application": "mark"}, "values": [ [ "1600845200000000000", "adfdfdf3432434565656" ] ] } ] }' } }