当前位置:   article > 正文

CENTOS9+RSYSLOG+LOGROTATE收集日志

CENTOS9+RSYSLOG+LOGROTATE收集日志

系统版版本为CentOS Stream release 9

rsyslog和lograte通常系统已经安装了。

  1. #rpm -qa | grep rsyslog
  2. rsyslog-logrotate-8.2310.0-4.el9.x86_64
  3. rsyslog-8.2310.0-4.el9.x86_64
  4. rsyslog-gnutls-8.2310.0-4.el9.x86_64
  5. rsyslog-gssapi-8.2310.0-4.el9.x86_64
  6. rsyslog-relp-8.2310.0-4.el9.x86_64
  7. rsyslog-mysql-8.2310.0-4.el9.x86_64
  8. #
  9. rpm -qa | grep logrotate
  10. logrotate-3.18.0-8.el9.x86_64
  11. rsyslog-logrotate-8.2310.0-4.el9.x86_64

rsyslog高配本支持低版本的配置语法,下面附上我的配置

  1. cat /etc/rsyslog.conf | egrep -v "^#|^$"
  2. global(workDirectory="/var/lib/rsyslog")
  3. $template myformat,"%$NOW% %TIMESTAMP:8:15% %hostname% %syslogtag% %msg%\n"
  4. $ActionFileDefaultTemplate myformat
  5. module(load="imuxsock" # provides support for local system logging (e.g. via logger command)
  6. SysSock.Use="off") # Turn off message reception via local log socket;
  7. # local messages are retrieved through imjournal now.
  8. module(load="imjournal" # provides access to the systemd journal
  9. UsePid="system" # PID nummber is retrieved as the ID of the process the journal entry originates from
  10. FileCreateMode="0644" # Set the access permissions for the state file
  11. StateFile="imjournal.state") # File to store the position in the journal
  12. module(load="ommysql")
  13. $ModLoad ommysql
  14. include(file="/etc/rsyslog.d/*.conf" mode="optional")
  15. module(load="imudp") # needs to be done just once
  16. input(type="imudp" port="514")
  17. module(load="imtcp") # needs to be done just once
  18. input(type="imtcp" port="514")
  19. 127.0.0.1.info;mail.none;authpriv.none;cron.none /var/log/messages
  20. authpriv.* /var/log/secure
  21. mail.* -/var/log/maillog
  22. cron.* /var/log/cron
  23. *.emerg :omusrmsg:*
  24. uucp,news.crit /var/log/spooler
  25. $template NetworkDevices,"/data/logs/network-devices/%FROMHOST-IP%.log"
  26. *.* ?NetworkDevices
  27. *.* :ommysql:127.0.0.1,Syslog,rsyslog,dbpasswd

其中

  1. $template myformat,"%$NOW% %TIMESTAMP:8:15% %hostname% %syslogtag% %msg%\n"
  2. $ActionFileDefaultTemplate myformat

rsylog传过来的时间差8小时的问题,我一直接没解决,传过来的timestamp使用的就是utc时间格式,所以时间就是差8小时,反正记录中另有时间就略过这个问题,能用就好。

下面这二行的使用是接收网络设备log

  1. $template NetworkDevices,"/data/logs/network-devices/%FROMHOST-IP%.log"
  2. *.* ?NetworkDevices

还有

*.info;mail.none;authpriv.none;cron.none                /var/log/messages

被我改成,避免local7的日志发到message文件里。

127.0.0.1.info;mail.none;authpriv.none;cron.none                /var/log/messages

 bootlog的一行给我删除了,没什么意义。

在/etc/logrotate.d下添加一个lograte配置 

  1. # cat network-devices
  2. # Note that logs are not compressed unless "compress" is configured,
  3. # which can be done either here or globally in /etc/logrotate.conf.
  4. /data/logs/network-devices/*log {
  5. daily
  6. rotate 180
  7. dateext
  8. missingok
  9. notifempty
  10. sharedscripts
  11. delaycompress
  12. postrotate
  13. /bin/systemctl reload rsyslog.service > /dev/null 2>/dev/null || true
  14. endscript
  15. }

参数dateext的作用是在文件名结尾添加日期。 

手动验证下 

  1. cd /etc/logrotate.d
  2. logrotate -f network-devices

检验效果

在防墙配置syslog发送到日志服务器,防火墙是华为的,日志类型为local7,日志的量比较大,一天有20G左右,所以配置按天生成日志。

不使用elk和数据库是因为需要统一记录日志的设备不多,在命令行下查找更快

如下

 cat 172.18.5.1.log | egrep "172.18.111.160" | more

 

参考文档:Linux日志切割工具Logrotate_logrotate日志切割-CSDN博客 

                  Rsyslog发送日志时间转化_rsyslog时间格式-CSDN博客

                  https://www.cnblogs.com/ggngggg/articles/17170721.html

                  CentOS7下日志轮转logrotate简单入门与实践-腾讯云开发者社区-腾讯云

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号