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
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

View File

@ -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

View File

@ -1,3 +1,3 @@
id = 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));
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<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) {
int exitCode = new CommandLine(new Application()).execute(args);
System.exit(exitCode);

View File

@ -110,13 +110,6 @@ public class SyslogPrinter {
* @param msg
* @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) {
StringBuilder sb = new StringBuilder("{ \"streams\": [ { \"stream\": {");
sb.append(String.format(" \"host\": \"%s\",", msg.hostname));

View File

@ -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<Integer, Facility> BY_NUMBER = new HashMap<>();

View File

@ -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<Integer, Severity> BY_NUMBER = new HashMap<>();

View File

@ -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();

View File

@ -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()
}
}

View File

@ -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" ] ] } ] }'
}
}