设置监听器

为下一代系统日志工具设置监听器实际上只需要在配置文件中添加几行。典型的监听器行看起来像这样:

source s_net { tcp((ip(127.0.0.1) port(1000) max-connections 5000)); udp (); };
source s_net = Network listener,表示网络监听器
tcp(ip(127.0.0.1) = Listen on localhost,表示监听本地主机。如果你有多块网卡,或者想指定某个IP地址与某块网卡进行绑定,将127.0.0.1更改成那块特定网卡的IP地址。
port (1000) = Listen to TCP port 1000,表示监听TCP端口1000。
max connections = Allow 5000 simultaneous connections (stops the dreaded 'run away server' syndrome) ,表示允许5000个同时连接杜绝可怕的‘失控服务器’综合征)
udp () = Some devices send their syslog messages via udp,表示一些设备通过udp发送系统日志消息,所以如果你无法指定TCP和端口号,就请启用udp。
encrypt(allow) = This could be an entire blog post in itself,表示这本身可能是整篇博文。下一代系统日志工具允许对系统日志消息采用加密基于TLS/证书)。
示例看起来像这样:
# Listen on TCP Port 1000 and UDP Port 514, Max 500
Connections source s_net {
tcp(port(1000) max-connections(500)); udp(););
目的地——有发就有收。在这里,发出去的必须在某个地方接收。一旦收到来自下一代系统日志工具服务器的消息,它就要发送到某地。因而syslog-ng.conf文件就有目的地这个部分。正如你所见,默认值包括本地机器上服务器消息的*nix目的地。但是入站消息又如何呢?它们发送到哪里?问得好!默认情况下,入站消息会将系统日志消息发送到在下一代系统日志工具指定的子系统。比如说,如果这个消息被归类成验证消息/var/log/auth),那么它会把消息转储到下一代系统日志工具的/var/log/auth.log文件中,附有相关信息主机名和日期/时间等)。

如果这实际上是你想要实现的,一堆服务器转储到与主服务器同一个文件上,那么我想任务完成了。但是下一代系统日志工具的功能绝不仅仅局限于此。目的地可能是平面文件、进入其他应用程序的管道、SQL数据库mysql、MS SQL和Oracle等)、远程日志服务器和Terminal Windows。我将着重探讨平面文件,假设你现在用的也是平面文件。

我搭建集中式系统日志服务器的方式可能与你的搭建方式不一样。我有一个文件夹含有每个主机名,来自主机名的系统日志位于该文件夹中。比如:/mount/syslog/macha和/mount/syslog/beag等等。Logrotate工具负责日志文件的压缩、删除将旧文件备份到远程服务器,以防万一)和清理。

我的目的地命令看起来像这样:
destination d_net_auth {
file("/var/log/syslog/remote/$HOSTNAME/auth.log"); }; destination d_net_cron {
file("/var/log/syslog/remote/$HOSTNAME/cron.log"); }; destination d_net_daemon
{ file("/var/log/syslog/remote/$HOSTNAME/daemon.log"); }; destination
d_net_kern { file("/var/log/syslog/remote/$HOSTNAME/kern.log"); }; destination
d_net_lpr { file("/var/log/syslog/remote/$HOSTNAME/lpr.log"); }; destination
d_net_mail { file("/var/log/syslog/remote/$HOSTNAME/mail.log"); }; destination
d_net_syslog { file("/var/log/syslog/remote/$HOSTNAME/syslog.log"); };
destination d_net_user { file("/var/log/syslog/remote/$HOSTNAME/user.log"); };
destination d_net_user { file("/var/log/syslog/remote/$HOSTNAME/uucp.log"); };
destination d_net_debug { file("/var/log/syslog/remote/$HOSTNAME/debug"); };
destination d_net_error { file("/var/log/syslog/remote/$HOSTNAME/error"); };
destination d_net_messages { file("/var/log/syslog/remote/$HOSTNAME/messages");
}; destination d_net_mailinfo {
file("/var/log/syslog/remote/$HOSTNAME/mail/mail.info"); }; destination
d_net_mailwarn { file("/var/log/syslog/remote/$HOSTNAME/mail/mail.warn"); };
destination d_net_mailerr {
file("/var/log/syslog/remote/$HOSTNAME/mail/mail.err"); };

从理论上来说,现在下一代系统日志工具服务器应该可以创建存放文件所必需的目录了在全局策略中已有指定),但有时我会遇到问题:目录无法顺利创建,下一代系统日志工具中的错误却在/var/log/errors中报告。为了以后减少麻烦和痛苦,我往往顺便创建好主机和日志文件,我丢失的任何信息会最后出现在/var/log/errors中。

如果你是资深的下一代系统日志工具用户,可能会想:我为何把本地主机目的地和远程异地客户端)目的地分开来,而从理论上来说我本该创建d_auth,把常用本地主机过滤器也放入到文件夹中。这么做的原因是,我想把本地主机系统日志流量与远程流量分开来——虽然配置行更多了,但对我来说更省事了。另外,当Linux子系统寻找把常用日志文件放在何处,就不会受到干扰。

过滤——下一代系统日志工具能够过滤消息,而这项功能标志着系统日志工具是否成熟。过滤功能让下一代系统日志工具与众不同。有了过滤功能,我就可以做下列事:过滤防火墙日志可以寻找某些关键词比如端口扫描),转储到单单一个文件夹,将分布式拒绝服务DDOS)攻击过滤到另一个文件夹中。我的VOIP适配器发送系统日志事件,我根据那些消息过滤到了每一个文件,而不是过滤到单个文件。过滤还让你可以指定根据哪几个主机进行过滤,过滤到多个目的地。不仅如此,你还可以在过滤时使用正则表达式。

可以像这样来创建过滤表达式:filter <identifier> { expression; };
<identifier>是你为过滤器赋予的名称。<expression>含有函数和布尔操作符and、or和not)。
就我的防火墙而言,示例如下:
filter firewall_ddos_filter { host("10.1.1.1") and match("Denial of Service"
value("MESSAGE")); };
该过滤器名为‘firewall_ddos_filter’,它监听来自10.1.1.1的入站系统日志消息,消息为‘Denial of Service’拒绝服务攻击)。为了完成过滤器,你需要一个日志语句:
log firewall_ddos_filter { source(s_net); filter
(firewall_ddos_filter);
destination(d_net_firewall_ddos); };

在上述目的地中,我为防火墙DDOS攻击、端口扫描等添加目的地。这样一来,把日志文件与不使用标准*nix日志功能的服务器/设备分离开来,或者让统管理员更容易过滤从防火墙出来的日志或者把许多防火墙过滤到一个日志)。

如果你想使用多个‘firewall’主机如示例),就不要添加它们,使用布尔操作符‘and’来创建日志/过滤器规则。这不管用。而是应该使用‘or’布尔操作符,就像这样:

filter firewall_ddos_filter { host("10.1.1.1") or host
("10.1.1.2") and match("Denial of Service" value("MESSAGE")
((; };
我的‘Default’过滤命令看起来像这样:
filter f_dbg { level(debug); };
filter f_info { level(info); };
filter f_notice{ level(notice); };
filter f_warn { level(warn); };
filter f_err { level(err);  };
filter f_crit { level(crit .. emerg); };

filter f_debug { level(debug) and not facility(auth,
authpriv, news, mail); };
filter f_error { level(err .. emerg) ; };
filter f_messages { level(info,notice,warn) and not
facility(auth,authpriv,cron,daemon,mail,news);
};

filter f_auth { facility(auth, authpriv) and not filter
(f_debug); };
filter f_cron { facility(cron) and not filter(f_debug); };
filter f_daemon { facility(daemon) and not filter
(f_debug); };
filter f_kern { facility(kern) and not filter(f_debug); };
filter f_lpr { facility(lpr) and not filter(f_debug);};
filter f_local { facility(local0, local1, local3, local4,
local5, local6, local7) and not filter(f_debug); };
filter f_mail { facility(mail) and not filter(f_debug); };
filter f_news { facility(news) and not filter(f_debug); };
filter f_syslog3 { not facility(auth, authpriv, mail) and
not filter(f_debug); };
filter f_user { facility(user) and not filter(f_debug); };
filter f_uucp { facility(uucp) and not filter(f_debug); };

filter f_cnews { level(notice, err, crit) and facility
(news); };
filter f_cother { level(debug, info, notice, warn) or
facility(daemon, mail); };

filter f_ppp { facility(local2) and not filter
(f_debug); };
filter f_console { level(warn .. emerg); };


相关内容