syslog-ng应用详解

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

最近做一个新项目,要做日志集中收集和处理,拿到机器一看,这批机器用的都是SUSE10 SP2系统,日志服务器用的都是syslog-ng。之前写过写过一篇关于rsyslog的博文,目前也用不上了。总不能把这些机器都挨个装一遍rsyslog吧。只能硬着头皮学一下syslog-ng了。搜索了一下syslog-ng。发现这个服务还不错。下面我们就具体来了解一下吧。


简介

Syslog-ng是由Balabit IT Security Ltd.维护的一套开源的Unix和类Unix系统的日志服务套件。它是一个灵活的、可伸缩的系统日志记录程序。对于服务器日志集中收集,使用它是一个不错的解决方案。


Syslog-ng主要特性有:

  • 支持SSL/TSL协议

  • 支持将日志写入数据库中,支持的数据库有MySQL, Microsoft SQL (MSSQL),Oracle, PostgreSQL, and SQLite.

  • 支持标准的syslog协议

  • 支持filter、parse以及rewrite

  • 支持更多的平台

  • 更高的负载能力


Syslog-ng工作原理:

232919401.png


Syslog-ng工作模式:

  • Client mode

233007581.png

  • Relay mode

233021274.png

  • Server mode

233034386.png


系统环境:

Centos5.8 x86_64

安装syslog-ng

源码安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
mkdir  -p  /data/software
cd /data/software
wget http: //www .balabit.com /downloads/files/syslog-ng/open-source-edition/3 .3.11 /source/syslog-ng_3 .3.11. tar .gz
wget http: //www .balabit.com /downloads/files/syslog-ng/open-source-edition/3 .3.11 /source/eventlog_0 .2.12. tar .gz
tar  zxvfeventlog_0.2.12. tar .gz
cd  eventlog-0.2.12/
. /configure  --prefix= /usr/local/eventlog
make  &&  make  install
cd  /data/software
tar  zxvf syslog-ng_3.3.11. tar .gz
cd  syslog-ng-3.3.11/
. /configure  --prefix= /usr/local/syslog-ng  PKG_CONFIG_PATH= "/usr/local/eventlog/lib/pkgconfig"
make  &&  make  install
cp  contrib /init .d.RedHat  /etc/init .d /syslog-ng
chmod +x  /etc/init .d /syslog-ng
vim  /etc/init .d /syslog-ng

233050965.jpg


YUM安装

由于Centos5.8 x86_64本身的YUM源并没有syslog-ng,所以需要先安装EPEL源。

1
2
rpm -ivh http: //dl .fedoraproject.org /pub/epel/5/i386/epel-release-5-4 .noarch.rpm
yum -y  install  syslog-ng

设置开机启动项

1
2
3
4
chkconfig syslog off
service syslog stop
chkconfig syslog-ng on
service syslog-ng start

配置文件详解:

如果你的syslog-ng是yum或者RPM包安装的,那配置文件路径为/etc/syslog-ng/syslog-ng.conf。如果你是源码包编译安装的,那配置文件路径一般在安装路径下的etc目录当中。

整个配置文件大致分为全局配置、消息源、过滤器、消息目的地和日志路径几部分。

如下图所示:

233807285.jpg

全局配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
options { opt1; opt2; ... };
chain_hostnames( yes |no)  # 是否打开主机名链功能,打开后可在多网络段转发日志时有效
long_hostnames( yes |no)  # 是chain_hostnames的别名,已不建议使用
keep_hostname( yes |no)  # 是否保留日志消息中保存的主机名称
use_dns( yes |no)  # 是否打开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_的宏代替日志记录的时间,而不要依靠该值定义。

消息源:

1
2
3
4
5
6
7
8
9
10
11
source  <sourcename> {sourcedriver params; sourcedriver params; ... };
internal()  # syslog-ng内部产生的消息
file ()  # 从指定的文件读取日志信息
pipe()  # 从指定的管道,读取日志信息
fifo()  # 从指定的FIFO设备,读取日志信息
program()  # 打开指定的应用程序,从它的标准输出读取消息
sun-stream(), sun-streams()  # 在solaris系统中,打开一个(多个)指定的STREAM设备,从其中读取日志消息
tcp(), tcp6()  # 在指定的TCP端口接收日志消息
udp(), udp6()  # 在指定的UDP端口接收日志消息
unix-dgram()  # 打开指定的SOCK_DGRAM模式的unix套接字,接收日志消息
unix-stream()  # 打开指定的SOCK_STREAM模式的unix套接字,接收日志消息

过滤器:

1
2
3
4
5
6
filter <filtername> {expression; };
facility()  # 根据facility(设备)选择日志消息
filter()  # 调用另一条过滤规则
host()  # 日志消息的主机名是否和一个正则表达式匹配
level() or priority()  # 根据level(优先级)选择日志消息
match()  # 对日志消息的内容进行正则匹配

消息目的地:

1
2
3
4
5
6
7
8
9
10
11
destination <destname> {destdriver params; destdriver params; ... ; };
file ()  # 把日志消息写入指定的文件
pipe()  # 把日志消息发送到指定的管道
fifo()  # 把日志消息发送到指定的FIFO设备
program()  # 启动指定的程序,并把日志消息发送到该进程的标准输入
sql()  # 把日志消息写入数据库,适用于3.x版本及更高版本的syslog-ng
tcp() and tcp6()  # 把日志消息发送到指定的TCP端口
udp() and udp6()  # 把日志消息发送到指定的UDP端口
unix-dgram()  # 把日志消息写入指定的SOCK_DGRAM模式的unix套接字
unix-stream()  # 把日志消息写入指定的SOCK_STREAM模式的unix套接字
usertty()  # 把日志消息发送到已经登陆的指定用户终端窗口

消息路径:

1
log {  source  S1;  source  S2; ...filter F1; filter F2; ... destination D1; destination D2; ... };

配置实例:

  • 单机配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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 );
};
source  s_sys {
file  ( "/proc/kmsg" log_prefix( "kernel: " ));
unix-stream ( "/dev/log" );
internal();
# udp(ip(0.0.0.0) 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"  sync (10)); };
destination d_spol { file ( "/var/log/spooler" ); };
destination d_boot { file ( "/var/log/boot.log" ); };
destination d_cron { file ( "/var/log/cron" ); };
destination d_kern {  file ( "/var/log/kern" );};
destination d_mlal {usertty( "*" ); };
filter f_kernel     { facility(kern); };
filter f_default    { level(info..emerg) and
not (facility(mail)
or facility(authpriv)
or facility( cron )); };
filter f_auth       { facility(authpriv); };
filter f_mail       { facility(mail); };
filter f_emergency  { level(emerg); };
filter f_news       { facility(uucp) or
(facility(news)
andlevel(crit..emerg)); };
filter f_boot   { facility(local7); };
filter f_cron   { facility( cron ); };
#log { source(s_sys);filter(f_kernel); destination(d_cons); };
log {  source (s_sys);filter(f_kernel); destination(d_kern); };
log {  source (s_sys);filter(f_default); destination(d_mesg); };
log {  source (s_sys);filter(f_auth); destination(d_auth); };
log {  source (s_sys);filter(f_mail); destination(d_mail); };
log {  source (s_sys);filter(f_emergency); destination(d_mlal); };
log {  source (s_sys);filter(f_news); destination(d_spol); };
log {  source (s_sys);filter(f_boot); destination(d_boot); };
log {  source (s_sys);filter(f_cron); destination(d_cron); };
  • C/S模式:

服务端配置:

1
2
3
4
5
6
7
8
source  s_sys {
file  ( "/proc/kmsg" log_prefix( "kernel: " ));
unix-stream ( "/dev/log" );
internal();
udp(ip(0.0.0.0) port(514));
};
destination d_mesg { file ( "/var/log/messages" ); };
log {  source (s_sys);destination(d_mesg); };

客户端配置:

1
2
3
4
5
6
7
source  s_sys {
file  ( "/proc/kmsg" log_prefix( "kernel: " ));
unix-stream ( "/dev/log" );
internal();
};
destination d_mesg {udp(ip(0.0.0.0) port(514));};
log {  source (s_sys);destination(d_mesg); };

日志入库:

创建需要的库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
CREATE  DATABASE  syslog;
USE syslog;
CREATE  TABLE  logs (
host  varchar (32)  default  NULL ,
facility  varchar (10) defaultNULL,
priority  varchar (10) defaultNULL,
level  varchar (10)  default  NULL ,
tag  varchar (10)  default  NULL ,
date  date  default  NULL ,
time  time  default  NULL ,
program  varchar (15)  default  NULL ,
msg text,
seq  int (10) unsigned  NOT  NULLauto_increment,
PRIMARY  KEY  (seq),
KEY  host (host),
KEY  seq (seq),
KEY  program (program),
KEY  time  ( time ),
KEY  date  ( date ),
KEY  priority (priority),
KEY  facility (facility)
) TYPE=MyISAM;
1
mkfifo  /tmp/mysql .pipe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
source  s_sys {
file  ( "/proc/kmsg" log_prefix( "kernel: " ));
unix-stream ( "/dev/log" );
internal();
udp(ip(0.0.0.0) port(514));
};
destination d_mysql {
program( "/usr/bin/mysql-uroot syslog < /tmp/mysql.pipe" );
pipe( "/tmp/mysql.pipe"
template("INSERT INTO logs(host, facility, priority, level, tag,  date ,
time , program, msg) VALUES ( '$HOST' '$FACILITY' '$PRIORITY' '$LEVEL' , '$TAG' ,
'$YEAR-$MONTH-$DAY' , '$HOUR:$MIN:$SEC' '$PROGRAM' '$MSG'  );\n") template-escape( yes ));
};
log {  source (s_sys);destination(d_mysql); };


到这里为止,syslog-ng常用的配置方法基本上都有了。大家可以尝试安装使用一下,只要灵活配置好配置文件,syslog-ng不光是可以收集系统日志,还可以收集各类服务日志等等。如果大家在使用当中有什么问题欢迎和我交流。O(∩_∩)O~



本文转自 cyr520 51CTO博客,原文链接:http://blog.51cto.com/cyr520/1245650

相关文章
|
缓存 Ubuntu 网络协议
syslog配置
syslog配置
322 0
syslog配置
|
安全
syslog
1、void openlog(const char *ident, int option, int facility); 第一个参数ident将是一个标记,ident所表示的字符串将固定地加在每行日志的前面以标识这个日志,通常就写成当前程序的名称以作标记。
1084 0
|
安全 Unix Python
|
缓存 监控 网络协议
|
Ubuntu Unix Linux