SysLog 学习的一点思路

【Syslog是什么?】

      Syslog protocol是一种工业标准的协议,Syslog是一个简单协议,常用来管理计算机系统和安全审计。虽然存在大量不足,Syslog获得了大量设备和接收者跨越多个平台和操作系统的支持。因此,Syslog可用来将日志数据从多种不同类型的系统整合到一个存贮中心。 由于每个进程、应用程序和操作系统都或多或少地被独立完成,在syslog信息内容会有一些不一致的地方。因此,协议中并没有任何关于信息的格式或内容的假设。这个协议就是简单地被设计用来传送事件信息。在所有情况下,都有一个产生这个信息的设备,其中的syslog 程序可以发送信息到接收者,但是事件已经被接收到不会被通知。

      Syslog 采用用户数据报协议(UDP)作为其底层传输层机制。被分配syslog的端口是514端口。数据通常是以简明文本的形式发送的,但诸如Stunnel, sslio或sslwrap的SSL wrapper会被用来通过SSL/TLS提供层加密。

      Syslog 协议和进程最基本原则就是其简便性,在协议的发送者和接收者之间不要求有严格的相互协调。事实上,syslog 信息的传递可以在接收器没有被配置甚至没有接收器的情况下开始。反过来,在没有被清晰配置或者定义的情况下,接收器也很可能接收到信息。这个简单原则很好地促进了syslog 的认可和开展。

      <以上部分摘自网络,我认为总结比较好的部分。>我们可以简单理解Syslog就是Unix下的一个组件,可以实现把系统中某些日志信息写到一个文件,或者用UDP包发送到某台服务器,也就是一台服务器A可以将域内不同服务器上的日志信息,通过配置汇总到A,A就成为域中SysLog日志服务器

【Syslog服务器实现简单思路】

1、开启局域网中Unix系统syslog服务

2、配置Syslog信息发送到一台主机A

3、配置A接收不同主机信息

4、具体项目中有时需要对A日志信息进行具体分析

      这里抽取一个最简单模型为例来说明:

      主机A:192.168.1.62

      主机B:192.168.1.85

      用A作为Syslog服务器,A上用一个简单Java类采集B主机发送过来的信息,打印出来。

配置192.168.1.62的Syslog配置文件:

1)修改/etc/sysconfig/syslog文件:,设置SYSLOGD_OPTIONS的值为"-rxm 60",其中60syslog记录时间戳标记消息(–MARK–)的频率(单位为秒)-r标示允许远程访问。设置结果如下:

 [root@KMC-DB log]# vi /etc/sysconfig/syslog # Options to syslogd # -m 0 disables ‘MARK’ messages. # -r enables logging from remote machines # -x disables DNS lookups on messages recieved with -r # See syslogd(8) for more details SYSLOGD_OPTIONS="-rxm 60" #SYSLOGD_OPTIONS="-m 0"  

配置192.168.1.85的Syslog配置文件:

2)/etc/syslog.conf文件,添加*.* @192.168.1.62 #发送到远端主机

 *.* @192.168.1.62 #发送到远端主机

表示所有信息发送到192.168.1.62服务器。

 

重启两台服务器:

 # service syslog restart

 

我们也可以在62上写一小段Java代码测试

测试代码例子如下<来源互联网>:

 

import java.net.*; import java.io.*; /** * Implements a basic syslog server receiving data on port 514 and logging it to * a file called syslog.log */ public class JavalogServer { public static void main(String args[]) throws Exception { // Create the buffer to store the data as it comes in byte[] log_buffer = new byte[2048]; int received_messages = 0; // Open the file for writing the log messages File out_file = new File("syslog.log"); // Create the output stream so we can dump the data FileOutputStream syslog_file = new FileOutputStream(out_file); // Create a DatagramPacket to receive the incoming log data DatagramPacket packet = new DatagramPacket(log_buffer, log_buffer.length); // Create a socket that listens on the net DatagramSocket socket = new DatagramSocket(514); while (received_messages < 5) { // Wait until some data arrives. Aren’t threads great? socket.receive(packet); // Increment the message count received_messages++; // Build a string of the packet data String packet_string = new String(log_buffer, 0, 0, packet .getLength()); // Put the packet data after a bit of header so we can see where it // comes // from String out_string = "<syslog from " + packet.getAddress().getHostName() + ">" + packet_string + "/n"; // Print the message to the standard out window System.out.println(out_string); // Convert the message to an array of bytes so it can be sent to the // file int msg_len = out_string.length(); byte[] out_buffer = new byte[msg_len]; out_string.getBytes(0, out_string.length(), out_buffer, 0); // Write the name of the host where the data came from to the file syslog_file.write(out_buffer, 0, out_string.length()); } socket.close(); } }   

 

打印信息:

<syslog from 192.168.1.85><6>kernel: Kernel logging (proc) stopped.


<syslog from 192.168.1.85><6>kernel: Kernel log daemon terminating.


<syslog from 192.168.1.85><46>exiting on signal 15


<syslog from 192.168.1.85><46>syslogd 1.4.1: restart (remote reception).


<syslog from 192.168.1.85><6>kernel: klogd 1.4.1, log source = /proc/kmsg started.

 

你可能感兴趣的:(exception,String,服务器,File,buffer,logging)