Linux Rsyslog 通过 Nginx 实现 HTML 网页访问 Log

日志是任何软件或操作系统的关键组件。日志通常记录用户的操作、系统事件、网络活动等等,具体取决于它们的用途。Linux 系统上使用最广泛的日志系统之一是rsyslog。

Rsyslog是一个强大、安全和高性能的日志处理系统,它接受来自不同类型源(系统/应用程序)的数据并将其输出为多种格式。

它已经从常规的syslog守护进程发展成为功能齐全的企业级日志系统。它采用客户端/服务器模型设计,因此可以配置为客户端和/或其他服务器、网络设备和远程应用程序的中央日志服务器。

测试环境
出于本指南的目的,我们将使用以下主机测试:

服务器:192.168.108.128
客户端:192.168.108.149/150  + 离线log 
CISCO:   交换机配置:

logging trap notifications
logging facility local6
logging source-interface Vlan 192
logging host 192.168.108.128

一、安装和配置 Rsyslog 服务器;

修改计算机名称  关闭防火墙:SELINUX=enforcing 修改为 disabled  
[root@Centostest01 ~]# vi /etc/hostname 
[root@Centostest01 ~]# systemctl stop firewalld
[root@Centostest01 ~]# systemctl disable firewalld
[root@localhost ~]# vi /etc/selinux/config
[root@localhost ~]# reboot

[root@rsyslog ~]# yum -y install rsyslog
[root@rsyslog ~]# systemctl enable rsyslog  
[root@rsyslog ~]# systemctl start rsyslog
[root@rsyslog ~]# systemctl status rsyslog
[root@rsyslog ~]# vi /etc/rsyslog.conf    
[root@rsyslog ~]# systemctl restart rsyslog 

# rsyslog configuration file

# For more information see /usr/share/doc/rsyslog-*/rsyslog_conf.html
# If you experience problems, see http://www.rsyslog.com/doc/troubleshoot.html

#### MODULES ####

# The imjournal module bellow is now used as a message source instead of imuxsock.
$ModLoad imuxsock # provides support for local system logging (e.g. via logger command)
$ModLoad imjournal # provides access to the systemd journal
#$ModLoad imklog # reads kernel messages (the same are read from journald)
#$ModLoad immark  # provides --MARK-- message capability

# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514

# Provides TCP syslog reception
$ModLoad imtcp
$InputTCPServerRun 514


#### GLOBAL DIRECTIVES ####

# Where to place auxiliary files
$WorkDirectory /var/lib/rsyslog

# Use default timestamp format
$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat

# File syncing capability is disabled by default. This feature is usually not required,
# not useful and an extreme performance hit
#$ActionFileEnableSync on

# Include all config files in /etc/rsyslog.d/
$IncludeConfig /etc/rsyslog.d/*.conf

# Turn off message reception via local log socket;
# local messages are retrieved through imjournal now.
$OmitLocalLogging on

# File to store the position in the journal
$IMJournalStateFile imjournal.state

$template IpTemplate,"/data/rsyslog/today/%FROMHOST-IP%.log"
*.* ?IpTemplate
& ~

$Umask 0022
$FileOwner es
$FileGroup es
$FileCreateMode 0644
$DirCreateMode 0644

#### RULES ####

# Log all kernel messages to the console.
# Logging much else clutters up the screen.
#kern.*                                                 /dev/console

# Log anything (except mail) of level info or higher.
# Don't log private authentication messages!
*.info;mail.none;authpriv.none;cron.none                /var/log/messages

# The authpriv file has restricted access.
authpriv.*                                              /var/log/secure

# Log all the mail messages in one place.
mail.*                                                  -/var/log/maillog


# Log cron stuff
cron.*                                                  /var/log/cron

# Everybody gets emergency messages
*.emerg                                                 :omusrmsg:*

# Save news errors of level crit and higher in a special file.
uucp,news.crit                                          /var/log/spooler

# Save boot messages also to boot.log
local7.*                                                /var/log/boot.log
# save switch messages also to %FROMHOST-HP%.log
local5.*						/data/rsyslog/today/%FROMHOST-IP%.log

# ### begin forwarding rule ###
# The statement between the begin ... end define a SINGLE forwarding
# rule. They belong together, do NOT split them. If you create multiple
# forwarding rules, duplicate the whole block!
# Remote Logging (we use TCP for reliable delivery)
#
# An on-disk queue is created for this action. If the remote host is
# down, messages are spooled to disk and sent when it is up again.
#$ActionQueueFileName fwdRule1 # unique name prefix for spool files
#$ActionQueueMaxDiskSpace 1g   # 1gb space limit (use as much as possible)
#$ActionQueueSaveOnShutdown on # save messages to disk on shutdown
#$ActionQueueType LinkedList   # run asynchronously
#$ActionResumeRetryCount -1    # infinite retries if host is down
# remote host is: name/ip:port, e.g. 192.168.0.1:514, port optional
*.* @@192.168.108.128:514
# ### end of the forwarding rule ###

二 、建立 Rsyslog 数据收集存放的目录:
[root@rsyslog ~]# cd /
[root@rsyslog /]# mkdir data
[root@rsyslog /]# cd data
[root@rsyslog data]# mkdir rsyslog
[root@rsyslog data]# chmod 777 rsyslog

三、安装 Nginx 

nginx最新安装方式:


[root@rsyslog ~]# vi /etc/yum.repos.d/nginx.repo

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[root@rsyslog ~]# yum install nginx

3.1 添加CentOS 7 Nginx yum资源库:

[root@rsyslog ~]# rpm -Uvh  http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

3.2 安装nginx:

[root@rsyslog ~]# yum -y install nginx

3.3 启动nginx

[root@rsyslog ~]# systemctl start nginx

3.4 安装完成,可以在浏览器测试访问:

[root@rsyslog ~]# systemctl status nginx

3.5  编辑  vi  /etc/nginx/nginx.conf  

[root@rsyslog ~]# vi /etc/nginx/nginx.conf 
[root@rsyslog ~]# systemctl restart nginx 

server { 
       listen 80; 
       server_name rsyslog.nginx; 
       root /data/rsyslog; 
 
       location /rsyslog { 
         alias /data/rsyslog; 
         autoindex on; 
       } 
} 

 

 3.6 可以看到 Nginx 网页访问 正常的,但没有生产本机的 rsyslog  重启一下 rsyslog 服务:

3.7  现在从其他 rsyslog  拷贝一些 log 到 /data/rsyslog 目录下看效果:  

 四、将 HTML 测试网站拷贝到 /usr/share/nginx/html 目录下   这个是 nginx 网页目录:

4.1 无法正常访问了,需要配置  nginx.conf  文件 

4.2 nginx.conf 配置文件恢复默认后,正常访问