centos5系列系統自帶為syslog1.4.1
centos6系列自帶rsyslog版本為5.8.10
centos7系列自帶rsyslog版本為7.4.7
目前最新版rsyslog為8.27.0,rsyslog從8.5.0后對imfile模塊進行重構,文件名中可以支持通配符。
rsyslog官網:http://www.rsyslog.com/
升級rsyslog需要配置yum源,centos默認的升級不到最新的版本。
1、配置yum
為了能夠使用RPM存儲庫,您需要一個.repo文件。使用您的webbrowser,請訪問http://rpms.adiscon.com。在這里,可以下載rsyslogall.repo文件,或者轉到所需版本的子文件夾(例如v8-stable),然后從中下載rsyslog.repo文件。(google翻譯來的)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[root@localhost yum.repos.d]
# pwd
/etc/yum
.repos.d
[root@localhost yum.repos.d]
# ls
dvd.repo packagekit-media.repo rhel-
source
.repo rsyslog.repo
[root@localhost yum.repos.d]
# cat rsyslog.repo
[rsyslog_v8]
name=Adiscon CentOS-$releasever -
local
packages
for
$basearch
baseurl=http:
//rpms
.adiscon.com
/v8-stable/epel-
$releasever/$basearch
enabled=1
gpgcheck=0
gpgkey=http:
//rpms
.adiscon.com
/RPM-GPG-KEY-Adiscon
protect=1
[root@localhost yum.repos.d]
# rpm -qa|grep -i rsyslog
rsyslog-5.8.10-10.el6_6.x86_64
[root@localhost yum.repos.d]
# yum -y install rsyslog
[root@localhost yum.repos.d]
# rpm -qa|grep -i rsyslog
rsyslog-8.27.0-2.el6.x86_64
[root@localhost yum.repos.d]
#
|
根據官方網文檔如果復制以上配置可能解析變量不正確導致安裝不成功,此時需要替換第三行中的變量“$ releasever”,$releasever的值表示當前系統的發行版本,可以通過rpm -qi centos-release命令查看,其中的Version:6就是我們系統的版本號;$basearch是我們的系統硬件架構(CPU指令集),使用命令arch得到。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
[root@localhost yum.repos.d]
# rpm -qi centos-release
Name : centos-release Relocations: (not relocatable)
Version : 6 Vendor: CentOS
Release : 5.el6.centos.11.1 Build Date: Wed 27 Nov 2013 07:53:33 PM CST
Install Date: Thu 27 Apr 2017 06:39:55 PM CST Build Host: c6b9.bsys.dev.centos.org
Group : System Environment
/Base
Source RPM: centos-release-6-5.el6.centos.11.1.src.rpm
Size : 32670 License: GPLv2
Signature : RSA
/SHA1
, Wed 27 Nov 2013 09:26:58 PM CST, Key ID 0946fca2c105b9de
Packager : CentOS BuildSystem <http:
//bugs
.centos.org>
Summary : CentOS release
file
Description :
CentOS release files
[root@localhost yum.repos.d]
# arch
x86_64
[root@localhost yum.repos.d]
#
|
2、配置rsyslog
rsyslog默認只可以傳送系統的日志,比如DHCP,cron、kern等,現在要傳送一個服務的日志到遠端的rsyslog服務器,要使用rsyslog的imfile模塊。該模塊提供將任何標准文本文件轉換為syslog消息的功能。該文件逐行讀取,任何讀取的行都傳遞給rsyslog的規則引擎。
官方文檔:
http://www.rsyslog.com/doc/v8-stable/configuration/modules/imfile.html
上圖為配置樣例,各參數可以參考文中的對應模塊參數說明,module參考文中Module Parameters說明;input參考文中Input Parameters說明。
1
2
3
4
5
6
|
[root@localhost yum.repos.d]
# cp /etc/rsyslog.conf{,.20170613bak}
[root@localhost yum.repos.d]
# vim /etc/rsyslog.conf
module(load=
"imfile"
PollingInterval=
"10"
)
input(
type
=
"imfile"
File=
"/opt/CalculationUnit/java/sh/logs/bigada.log"
Tag=
"CalculationUnit"
Severity=
"info"
Facility=
"local0"
freshStartTail=
"on"
deleteStateOnFileDelete=
"on"
)
local0.* @10.10.15.175:514
[root@localhost yum.repos.d]
# /etc/init.d/rsyslog restart
|
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
1
2
3
|
Jun 27 18:33:46 localhost rsyslogd: imfile warning: directory
'/opt/CalculationUnit/java/sh/logs'
: Permission denied [v8.27.0 try http:
//www
.rsyslog.com
/e/2046
]
Jun 27 18:33:46 localhost rsyslogd: [origin software=
"rsyslogd"
swVersion=
"8.27.0"
x-pid=
"19409"
x-info=
"http://www.rsyslog.com"
] start
Jun 27 18:33:46 localhost rsyslogd: imfile: error with inotify API, ignoring
file
'/opt/CalculationUnit/java/sh/logs/bigada.log'
: Permission denied [v8.27.0]
|
檢查selinux
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
[root@localhost ~]
# sestatus
SELinux status: enabled
SELinuxfs
mount
:
/selinux
Current mode: enforcing
Mode from config
file
: enforcing
Policy version: 24
Policy from config
file
: targeted
[root@localhost ~]
# getenforce
Enforcing
[root@localhost ~]
# setenforce 0
[root@localhost ~]
# getenforce
Permissive
[root@localhost ~]
# sestatus
SELinux status: enabled
SELinuxfs
mount
:
/selinux
Current mode: permissive
Mode from config
file
: enforcing
Policy version: 24
Policy from config
file
: targeted
[root@localhost ~]
# vim /etc/sysconfig/selinux
[root@localhost ~]
# /etc/init.d/rsyslog restart
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
[root@localhost ~]
#
|
現在可以在syslog服務端數據庫里查看到信息