Chinaunix首页 | 论坛 | 博客
  • 博客访问: 236110
  • 博文数量: 49
  • 博客积分: 3040
  • 博客等级: 少校
  • 技术积分: 450
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-29 09:28
文章分类

全部博文(49)

文章存档

2011年(8)

2010年(16)

2009年(25)

分类: 网络与安全

2010-06-21 17:05:44

<序言>
我已经写过两篇实用型的博文,可以作为参考:
1) 如何将Linux主机设置成syslog服务器;
2) 使用cron和logrotate来管理日志文件。
     这篇文章主要介绍日志服务的基础知识。涉及日志的服务有两个,在Fedora9中是rsyslog服务和logrotate服务。前者负责写入日志,后者负责备份和删除旧日志,以及更新日志文件。
1. rsyslogd 服务
1.1 查看当前rsyslogd服务的状态

     在Fedora 9开始,负责写入日志信息的服务是rsyslogd,它的配置文件为:/etc/rsyslog.conf。我们检测一下当前这个服务是不是在运行。
     [flagonxia@airhouse etc]$ ps -ef | grep -v grep | grep rsyslog
     root      1584     1  0 10:33 ?        00:00:00 rsyslogd -c 3
      从上面命令的输出结果看到rsyslogd执行时使用的参数是-c 3。这个输入参数在文件/etc/sysconfig/rsyslog中指定。
      [flagonxia@airhouse sysconfig]$ less rsyslog
      # Options to syslogd
      # syslogd options are deprecated in rsyslog v3
      # if you want to use them, switch to compatibility mode 2 by "-c 2"
      SYSLOGD_OPTIONS="-c 3"
      也可以通过另一种方式,查看当前运行级别中,rsyslogd是否运行。
      [flagonxia@airhouse sysconfig]$ sudo chkconfig --list rsyslog
      rsyslog         0:off   1:off   2:on    3:on    4:on    5:on    6:off
      注意,这里的服务名是rsyslog。
1.2 配置文件/etc/rsyslog.conf
1.2.1 配置文件的基本信息
      配置文件/etc/rsyslog.conf中有很多内容,但最主要的是指定需要记录哪些服务和需要记录什么等级的信息。
      下面是rsyslog.conf的内容。
      # /etc/rsyslog.conf
      ... ...
      #### RULES ####
      # Log all kernel messages to the console.
      # Logging much else clutters up the screen.
      #kern.*                                                 /dev/console
      # Log anything (except mail) of level info or higher.
      # Don't log private authentication messages!
A    *.info;mail.none;authpriv.none;cron.none                /var/log/messages
      # The authpriv file has restricted access.
      authpriv.*                                              /var/log/secure
      # Log all the mail messages in one place.
B    mail.*                                                  -/var/log/maillog
 
      # Log cron stuff
      cron.*                                                  /var/log/cron
      # Everybody gets emergency messages
C      *.emerg                                                 *
      # Save news errors of level crit and higher in a special file.
D    uucp,news.crit                                       /var/log/spooler
      # Save boot messages also to boot.log
      local7.*                                                /var/log/boot.log     

     【注释】
      A:把所有大于info级别的信息都记录到/var/log/messages中,但不要记录mail,authpriv和cron服务产生的信息;
      B:把mail产生的信息都记录到/var/log/maillog中
      C:把所有服务输出的“大于”emergy级别的信息显示给每个在线的人,通过wall工具
      D:把uucp和news输出的大雨crit级别的信息记录到/var/log/spooler中
1.2.2 信息的等级及其在配置文件中指定的方式
     A 七种信息等级
        1)info
        2)notice
        3)warning或warn
        4)err或error
        5)crit
        6)alert
        7)emerg或panic:导致系统几乎要死机
     B 信息等级的指定方式
        1). xxx: 表示大于xxx级别的信息
        2).=xxx:表示等于xxx级别的信息
        3).!xxx:表示在xxx之外的等级的信息
2. logrotate服务
 
2.1 logrotate服务的启动方式
       logrotate是一个日志管理程序,用来把旧的日志文件删除(备份),并创建新的日志文件,这个过程称为“转储”。我们可以根据日志的大小,或者根据其使用的天数来转储。
       logrotate的执行由crond服务实现。在/etc/cron.daily目录中,有个文件logrotate,它实际上是个shell script,用来启动logrotate。logrotate程序每天由cron在指定的时间(/etc/crontab)启动。
       [flagonxia@airhouse cron.daily]$ less logrotate
       #!/bin/sh
       /usr/sbin/logrotate /etc/logrotate.conf
       EXITVALUE=$?
       if [ $EXITVALUE != 0 ]; then
       /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
       fi
       exit 0
       因此,使用ps是无法查看到logrotate的。如果它没有起来,就要查看一下crond服务有没有在运行。
       [flagonxia@airhouse logrotate.d]$ ps -ef | grep -v grep | grep cron
       root      1984     1  0 10:34 ?        00:00:00 crond
2.2 logrotate的配置文件/etc/logrotate.conf
       在执行logrotate时,需要指定其配置文件/etc/logrotate.conf。这个文件定义了如何转储日志文件的规则。如下:
       # see "man logrotate" for details
       # rotate log files weekly
       weekly
       # keep 4 weeks worth of backlogs
       rotate 4
       # create new (empty) log files after rotating old ones
       create
       # use date as a suffix of the rotated file
       dateext
       # uncomment this if you want your log files compressed
       #compress
       # RPM packages drop log rotation information into this directory
       include /etc/logrotate.d
       # no packages own wtmp and btmp -- we'll rotate them here
       /var/log/wtmp {
              monthly
              create 0664 root utmp
              rotate 1
        }
        /var/log/btmp {
              missingok
              monthly
              create 0600 root utmp
              rotate 1
         }
         # system-specific logs may be also be configured here.
      这个配置文件的注释写得很清楚,没有必要再罗嗦了。只想强调下面这行,它的作用包含存放在/etc/logrotate.d目录下面的配置文件,不可或缺。如果你安装了一个新的服务,它的日志转储的规则可以建立一个专门的配置文件,放在/etc/logrotate.d下面。它其实也因为下面的这句话,在logrotate服务启动时被读取。
       include /etc/logrotate.d
       这里有个例子:/etc/logrotate.d/syslog
       /var/log/messages /var/log/secure /var/log/maillog /var/log/spooler /var/log/boot.log /var/log/cron {
             sharedscripts
             postrotate
                  /bin/kill -HUP `cat /var/run/rsyslogd.pid 2> /dev/null` 2> /dev/null || true
             endscript
       }
      上面的配置对于/var/log/messages, /var/log/secure, /var/log/mailog/ /var/log/spooler, /var/log/boot.log, /var/log/cron都是适用的。
      注意,prerotate和postrotate必须和sharescripts...endscript一起用。上面的信息表示日志文件转储后,重启rsyslogd服务。
      每个存放在/etc/logrotate.d目录里的文件,都有上面格式的配置信息。在{}中定义的规则,如果与logrotate.conf中的冲突,以/etc/logrotatate.d/中的文件定义的为准。
 
 
阅读(2333) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~