概念
一台服务器的日志对系统工程师来说是至关重要的,一旦服务器出现故障或被入侵,我们需要查看日志来定位问题的关键所在,所以说对于线上跑的服务器而言日志应该合理的处理及管理.下面来介绍下linux系统的syslog日志服务器.
几乎所有的网络设备都可以通过syslog协议,将日志信息以用户数据报协议(UDP)方式传送到远端服务器,远端接收日志服务器必须通过syslogd监听UDP端口514,并根据 syslog.conf配置文件中的配置处理本机,接收访问系统的日志信息,把指定的事件写入特定文件中,供后台数据库管理和响应之用。
- syslog详解
- syslog简介
syslog 系统日志,记录linux系统启动及运行的过程中产生的信息,rhel5.x系统上默认自带了syslog 其配置文件是/etc/syslog.conf
syslog 默认有两个守护进程,klogd,syslogd,
klogd 进程是记录系统运行的过程中内核生成的日志,而在系统启动的过程中内核初始化过程中 生成的信息记录到控制台(/dev/console)当系统启动完成之后会把此信息存放到/var/log/dmesg文件中,我可以通过cat /var/log/dmesg查看这个文件,也可以通过dmesg命令来查看
syslogd 进程是记录非内核以外的信息
而为什么需要两个守护进程呢?是因为内核跟其他信息需要记录的详细程度及格式的不同
我们使用ps命令可以看到syslog的两个守护进程
|
|
修改日志服务器/etc/sysconfig/syslog文件:
|
|
修改SYSLOGD_OPTIONS=”-r -x -m 0”
-r : 开接受外来日志消息的功能,其监控514 UDP端口;
-x : 关闭自动解析对方日志服务器的FQDN信息,这能避免DNS不完整所带来的麻烦;
-m : 修改syslog的内部mark消息写入间隔时间(0为关闭),例如240为每隔240分钟写入一次“–MARK–”信息;
-h : 默认情况下,syslog不会发送从远端接受过来的消息到其他主机,而使用该选项,则把该开关打开,所有接受到的信息都可根据syslog.conf中定义的@主机转发过去
上面通过ps命令可以看到syslog的两个守护进程,而这两个守护进程是共用一个配置文件/etc/syslog.conf,下面介绍下其配置文件
- syslog配置文件详解
配置文件定义格式为 facility.priority action
facility,可以理解为日志的来源或设备目前常用的facility(设备)有以下及中
1,auth # 认证相关的
2,authpriv # 权限,授权相关的
3,cron # 任务计划相关的
4,daemon # 守护进程相关的
5,kern # 内核相关的
6,lpr # 打印相关的
7,mail # 邮件相关的
8,mark # 标记相关的
9,news # 新闻相关的
10,security # 安全相关的,与auth 类似
11,syslog # syslog自己的
12,user # 用户相关的
13,uucp # unix to unix cp 相关的
14,local0 到 local7 # 用户自定义使用
15, # 表示所有的facility
等…..
priority(log level)日志的级别,一般有以下几种级别(从低到高) debug # 程序或系统的调试信息 info # 一般信息, notice # 不影响正常功能,需要注意的消息 warning/warn # 可能影响系统功能,需要提醒用户的重要事件 err/error # 错误信息 crit # 比较严重的 蓝色警报 alert # 必须马上处理的 橙色警报 emerg/panic # 会导致系统不可用的 红色警报 * # 表示所有的日志级别 none # 跟* 相反,表示啥也没有
action(动作)日志记录的位置 系统上的绝对路径 # 普通文件 如: /var/log/xxx | # 管道 通过管道送给其他的命令处理 终端 # 终端 如:/dev/console @HOST # 远程主机 如: @10.0.0.1 用户 # 系统用户 如: root * # 登录到系统上的所有用户,一般emerg级别的日志是这样定义的
定义格式例子:
mail.info /var/log/mail.log # 表示将mail相关的,级别为info及# info以上级别的信息记录到/var/log/mail.log文件中 auth.=info @10.0.0.1 # 表示将auth相关的,基本为info的信息记录到10.0.0.1主机上去 # 前提是10.0.0.1要能接收其他主机发来的日志信息 user.!=error # 表示记录user相关的,不包括error级别的信息 user.!error # 与user.error相反 *.info # 表示记录所有的日志信息的info级别 mail.* # 表示记录mail相关的所有级别的信息 *.* # 你懂的. cron.info;mail.info # 多个日志来源可以用";" 隔开 cron,mail.info # 与cron.info;mail.info 是一个意思 mail.*;mail.!=info # 表示记录mail相关的所有级别的信息,但是不包括info级别的
接下来去翻译下rhel5.x系统上自带的syslog的配置文件/etc/syslog.conf
表示将所有facility的info级别,但不包括mail,authpriv,cron相关的信息,记录到 /var/log/messages文件
*.info;mail.none;authpriv.none;cron.none /var/log/messages
表示将权限,授权相关的所有基本的信息,记录到/var/log/secure文件中.这个文件的权限是600
authpriv.* /var/log/secure
表示将mail相关的所有基本的信息记录到/var/log/maillog文件中,可以看到路径前面有一个”-“
“-“ 表示异步写入磁盘,
mail.* -/var/log/maillog
表示将任务计划相关的所有级别的信息记录到/var/log/cron文件中
cron.* /var/log/cron
表示将所有facility的emerg级别的信息,发送给登录到系统上的所有用户
.emerg
表示将uucp及news的crit级别的信息记录到/var/log/spooler文件中
uucp,news.crit /var/log/spooler
# 表示将local7的所有级别的信息记录到/var/log/boot.log文件中,
# 上面说过local0 到local7这8个是用户自定义使用的,这里的local7记录的是系统启动相关的信息
local7.* /var/log/boot.log
syslog默认记录的日志格式有四个字段,时间标签 主机 子系统名称 消息
可以使用tail /var/log/messages 看下
syslog的介绍到这里基本上已经完成了,下面来瞧瞧syslog的升级版,syslog-ng俗称syslog下一代日志服务器,到底有啥好的呢…..
- syslog-ng详解
syslog-ng简介
syslog-ng (syslog-Next generation) 是syslog的升级版,syslog-ng有两个版本,一个是收费的,一个是开源的,那么作为syslog的下一代产品,功能是可想而知,肯定比syslog的功能强大的多,如高性能
可靠的传输
支持多平台
高可靠性
众多的用户群体
强大的日志过滤及排序
事件标签和关联性
支持最新的IETF标准
等….
开源版本的主页 http://www.balabit.com/network-security/syslog-ng/opensource-logging-system/overviewsyslog-ng的安装
rhel5.x的系统上默认没有使用syslog-ng来记录日志的,需要使用的话,需要自己编译安装,安装方法如下
|
|
#!/bin/bash
#
# chkconfig: - 60 27
# description: syslog-ng SysV script.
. /etc/rc.d/init.d/functions
syslog_ng=/usr/local/syslog-ng/sbin/syslog-ng
prog=syslog-ng
pidfile=/usr/local/syslog-ng/var/syslog-ng.pid
lockfile=/usr/local/syslog-ng/var/syslog-ng.lock
RETVAL=0
STOP_TIMEOUT=${STOP_TIMEOUT-10}
start() {
echo -n $"Starting $prog: "
daemon --pidfile=$pidfile $syslog_ng $OPTIONS
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch ${lockfile}
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc -p $pidfile -d $STOP_TIMEOUT $syslog_ng
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f $lockfile $pidfile
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p $pidfile $syslog_ng
RETVAL=$?
;;
restart)
stop
start
;;
*)
echo $"Usage: $prog {start|stop|restart|status}"
RETVAL=2
esac
exit $RETVAL
------------------------------------------------------------
|
|
chmod a+x /etc/init.d/syslog-ng
killall syslogd
chkconfig --add syslog-ng
chkconfig syslog-ng on
service syslog-ng start
|
|
一条日志的处理流程大概是这样的,如下
首先是 “日志的来源 source s_name { … };”
然后是 “过滤规则 filter f_name { … };”
再然后是 “消息链 log { source(s_name); filter(f_name); destination(d_name) };”
最后是 “目标动作 destination d_name { … };”
这样以来一条日志就根据你的意思来处理了,需要注意的是一条日志消息过了之后,会匹配定义的所有配置,并不是匹配到以后就不再往下匹配了.
syslog-ng配置文件例子
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98$syslog-ng_path/etc/syslog-ng.conf 内容如下
options { long_hostnames(off); sync(0); perm(0640); stats(3600); };
source src {
internal();
unix-dgram("/dev/log");
# 表示日志来源为本机udp的514端口,
udp(ip("0.0.0.0") port(514));
};
filter f_iptables { facility(kern) and match("IN=") and match("OUT="); };
filter f_console { level(warn) and facility(kern) and not filter(f_iptables)
or level(err) and not facility(authpriv); };
filter f_newsnotice { level(notice) and facility(news); };
filter f_newscrit { level(crit) and facility(news); };
filter f_newserr { level(err) and facility(news); };
filter f_news { facility(news); };
filter f_mailinfo { level(info) and facility(mail); };
filter f_mailwarn { level(warn) and facility(mail); };
filter f_mailerr { level(err, crit) and facility(mail); };
filter f_mail { facility(mail); };
filter f_cron { facility(cron); };
filter f_local { facility(local0, local1, local2, local3,
local4, local6, local7); };
filter f_acpid_full { match('^acpid:'); };
filter f_acpid { level(emerg..notice) and match('^acpid:'); };
filter f_acpid_old { match('^\[acpid\]:'); };
filter f_netmgm { match('^NetworkManager:'); };
filter f_messages { not facility(news, mail) and not filter(f_iptables); };
filter f_warn { level(warn, err, crit) and not filter(f_iptables); };
filter f_alert { level(alert); };
destination console { pipe("/dev/tty10" owner(-1) group(-1) perm(-1)); };
log { source(src); filter(f_console); destination(console); };
destination xconsole { pipe("/dev/xconsole" owner(-1) group(-1) perm(-1)); };
log { source(src); filter(f_console); destination(xconsole); };
destination newscrit { file("/var/log/news/news.crit"
owner(news) group(news)); };
log { source(src); filter(f_newscrit); destination(newscrit); };
destination newserr { file("/var/log/news/news.err"
owner(news) group(news)); };
log { source(src); filter(f_newserr); destination(newserr); };
destination newsnotice { file("/var/log/news/news.notice"
owner(news) group(news)); };
log { source(src); filter(f_newsnotice); destination(newsnotice); };
destination mailinfo { file("/var/log/mail.info"); };
log { source(src); filter(f_mailinfo); destination(mailinfo); };
destination mailwarn { file("/var/log/mail.warn"); };
log { source(src); filter(f_mailwarn); destination(mailwarn); };
destination mailerr { file("/var/log/mail.err" fsync(yes)); };
log { source(src); filter(f_mailerr); destination(mailerr); };
destination mail { file("/var/log/mail"); };
log { source(src); filter(f_mail); destination(mail); };
destination acpid { file("/var/log/acpid"); };
destination null { };
log { source(src); filter(f_acpid); destination(acpid); flags(final); };
log { source(src); filter(f_acpid_full); destination(null); flags(final); };
log { source(src); filter(f_acpid_old); destination(acpid); flags(final); };
destination netmgm { file("/var/log/NetworkManager"); };
log { source(src); filter(f_netmgm); destination(netmgm); flags(final); };
destination localmessages { file("/var/log/localmessages"); };
log { source(src); filter(f_local); destination(localmessages); };
destination messages { file("/var/log/messages"); };
log { source(src); filter(f_messages); destination(messages); };
destination firewall { file("/var/log/firewall"); };
log { source(src); filter(f_iptables); destination(firewall); };
destination warn { file("/var/log/warn" fsync(yes)); };
log { source(src); filter(f_warn); destination(warn); };
filter f_ha { facility(local5); };
destination hamessages { file(/var/log/ha); };
log { source(src); filter(f_ha); destination(hamessages); };
参考:http://ant595.blog.51cto.com/5074217/1080922http://iminmin.blog.51cto.com/689308/437168http://wenku.baidu.com/link?url=uZwXNVwxcAFCb26Ql3VP2Myr3t2oScepMJTEugvz0Cn6UB_5fdbJtQ-9_Rw71TdBw2tdZnkgRguCXk1TH_XWLdqr8wUIPeY_muf4f9aj8va
(如果侵害了原作者权益,请及时告知,会立即删除)