由于网络及安全设备等存储空间很小,导致日志存储时间不足,因此需要搭建rsyslog日志服务器。此日志服务器为简约版,仅仅以文件方式存储日志。
一、搭建目标:
日志文件存放在/var/log文件夹下,每天按日期创建日志文件夹。`
二、测试环境:
服务端:Centos7 和中标麒麟V7.4版本下测试成功。
客户端:华三45xx交换机、华为2700交换机、锐捷20-X路由器、锐捷78xx交换机、网御漏扫均能正常上报debug日志。
编辑服务端的/etc/rsyslog.conf文件
注意下中文注释地方
# 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 #** 引用udp协议的模块**
$UDPServerRun 514 # **设置udp协议使用端口**
# Provides TCP syslog reception
$ModLoad imtcp # **引用tcp协议的模块**
$InputTCPServerRun 514 #** 设置tcp协议使用端口**
#### GLOBAL DIRECTIVES ####
# Where to place auxiliary files
$WorkDirectory /var/lib/rsyslog
# Use default timestamp format
$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat
$template Remote,"/var/log/%$YEAR%-%$MONTH%-%$DAY%/%fromhost-ip%.log" # ****设置远程日志存放路径和文件格式****
:fromhost-ip, !isequal, "127.0.0.1" ?Remote #** 如果是本机日志则不记录**
# 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
#### RULES ####
# Log all kernel messages to the console.
# Logging much else clutters up the screen.
# **关于内核的所有日志都放到/dev/console(控制台)**
#kern.* /dev/console
# Log anything (except mail) of level info or higher.
# Don't log private authentication messages!
# 记录所有日志类型的info级别以及大于info级别的信息到/var/log/messages,但是mail邮件信息,authpriv验证方面的信息和cron时间任务相关的信息除外
*.info;mail.none;authpriv.none;cron.none /var/log/messages
# The authpriv file has restricted access.
# authpriv验证相关的所有信息存放在/var/log/secure
authpriv.* /var/log/secure
# Log all the mail messages in one place.
# 邮件的所有信息存放在/var/log/maillog; 这里有一个-符号, 表示是使用异步的方式记录, 因为日志一般会比较大
mail.* -/var/log/maillog
# Log cron stuff
# 计划任务有关的信息存放在/var/log/cron
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
# ### 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
#*.* @@remote-host:514
# ### end of the forwarding rule ###```
三、重启rsyslog
systemctl restart rsyslog
systemctl status rsyslog # 必须查看rsyslog是否正常启动(很多按网上的问价格配置完后,查看状态会提示没有权限创建文件夹或文件信息,那此文档就不能使用)
查看监听端口
netstat -antup |grep 514
[root@localhost ~]# netstat -uanp|grep rsyslog
tcp 0 0 0.0.0.0:514 0.0.0.0: LISTEN 20228/rsyslogd
tcp 0 0 :::514 ::: LISTEN 20228/rsyslogd
udp 0 0 0.0.0.0:514 0.0.0.0: 20228/rsyslogd
udp 0 0 :::514 ::: 20228/rsyslogd*
四、关闭防火墙
systemctl stop firewalld #根据各自系统命令关闭防火墙
systemctl disable firewalld
systemctl status firewalld
五、查看日志
cd /var/log
ll
六、客户端配置(可供参考)
华为设备syslog配置:
info-center loghost source Vlanif1
info-center loghost 1.1.1.1 #日志服务地址
华三设备syslog配置:
info-center loghost source Vlan-interface30
info-center loghost 1.1.1.1 #日志服务地址
锐捷设备syslog配置:
logging enable
logging trap debugging #锐捷debugging级别就是7,为所有日志
logging source interface GigabitEthernet 0/2
logging server 1.1.1.1 #日志服务地址
以上部分内容参考此链接:
https://note.guotianyu.cn/linux/centos7-rsyslog.html