日志是任何软件或操作系统的关键组件。日志通常记录用户的操作、系统事件、网络活动等等,具体取决于它们的用途。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
时区配置,这点很重要:timedatectl set-timezone Asia/Ho_Chi_Minh
[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
# or latest version online at http://www.rsyslog.com/doc/rsyslog_conf.html
# If you experience problems, see http://www.rsyslog.com/doc/troubleshoot.html
#### GLOBAL DIRECTIVES ####
# Where to place auxiliary files
global(workDirectory="/var/lib/rsyslog")
# Use default timestamp format
module(load="builtin:omfile" Template="RSYSLOG_TraditionalFileFormat")
# Include all config files in /etc/rsyslog.d/
include(file="/etc/rsyslog.d/*.conf" mode="optional")
#### MODULES ####
module(load="imuxsock" # provides support for local system logging (e.g. via logger command)
SysSock.Use="off") # Turn off message reception via local log socket;
# local messages are retrieved through imjournal now.
module(load="imjournal" # provides access to the systemd journal
StateFile="imjournal.state") # File to store the position in the journal
#module(load="imklog") # reads kernel messages (the same are read from journald)
#module(load="immark") # provides --MARK-- message capability
# Provides UDP syslog reception
# for parameters see http://www.rsyslog.com/doc/imudp.html
#module(load="imudp") # needs to be done just once
#input(type="imudp" port="514")
# Provides TCP syslog reception
# for parameters see http://www.rsyslog.com/doc/imtcp.html
#module(load="imtcp") # needs to be done just once
#input(type="imtcp" port="514")
#### 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
*.error;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
$FileOwner elk
$FileGroup elk
$FileCreateMode 0755
$DirCreateMode 0755
$Umask 0022
#####开启udp接收日志
$ModLoad imudp
$UDPServerRun 514
$template RemoteHost,"/data/rsyslog/%$YEAR%-%$MONTH%-%$DAY%/%FROMHOST-IP%.log"
*.* ?RemoteHost
& ~
####开启tcp协议接受日志
$ModLoad imtcp
$InputTCPServerRun 514
$WorkDirectory /var/lib/rsyslog
$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat
-----------------------------------
# ### sample forwarding rule ###
#action(type="omfwd"
# # 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.
#queue.filename="fwdRule1" # unique name prefix for spool files
#queue.maxdiskspace="1g" # 1gb space limit (use as much as possible)
#queue.saveonshutdown="on" # save messages to disk on shutdown#
二 、建立 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 enable nginx
[root@rsyslog ~]# systemctl start nginx
3.4 安装完成,可以在浏览器测试访问:
[root@rsyslog ~]# systemctl status nginx
[root@rsyslog ~]# systemctl restart 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 配置文件恢复默认后,正常访问