日期:2014-05-16  浏览次数:20711 次

关于linux中 syslog-ng 如何在转发时修改其facility以及level
      国内关于Syslog-ng的内容比较少,就是找到了也都是些许的只言片语,或者都仅仅是一些简简单单的配置facility或则和level以及destination等。

      这两天碰到一个问题,就是在日志转发时,需要更改收到的日志的facility和level,结果中文文档几乎木有,英文的文档倒是碰到一些,兴奋之余,一打开傻眼了。。。好几个都是问how to change the facility and level的,然后在balabit中看到回复如下:
"Simply because we're not there yet and because I didn't feel it that
important when we implemented the rewrite functionality."。。。

原来还木有实现这个功能啊,直接修改看来是行不通了,然后去下载“syslog-ng-v2.0-guide-admin-en.pdf”学习Syslog-ng的这本书可真是指明灯啊。终于找到解决方案:利用Syslog-ng中的template可以完美解决此问题

实例如下, 比如当我收到facility为local 6,level为debug的日志时,我需要将其facility改为local0, 同时 level 改为info然后存储到本机,实现如下:

filter f_local6 {facility(local6);};
filter f_level  {level(debug);};
destination d_template1 {udp('127.0.0.1' port(514) template("<166>$DATE $HOST $MSGHDR$MSG\n"));
};
log { source(src);filter(f_tag_acs);filter(f_local6);destination( d_template1);};


同时发往的目的地服务器(此处问本机127.0.0.1) 也要配置接收log,配置如下:

source src          { 
       		    file("/proc/kmsg"); 
		    unix-stream("/dev/log" max-connections(256)); 
		    internal(); };

source s_remote { udp(ip(0.0.0.0) port(514)); };


filter f_facility{ facility (local4 ); };
filter f_level{ level (info ); };
filter f_tag {  match('acs')  ; };
destination dest{ file('/var/log/filter1.log');};
log {
	source(src);
	source(s_remote);
	filter(f_facility;
	filter(f_level);
	filter(f_tag);
	destination(dest);
   };



流程如下:
1. 本机收到属性为local6.debug的日志,
2.发往d_template1 由于其应用规则为:{udp('127.0.0.1' port(514) template("<166>$DATE $HOST $MSG\n")); 故将其属性转换为local4.info,同时应用其他规则,
3.将应用新规则的log转发给127.0.0.1,
4.本机的其他过滤器dest进行接收。
此过程完成也就将log的facility和level修改并存储了。

此处转换的核心是template中的规则,template("<166>$DATE $HOST $MSG\n"); , 下面进行一个大致的说明:
166 代表的意思是local4.info, 这个是PRI标识,首先请看下表,这是syslog-ng中预定义的。
Numerical Code Facility
0 kernel messages
1 user-level messages
2 mail system
3 system daemons
4 security/authorization messages
5 messages generated internally by syslogd
6 line printer subsystem
7 network news subsystem
8 UUCP subsystem
9 clock daemon
10 security/authorization messages
11 FTP daemon
12 NTP subsystem
13 log audit
14 log alert
15 clock daemon
16-23 locally used facilities (local0-local7)

左边是值,右边是对应的facility,下表的level也是如此。
Numerical Code Severity
0 Emergency: system is unusable
1 Alert: action must be taken immediately
2 Critical: critical conditions
3 Error: error conditions
4 Warning: warning conditions
5 Notice: normal but significant condition
6 Informational: informational messages
7 Debug: debug-level messages

PRI 就等于facility * 8 + level, 166 = local4*8 + info,即166代表local4.info
至于$DATE就比较简单了,代表日期, $MSG代表日志内容, $HOST代表主机。

时间有限,暂时先写到这里吧~~