总体来说,如果做企业级日志管理分析,我们第一步就需要接入设备的日志,而大部分的操作系统都是支持syslog的,一般我们对日志这种敏感级别不是很高的信息通过UDP的形式从客户机发送到syslog接收服务器,syslog我们一般不会主动去采集,而是当客户机上有操作时产生的日志会自动发送到syslog接收服务器。下面是一个接收syslog的例子。

package com.tony.util;

import java.io.IOException;

import java.net.DatagramPacket;

import java.net.DatagramSocket;

import java.net.InetAddress;

import java.net.InetSocketAddress;

import java.net.SocketException;

/**

* Copyright 2015 HP

* All right reserved.

* UDP服务类. 采集syslog

* @version 1.0

* Creation date: 2015-8-18 - 下午16:32:31

*/

public class UdpServerSocket {

private byte[] buffer = new byte[1024];

private DatagramSocket ds = null;

private DatagramPacket packet = null;

private InetSocketAddress socketAddress = null;

private String orgIp;

/**

* 构造函数,绑定主机和端口.

* @param host 主机

* @param port 端口

* @throws Exception

*/

public UdpServerSocket(String host, int port) throws Exception {

socketAddress = new InetSocketAddress(host, port);

ds = new DatagramSocket(socketAddress);

System.out.println("--------------service start----------------");

}

public final String getOrgIp() {

return orgIp;

}

/**

* 设置超时时间,该方法必须在bind方法之后使用.

* @param timeout 超时时间

* @throws Exception

*/

public final void setSoTimeout(int timeout) throws Exception {

ds.setSoTimeout(timeout);

}

/**

* 获得超时时间.

* @return 返回超时时间.

* @throws Exception

- 下午10:34:36

*/

public final int getSoTimeout() throws Exception {

return ds.getSoTimeout();

}

/**

* 绑定监听地址和端口.

* @param host 主机IP

* @param port 端口

* @throws SocketException

- 下午10:36:17

*/

public final void bind(String host, int port) throws SocketException {

socketAddress = new InetSocketAddress(host, port);

ds = new DatagramSocket(socketAddress);

}

/**

* 接收数据包,该方法会造成线程阻塞.

* @return 返回接收的数据串信息

* @throws IOException

- 下午10:38:24

*/

public final String receive() throws IOException {

packet = new DatagramPacket(buffer, buffer.length);

ds.receive(packet);

orgIp = packet.getAddress().getHostAddress();

String info = new String(packet.getData(), 0, packet.getLength());

System.out.println(info);

//System.out.println("CONTENT="+info+":SOURCE_IP="+packet.getAddress().getHostAddress()+"SOURCE_PORT:"+packet.getPort());

return info;

}

/**

* 将响应包发送给请求端.

* @param bytes 回应报文

* @throws IOException

- 下午11:05:31

*/

public final void response(String info) throws IOException {

System.out.println("Client IP" + packet.getAddress().getHostAddress()

+ ",Port:" + packet.getPort());

DatagramPacket dp = new DatagramPacket(buffer, buffer.length, packet

.getAddress(), packet.getPort());

dp.setData(info.getBytes());

ds.send(dp);

}

/**

* 设置报文的缓冲长度.

* @param bufsize 缓冲长度

- 下午10:47:49

*/

public final void setLength(int bufsize) {

packet.setLength(bufsize);

}

/**

* 获得发送回应的IP地址.

* @return 返回回应的IP地址

- 下午10:48:27

*/

public final InetAddress getResponseAddress() {

return packet.getAddress();

}

/**

* 获得回应的主机的端口.

* @return 返回回应的主机的端口.

- 下午10:48:56

*/

public final int getResponsePort() {

return packet.getPort();

}

/**

* 关闭udp监听口.

- 下午10:49:23

*/

public final void close() {

try {

ds.close();

} catch (Exception ex) {

ex.printStackTrace();

}

}

/**

* 测试方法.

* @param args

* @throws Exception

- 下午10:49:50

*/

public static void main(String[] args) throws Exception {

//这里的IP是你本机的IP也就是syslog服务器的IP

String serverHost = "192.168.100.188";

int serverPort = 514;

UdpServerSocket udpServerSocket = new UdpServerSocket(serverHost, serverPort);

while (true) {

udpServerSocket.receive();

}

}

}

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐