2020-09-22 18:45:16 +00:00
|
|
|
/*
|
|
|
|
Copyright 2020 mark.nellemann@gmail.com
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
2020-09-22 18:33:22 +00:00
|
|
|
package biz.nellemann.syslogd;
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import picocli.CommandLine;
|
|
|
|
import picocli.CommandLine.Command;
|
2020-10-05 08:40:42 +00:00
|
|
|
|
|
|
|
import java.io.*;
|
2020-09-22 18:33:22 +00:00
|
|
|
import java.util.concurrent.Callable;
|
|
|
|
|
2020-10-05 08:40:42 +00:00
|
|
|
@Command(name = "syslogd",
|
|
|
|
mixinStandardHelpOptions = true,
|
|
|
|
description = "Basic syslog server.",
|
|
|
|
versionProvider = biz.nellemann.syslogd.VersionProvider.class)
|
2020-09-22 18:33:22 +00:00
|
|
|
public class SyslogServer implements Callable<Integer>, LogListener {
|
|
|
|
|
|
|
|
private final static Logger log = LoggerFactory.getLogger(SyslogServer.class);
|
2020-10-05 08:40:42 +00:00
|
|
|
private BufferedWriter bw;
|
2020-09-22 18:33:22 +00:00
|
|
|
|
2020-10-05 08:40:42 +00:00
|
|
|
@CommandLine.Option(names = {"-p", "--port"}, description = "Listening port [default: 514]")
|
2020-09-22 18:45:16 +00:00
|
|
|
private int port = 514;
|
2020-09-22 18:33:22 +00:00
|
|
|
|
2020-10-05 08:40:42 +00:00
|
|
|
@CommandLine.Option(names = "--no-udp", negatable = true, description = "Listen on UDP [default: true]")
|
2020-09-22 18:33:22 +00:00
|
|
|
boolean udpServer = true;
|
|
|
|
|
2020-10-05 08:40:42 +00:00
|
|
|
@CommandLine.Option(names = "--no-tcp", negatable = true, description = "Listen on TCP [default: true]")
|
2020-09-22 18:33:22 +00:00
|
|
|
boolean tcpServer = true;
|
|
|
|
|
2020-10-05 15:59:31 +00:00
|
|
|
@CommandLine.Option(names = "--rfc5424", negatable = false, description = "Parse RFC5424 messages [default: RFC3164]")
|
|
|
|
boolean rfc5424 = false;
|
2020-09-22 18:33:22 +00:00
|
|
|
|
2020-10-05 15:59:31 +00:00
|
|
|
@CommandLine.Option(names = "--no-ansi", negatable = true, description = "Output ANSI colors [default: true]")
|
2020-10-05 08:40:42 +00:00
|
|
|
boolean ansiOutput = true;
|
|
|
|
|
|
|
|
@CommandLine.Option(names = {"-f", "--file"}, description = "Write output to file [default: STDOUT]")
|
|
|
|
File outputFile;
|
|
|
|
|
2020-09-22 18:33:22 +00:00
|
|
|
public static void main(String... args) {
|
|
|
|
int exitCode = new CommandLine(new SyslogServer()).execute(args);
|
|
|
|
System.exit(exitCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Integer call() throws Exception {
|
|
|
|
|
2020-10-05 08:40:42 +00:00
|
|
|
FileOutputStream fos = null;
|
|
|
|
OutputStreamWriter w;
|
|
|
|
|
|
|
|
if(outputFile != null) {
|
|
|
|
fos = new FileOutputStream(outputFile);
|
|
|
|
w = new OutputStreamWriter(fos, "UTF-8");
|
|
|
|
bw = new BufferedWriter(w);
|
|
|
|
}
|
|
|
|
|
2020-09-22 18:33:22 +00:00
|
|
|
if(udpServer) {
|
|
|
|
UdpServer udpServer = new UdpServer(port);
|
|
|
|
udpServer.addEventListener(this);
|
|
|
|
udpServer.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(tcpServer) {
|
|
|
|
TcpServer tcpServer = new TcpServer(port);
|
|
|
|
tcpServer.addEventListener(this);
|
|
|
|
tcpServer.start();
|
|
|
|
}
|
|
|
|
|
2020-10-05 08:40:42 +00:00
|
|
|
if(outputFile != null) {
|
|
|
|
bw.close();
|
|
|
|
fos.close();
|
|
|
|
}
|
|
|
|
|
2020-09-22 18:33:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLogEvent(LogEvent event) {
|
|
|
|
|
2020-10-05 08:40:42 +00:00
|
|
|
// Parse message
|
2020-09-22 18:33:22 +00:00
|
|
|
String message = event.getMessage();
|
|
|
|
SyslogMessage msg = null;
|
|
|
|
try {
|
2020-10-05 15:59:31 +00:00
|
|
|
if(rfc5424) {
|
2020-09-23 05:01:55 +00:00
|
|
|
msg = SyslogParser.parseRfc5424(message);
|
2020-10-05 15:59:31 +00:00
|
|
|
} else {
|
|
|
|
msg = SyslogParser.parseRfc3164(message);
|
2020-09-22 18:33:22 +00:00
|
|
|
}
|
|
|
|
} catch(Exception e) {
|
|
|
|
log.error("Problem parsing message: ", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(msg != null) {
|
2020-10-05 08:40:42 +00:00
|
|
|
if(bw != null) {
|
|
|
|
writeToFile(msg);
|
|
|
|
} else {
|
|
|
|
writeToStdout(msg);
|
|
|
|
}
|
2020-09-22 18:33:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-10-05 08:40:42 +00:00
|
|
|
|
|
|
|
void writeToFile(SyslogMessage msg) {
|
|
|
|
try {
|
|
|
|
if(ansiOutput) {
|
|
|
|
bw.append(msg.toAnsiString());
|
|
|
|
} else {
|
|
|
|
bw.append(msg.toString());
|
|
|
|
}
|
|
|
|
bw.newLine();
|
|
|
|
bw.flush();
|
|
|
|
} catch (IOException e) {
|
|
|
|
log.error("file error", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void writeToStdout(SyslogMessage msg) {
|
|
|
|
if(ansiOutput) {
|
|
|
|
System.out.println(msg.toAnsiString());
|
|
|
|
} else {
|
|
|
|
System.out.println(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-22 18:33:22 +00:00
|
|
|
}
|