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.
This commit is contained in:
Mark Nellemann 2021-03-17 14:30:37 +01:00
parent bedcaad3b7
commit 5e1481e770
10 changed files with 45 additions and 70 deletions

View file

@ -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 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. 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 ## Development Notes

View file

@ -4,7 +4,7 @@ Description=Simple Syslog Service
[Service] [Service]
TimeoutStartSec=0 TimeoutStartSec=0
Restart=always Restart=always
ExecStart=/opt/syslogd/bin/syslogd --no-stdout --forward=localhost:1514 ExecStart=/opt/syslogd/bin/syslogd --no-stdout --syslog=udp://localhost:1514
[Install] [Install]
WantedBy=default.target WantedBy=default.target

View file

@ -1,3 +1,3 @@
id = syslogd id = syslogd
group = biz.nellemann.syslogd group = biz.nellemann.syslogd
version = 1.0.14 version = 1.2.1

View file

@ -98,17 +98,16 @@ public class Application implements Callable<Integer>, LogListener {
udpClient = new UdpClient(getInetSocketAddress(syslog)); udpClient = new UdpClient(getInetSocketAddress(syslog));
doForward = true; doForward = true;
} else { } else {
throw new UnsupportedOperationException("Syslog protocol not implemented: " + syslog.getScheme()); throw new UnsupportedOperationException("Forward protocol not implemented: " + syslog.getScheme());
} }
} }
if(gelf != null) { if(gelf != null) {
System.err.println(gelf.getScheme());
if(gelf.getScheme().toLowerCase(Locale.ROOT).equals("udp")) { if(gelf.getScheme().toLowerCase(Locale.ROOT).equals("udp")) {
gelfClient = new UdpClient(getInetSocketAddress(gelf)); gelfClient = new UdpClient(getInetSocketAddress(gelf));
doForward = true; doForward = true;
} else { } 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<Integer>, 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) { public static void main(String... args) {
int exitCode = new CommandLine(new Application()).execute(args); int exitCode = new CommandLine(new Application()).execute(args);
System.exit(exitCode); System.exit(exitCode);

View file

@ -110,13 +110,6 @@ public class SyslogPrinter {
* @param msg * @param msg
* @return * @return
*/ */
/*
{ "streams": [ { "stream": { "label": "value" }, "values": [ [ "<unix epoch in nanoseconds>", "<log line>" ], [ "<unix epoch in nanoseconds>", "<log line>" ] ] } ] }
{ "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) { public static String toLoki(SyslogMessage msg) {
StringBuilder sb = new StringBuilder("{ \"streams\": [ { \"stream\": {"); StringBuilder sb = new StringBuilder("{ \"streams\": [ { \"stream\": {");
sb.append(String.format(" \"host\": \"%s\",", msg.hostname)); sb.append(String.format(" \"host\": \"%s\",", msg.hostname));

View file

@ -31,30 +31,30 @@ import java.util.Map;
*/ */
public enum Facility { public enum Facility {
KERNEL(0), kernel(0),
USER(1), user(1),
MAIL(2), mail(2),
DAEMON(3), daemon(3),
AUTH(4), auth(4),
SYSLOG(5), syslog(5),
PRINT(6), print(6),
NEWS(7), news(7),
UUCP(8), uucp(8),
CRON(9), cron(9),
AUTHPRIV(10), authpriv(10),
FTP(11), ftp(11),
NTP(12), ntp(12),
AUDIT(13), audit(13),
ALERT(14), alert(14),
TIME(15), time(15),
LOCAL0(16), local0(16),
LOCAL1(17), local1(17),
LOCAL2(18), local2(18),
LOCAL3(19), local3(19),
LOCAL4(20), local4(20),
LOCAL5(21), local5(21),
LOCAL6(22), local6(22),
LOCAL7(23); local7(23);
// Cache lookups // Cache lookups
private static final Map<Integer, Facility> BY_NUMBER = new HashMap<>(); private static final Map<Integer, Facility> BY_NUMBER = new HashMap<>();

View file

@ -15,14 +15,14 @@ import java.util.Map;
*/ */
public enum Severity { public enum Severity {
EMERG(0), emerg(0),
ALERT(1), alert(1),
CRIT(2), crit(2),
ERROR(3), error(3),
WARN(4), warn(4),
NOTICE(5), notice(5),
INFO(6), info(6),
DEBUG(7); debug(7);
// Cache lookups // Cache lookups
private static final Map<Integer, Severity> BY_NUMBER = new HashMap<>(); private static final Map<Integer, Severity> BY_NUMBER = new HashMap<>();

View file

@ -28,6 +28,8 @@ public class LokiClient {
con = (HttpURLConnection)pushUrl.openConnection(); con = (HttpURLConnection)pushUrl.openConnection();
con.setRequestMethod("POST"); con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json"); con.setRequestProperty("Content-Type", "application/json");
con.setConnectTimeout(500);
con.setReadTimeout(100);
con.setDoOutput(true); con.setDoOutput(true);
byte[] input = msg.getBytes(StandardCharsets.UTF_8); byte[] input = msg.getBytes(StandardCharsets.UTF_8);
@ -43,7 +45,7 @@ public class LokiClient {
} }
} catch (IOException e) { } catch (IOException e) {
log.warn("send() - " + e.getMessage()); log.error("send() - " + e.getMessage());
} finally { } finally {
if(con != null) { if(con != null) {
con.disconnect(); con.disconnect();

View file

@ -19,7 +19,7 @@ class SyslogParserTest extends Specification {
int code = syslogParser.getFacility("132") int code = syslogParser.getFacility("132")
then: then:
code == Facility.LOCAL0.toNumber() code == Facility.local0.toNumber()
} }
void "test severity WARN"() { void "test severity WARN"() {
@ -27,7 +27,7 @@ class SyslogParserTest extends Specification {
int code = syslogParser.getSeverity("132") int code = syslogParser.getSeverity("132")
then: then:
code == Severity.WARN.toNumber() code == Severity.warn.toNumber()
} }
} }

View file

@ -34,7 +34,7 @@ class SyslogPrinterTest extends Specification {
String output = SyslogPrinter.toLoki(msg) String output = SyslogPrinter.toLoki(msg)
then: 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" ] ] } ] }'
} }
} }