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文件

    下一页将介绍syslog, syslog是一个被UNIX和Linux广泛使用的日志系统。

    syslog是一个被UNIX和Linux广泛使用的日志系统,Linux系统中大部分的日志文件都是通过它进行管理的。本节将对syslog的功能及配置、日志文件的查看和管理,以及syslog中默认配置的日志文件进行介绍。

    12.2.1 syslog简介

    syslog是一个历史悠久的日志系统,几乎所有的UNIX和Linux操作系统都是采用syslog进行系统日志的管理和配置。Linux系统内核和许多程序会产生各种错误信息、警告信息和其他的提示信息。这些信息对管理员了解系统的运行状态是非常有用的,所以应该把它们写到日志文件中去。而执行这个过程的程序就是syslog。syslog可以根据信息的来源以及信息的重要程度将信息保存到不同的日志文件中,例如,为了方便查阅,可以把内核信息与其他信息分开,单独保存到一个独立的日志文件中。在默认的syslog配置下,日志文件通常都保存在“/var/log”目录下。syslog的守护进程为syslogd,系统启动时,默认会自动运行syslogd守护进程。

               

    如果要手工启动,可以使用如下命令:

    /sbin/syslogd

    在修改syslog配置后,需要重新启动syslogd守护进程才能使新的配置生效。其命令如下所示。

    # killall -HUP syslogd

    Red Hat Enterprise Linux 5.2安装后默认就已经在syslog中定义了一些日志文件,这些日志的位置以及它们的说明如表12.2所示。

    表12.2 默认配置syslog日志

    日 志 文 件

    说    明

    /var/log/message

    系统启动后的信息和错误日志,是Red Hat Linux中最常用的日志之一

    /var/log/secure

    与安全相关的日志信息

    /var/log/maillog

    与邮件相关的日志信息

    /var/log/cron

    与定时任务相关的日志信息

    /var/log/spooler

    与UUCP和news设备相关的日志信息

    /var/log/boot.log

    守护进程启动和停止相关的日志消息

  • 相关阅读:
    笔试
    Java
    工作中问题总结
    suitcrm安装及虚拟机
    python邮件读取2
    restful api
    python 邮件读取
    suiteCRM____Admin
    pdf提取信息到excel
    Maven笔记
  • 原文地址:https://www.cnblogs.com/zsmynl/p/3602307.html
Copyright © 2011-2022 走看看