Skip to content

Commit

Permalink
UDP working
Browse files Browse the repository at this point in the history
Caveats: use UDP and the send(String) method, send(String, Level, Map) doesn't work yet
  • Loading branch information
Spiros Xanthos committed Mar 28, 2014
1 parent 4d83135 commit b27df5b
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 35 deletions.
Binary file added lib/joda-time-2.3.jar
Binary file not shown.
Binary file added lib/syslog4j-0.9.46-bin.jar
Binary file not shown.
23 changes: 21 additions & 2 deletions src/com/vmware/loginisght/send/ConsoleApp.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
package com.vmware.loginisght.send;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class ConsoleApp {

public static void main (String[] args) {
Random r = new Random();
LogUploadClient client = new LogUploadClient("10.148.104.186", 514, LogUploadClient.LogInsigntProtocol.SYSLOG_TCP);
client.sendMessage("test " + String.valueOf(r.nextInt()));
// LogUploadClient client = new LogUploadClient("10.148.104.186", 514, LogInsigntProtocol.SYSLOG_TCP);
SyslogClient sc = null;
try {
sc = new SyslogClient("10.148.104.186", 514, LogInsigntProtocol.SYSLOG_UDP);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
for (int i = 0; i < 10; i++) {
try {
Map<String, String> m = new HashMap<String, String>();
m.put("hostname", "android");
m.put("whoIsYourDaddy", "notBill");
sc.send("test " + String.valueOf(r.nextInt()) + " " + i + 10);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}
22 changes: 22 additions & 0 deletions src/com/vmware/loginisght/send/LogInsigntProtocol.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.vmware.loginisght.send;

public enum LogInsigntProtocol {
SYSLOG_TCP(1), SYSLOG_UDP(2), SYSLOG_TLS(3), HTTP(4),

UNKNOWN(0);

private int value;

private LogInsigntProtocol(int value) {
this.value = value;
}

public int getValue() {
return value;
}

public boolean isSyslog() {
return (this.equals(SYSLOG_TCP) || this.equals(SYSLOG_UDP) || this
.equals(SYSLOG_TLS));
}
}
15 changes: 15 additions & 0 deletions src/com/vmware/loginisght/send/LogLevel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.vmware.loginisght.send;

public enum LogLevel {
DEBUG("debug"), CRITICAL("critical"), ERROR("error"), ALERT("alert"), INFO("info"), WARN("warn");

private String level;

private LogLevel(String level) {
this.level = level;
}

public String getValue() {
return level;
}
}
58 changes: 25 additions & 33 deletions src/com/vmware/loginisght/send/LogUploadClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,45 @@
import java.util.ArrayList;
import java.util.List;

import org.productivity.java.syslog4j.Syslog;
import org.productivity.java.syslog4j.SyslogIF;
//import org.productivity.java.syslog4j.impl.message.structured.StructuredSyslogMessage;

public class LogUploadClient {
public final String url;
public final int port;
public final LogInsigntProtocol proto;
private SyslogIF syslog = null;

public LogUploadClient(String url, int port, LogInsigntProtocol proto) {
this.url = url;
this.port = port;
this.proto = proto;

assert (!proto.equals(LogInsigntProtocol.UNKNOWN));

if (proto.equals(LogInsigntProtocol.SYSLOG_TCP)) {
syslog = Syslog.getInstance("tcp");
} else if (proto.equals(LogInsigntProtocol.SYSLOG_UDP)) {
syslog = Syslog.getInstance("udp");
} else {
System.err.println(proto + " not supported. Exiting...");
System.exit(1);
}
syslog.getConfig().setHost(url);
syslog.getConfig().setPort(port);
}

private LogUploadClientReturnStatus sendMessageBySyslog(String msg, List<Field> fields, List<String> filters) {
return null;
//StructuredSyslogMessage ssm = new StructuredSyslogMessage("", null, "");

try {
syslog.info(msg);
} catch (Exception e) {
System.err.println(e.getMessage());
return LogUploadClientReturnStatus.FAIL;
}
return LogUploadClientReturnStatus.SUCCESS;
}

private LogUploadClientReturnStatus sendMessageByHttp(String msg, List<Field> fields, List<String> filters) {
Expand All @@ -35,38 +59,6 @@ public LogUploadClientReturnStatus sendMessage(String msg, List<Field> fields, L
}
}

public enum LogInsigntProtocol {
SYSLOG_TCP(1), SYSLOG_UDP(2), SYSLOG_TLS(3), HTTP(4),

UNKNOWN(0);

private int value;

private LogInsigntProtocol(int value) {
this.value = value;
}

public int getValue() {
return value;
}

public boolean isSyslog() {
return (this.equals(SYSLOG_TCP) || this.equals(SYSLOG_UDP) || this
.equals(SYSLOG_TLS));
}
}

public enum LogUploadClientReturnStatus {
FAIL(-1),
SUCCESS(1);

int value;
private LogUploadClientReturnStatus(int value) {
this.value = value;
}

}

public static class Field {
public String key;
public String value;
Expand Down
12 changes: 12 additions & 0 deletions src/com/vmware/loginisght/send/LogUploadClientReturnStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.vmware.loginisght.send;

public enum LogUploadClientReturnStatus {
FAIL(-1),
SUCCESS(1);

int value;
private LogUploadClientReturnStatus(int value) {
this.value = value;
}

}
73 changes: 73 additions & 0 deletions src/com/vmware/loginisght/send/SyslogClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.vmware.loginisght.send;

import java.util.HashMap;
import java.util.Map;

import org.productivity.java.syslog4j.Syslog;
import org.productivity.java.syslog4j.SyslogIF;
import org.productivity.java.syslog4j.impl.message.structured.StructuredSyslogMessage;
import org.productivity.java.syslog4j.impl.net.tcp.ssl.SSLTCPNetSyslogConfig;

public class SyslogClient {

private SyslogIF syslog = null;

public SyslogClient(String url, int port, LogInsigntProtocol proto) throws Exception {

checkParam(url, "url");
checkParam(proto, "protocol");

if (proto.equals(LogInsigntProtocol.SYSLOG_TCP)) {
syslog = Syslog.getInstance("tcp");
} else if (proto.equals(LogInsigntProtocol.SYSLOG_UDP)) {
syslog = Syslog.getInstance("udp");
} else if (proto.equals(LogInsigntProtocol.SYSLOG_TLS)) {
SSLTCPNetSyslogConfig syslogConfig = new SSLTCPNetSyslogConfig(url, port);
syslog = Syslog.createInstance("sslTcp", syslogConfig);
} else {
throw new Exception("Protocol " + proto.toString() + " is not supported. Use one of " +
LogInsigntProtocol.SYSLOG_TCP + ", " + LogInsigntProtocol.SYSLOG_UDP +
" or " + LogInsigntProtocol.SYSLOG_TLS);
}

syslog.getConfig().setHost(url);
syslog.getConfig().setPort(port);
}

private void checkParam(Object param, String name) throws Exception {
if (param == null) {
throw new Exception("The " + name + " parameter is invalid, provide value = " + "null");
}
}

public void send(String msg) throws Exception {
send(msg, LogLevel.DEBUG, new HashMap<String, String>());
}

public void send(String msg, LogLevel l, Map<String, String> fields) throws Exception {
checkParam(msg, "msg");

Map<String, String> myFields;
if (fields == null) {
myFields = new HashMap<String, String>();
} else {
myFields = fields;
}

// StructuredSyslogMessage message = new StructuredSyslogMessage("", myFields, msg);
String message = msg;
if (l.equals(LogLevel.ALERT)) {
syslog.alert(message);
} else if (l.equals(LogLevel.CRITICAL)) {
syslog.critical(message);
} else if (l.equals(LogLevel.DEBUG)) {
syslog.debug(message);
} else if (l.equals(LogLevel.ERROR)) {
syslog.error(message);
} else if (l.equals(LogLevel.INFO)) {
syslog.info(message);
} else if (l.equals(LogLevel.WARN)) {
syslog.warn(message);
}
}
}

0 comments on commit b27df5b

Please sign in to comment.