前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >非常详细的rsyslogd & logrotate配置文件解析[通俗易懂]

非常详细的rsyslogd & logrotate配置文件解析[通俗易懂]

作者头像
全栈程序员站长
发布2022-09-21 11:05:44
1.6K0
发布2022-09-21 11:05:44
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

rsyslog服务和logrotate服务 ====================================================================== rsyslog 是一个 syslogd 的多线程增强版。 现在Fedora和Ubuntu, rhel6默认的日志系统都是rsyslog了

rsyslog服务和logrotate服务 ====================================================================== rsyslog 是一个 syslogd 的多线程增强版。 现在Fedora和Ubuntu, rhel6默认的日志系统都是rsyslog了 rsyslog负责写入日志, logrotate负责备份和删除旧日志, 以及更新日志文件 ———————————————————————- rsyslog 服务 ———————————————————————- 软件包: [root@centos ~]# yum install rsyslog rsyslog-MySQL logrotate 查看当前rsyslog服务的状态: [root@centos ~]#/etc/init.d/rsyslog status rsyslogd (pid 1343) is running… 在centos6中, rsyslog服务默认是开机启动的 我们先看一下它的进程:: [root@centos ~]# ps -ef | grep rsyslogd | grep -v grep root 1343 1 0 12:09 ? 00:00:00 /sbin/rsyslogd -c 4 从上面命令的输出结果看到rsyslog执行时使用的参数是-c 4. 它的意思是指定rsyslog运行(兼容)的版本号, 这个参数必须是第一个参数, 当然也可以省略, 默认为-c0, (命令行兼容sysklogd) 这个参数是在文件/etc/sysconfig/rsyslog中指定:: [root@centos ~]# cat /etc/sysconfig/rsyslog

  1. # Options to syslogd
  2. # syslogd options are deprecated since rsyslog v3
  3. # if you want to use them, switch to compatibility mode 2 by “-c 2″
  4. SYSLOGD_OPTIONS=”-c 4″

[root@centos ~]# chkconfig –list | grep rsyslog rsyslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off 注意,这里的服务名是rsyslog! ———————————————————————- 配置文件 ———————————————————————- /etc/rsyslog.conf 配置文件的基本信息 配置文件中有很多内容, 但最主要的是指定需要记录哪些服务和需要记录什么等级的信息:: cat /etc/rsyslog.conf

  1. #rsyslog v3 config file
  2. # if you experience problems, check
  3. # http://www.rsyslog.com/troubleshoot for assistance
  4. #### MODULES #### 加载 模块
  5. $ModLoad imuxsock.so –> 模块名 # provides support for local system logging (e.g. via logger command) 本地系统日志
  6. $ModLoad imklog.so # provides kernel logging support (previously done by rklogd)
  7. #$ModLoad immark.so # provides –MARK– message capability
  8. # Provides UDP syslog reception
  9. # 允许514端口接收使用UDP协议转发过来的日志
  10. #$ModLoad imudp.so
  11. #$UDPServerRun 514
  12. # Provides TCP syslog reception
  13. # 允许514端口接收使用TCP协议转发过来的日志
  14. #$ModLoad imtcp.so
  15. #$InputTCPServerRun 514
  16. #### GLOBAL DIRECTIVES ####
  17. 定义日志格式默认模板
  18. # Use default timestamp format
  19. $ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat
  20. # File syncing capability is disabled by default. This feature is usually not required,
  21. # not useful and an extreme performance hit
  22. #$ActionFileEnableSync on
  23. #### RULES ####
  24. # Log all kernel messages to the console.
  25. # Logging much else clutters up the screen.
  26. #kern.* /dev/console 关于内核的所有日志都放到/dev/console(控制台)
  27. # Log anything (except mail) of level info or higher.
  28. # Don’t log private authentication messages!
  29. # 记录所有日志类型的info级别以及大于info级别的信息到/var/log/messages,但是mail邮件信息,authpriv验证方面的信息和cron时间任务相关的信息除外
  30. *.info;mail.none;authpriv.none;cron.none /var/log/messages
  31. # The authpriv file has restricted access.
  32. # authpriv验证相关的所有信息存放在/var/log/secure
  33. authpriv.* /var/log/secure
  34. # Log all the mail messages in one place.
  35. # 邮件的所有信息存放在/var/log/maillog; 这里有一个-符号, 表示是使用异步的方式记录, 因为日志一般会比较大
  36. mail.* -/var/log/maillog
  37. # Log cron stuff
  38. # 计划任务有关的信息存放在/var/log/cron
  39. cron.* /var/log/cron
  40. # Everybody gets emergency messages
  41. # 记录所有的大于等于emerg级别信息, 以wall方式发送给每个登录到系统的人
  42. *.emerg * *代表所有在线用户
  43. # Save news errors of level crit and higher in a special file.
  44. # 记录uucp,news.crit等存放在/var/log/spooler
  45. uucp,news.crit /var/log/spooler
  46. # Save boot messages also to boot.log 启动的相关信息
  47. local7.* /var/log/boot.log
  48. #:rawmsg, contains, “sdns_log” @@192.168.56.7:10514
  49. #:rawmsg, contains, “sdns_log” ~
  50. # ### begin forwarding rule ### 转发规则
  51. # The statement between the begin … end define a SINGLE forwarding
  52. # rule. They belong together, do NOT split them. If you create multiple
  53. # forwarding rules, duplicate the whole block!
  54. # Remote Logging (we use TCP for reliable delivery)
  55. #
  56. # An on-disk queue is created for this action. If the remote host is
  57. # down, messages are spooled to disk and sent when it is up again.
  58. #$WorkDirectory /var/spppl/rsyslog # where to place spool files
  59. #$ActionQueueFileName fwdRule1 # unique name prefix for spool files
  60. #$ActionQueueMaxDiskSpace 1g # 1gb space limit (use as much as possible)
  61. #$ActionQueueSaveOnShutdown on # save messages to disk on shutdown
  62. #$ActionQueueType LinkedList # run asynchronously
  63. #$ActionResumeRetryCount -1 # infinite retries if host is down
  64. # remote host is: name/ip:port, e.g. 192.168.0.1:514, port optional
  65. #*.* @@remote-host:514 # @@表示通过tcp协议发送 @表示通过udp进行转发
  66. #local3.info @@localhost :514
  67. #local7.* # @@192.168.56.7:514
  68. # ### end of the forwarding rule ###

格式: 日志设备(类型).(连接符号)日志级别 日志处理方式(action) 日志设备(可以理解为日志类型): ———————————————————————- auth –pam产生的日志 authpriv –ssh,ftp等登录信息的验证信息 cron –时间任务相关 kern –内核 lpr –打印 mail –邮件 mark(syslog)–rsyslog服务内部的信息,时间标识 news –新闻组 user –用户程序产生的相关信息 uucp –unix to unix copy, unix主机之间相关的通讯 local 1~7 –自定义的日志设备 日志级别: ———————————————————————- debug –有调式信息的,日志信息最多 info –一般信息的日志,最常用 notice –最具有重要性的普通条件的信息 warning –警告级别 err –错误级别,阻止某个功能或者模块不能正常工作的信息 crit –严重级别,阻止整个系统或者整个软件不能正常工作的信息 alert –需要立刻修改的信息 emerg –内核崩溃等严重信息 none –什么都不记录 从上到下,级别从低到高,记录的信息越来越少 详细的可以查看手册: man 3 syslog 连接符号 ———————————————————————- .xxx: 表示大于等于xxx级别的信息 .=xxx:表示等于xxx级别的信息 .!xxx:表示在xxx之外的等级的信息 Actions ———————————————————————- 1. 记录到普通文件或设备文件:: *.* /var/log/file.log # 绝对路径 *.* /dev/pts/0 测试: logger -p local3.info ‘KadeFor is testing the rsyslog and logger ‘ logger 命令用于产生日志 2. 转发到远程:: *.* @192.168.0.1 # 使用UDP协议转发到192.168.0.1的514(默认)端口 *.* @@192.168.0.1:10514 # 使用TCP协议转发到192.168.0.1的10514(默认)端口 3. 发送给用户(需要在线才能收到):: *.* root *.* root,kadefor,up01 # 使用,号分隔多个用户 *.* * # *号表示所有在线用户 4. 忽略,丢弃:: local3.* ~ # 忽略所有local3类型的所有级别的日志 5. 执行脚本:: local3.* ^/tmp/a.sh # ^号后跟可执行脚本或程序的绝对路径 # 日志内容可以作为脚本的第一个参数. # 可用来触发报警 .. note:: 日志记录的顺序有先后关系! ====================================================================== 一个标准的简单的配置文件 ====================================================================== :: *.info;mail.none;authpriv.none;cron.none /var/log/messages authpriv.* /var/log/secure mail.* /var/log/maillog cron.* /var/log/cron *.emerg * uucp,news.crit /var/log/spooler local7.* /var/log/boot.log ====================================================================== 实例: 指定日志文件, 或者终端 ====================================================================== [root@kadefor ule-sa3]# vi /etc/rsyslog.conf [root@kadefor ule-sa3]# grep local3 !$ grep local3 /etc/rsyslog.conf local3.* /var/log/local3.log [root@kadefor ule-sa3]# rm -rf /var/log/local3.log [root@kadefor ule-sa3]# /etc/init.d/rsyslog reload Reloading system logger… [ OK ] [root@kadefor ule-sa3]# ls /var/log/local3.log /var/log/local3.log [root@kadefor ule-sa3]# logger -t ‘LogTest’ -p local3.info ‘KadeFor is testing the rsyslog and logger’ [root@kadefor ule-sa3]# cat /var/log/local3.log Jun 10 04:55:52 kadefor LogTest: KadeFor is testing the rsyslog and logger [root@kadefor ule-sa3]# 自己实验日志发送给某个终端 ====================================================================== 实例: 过滤特定的日志到文件, 忽略(丢弃)包含某个字符串的日志 ====================================================================== # 过滤日志, 由:号开头 :msg, contains, “error” /var/log/error.log :msg, contains, “error” ~ # 忽略包含error的日志 :msg, contains, “user nagios” ~ :msg, contains, “user kadefor” ~ :msg, contains, “module-alsa-sink.c: ALSA woke us up to write new data to the device, but there was actually nothing to write” ~ local3.* ~ PS. & ~ # 忽略所有的日志 把包含’Oracle’的日志保存在/var/log/oracle.log ====================================================================== 实例: 使用模板来定义日志格式 ====================================================================== 定义默认的日志格式:

  1. $template myFormat,”%rawmsg%\n”
  2. $ActionFileDefaultTemplate myFormat
  3. #如果不要$ActionFileDefaultTemplate myFormat这一行, 就需要像这样来使用模板:
  4. #在日志文件后添加模板名, 并用;号分隔
  5. $template myFormat,”%rawmsg%\n”
  6. # The authpriv file has restricted access.
  7. authpriv.* /var/log/secure;myFormat
  8. # Log all the mail messages in one place.
  9. mail.* /var/log/maillog;myFormat
  10. # Log cron stuff
  11. cron.* /var/log/cron;myFormat
  12. # Everybody gets emergency messages
  13. *.emerg *
  14. # Save news errors of level crit and higher in a special file.
  15. uucp,news.crit /var/log/spooler;myFormat
  16. # Save boot messages also to boot.log
  17. local7.* /var/log/boot.log;myFormat

====================================================================== 实例: remote log 远程发送与接收: ====================================================================== 如果要修改为非514的端口, 需要设置selinux 只要在rsyslog.conf中加入 *.* @192.168.0.10 *.* @192.168.0.10:10514 # 带端口号 *.* @@192.168.0.10 # TCP 但是没有定义保存在远程的哪一个文件啊? 其实保存在什么文件, 那是远程日志服务器接收到日志之后它自己的事情了. 例1: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ———————————————————————- Client(send): ———————————————————————- :: local3.* @@192.0.2.1:10514 # if you need to forward to other systems as well, just # add additional config lines: # *.* @@other-server.example.NET:10514 # 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 * # 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 ———————————————————————- Server(receive): <1> ———————————————————————- :: # for TCP use: modload imudp UDPServerRun 514 # 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 * # 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 local3.* /var/log/local3.log # 测试用 例2 (仅做了解, 不做要求) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #配置服务端(接收) ———————————————————————- vi /etc/rsyslog.conf #在文件开始加上,同时确保514端口能够被客户端用tcp访问 ModLoad imtcp.so # needs to be done just once #使用tcp方式 InputTCPMaxSessions 500 # tcp接收连接数为500个 InputTCPServerRun 514 # tcp接收信息的端口 template logformat,”%TIMESTAMP:::date-MySQL% %FROMHOST-IP%%msg%\n” # 定义一个名为logformat模板, 为信息加上日志时间 template DynFile,”/var/log/tlog%year%%month%%day%.log” # 定义日志文件的名称,按照年月日 :rawmsg, contains, “sdns_log” ?DynFile;logformat # 把rawmsg(也可以使用msg)日志中包含sdns_log标志的信息写到DynFile定义的日志文件里 :rawmsg, contains, “sdns_log” ~ # 这个表示丢弃包含sdns_log标志的信息, 一般都加上它, 以免多个日志文件记录重复的日志 #配置客户端(发送) ———————————————————————- vi /etc/rsyslog.conf #在文件开始加上 #把包含sdns_log的信息通过tcp发到192.168.1.2 @@表示tcp @表示udp :rawmsg, contains, “sdns_log” @@192.168.1.2 # 默认514端口 #这个表示丢弃包含sdns_log标志的信息,防止这个信息写到本机的/var/log/message :rawmsg, contains, “sdns_log” ~ #测试 ———————————————————————- 在客户端上执行 logger -p user.info “sdns_log 34334″ 在服务端的/var/log/目录里是否有tlog*日志产生 补充: ———————————————————————- 如果要把不同服务器发送过来的日志保存到不同的文件, 可以这样操作: :fromhost-ip, isequal, “192.168.0.160″ /var/log/host160.log :FROMHOST-IP, isequal, “192.168.0.161″ /var/log/host161.log :FROMHOST-IP, startswith, “192.168.1.” /var/log/network1.log :FROMHOST-IP, startswith, “192.168.2.” /var/log/network2.log 练习: ====================================================================== 1. 实现把ssh服务的日志自定义保存到/var/log/newsshd.log (先不做) 2. mail日志保存在远程日志服务器/var/log/newmail.log 3. 过滤日志, 如果日志包含有”daydayup”, 则执行脚本/tmp/a.sh 脚本内容: #!/bin/bash echo “KO::**

原网址: http://smartest.blog.51cto.com/3585938/1280933

———————————————————————- rsyslog 服务 ———————————————————————- 软件包: [root @centos ~]# yum install rsyslog rsyslog- MySQL logrotate 查看当前rsyslog服务的状态: [root @centos ~]#/etc/init.d/rsyslog status rsyslogd (pid 1343) is running… 在centos6中, rsyslog服务默认是开机启动的 我们先看一下它的进程:: [root @centos ~]# ps -ef | grep rsyslogd | grep -v grep root 1343 1 0 12:09 ? 00:00:00 /sbin/rsyslogd -c 4 从上面命令的输出结果看到rsyslog执行时使用的参数是-c 4. 它的意思是指定rsyslog运行(兼容)的版本号, 这个参数必须是第一个参数, 当然也可以省略, 默认为-c0, (命令行兼容sysklogd) 这个参数是在文件/etc/sysconfig/rsyslog中指定:: [root @centos ~]# cat /etc/sysconfig/rsyslog

  1. # Options to syslogd
  2. # syslogd options are deprecated since rsyslog v3
  3. # if you want to use them, switch to compatibility mode 2 by “-c 2″
  4. SYSLOGD_OPTIONS=”-c 4″

[root@centos ~]# chkconfig –list | grep rsyslog rsyslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off 注意,这里的服务名是rsyslog! ———————————————————————- 配置文件 ———————————————————————- /etc/rsyslog.conf 配置文件的基本信息 配置文件中有很多内容, 但最主要的是指定需要记录哪些服务和需要记录什么等级的信息:: cat /etc/rsyslog.conf

  1. #rsyslog v3 config file
  2. # if you experience problems, check
  3. # http://www.rsyslog.com/troubleshoot for assistance
  4. #### MODULES #### 加载 模块
  5. $ModLoad imuxsock.so –> 模块名 # provides support for local system logging (e.g. via logger command) 本地系统日志
  6. $ModLoad imklog.so # provides kernel logging support (previously done by rklogd)
  7. #$ModLoad immark.so # provides –MARK– message capability
  8. # Provides UDP syslog reception
  9. # 允许514端口接收使用UDP协议转发过来的日志
  10. #$ModLoad imudp.so
  11. #$UDPServerRun 514
  12. # Provides TCP syslog reception
  13. # 允许514端口接收使用TCP协议转发过来的日志
  14. #$ModLoad imtcp.so
  15. #$InputTCPServerRun 514
  16. #### GLOBAL DIRECTIVES ####
  17. 定义日志格式默认模板
  18. # Use default timestamp format
  19. $ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat
  20. # File syncing capability is disabled by default. This feature is usually not required,
  21. # not useful and an extreme performance hit
  22. #$ActionFileEnableSync on
  23. #### RULES ####
  24. # Log all kernel messages to the console.
  25. # Logging much else clutters up the screen.
  26. #kern.* /dev/console 关于内核的所有日志都放到/dev/console(控制台)
  27. # Log anything (except mail) of level info or higher.
  28. # Don’t log private authentication messages!
  29. # 记录所有日志类型的info级别以及大于info级别的信息到/var/log/messages,但是mail邮件信息,authpriv验证方面的信息和cron时间任务相关的信息除外
  30. *.info;mail.none;authpriv.none;cron.none /var/log/messages
  31. # The authpriv file has restricted access.
  32. # authpriv验证相关的所有信息存放在/var/log/secure
  33. authpriv.* /var/log/secure
  34. # Log all the mail messages in one place.
  35. # 邮件的所有信息存放在/var/log/maillog; 这里有一个-符号, 表示是使用异步的方式记录, 因为日志一般会比较大
  36. mail.* -/var/log/maillog
  37. # Log cron stuff
  38. # 计划任务有关的信息存放在/var/log/cron
  39. cron.* /var/log/cron
  40. # Everybody gets emergency messages
  41. # 记录所有的大于等于emerg级别信息, 以wall方式发送给每个登录到系统的人
  42. *.emerg * *代表所有在线用户
  43. # Save news errors of level crit and higher in a special file.
  44. # 记录uucp,news.crit等存放在/var/log/spooler
  45. uucp,news.crit /var/log/spooler
  46. # Save boot messages also to boot.log 启动的相关信息
  47. local7.* /var/log/boot.log
  48. #:rawmsg, contains, “sdns_log” @@192.168.56.7:10514
  49. #:rawmsg, contains, “sdns_log” ~
  50. # ### begin forwarding rule ### 转发规则
  51. # The statement between the begin … end define a SINGLE forwarding
  52. # rule. They belong together, do NOT split them. If you create multiple
  53. # forwarding rules, duplicate the whole block!
  54. # Remote Logging (we use TCP for reliable delivery)
  55. #
  56. # An on-disk queue is created for this action. If the remote host is
  57. # down, messages are spooled to disk and sent when it is up again.
  58. #$WorkDirectory /var/spppl/rsyslog # where to place spool files
  59. #$ActionQueueFileName fwdRule1 # unique name prefix for spool files
  60. #$ActionQueueMaxDiskSpace 1g # 1gb space limit (use as much as possible)
  61. #$ActionQueueSaveOnShutdown on # save messages to disk on shutdown
  62. #$ActionQueueType LinkedList # run asynchronously
  63. #$ActionResumeRetryCount -1 # infinite retries if host is down
  64. # remote host is: name/ip:port, e.g. 192.168.0.1:514, port optional
  65. #*.* @@remote-host:514 # @@表示通过tcp协议发送 @表示通过udp进行转发
  66. #local3.info @@localhost :514
  67. #local7.* # @@192.168.56.7:514
  68. # ### end of the forwarding rule ###

格式:: 日志设备(类型).(连接符号)日志级别 日志处理方式(action) 日志设备(可以理解为日志类型): ———————————————————————- auth –pam产生的日志 authpriv –ssh,ftp等登录信息的验证信息 cron –时间任务相关 kern –内核 lpr –打印 mail –邮件 mark(syslog)–rsyslog服务内部的信息,时间标识 news –新闻组 user –用户程序产生的相关信息 uucp –unix to unix copy, unix主机之间相关的通讯 local 1~7 –自定义的日志设备 日志级别: ———————————————————————- debug –有调式信息的,日志信息最多 info –一般信息的日志,最常用 notice –最具有重要性的普通条件的信息 warning –警告级别 err –错误级别,阻止某个功能或者模块不能正常工作的信息 crit –严重级别,阻止整个系统或者整个软件不能正常工作的信息 alert –需要立刻修改的信息 emerg –内核崩溃等严重信息 none –什么都不记录 从上到下,级别从低到高,记录的信息越来越少 详细的可以查看手册: man 3 syslog 连接符号 ———————————————————————- .xxx: 表示大于等于xxx级别的信息 .=xxx:表示等于xxx级别的信息 .!xxx:表示在xxx之外的等级的信息 Actions ———————————————————————- 1. 记录到普通文件或设备文件:: *.* /var/log/file.log # 绝对路径 *.* /dev/pts/0 测试: logger -p local3.info ‘KadeFor is testing the rsyslog and logger ‘ logger 命令用于产生日志 2. 转发到远程:: *.* @192.168.0.1 # 使用UDP协议转发到192.168.0.1的514(默认)端口 *.* @@192.168.0.1:10514 # 使用TCP协议转发到192.168.0.1的10514(默认)端口 3. 发送给用户(需要在线才能收到):: *.* root *.* root,kadefor,up01 # 使用,号分隔多个用户 *.* * # *号表示所有在线用户 4. 忽略,丢弃:: local3.* ~ # 忽略所有local3类型的所有级别的日志 5. 执行脚本:: local3.* ^/tmp/a.sh # ^号后跟可执行脚本或程序的绝对路径 # 日志内容可以作为脚本的第一个参数. # 可用来触发报警 .. note:: 日志记录的顺序有先后关系! ====================================================================== 一个标准的简单的配置文件 ====================================================================== :: *.info;mail.none;authpriv.none;cron.none /var/log/messages authpriv.* /var/log/secure mail.* /var/log/maillog cron.* /var/log/cron *.emerg * uucp,news.crit /var/log/spooler local7.* /var/log/boot.log ====================================================================== 实例: 指定日志文件, 或者终端 ====================================================================== [root@kadefor ule-sa3]# vi /etc/rsyslog.conf [root@kadefor ule-sa3]# grep local3 !$ grep local3 /etc/rsyslog.conf local3.* /var/log/local3.log [root@kadefor ule-sa3]# rm -rf /var/log/local3.log [root@kadefor ule-sa3]# /etc/init.d/rsyslog reload Reloading system logger… [ OK ] [root@kadefor ule-sa3]# ls /var/log/local3.log /var/log/local3.log [root@kadefor ule-sa3]# logger -t ‘LogTest’ -p local3.info ‘KadeFor is testing the rsyslog and logger’ [root@kadefor ule-sa3]# cat /var/log/local3.log Jun 10 04:55:52 kadefor LogTest: KadeFor is testing the rsyslog and logger [root@kadefor ule-sa3]# 自己实验日志发送给某个终端 ====================================================================== 实例: 过滤特定的日志到文件, 忽略(丢弃)包含某个字符串的日志 ====================================================================== # 过滤日志, 由:号开头 :msg, contains, “error” /var/log/error.log :msg, contains, “error” ~ # 忽略包含error的日志 :msg, contains, “user nagios” ~ :msg, contains, “user kadefor” ~ :msg, contains, “module-alsa-sink.c: ALSA woke us up to write new data to the device, but there was actually nothing to write” ~ local3.* ~ PS. & ~ # 忽略所有的日志 把包含’oracle’的日志保存在/var/log/oracle.log ====================================================================== 实例: 使用模板来定义日志格式 ====================================================================== 定义默认的日志格式:

  1. $template myFormat,”%rawmsg%\n”
  2. $ActionFileDefaultTemplate myFormat
  3. #如果不要$ActionFileDefaultTemplate myFormat这一行, 就需要像这样来使用模板:
  4. #在日志文件后添加模板名, 并用;号分隔
  5. $template myFormat,”%rawmsg%\n”
  6. # The authpriv file has restricted access.
  7. authpriv.* /var/log/secure;myFormat
  8. # Log all the mail messages in one place.
  9. mail.* /var/log/maillog;myFormat
  10. # Log cron stuff
  11. cron.* /var/log/cron;myFormat
  12. # Everybody gets emergency messages
  13. *.emerg *
  14. # Save news errors of level crit and higher in a special file.
  15. uucp,news.crit /var/log/spooler;myFormat
  16. # Save boot messages also to boot.log
  17. local7.* /var/log/boot.log;myFormat

====================================================================== 实例: remote log 远程发送与接收: ====================================================================== 如果要修改为非514的端口, 需要设置selinux 只要在rsyslog.conf中加入 *.* @192.168.0.10 *.* @192.168.0.10:10514 # 带端口号 *.* @@192.168.0.10 # TCP 但是没有定义保存在远程的哪一个文件啊? 其实保存在什么文件, 那是远程日志服务器接收到日志之后它自己的事情了. 例1: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ———————————————————————- Client(send): ———————————————————————- :: local3.* @@192.0.2.1:10514 # if you need to forward to other systems as well, just # add additional config lines: # *.* @@other-server.example.Net:10514 # 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 * # 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 ———————————————————————- Server(receive): <1> ———————————————————————- :: # for TCP use: modload imudp UDPServerRun 514 # 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 * # 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 local3.* /var/log/local3.log # 测试用 例2 (仅做了解, 不做要求) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #配置服务端(接收) ———————————————————————- vi /etc/rsyslog.conf #在文件开始加上,同时确保514端口能够被客户端用tcp访问 ModLoad imtcp.so # needs to be done just once #使用tcp方式 InputTCPMaxSessions 500 # tcp接收连接数为500个 InputTCPServerRun 514 # tcp接收信息的端口 template logformat,”%TIMESTAMP:::date-mysql% %FROMHOST-IP%%msg%\n” # 定义一个名为logformat模板, 为信息加上日志时间 template DynFile,”/var/log/tlog%year%%month%%day%.log” # 定义日志文件的名称,按照年月日 :rawmsg, contains, “sdns_log” ?DynFile;logformat # 把rawmsg(也可以使用msg)日志中包含sdns_log标志的信息写到DynFile定义的日志文件里 :rawmsg, contains, “sdns_log” ~ # 这个表示丢弃包含sdns_log标志的信息, 一般都加上它, 以免多个日志文件记录重复的日志 #配置客户端(发送) ———————————————————————- vi /etc/rsyslog.conf #在文件开始加上 #把包含sdns_log的信息通过tcp发到192.168.1.2 @@表示tcp @表示udp :rawmsg, contains, “sdns_log” @@192.168.1.2 # 默认514端口 #这个表示丢弃包含sdns_log标志的信息,防止这个信息写到本机的/var/log/message :rawmsg, contains, “sdns_log” ~ #测试 ———————————————————————- 在客户端上执行 logger -p user.info “sdns_log 34334″ 在服务端的/var/log/目录里是否有tlog*日志产生 补充: ———————————————————————- 如果要把不同服务器发送过来的日志保存到不同的文件, 可以这样操作: :fromhost-ip, isequal, “192.168.0.160″ /var/log/host160.log :FROMHOST-IP, isequal, “192.168.0.161″ /var/log/host161.log :FROMHOST-IP, startswith, “192.168.1.” /var/log/network1.log :FROMHOST-IP, startswith, “192.168.2.” /var/log/network2.log 练习: ====================================================================== 1. 实现把ssh服务的日志自定义保存到/var/log/newsshd.log (先不做) 2. mail日志保存在远程日志服务器/var/log/newmail.log 3. 过滤日志, 如果日志包含有”daydayup”, 则执行脚本/tmp/a.sh 脚本内容: #!/bin/bash echo “KO::**

原网址: http://smartest.blog.51cto.com/3585938/1280933

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/170611.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
日志服务
日志服务(Cloud Log Service,CLS)是腾讯云提供的一站式日志服务平台,提供了从日志采集、日志存储到日志检索,图表分析、监控告警、日志投递等多项服务,协助用户通过日志来解决业务运维、服务监控、日志审计等场景问题。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档