温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-07-08 21:57:27 来源:网络 阅读:8669 作者:justin_peng 栏目:建站服务器

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

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

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

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

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


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

[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得到。

[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

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

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

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

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

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

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

[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 

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

[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服务端数据库里查看到信息

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



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI