SysLog 学习的一点思路

本文介绍了Syslog协议的基本概念及其在系统管理和安全审计中的应用,并详细阐述了如何配置简单的Syslog服务器,包括配置文件调整及Java代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


【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",其中60为syslog记录时间戳标记消息(–MARK–)的频率(单位为秒)-r标示允许远程访问。设置结果如下:

 

  1. [root@KMC-DB log]# vi /etc/sysconfig/syslog   
  2.   
  3.    
  4.   
  5. # Options to syslogd  
  6.   
  7. # -m 0 disables ‘MARK’ messages.  
  8.   
  9. # -r enables logging from remote machines  
  10.   
  11. # -x disables DNS lookups on messages recieved with -r  
  12.   
  13. # See syslogd(8) for more details  
  14.   
  15. SYSLOGD_OPTIONS="-rxm 60"  
  16.   
  17. #SYSLOGD_OPTIONS="-m 0"  
 

配置192.168.1.85的Syslog配置文件:

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

 

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

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

 

重启两台服务器:

 

  1. # service syslog restart  

 

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

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

 

[c-sharp] view plain copy
  1. import java.net.*;  
  2. import java.io.*;  
  3.   
  4. /** 
  5.  * Implements a basic syslog server receiving data on port 514 and logging it to 
  6.  * a file called syslog.log 
  7.  */  
  8.   
  9. public class JavalogServer {  
  10.   
  11.     public static void main(String args[]) throws Exception {  
  12.         // Create the buffer to store the data as it comes in  
  13.         byte[] log_buffer = new byte[2048];  
  14.   
  15.         int received_messages = 0;  
  16.   
  17.         // Open the file for writing the log messages  
  18.         File out_file = new File("syslog.log");  
  19.   
  20.         // Create the output stream so we can dump the data  
  21.         FileOutputStream syslog_file = new FileOutputStream(out_file);  
  22.   
  23.         // Create a DatagramPacket to receive the incoming log data  
  24.         DatagramPacket packet = new DatagramPacket(log_buffer,  
  25.                 log_buffer.length);  
  26.   
  27.         // Create a socket that listens on the net  
  28.         DatagramSocket socket = new DatagramSocket(514);  
  29.   
  30.         while (received_messages < 5) {  
  31.             // Wait until some data arrives. Aren’t threads great?  
  32.             socket.receive(packet);  
  33.   
  34.             // Increment the message count  
  35.             received_messages++;  
  36.   
  37.             // Build a string of the packet data  
  38.             String packet_string = new String(log_buffer, 0, 0, packet  
  39.                     .getLength());  
  40.   
  41.             // Put the packet data after a bit of header so we can see where it  
  42.             // comes  
  43.             // from  
  44.             String out_string = "<syslog from "  
  45.                     + packet.getAddress().getHostName() + ">" + packet_string  
  46.                     + "/n";  
  47.   
  48.             // Print the message to the standard out window  
  49.             System.out.println(out_string);  
  50.   
  51.             // Convert the message to an array of bytes so it can be sent to the  
  52.             // file  
  53.             int msg_len = out_string.length();  
  54.   
  55.             byte[] out_buffer = new byte[msg_len];  
  56.   
  57.             out_string.getBytes(0, out_string.length(), out_buffer, 0);  
  58.   
  59.             // Write the name of the host where the data came from to the file  
  60.             syslog_file.write(out_buffer, 0, out_string.length());  
  61.         }  
  62.         socket.close();  
  63.     }  
  64. }  
  

 

打印信息:

<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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值