-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
8 changed files
with
168 additions
and
35 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/com/vmware/loginisght/send/LogUploadClientReturnStatus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |