syslog-ng日志集中

syslog-ng学习心得之一
来源: ChinaUnix博客  日期: 2008.08.24 23:54 (共有条评论)我要评论
 

  转载一篇syslog-ng的文章的,本来是想打算自己写一篇的,看到这篇文章写的很详细,就转载过来吧。后面我再补充一下
先说说自己对syslog-ng的理解:用于集中日志管理,可以对日志进行筛选过滤。

原文地址:
http://www.linuxfly.org/read.php?171

前面详细介绍了sysklog套件的配置和使用。但毕竟该套件已经比较老了,很多功能都不够完善,而系统日志又是系统维护中至关重要的参考信息。我们可以使用syslog-ng(下一代系统日志工具)来替代原来的sysklog服务。syslog-ng可以通过对日志信息执行正规表达式过滤,并且支持主机链方式等工作,都能更好的协助我们管理主机。

一、基础
syslog-ng作为syslog的替代工具,可以完全替代syslog的服务,并且通过定义规则,实现更好的过滤功能。
系统自带版本:
引用
# rpm -qa|grep syslog-ng
syslog-ng-1.6.7-1
syslog官方网站:
点击
最新版本是2.0.5。
为方便使用,暂以系统自带的版本1.6.7说明,以后再提供更新的rpm包。
1、前提
使用syslog-ng前,建议先详细了解syslog的概念。
例如,什么是facitily(设备),level(等级)。可以参考这里:
点击
否则,后面的说明可能会有点不知所云的。
2、使用
若不增加其他设定,可通过下面的简单命令即可替换原syslog服务:
# service syslog stop
# service syslog-ng start
3、设计原则
syslog-ng替代syslog是基于以下的设计原则的:
引用
a、通过正规表达式协助,除支持原facitily/level方式,还支持内容过滤等以建立更好的消息过滤机制;
b、支持主机链,即使日志消息经过多重网络转发,仍可找到原发出主机的信息和整个消息链;
c、支持强大的自定义配置,并且清晰、明了。
二、配置说明
syslog-ng的主配置文件存放在:/etc/syslog-ng/syslog-ng.conf
1、架构
syslog-ng的配置基于下面的架构:
引用
LOG STATEMENTS『SOURCES - FILTERS -DESTINATIONS』
消息路径 『消息源 - 过滤器 - 目的站』
也就是说,通过定义多个消息源,把匹配上若干个过滤器的消息导向到指定的目的地,从而组成一个消息路径。
2、消息源SOURCES
定义格式为:
引用
source  { sourcedriver params; sourcedriver params; ... };
含义:
引用
:一个消息源的标识
sourcedriver:消息源驱动器,可以支持若干参数,并使用分号“;”隔离多个消息源驱动器
消息源驱动器有:
引用
file (filename) : 从指定的文件读取日志信息
unix-dgram  (filename) : 打开指定的SOCK_DGRAM模式的unix套接字,接收日志消息
unix-stream (filename) : 打开指定的SOCK_STREAM模式的unix套接字,接收日志消息
udp ( (ip),(port) ) : 在指定的UDP端口接收日志消息
tcp ( (ip),(port) ) : 在指定的TCP端口接收日志消息
sun-streams (filename) : 在solaris系统中,打开一个(多个)指定的STREAM设备,从其中读取日志消息
internal() : syslog-ng内部产生的消息
pipe(filename),fifo(filename) : 从指定的管道或者FIFO设备,读取日志信息
例如:
引用
source s_sys {
   file ("/proc/kmsg" log_prefix("kernel: "));
   unix-stream ("/dev/log");
   internal();
   # udp(ip(0.0.0.0) port(514)); #如果取消注释,则可以从udp的514端口获取消息
};
※linux使用/dev/log作为SOCK_STREAM unix的套接字,BSD使用/var/run/log;
参数需要使用括号括住。
3、过滤器FILTERS

定义格式为:
引用
filter  { expression; };
含义:
引用
:一个过滤器标识
expression:表达式
表达式支持:
引用
逻辑操作符:and(和)、or(或)、not(非);
函数:可使用正规表达式描述内容
过滤函数有:
引用
facility(,): 根据facility(设备)选择日志消息,使用逗号分割多个facility
level(,): 根据level(优先级)选择日志消息,使用逗号分割多个level,或使用“..”表示一个范围
program(regexp): 日志消息的程序名是否匹配一个正则表达式
host(regexp): 日志消息的主机名是否和一个正则表达式匹配
match(regexp): 对日志消息的内容进行正则匹配
filter(): 调用另一条过滤规则并判断它的值
例如:
引用
filter f_filter2   { level(info..emerg) and
                    not facility(mail,authpriv,cron); };
※这里的level定义info,相当于syslog的.=info,并不包括更低的等级;
若需要包括更低的等级,请使用“..”表示一个等级范围;
另外,filter(DEFAULT),用于捕获所有没有匹配上的日志消息。filter(*)是无效的。
4、目的地DESTINATIONS

定义格式为:
引用
destination  { destdriver params; destdriver params;  ...  ;};
含义:
引用
:一个目的地的标识
destdriver :目的地驱动器
目的地驱动器有:
引用
file (filename) :把日志消息写入指定的文件
unix-dgram  (filename) :把日志消息写入指定的SOCK_DGRAM模式的unix套接字
unix-stream (filename) :把日志消息写入指定的SOCK_STREAM模式的unix套接字
udp  (ip),(port) :把日志消息发送到指定的UDP端口
tcp (ip),(port) :把日志消息发送到指定的TCP端口
usertty(username) :把日志消息发送到已经登陆的指定用户终端窗口
pipe(filename),fifo(filename) :把日志消息发送到指定的管道或者FIFO设备
program(parm) :启动指定的程序,并把日志消息发送到该进程的标准输入
举例:
引用
destination d_mesg { file("/var/log/messages"); };
destination d_syslog { udp ("192.168.228.225" port(514)); };
※配合使用udp或tcp即可实现集中的日志服务器。注意,udp函数的写法上和消息源驱动器中的定义不同。
5、消息路径LOG STATEMENTS

定义格式为:
引用
log  { source S1; source S2; ... filter F1; filter F2; ... destination
      D1; destination D2; ... };
把消息源、过滤器、消息目的组合起来就形成一条完整的指令。日志路径中的成员是顺序执行的。凡是来源于指定的消息源,匹配所有指定的过滤器,并送到指定的地址。
※同样的,每条日志消息都会经过所有的消息路径,并不是匹配后就不再往下执行的,请留意。
三、选项参数
除了上述的消息路径定义外,syslog-ng还可以设定一些选项参数以优化其操作。
全局的选项参数,定义在配置文件的开头位置:

引用
options { opt1; opt2; ... };
选项有:

引用
chain_hostnames(yes|no) :是否打开主机名链功能,打开后可在多网络段转发日志时有效
long_hostnames(yes|no) :是chain_hostnames的别名,已不建议使用
keep_hostname(yes|no) :是否保留日志消息中保存的主机名称,否时,总是使用来源主机来作重写日志的主机名
use_dns(yes|no) :是否打开DNS查询功能,应使用防火墙保护使用syslog-ng的节点安全,并确认所有主机都是可以通过dns解释的,否则请关闭该选项。
use_fqdn(yes|no) :是否使用完整的域名
check_hostname(yes|no) :是否检查主机名有没有包含不合法的字符
bad_hostname(regexp) :可通过正规表达式指定某主机的信息不被接受
dns_cache(yes|no) :是否打开DNS缓存功能
dns_cache_expire(n) :DNS缓存功能打开时,一个成功缓存的过期时间
dns_cache_expire_failed(n) :DNS缓存功能打开时,一个失败缓存的过期时间
dns_cache_size(n) :DNS缓存保留的主机名数量
create_dirs(yes|no) :当指定的目标目录不存在时,是否创建该目录
dir_owner(uid) :目录的UID
dir_group(gid) :目录的GID
dir_perm(perm) :目录的权限,使用八进制方式标注,例如0644
owner(uid) :文件的UID
group(gid) :文件的GID
perm(perm) :文件的权限,同样,使用八进制方式标注
gc_busy_threshold(n) :当syslog-ng忙时,其进入垃圾信息收集状态的时间。一旦分派的对象达到这个数字,syslog-ng就启动垃圾信息收集状态。默认值是:3000。
gc_idle_threshold(n) :当syslog-ng空闲时,其进入垃圾信息收集状态的时间。一旦被分派的对象到达这个数字,syslog-ng就会启动垃圾信息收集状态,默认值是:100
log_fifo_size(n) :输出队列的行数
log_msg_size(n) :消息日志的最大值(bytes)
mark(n) :多少时间(秒)写入两行MARK信息供参考,目前没有实现
stats(n) :多少时间(秒)写入两行STATUS信息供,默认值是:600
sync(n) :缓存多少行的信息再写入文件中,0为不缓存,局部参数可以覆盖该值。
time_reap(n) :在没有消息前,到达多少秒,即关闭该文件的连接
time_reopen(n) :对于死连接,到达多少秒,会重新连接
use_time_recvd(yes|no) :宏产生的时间是使用接受到的时间,还是日志中记录的时间;建议使用R_的宏代替接收时间,S_的宏代替日志记录的时间,而不要依靠该值定义。

例如:

引用
options {
   sync (0);
   time_reopen (10);
   log_fifo_size (1000);
   long_hostnames (off);
   use_dns (no);
   use_fqdn (no);
   create_dirs (no);
   keep_hostname (yes);
};

四、部分函数的参数
syslog-ng除了有全局选项参数外,不同的函数还可以定义其参数,其中包括:
1、扩展file的宏

引用
HOST 日志消息的源发主机名。如果日志消息穿过几个主机,并且chain_hostname()功能已经打开,就使用第一个主机名。
FACILITY :日志消息来自的日志设备
PRIOPRITY/LEVEL :日志消息的优先级
PROGRAM :发送日志消息的程序
YEAR :发送日志消息的年份,这个宏既可以指定日志消息送出的时间,也可以指定日志消息收到的时间。这由use_time_recvd()选项控制
MONTH :发送日志消息的月份
DAY :发送日志消息的日子
HOUR :小时
MIN : 分钟
SEC :秒

2、file的参数
例如:log_file_size()、sync()、owner()、perm()等,请参考上面的全局设定
3、tcp和upd的参数
引用
ip(xxx.xxx.xxx.xxx): 定义绑定的IP地址
port(n):定义绑定的端口
max-connections(n) : 定义最大连接数
※TCP基于连接方式传输,不会造成日志丢失,而UDP则不同。但因为传统的syslog基于UDP的514端口,所以,UDP方式也经常会使用到。
另外,514也是rshell的默认端口,请注意冲突。

举例:
引用
destination d_mail { file("/var/log/maillog" sync(10)); };
这里定义的sync(10)会覆盖全局配置,表示若写入的日志数量达到10,才写入maillog文件。
五、关于垃圾收集状态
当满足一定的条件,syslog-ng即会进入垃圾收集状态,而暂时不再接受日志信息。这时,会造成非连接的传输协议的日志丢失(例如UDP)。通过设置下面两个选项可以控制:
引用
gc_idle_threshold(n) :
意思是,一旦被分派的对象到达这个数字,并且当syslog-ng空闲时(100微秒内没有日志消息到达)。此时,syslog-ng就会启动垃圾信息收集状态。
已分配的对象可通过-v命令行参数指定其的最小值。而syslog-ng这个值应该比较小,但比已分配的对象要大即可。
例如,空闲状态,syslog-ng会显示:
引用
Nov 13 16:35:35 syslogng syslog-ng[4510]: STATS: dropped 0
Nov 13 16:45:35 syslogng syslog-ng[4510]: STATS: dropped 0
当忙时:
引用
gc_busy_threshold(n) :当syslog-ng忙时,一旦分派的对象达到这个数字,syslog-ng就进入垃圾信息收集状态的时间。该值应该比较高,以保证正常情况下不会打断日志消息的收取。
六、参考资料
man syslog-ng.conf
man 8 syslog-ng


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/18637/showart_1146056.html

config syslog-ng

分享
标签: syslog  file  build  service  installed  分类: ESX 2011-04-27 15:51

    On the client side, you can use any Linux machine. Many have syslog-ng already installed, or you can download it from the web. Then do the following:

 

1. Edit /opt/syslog-ng/etc/syslog-ng.conf, adding a section similar to the following:

source network_tcp {
       tcp(port(2222)); #if you have error with this port, then just use the default 314.
#       tcp(port(514));
};

destination log_remote_tcp {
        file("/var/log/remote_tcp/$HOST_FROM/$YEAR-$MONTH/messages-$YEAR-$MONTH-$DAY"
                create_dirs(yes) frac_digits(3)
                template("$ISODATE $PROGRAM $MSGONLY\n")
                template_escape(no)
        );
};

log {
        source(network_tcp);
        destination(log_remote_tcp);
};

==or=======================================

source s_sys {

#       file ("/proc/kmsg" log_prefix("kernel: "));

        unix-stream ("/dev/log");

        internal();

        #udp(ip(0.0.0.0) port(514));

        udp(ip(172.16.29.167) port(514));  

        tcp(ip(172.16.29.167) port(514));  

};


#source network_tcp {

#      tcp(port(2222)); #if you have error with this port, then just use the default 314.

#      udp(port(514));

#};


destination log_remote_log {

#        file("/var/log/remote_tcp/$HOST_FROM/$YEAR-$MONTH/messages-$YEAR-$MONTH-$DAY"

     file("/var/log/HOSTS/$HOST/$YEAR/$MONTH/$DAY/$FACILITY$YEAR$MONTH$DAY"

   owner(root) group(root) perm(0600) dir_perm(0700) create_dirs(yes)); 

#          create_dirs(yes) frac_digits(3)

#                template("$ISODATE $PROGRAM $MSGONLY\n")

#                template_escape(no)

};

log {

        source(s_sys);

        destination(log_remote_log);

};

destination log_remote {

        file("/var/log/remote/$HOST_FROM/$YEAR-$MONTH/messages-$YEAR-$MONTH-$DAY"

                create_dirs(yes) frac_digits(3)

                template("$ISODATE $PROGRAM $MSGONLY\n")

                template_escape(no)

        );

};


log {

        source(s_sys);

        destination(log_remote);

};

==========================================

2. Restart the syslog-ng service: /etc/init.d/syslog-ng reload

 

Tips:

1. How to verify if syslog-ng is already installed because I tried

    /etc/init.d/syslog-ng status and it returns me 'no such file or directory exists' but I could find /etc/init.d/syslog

 

Answer: Use “ps –ef | grep syslog” to check if syslog-ng is up, otherwise use “rpm –qa” to find if the syslog-ng is installed

2. Assuming syslog-ng not installed, we downloaded syslog-ng-2.0.9-27.23.1.x86_64.rpm from corresponding VCVA build but would like to know where to put this file on installed VCVA ? (We are working from contractor site and cannot directly install/ point files from build-web location)

 Answer: You just need to install the rpm using “rpm –ivh *.rpm” to default location

 

3. As you mention, we could not detect /opt/syslog-ng/etc/syslog-ng.conf file but found /etc/syslog-ng.conf so we did the setup as mentioned below but no luck.

 

Even when I tried to restart syslog service, found below output:

Shutting down syslog services                                                                       done
Starting syslog services

Error resolving user; user='news'

Answer: You probably should start syslog-ng service not syslog.

sample:for RHEL5.2
[root@localhost etc]# vi syslog-ng.conf

# syslog-ng configuration file.
#
# This should behave pretty much like the original syslog on RedHat. But
# it could be configured a lot smarter.
#
# See syslog-ng(8) and syslog-ng.conf(5) for more information.
#
# 20000925 [email protected]
#
# Updated by Frank Crawford (<[email protected]>) - 10 Aug 2002
#       - for Red Hat 7.3
#       - totally do away with klogd
#       - add message "kernel:" as is done with klogd.
#
# Updated by Frank Crawford (<[email protected]>) - 22 Aug 2002
#       - use the log_prefix option as per Balazs Scheidler's email
#

options { sync (0);
          time_reopen (10);
          log_fifo_size (1000);
          long_hostnames (off);
          use_dns (no);
          use_fqdn (no);
          create_dirs (no);
          keep_hostname (yes);
        };

#
# At around 1999 some distributions have changed from using SOCK_STREAM
# to SOCK_DGRAM sockets, see these posts about the issue:
#
# http://www.security-express.com/archives/bugtraq/1999-q4/0071.html
# http://marc.theaimsgroup.com/?l=sysklogd&m=96989685607952&w=2
#
# libc and syslog clients generally automatically detect the socket type,
# so you are free to decide which of unix-stream or unix-dgram you want to use.
#
source s_sys { file ("/proc/kmsg" log_prefix("kernel: ")); unix-stream ("/dev/log"); internal();
udp(ip(172.16.29.75) port(2222));
#tcp(ip(172.16.29.75) port(2222));
};

#source network_tcp {
#       tcp(port(2222)); #if you have error with this port, then just use the default 314.
#       tcp(port(514));
#};

destination d_cons { file("/dev/console"); };
destination d_mesg { file("/var/log/messages"); };
destination d_auth { file("/var/log/secure"); };
destination d_mail { file("/var/log/maillog"); };
destination d_spol { file("/var/log/spooler"); };
destination d_boot { file("/var/log/boot.log"); };
destination d_cron { file("/var/log/cron"); };
destination d_mlal { usertty("*"); };

filter f_filter1     { facility(kern); };
filter f_filter2     { level(info) and
                     not (facility(mail)
                        or facility(authpriv) or facility(cron)); };
filter f_filter3     { facility(authpriv); };
filter f_filter4     { facility(mail); };
filter f_filter5     { level(emerg); };
filter f_filter6     { facility(uucp) or
                     (facility(news) and level(crit)); };
filter f_filter7     { facility(local7); };
filter f_filter8     { facility(cron); };

destination log_remote_tcp {
#        file("/var/log/remote_tcp/$HOST_FROM/$YEAR-$MONTH/messages-$YEAR-$MONTH-$DAY"
#                create_dirs(yes) frac_digits(3)
#                template("$ISODATE $PROGRAM $MSGONLY\n")
#                template_escape(no)

 file("/var/log/HOSTS/$HOST/$YEAR/$MONTH/$DAY/$FACILITY$YEAR$MONTH$DAY"
owner(root) group(root) perm(0600) dir_perm(0700) create_dirs(yes));
};

log {
        source(s_sys);
        destination(log_remote_tcp);
};

destination log_remote {
        file("/var/log/remote/$HOST_FROM/$YEAR-$MONTH/messages-$YEAR-$MONTH-$DAY"
                create_dirs(yes) frac_digits(3)
                template("$ISODATE $PROGRAM $MSGONLY\n")
                template_escape(no)
        );
};
[root@localhost etc]# cat syslog-ng.conf
# syslog-ng configuration file.
#
# This should behave pretty much like the original syslog on RedHat. But
# it could be configured a lot smarter.
#
# See syslog-ng(8) and syslog-ng.conf(5) for more information.
#
# 20000925 [email protected]
#
# Updated by Frank Crawford (<[email protected]>) - 10 Aug 2002
#       - for Red Hat 7.3
#       - totally do away with klogd
#       - add message "kernel:" as is done with klogd.
#
# Updated by Frank Crawford (<[email protected]>) - 22 Aug 2002
#       - use the log_prefix option as per Balazs Scheidler's email
#

options { sync (0);
          time_reopen (10);
          log_fifo_size (1000);
          long_hostnames (off);
          use_dns (no);
          use_fqdn (no);
          create_dirs (no);
          keep_hostname (yes);
        };

#
# At around 1999 some distributions have changed from using SOCK_STREAM
# to SOCK_DGRAM sockets, see these posts about the issue:
#
# http://www.security-express.com/archives/bugtraq/1999-q4/0071.html
# http://marc.theaimsgroup.com/?l=sysklogd&m=96989685607952&w=2
#
# libc and syslog clients generally automatically detect the socket type,
# so you are free to decide which of unix-stream or unix-dgram you want to use.
#
source s_sys { file ("/proc/kmsg" log_prefix("kernel: ")); unix-stream ("/dev/log"); internal(); 
udp(ip(172.16.29.75) port(2222)); 
#tcp(ip(172.16.29.75) port(2222)); 
};

#source network_tcp {
#       tcp(port(2222)); #if you have error with this port, then just use the default 314.
#       tcp(port(514));
#};

destination d_cons { file("/dev/console"); };
destination d_mesg { file("/var/log/messages"); };
destination d_auth { file("/var/log/secure"); };
destination d_mail { file("/var/log/maillog"); };
destination d_spol { file("/var/log/spooler"); };
destination d_boot { file("/var/log/boot.log"); };
destination d_cron { file("/var/log/cron"); };
destination d_mlal { usertty("*"); };

filter f_filter1     { facility(kern); };
filter f_filter2     { level(info) and
                     not (facility(mail)
                        or facility(authpriv) or facility(cron)); };
filter f_filter3     { facility(authpriv); };
filter f_filter4     { facility(mail); };
filter f_filter5     { level(emerg); };
filter f_filter6     { facility(uucp) or
                     (facility(news) and level(crit)); };
filter f_filter7     { facility(local7); };
filter f_filter8     { facility(cron); };

destination log_remote_tcp {
#        file("/var/log/remote_tcp/$HOST_FROM/$YEAR-$MONTH/messages-$YEAR-$MONTH-$DAY"
#                create_dirs(yes) frac_digits(3)
#                template("$ISODATE $PROGRAM $MSGONLY\n")
#                template_escape(no)

 file("/var/log/HOSTS/$HOST/$YEAR/$MONTH/$DAY/$FACILITY$YEAR$MONTH$DAY"
owner(root) group(root) perm(0600) dir_perm(0700) create_dirs(yes));  
};

log {
        source(s_sys);
        destination(log_remote_tcp);
};

destination log_remote {
        file("/var/log/remote/$HOST_FROM/$YEAR-$MONTH/messages-$YEAR-$MONTH-$DAY"
                create_dirs(yes) frac_digits(3)
                template("$ISODATE $PROGRAM $MSGONLY\n")
                template_escape(no)
        );
};

log {
        source(s_sys);
        destination(log_remote);
};


#log { source(s_sys); filter(f_filter1); destination(d_cons); };
log { source(s_sys); filter(f_filter2); destination(d_mesg); };
log { source(s_sys); filter(f_filter3); destination(d_auth); };
log { source(s_sys); filter(f_filter4); destination(d_mail); };
log { source(s_sys); filter(f_filter5); destination(d_mlal); };
log { source(s_sys); filter(f_filter6); destination(d_spol); };
log { source(s_sys); filter(f_filter7); destination(d_boot); };
log { source(s_sys); filter(f_filter8); destination(d_cron); };


如果不能正常接收syslog,可以尝试关闭linux和esxi的防火墙,或者更换一个未被占用的端口。
 
安装
cd eventlog-0.2.12
./configure --prefix=/data/workspace/eventlog
make 
make install
2)安装好eventlog后,就可以进行syslog-ng的步骤了:
a 安装脚本:
cd syslog-ng-3.3.4
export EVTLOG_CFLAGS="-I/data/workspace/eventlog/include/eventlog/"
export EVTLOG_LIBS="-levtlog -L/data/workspace/eventlog/lib"                                                                                                            
./configure  CFLAGS="-I/data/workspace/eventlog/include/eventlog/"  LDFLAGS="-L/data/workspace/eventlog/lib " --prefix=/data/workspace/syslog-ng
make
make install

 

你可能感兴趣的:(syslog-ng日志集中)