Linux系统日志管理


一、连接时间的日志
 
    连接时间日志一般由/var/log/wtmp和/var/run/utmp这两个文件记录,不过这两个文件都无法使用tail或cat命令直接查看。该文件由系统自动更新。Linux提供了如w, who, finger, id, last, lastlog,ac等命令读取这部分的信息。
 
 
 
1
ggd543@ubuntu:/home/test$ w  # 显示当前有哪些用户登陆了系统,并且正在做什么
2
 23:20:44 up 1 day,  1:20,  2 users,  load average: 0.00, 0.01, 0.05
3
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
4
test     pts/0    192.168.195.1    20:21    2:59m  0.45s  0.45s -bash
5
test     pts/1    192.168.195.1    23:00    0.00s  1.45s  0.07s sshd: test [priv]
1
ggd543@ubuntu:~$ who # 显示当前有哪些用户已登陆系统
2
ggd543   pts/0        Feb 25 23:22 (192.168.195.1)
3
test     pts/1        Feb 25 23:00 (192.168.195.1)
1
ggd543@ubuntu:~$ finger   # 作用同whom命令
2
Login     Name       Tty      Idle  Login Time   Office     Office Phone
3
ggd543    ggd543     pts/0          Feb 25 23:22 (192.168.195.1)
4
test                 pts/1          Feb 25 23:00 (192.168.195.1)
01
ggd543@ubuntu:/home/test$ ac -p # 查看系统中每个用户连接到系统的累计时间(单位:小时)
02
        test                                39.37
03
        portaluser                          33.44
04
        ggd543                              88.59
05
 
06
ggd543@ubuntu:/home/test$ ac -a   # 显示系统中所有用户连接到系统的累计总时间(单位:小时)
07
        total      161.42
08
 
09
 
10
 
11
ggd543@ubuntu:/home/test$ ac -d    # 查看每天系统中所有用户连接到系统的累计总时间(单位:小时)
12
Feb  4  total        1.55
13
Feb  7  total       33.50
14
Feb  8  total       29.55
15
Feb  9  total       26.46
16
Feb 10  total        4.00
17
Feb 11  total        9.33
18
Feb 20  total       12.04
19
Feb 21  total       18.26
20
Feb 24  total        1.64
21
Today   total       25.12
 
关于这几个命令的更多用法,请查看man手册。
 
二、进程监控日志
        进程统计监控日志对于监控用户的操作指令是非常有效的。当服务器经常发生无故关机或文件无故被删除的时候,可以通过查看该日志分析一些原因.
1
ggd543@ubuntu:/home/test$ sudo accton on   # 开启进程日志统计
2
Turning on process accounting, file set to the default '/var/log/account/pacct'.
3
ggd543@ubuntu:/home/test$ sudo accton off  # 关闭进程日志统计
4
Turning off process accounting.
5
ggd543@ubuntu:/home/test$ sudo accton on
6
Turning on process accounting, file set to the default '/var/log/account/pacct'.
7
ggd543@ubuntu:/home/test$ lastcomm        # 查看进程统计日志情况
可以通过sudo accton $log_file 指定进程统计日志
 
三、系统和服务日志
 
        系统日志服务是由一个名为syslog的服务管理的,比如下面的日志文件都是由syslog日志服务驱动的:     
/var/log/lastlog :记录最后一次用户成功登陆的时间、登陆IP等信息
/var/log/messages :记录Linux操作系统常见的系统和服务错误信息
/var/log/secure :Linux系统安全日志,记录用户和工作组变坏情况、用户登陆认证情况
/var/log/btmp :记录Linux登陆失败的用户、时间以及远程IP地址
/var/log/cron :记录crond计划任务服务执行情况
syslog服务由配置文件/etc/syslog.conf (ubuntu 11.10是/etc/rsyslog.conf, 而该文件会包含/etc/rsyslog.d目录下的所有辅助配置文件).  /etc/syslog.conf的内容格式为:
1
消息类型.错误级别    动作域
消息类型:auth,authpriv,security,cron, daemon, kern, lpr,mail, news, syslog, user, uucp, local0~local7
错误级别(8级,从低到高):  debug,info,notice,warning|warn;err|error;crit,alert,emerg|panic
动作域: file,user,console,@remote_ip
 
比如
 
 
1
*.info;mail.none;authpriv.none;cron.none /var/log/messages
表示info级别的任何消息都发送到/var/log/messages日志文件,但邮件系统、验证系统和计划任务的错误级别信息就除外,不发送(none表示禁止)。又比如:
 
1
cron.* /var/log/cron
表示所有级别的cron信息发到/var/log/cron文件。 而
1
*.emerg *
 表示emerg错误级别(危险状态)的所有消息类型发给所有用户
        假如需要将一台ubuntu系统(假设IP为10.123.76.11)上的所有info级别的auth信息发给一台日志服务器(假设该系统为RHEL 5),则需要在ubuntu系统的/etc/rsyslog.d/50-default.conf中添加一行配置:
 
1
auth.info @10.123.76.11
并且在日志服务器的/etc/syslog.conf中添加如下的配置:
1
SYSLOGD_OPTIONS="-m 0 -r" //只要在这里添加“-r”就行咯
2
KLOGD_OPTIONS="-x"
3
SYSLOG_UMASK=077
然后重启日志服务器的syslog服务
1
service syslog restart
        随着时间的推移,日志文件会变得越来越大.当日志文件的容量超过一定大小时,会对系统的性能产生影响;有时我们也系统将日志文件做个备份。因此我们需要将日志转储。日志转储方式有每年转储、每月转储、每周转储或者达到一定大小转储等。Linux中使用logrotate命令进行日志转储。可以配合cron计划任务轻松实现日志文件的定时转储, /etc/logrotate.conf提供了日志转储的相关配置.下面对该配置文件的一些设置项进行解释:
 
01
[root@xhot ~]# cat /etc/logrotate.conf
02
  # see "man logrotate" for details //可以查看帮助文档
03
  # rotate log files weekly
04
  weekly //按每周转储
05
  # keep 4 weeks worth of backlogs
06
  rotate 4 // 最多转储4次,由于每次转储按周进行,因此只保留最近4周的日志内容
07
  # create new (empty) log files after rotating old ones
08
  create //当转储后的文件不存在时创建它
09
  # uncomment this if you want your log files compressed
10
  #compress //以压缩方式转储
11
  # RPM packages drop log rotation information into this directory
12
  include /etc/logrotate.d //其他日志文件的转储方式,包含在该目录下(自定义日志转储)
13
 
14
  # no packages own wtmp -- we'll rotate them here
15
  /var/log/wtmp { //设置/var/log/wtmp日志文件的转储参数
16
  monthly //每月转储
17
  create 0664 root utmp //转储后文件不存在时创建它,文件所有者为root,
18
  所属组为utmp,对应的权限为0664
19
  rotate 1 //转储一次
20
  }
21
 
22
  # system-specific logs may be also be configured here.
举两个例子:
1) 为/var/log/news/目录下的所有文件设置转储参数,每周转储,转储2次,转储时将老的日志文件放到/var/log/news/old目录下,若日志文件不存在,则跳过。完成后重启news新闻组服务,转储时不压缩。那么可以在/etc/logrotate.conf文件的最后添加如下:
 
01
/var/log/news/*{
02
  monthly
03
  rotate 2
04
  olddir /var/log/news/old
05
  missingok
06
  postrotate
07
  kill -HUP `cat /var/run/inn.pid`
08
  endscript
09
  nocompress
10
}
2) :为/var/log/httpd/access.log和/var/log/httpd/error.log日志设置转储参数。转储5次,转储时发送邮件给root@localhost用户,当日志文件达到100KB时才转储,转储后重启httpd服务,那么可以直接在/etc/logrotate.conf文件的最后添加如下:
1
/var/log/httpd/access.log /var/log/http/error.log{
2
  rotate 5
3
  mail root@localhost
4
  size=100k
5
  sharedscripts
6
  /sbin/killall -HUP httpd
7
  endscript
8
}
最后记得重启syslog服务

相关内容

    暂无相关文章