zoukankan      html  css  js  c++  java
  • Linux系统日志管理 新风宇宙

     

      

    Linux系统日志管理

    日志分类:

    1. 连接时间的日志

      连接时间日志一般由/var/log/wtmp和/var/run/utmp这两个文件记录,不过这

      两个文件无法直接cat查看,并且该文件由系统自动更新,可以通过如下:

      w/who/finger/id/last/lastlog/ac 进行查看

      [root@xhot ~]# who

      root tty1 2010-10-06 22:56

      root pts/0 2010-10-06 22:26 (218.192.87.4)

      root pts/1 2010-10-06 23:41 (218.192.87.4)

      root pts/3 2010-10-06 23:18 (218.192.87.4)

      [root@xhot ~]# w

      01:01:02 up 2:36, 4 users, load average: 0.15, 0.03, 0.01

      USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT

      root tty1 - 22:56 1:20m 0.16s 0.16s -bash

      root pts/0 218.192.87.4 22:26 2:05m 0.18s 0.18s -bash

      root pts/1 218.192.87.4 23:41 0.00s 0.41s 0.00s w

      root pts/3 218.192.87.4 23:18 1:38m 0.03s 0.03s -bash

      [root@xhot ~]# ac -p //查看每个用户的连接时间

      u51 1.23

      u55 0.04

      root 95.21 //可以看到root连接时间最长吧

      xhot 0.06

      user1 3.93

      total 100.48

      [root@xhot ~]# ac -a //查看所有用户的连接时间

      total 100.49

      [root@xhot ~]# ac -d //查看用户每天的连接时间

      Sep 24 total 0.14

      Sep 25 total 14.60

      Sep 26 total 13.71

      Sep 27 total 21.47

      Sep 28 total 11.74

      Sep 29 total 6.60

      Sep 30 total 8.81

      Oct 1 total 9.04

      Oct 2 total 0.47 //可以看到我国庆3、4、5号出去玩了

      Oct 6 total 8.62

      Today total 5.29

      其他几个命令不做具体介绍了

    2. 进程监控日志

      进程统计监控日志在监控用户的操作指令是非常有效的。当服务器最近发现经常

      无故关机或者无故被人删除文件等现象时,可以通过使用进程统计日志查看:

      [root@xhot ~]# accton /var/account/pacct //开启进程统计日志监控

      [root@xhot ~]# lastcomm //查看进程统计日志情况

      accton S root pts/1 0.00 secs Thu Oct 7 01:20

      accton root pts/1 0.00 secs Thu Oct 7 01:20

      ac root pts/1 0.00 secs Thu Oct 7 01:14

      ac root pts/1 0.00 secs Thu Oct 7 01:14

      free root pts/1 0.00 secs Thu Oct 7 01:10

      lastcomm root pts/1 0.00 secs Thu Oct 7 01:09

      bash F root pts/1 0.00 secs Thu Oct 7 01:09

      lastcomm root pts/1 0.00 secs Thu Oct 7 01:09

      ifconfig root pts/1 0.00 secs Thu Oct 7 01:09

      lastcomm root pts/1 0.00 secs Thu Oct 7 01:09

      lastcomm root pts/1 0.00 secs Thu Oct 7 01:09

      lastcomm root pts/1 0.00 secs Thu Oct 7 01:09

      accton S root pts/1 0.00 secs Thu Oct 7 01:09

      [root@xhot ~]# accton //关闭进程统计日志监控

    3. 系统和服务日志

      系统日志服务是由一个名为syslog的服务管理的,如一下日志文件都是由syslog日志服务驱动的:

      /var/log/lastlog :记录最后一次用户成功登陆的时间、登陆IP等信息

      /var/log/messages :记录Linux操作系统常见的系统和服务错误信息

      /var/log/secure :Linux系统安全日志,记录用户和工作组变坏情况、用户登陆认证情况

      /var/log/btmp :记录Linux登陆失败的用户、时间以及远程IP地址

      /var/log/cron :记录crond计划任务服务执行情况

      …...

      [root@xhot ~]# cat /var/log/lastlog

      Lpts/0218.192.87.4

      Lpts/1218.192.87.4

      Lpts/1218.192.87.4

      Lpts/0218.192.87.46

      Lpts/0218.192.87.4

      …...


    Linux日志服务介绍

    1. 在Linux系统,大部分日志都是由syslog日志服务驱动和管理的
          syslog服务由两个重要的配置文件控制管理,分别是/etc/syslog.conf主配置文件和/etc/sysconfig/syslog辅助

      配置文件, /etc/init.d/syslog是启动脚本,这里主讲主配置文件/etc/syslog.conf:

      /etc/syslog.conf 语句结构:

      [root@xhot ~]# grep -v "#" /etc/syslog.conf //列出非#打头的每一行

      *.info;mail.none;authpriv.none;cron.none /var/log/messages

      authpriv.* /var/log/secure

      mail.* -/var/log/maillog

      cron.* /var/log/cron

      *.emerg *

      uucp,news.crit /var/log/spooler

      local7.* /var/log/boot.log

      选择域(消息类型.错误级别) 动作域

    2. 消息类型:auth,authpriv,security;cron,daemon,kern,lpr,mail, mark,news,syslog,user,uucp,local0~local7.

      错误级别:(8级)debug,info,notice,warning|warn;err|error;crit,alert,emerg|panic

      动作域:file,user,console,@remote_ip

      举如上的/etc/syslog.conf文件三个例子:

      *.info;mail.none;authpriv.none;cron.none /var/log/messages

      表示info级别的任何消息都发送到/var/log/messages日志文件,但邮件系统、验证系统

      和计划任务的错误级别信息就除外,不发送(none表示禁止)

      cron.* /var/log/cron 表示所有级别的cron信息发到/var/log/cron文件

      *.emerg * 表示emerg错误级别(危险状态)的所有消息类型发给所有用户


    Linux日志服务器配置

      此服务器的配置非常简单,只是修改一个文件的一个地方,然后重启服务即可:

      [root@xhot ~]# grep -v "#" /etc/sysconfig/syslog

      SYSLOGD_OPTIONS="-m 0 -r" //只要在这里添加“-r”就行咯

      KLOGD_OPTIONS="-x"

      SYSLOG_UMASK=077

      [root@xhot ~]# service syslog restart

      关闭内核日志记录器: [确定]

      关闭系统日志记录器: [确定]

      启动系统日志记录器: [确定]

      启动内核日志记录器: [确定]

      对于发送消息到服务器的OS,只要在写/etc/syslog.conf主配置文件的时候,作用域

      为@server-ip就行了,比如针对218.192.87.24这台日志服务器,把一台ubuntu系统的所有

      info级别的auth信息发给日志服务器,那么对于ubuntu系统的/etc/syslog.conf文件最后一

      行添加 auth.info @218.192.87.24 就OK了


    日志转储服务

      系统工作到了一定时间后,日志文件的内容随着时间和访问量的增加而越来越多,

      日志文件也越来越大。而且当日志文件超过系统控制范围时候,还会对系统性能

      造成影响。转储方式可以设为每年转储、每月转储、每周转储、达到一定大小转储。

      在Linux系统,经常使用“logrotate”工具进行日志转储,结合cron计划任务,可以轻松

      实现日志文件的转储。转储方式的设置由“/etc/logrotate.conf”配置文件控制:

      [root@xhot ~]# cat /etc/logrotate.conf

      # see "man logrotate" for details //可以查看帮助文档

      # rotate log files weekly

      weekly //设置每周转储

      # keep 4 weeks worth of backlogs

      rotate 4 //最多转储4次

      # create new (empty) log files after rotating old ones

      create //当转储后文件不存储时创建它

      # 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 -- we'll rotate them here

      /var/log/wtmp { //设置/var/log/wtmp日志文件的转储参数

      monthly //每月转储

      create 0664 root utmp //转储后文件不存在时创建它,文件所有者为root,

      所属组为utmp,对应的权限为0664

      rotate 1 //转储一次

      }

      # system-specific logs may be also be configured here.


    举两个例子:
    为/var/log/news/目录下的所有文件设置转储参数,每周转储,转储2次,转储

    时将老的日志文件放到/var/log/news/old目录下,若日志文件不存在,则跳过。完成后重启

    news新闻组服务,转储时不压缩。那么可以在/etc/logrotate.conf文件的最后添加如下:

      /var/log/news/*{

      monthly

      rotate 2

      olddir /var/log/news/old

      missingok

      postrotate

      kill -HUP `cat /var/run/inn.pid`

      endscript

      nocompress

      }


      另一个例子:为/var/log/httpd/access.log和/var/log/httpd/error.log日志设置转储参数。转储

      5次,转储时发送邮件给root@localhost用户,当日志文件达到100KB时才转储,转储后重启

      httpd服务,那么可以直接在/etc/logrotate.conf文件的最后添加如下:

      /var/log/httpd/access.log /var/log/http/error.log{

      rotate 5

      mail root@localhost

      size=100k

      sharedscripts

      /sbin/killall -HUP httpd

      endscript

      }

    自定义日志转储(/etc/logrotate.d/*)

      通过下面一个例子将所有类型错误级别为info的日志转储到/var/log/test.log日志文件中,并设置

      /var/log/test.log达到50KB后进行转储,转储10次,转储时压缩,转储后重启syslog服务

      1、修改/etc/syslog.conf文件使得如下:

      [root@xhot ~]# tail -1 /etc/syslog.conf //查看该文件的最后一行

      *.info /var/log/test.log

      2、重启syslog服务:

      [root@xhot ~]# /sbin/service syslog restart

      关闭内核日志记录器: [确定]

      关闭系统日志记录器: [确定]

      启动系统日志记录器: [确定]

      启动内核日志记录器: [确定]

      3、创建/etc/logrotate.d/test.log日志转储参数配置文件,添加如下:

      [root@xhot ~]# vim /etc/logrotate.d/test.log

      [root@xhot ~]# cat /etc/logrotate.d/test.log

      /var/log/test.log{

      rotate 10

      size = 50k

      compress

      postrotate

      killall -HUP syslog

      endscript

      }

      4、查看文件/etc/cron.daily/logrotate确保如下:

      [root@xhot ~]# cat /etc/cron.daily/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

      5、查看转储后的文件

      [root@xhot log]# pwd

      /var/log

      [root@xhot log]# ls test.log*

      …… //结果等要转储的时候会发现压缩文件和原本的test.log文件

  • 相关阅读:
    1046 Shortest Distance (20 分)(模拟)
    1004. Counting Leaves (30)PAT甲级真题(bfs,dfs,树的遍历,层序遍历)
    1041 Be Unique (20 分)(hash散列)
    1036 Boys vs Girls (25 分)(查找元素)
    1035 Password (20 分)(字符串处理)
    1044 Shopping in Mars (25 分)(二分查找)
    onenote使用小Tip总结^_^(不断更新中...)
    1048 Find Coins (25 分)(hash)
    三个故事
    领导者的举止
  • 原文地址:https://www.cnblogs.com/php5/p/1873206.html
Copyright © 2011-2022 走看看