zoukankan      html  css  js  c++  java
  • syslog强大而安全的日志处理系统

    Ryslog是一个强大而安全的日志处理系统。Rsylog通过多个物理或虚拟服务器在网络上接收日志,并监视不同服务的健康状况。使用Rsyslog,您可以从集中位置监视其他服务器、网络设备和远程应用程序的日志。
    简介

    日志对于分析和排除Linux中的任何问题非常有用。默认情况下,所有日志文件都位于Linux的/var/log目录中。日志文件有几种类型,包括cron、内核、用户、安全性,这些文件中的大多数都由Rsyslog服务控制。

    Ryslog是一个强大而安全的日志处理系统。Rsylog通过多个物理或虚拟服务器在网络上接收日志,并监视不同服务的健康状况。使用Rsyslog,您可以从集中位置监视其他服务器、网络设备和远程应用程序的日志。

    准备
    安装Rsyslog

    默认情况下,Rsyslog安装在Ubuntu 18.04服务器上。如果没有安装,您可以通过运行以下命令来安装它:

    linuxprobe@ubuntu-18-04-lts:~$ apt-get install rsyslog -y
    

    在安装Rsyslog之后,您可以使用以下命令检查Rsyslog的版本:

    linuxprobe@ubuntu-18-04-lts:~$ rsyslogd -v
    rsyslogd 8.32.0, compiled with:
    	PLATFORM:				x86_64-pc-linux-gnu
    	PLATFORM (lsb_release -d):		
    	FEATURE_REGEXP:				Yes
    	GSSAPI Kerberos 5 support:		Yes
    	FEATURE_DEBUG (debug build, slow code):	No
    	32bit Atomic operations supported:	Yes
    	64bit Atomic operations supported:	Yes
    	memory allocator:			system default
    	Runtime Instrumentation (slow code):	No
    	uuid support:				Yes
    	systemd support:			Yes
    	Number of Bits in RainerScript integers: 64
    
    See http://www.rsyslog.com for more information.
    

    还可以用这个命令检查Rsyslog的状态:

    linuxprobe@ubuntu-18-04-lts:~$ systemctl status rsyslog
    ? rsyslog.service - System Logging Service
       Loaded: loaded (/lib/systemd/system/rsyslog.service; enabled; vendor preset: enabled)
       Active: active (running) since Thur 2020-01-16 11:20:32 CST; 1min 31s ago
         Docs: man:rsyslogd(8)
     Main PID: 724 (rsyslogd)
        Tasks: 4 (limit: 1114)
       CGroup: /system.slice/rsyslog.service
               ??724 /usr/sbin/rsyslogd -n
    
    Jan 16 04:28:53 ubuntu-18-04-lts systemd[1]: Starting System Logging Service...
    Jan 16 04:28:54 ubuntu-18-04-lts rsyslogd[724]: imuxsock: Acquired UNIX socket '/run/systemd/journal/syslog' (fd 3) from systemd.  [v8.32.0]
    Jan 16 04:28:54 ubuntu-18-04-lts rsyslogd[724]: rsyslogd's groupid changed to 106
    Jan 16 04:28:54 ubuntu-18-04-lts rsyslogd[724]: rsyslogd's userid changed to 102
    Jan 16 04:28:54 ubuntu-18-04-lts rsyslogd[724]:  [origin software="rsyslogd" swVersion="8.32.0" x-pid="724" x-info="http://www.rsyslog.com"] start
    Jan 16 04:28:55 ubuntu-18-04-lts systemd[1]: Started System Logging Service.
    
    配置Rsyslog服务端
    linuxprobe@ubuntu-18-04-lts:~$ vim /etc/rsyslog.conf
    

    取消这几行前面的注释,同事使用UDP和TCP协议的514端口

    $ModLoad imudp
    $UDPServerRun 514
    $ModLoad imtcp
    $InputTCPServerRun 514
    

    指定子网、IP或域名来限制访问,如下所示:

    $AllowedSender TCP, 127.0.0.1, 192.168.0.0/24, *.example.com
    $AllowedSender UDP, 127.0.0.1, 192.168.0.0/24, *.example.com
    

    创建一个模板来告诉Rsyslog如何存储传入的syslog消息。在GLOBAL DIRECTIVES部分之前添加以下几行:

    $template remote-incoming-logs,"/var/log/%HOSTNAME%/%PROGRAMNAME%.log" 
    *.* ?remote-incoming-logs
    

    用以下命令检查Rsyslog配置信息是否有语法错误:

    linuxprobe@ubuntu-18-04-lts:~$ rsyslogd -f /etc/rsyslog.conf -N1
    rsyslogd: version 8.32.0, config validation run (level 1), master config /etc/rsyslog.conf
    rsyslogd: End of config validation run. Bye.
    

    重新启动Rsyslog:

    linuxprobe@ubuntu-18-04-lts:~$ systemctl restart rsyslog
    

    验证Rsyslog正在使用以下命令监听TCP/UDP:

    linuxprobe@ubuntu-18-04-lts:~$ netstat -4altunp | grep 514
    tcp        0      0 0.0.0.0:514             0.0.0.0:*               LISTEN      1332/rsyslogd       
    udp        0      0 0.0.0.0:514             0.0.0.0:*                           1332/rsyslogd       
    
    配置Rsyslog客户端

    配置Rsyslog客户端来向远程服务端发送系统日志消息。登录客户端,打开/etc/rsyslog.conf添加如下信息:

    linuxprobe@ubuntu-18-04-lts:~$ vim /etc/rsyslog.conf
    ##Enable sending of logs over UDP add the following line:
    
    *.* @192.168.0.101:514
    
    
    ##Enable sending of logs over TCP add the following line:
    
    *.* @@192.168.0.101:514
    
    ##Set disk queue when rsyslog server will be down:
    
    $ActionQueueFileName queue
    $ActionQueueMaxDiskSpace 1g
    $ActionQueueSaveOnShutdown on
    $ActionQueueType LinkedList
    $ActionResumeRetryCount -1
    

    重新启动Rsyslog:

    linuxprobe@ubuntu-18-04-lts:~$ systemtcl restart rsyslog
    
    查看日志

    此时,Rsyslog客户端被配置为将它们的日志发送到Rsyslog服务端。
    现在,登录到Rsyslog服务器并检查/var/log目录。看到客户端机器的主机名,包括几个日志文件:

    linuxprobe@ubuntu-18-04-lts:~$ ls /var/log/rsyslog-client/
    CRON.log  kernel.log  rsyslogd-2039.log  rsyslogd.log  sudo.log  wpa_supplicant.log
    
    总结

    在上面的文章中,我们学习了如何在Ubuntu 18.04服务器上安装和配置RysLogServer。我们还学习了如何配置rsyslog客户端 向rsyslog服务端发送日志。

    本文原创地址:https://www.linuxprobe.com/ubuntu-18-rsyslog.html编辑:逄增宝,审核员:逄增宝

  • 相关阅读:
    Code Forces 650 C Table Compression(并查集)
    Code Forces 645B Mischievous Mess Makers
    POJ 3735 Training little cats(矩阵快速幂)
    POJ 3233 Matrix Power Series(矩阵快速幂)
    PAT 1026 Table Tennis (30)
    ZOJ 3609 Modular Inverse
    Java实现 LeetCode 746 使用最小花费爬楼梯(递推)
    Java实现 LeetCode 745 前缀和后缀搜索(使用Hash代替字典树)
    Java实现 LeetCode 745 前缀和后缀搜索(使用Hash代替字典树)
    Java实现 LeetCode 745 前缀和后缀搜索(使用Hash代替字典树)
  • 原文地址:https://www.cnblogs.com/linuxprobe/p/13401498.html
Copyright © 2011-2022 走看看