天天看點

應用程式的日志通過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,如需轉載請自行聯系原作者