天天看点

应用程序的日志通过rsyslog推送到syslog服务器

目前最新版rsyslog为8.27.0,rsyslog从8.5.0后对imfile模块进行重构,文件名中可以支持通配符。

升级rsyslog需要配置yum源,centos默认的升级不到最新的版本。

1、配置yum

<a href="https://s2.51cto.com/wyfs02/M00/98/A8/wKioL1k_Wr_SllGiAADgHZXOFX0716.png" target="_blank"></a>

<a href="https://s5.51cto.com/wyfs02/M01/98/A8/wKiom1k_WsCRgf3NAACoKXgFAc4607.png" target="_blank"></a>

<a href="https://s5.51cto.com/wyfs02/M02/98/A8/wKiom1k_WsGRZ3sAAAEJVShx23w513.png" target="_blank"></a>

为了能够使用RPM存储库,您需要一个.repo文件。使用您的webbrowser,请访问http://rpms.adiscon.com。在这里,可以下载rsyslogall.repo文件,或者转到所需版本的子文件夹(例如v8-stable),然后从中下载rsyslog.repo文件。(google翻译来的)

<a href="https://s5.51cto.com/wyfs02/M01/98/A8/wKioL1k_WsLTIbYAAAB_6q6JGmg596.png" target="_blank"></a>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

<code>[root@localhost yum.repos.d]</code><code># pwd</code>

<code>/etc/yum</code><code>.repos.d</code>

<code>[root@localhost yum.repos.d]</code><code># ls</code>

<code>dvd.repo  packagekit-media.repo  rhel-</code><code>source</code><code>.repo  rsyslog.repo</code>

<code>[root@localhost yum.repos.d]</code><code># cat rsyslog.repo </code>

<code>[rsyslog_v8]</code>

<code>name=Adiscon CentOS-$releasever - </code><code>local</code> <code>packages </code><code>for</code> <code>$basearch</code>

<code>baseurl=http:</code><code>//rpms</code><code>.adiscon.com</code><code>/v8-stable/epel-</code><code>$releasever/$basearch</code>

<code>enabled=1</code>

<code>gpgcheck=0</code>

<code>gpgkey=http:</code><code>//rpms</code><code>.adiscon.com</code><code>/RPM-GPG-KEY-Adiscon</code>

<code>protect=1</code>

<code>[root@localhost yum.repos.d]</code><code># rpm -qa|grep -i rsyslog</code>

<code>rsyslog-5.8.10-10.el6_6.x86_64</code>

<code>[root@localhost yum.repos.d]</code><code># yum -y install rsyslog</code>

<code>rsyslog-8.27.0-2.el6.x86_64</code>

<code>[root@localhost yum.repos.d]</code><code>#</code>

根据官方网文档如果复制以上配置可能解析变量不正确导致安装不成功,此时需要替换第三行中的变量“$ releasever”,$releasever的值表示当前系统的发行版本,可以通过rpm -qi centos-release命令查看,其中的Version:6就是我们系统的版本号;$basearch是我们的系统硬件架构(CPU指令集),使用命令arch得到。

<code>[root@localhost yum.repos.d]</code><code># rpm -qi centos-release</code>

<code>Name        : centos-release               Relocations: (not relocatable)</code>

<code>Version     : 6                                 Vendor: CentOS</code>

<code>Release     : 5.el6.centos.11.1             Build Date: Wed 27 Nov 2013 07:53:33 PM CST</code>

<code>Install Date: Thu 27 Apr 2017 06:39:55 PM CST      Build Host: c6b9.bsys.dev.centos.org</code>

<code>Group       : System Environment</code><code>/Base</code>       <code>Source RPM: centos-release-6-5.el6.centos.11.1.src.rpm</code>

<code>Size        : 32670                            License: GPLv2</code>

<code>Signature   : RSA</code><code>/SHA1</code><code>, Wed 27 Nov 2013 09:26:58 PM CST, Key ID 0946fca2c105b9de</code>

<code>Packager    : CentOS BuildSystem &lt;http:</code><code>//bugs</code><code>.centos.org&gt;</code>

<code>Summary     : CentOS release </code><code>file</code>

<code>Description :</code>

<code>CentOS release files</code>

<code>[root@localhost yum.repos.d]</code><code># arch</code>

<code>x86_64</code>

2、配置rsyslog

rsyslog默认只可以传送系统的日志,比如DHCP,cron、kern等,现在要传送一个服务的日志到远端的rsyslog服务器,要使用rsyslog的imfile模块。该模块提供将任何标准文本文件转换为syslog消息的功能。该文件逐行读取,任何读取的行都传递给rsyslog的规则引擎。

官方文档:

http://www.rsyslog.com/doc/v8-stable/configuration/modules/imfile.html

<a href="https://s2.51cto.com/wyfs02/M00/98/AB/wKiom1k_dv-TufZvAAELF2_2OiM310.png" target="_blank"></a>

<a href="https://s3.51cto.com/wyfs02/M02/98/AB/wKiom1k_dwDQcPN7AAGvvvwCeT0307.png" target="_blank"></a>

<a href="https://s2.51cto.com/wyfs02/M00/98/AB/wKioL1k_dwGQJKc2AAEk8V9HFzI944.png" target="_blank"></a>

<a href="https://s3.51cto.com/wyfs02/M02/98/AB/wKioL1k_dwLyKFR7AAF1ddH4HRw147.png" target="_blank"></a>

<a href="https://s3.51cto.com/wyfs02/M01/98/AB/wKiom1k_dwOxWL5aAACxV8l37FU693.png" target="_blank"></a>

上图为配置样例,各参数可以参考文中的对应模块参数说明,module参考文中Module Parameters说明;input参考文中Input Parameters说明。

<code>[root@localhost yum.repos.d]</code><code># cp /etc/rsyslog.conf{,.20170613bak}</code>

<code>[root@localhost yum.repos.d]</code><code># vim /etc/rsyslog.conf</code>

<code>module(load=</code><code>"imfile"</code> <code>PollingInterval=</code><code>"10"</code><code>)</code>

<code>input(</code><code>type</code><code>=</code><code>"imfile"</code> <code>File=</code><code>"/opt/CalculationUnit/java/sh/logs/bigada.log"</code> <code>Tag=</code><code>"CalculationUnit"</code> <code>Severity=</code><code>"info"</code> <code>Facility=</code><code>"local0"</code> <code>freshStartTail=</code><code>"on"</code> <code>deleteStateOnFileDelete=</code><code>"on"</code><code>)</code>

<code>local0.* @10.10.15.175:514</code>

<code>[root@localhost yum.repos.d]</code><code># /etc/init.d/rsyslog restart</code>

module

      load="imfile"    加载imfile模块

      PollingInterval="10"     轮询文件的频率,单位秒,默认10秒,

input

     type="imfile" 

     File="/opt/CalculationUnit/java/sh/logs/bigada.log"    发送到syslog的文件绝对路径

     Tag="CalculationUnit" 

     Severity="info"     

     Facility="local0" 

     freshStartTail="on"    设置为on每次重启rsyslog时只读取最新的数据丢弃旧日志,默认关

     deleteStateOnFileDelete="on" 如此文件会重新生成需要开启次参数,例如bigdata.log每天凌晨会重命名为bigdata%YYYY%mm%dd%.log,然后重新生成bigdata.log

local0.* @10.10.15.175:514   定义syslog服务器地址

错误:Permission denied 

<code>Jun 27 18:33:46 localhost rsyslogd: imfile warning: directory </code><code>'/opt/CalculationUnit/java/sh/logs'</code><code>: Permission denied [v8.27.0 try http:</code><code>//www</code><code>.rsyslog.com</code><code>/e/2046</code> <code>]</code>

<code>Jun 27 18:33:46 localhost rsyslogd:  [origin software=</code><code>"rsyslogd"</code> <code>swVersion=</code><code>"8.27.0"</code> <code>x-pid=</code><code>"19409"</code> <code>x-info=</code><code>"http://www.rsyslog.com"</code><code>] start</code>

<code>Jun 27 18:33:46 localhost rsyslogd: imfile: error with inotify API, ignoring </code><code>file</code> <code>'/opt/CalculationUnit/java/sh/logs/bigada.log'</code><code>: Permission denied  [v8.27.0]</code>

检查selinux

19

20

21

22

23

24

<code>[root@localhost ~]</code><code># sestatus</code>

<code>SELinux status:                 enabled</code>

<code>SELinuxfs </code><code>mount</code><code>:                </code><code>/selinux</code>

<code>Current mode:                   enforcing</code>

<code>Mode from config </code><code>file</code><code>:          enforcing</code>

<code>Policy version:                 24</code>

<code>Policy from config </code><code>file</code><code>:        targeted</code>

<code>[root@localhost ~]</code><code># getenforce </code>

<code>Enforcing</code>

<code>[root@localhost ~]</code><code># setenforce 0</code>

<code>Permissive</code>

<code>Current mode:                   permissive</code>

<code>[root@localhost ~]</code><code># vim /etc/sysconfig/selinux </code>

<code>[root@localhost ~]</code><code># /etc/init.d/rsyslog restart</code>

<code>Shutting down system logger:                               [  OK  ]</code>

<code>Starting system logger:                                    [  OK  ]</code>

<code>[root@localhost ~]</code><code>#</code>

现在可以在syslog服务端数据库里查看到信息

<a href="https://s4.51cto.com/wyfs02/M00/98/AC/wKiom1k_eiiwKafvAADQrbYnfak416.png" target="_blank"></a>

本文转自 justin_peng 51CTO博客,原文链接:http://blog.51cto.com/ityunwei2017/1934910,如需转载请自行联系原作者