zoukankan      html  css  js  c++  java
  • Linux日志管理

    系统日志rsyslog

    常见的日志文件

    • /var/log/boot.log: 开机启动系统内核会检测和启动硬件,然后开启各种内核支持的功能,这都会记录在这个文件中,但只会存储本次开机的信息,之前的启动信息会被覆盖。

    • /var/log/cron: 计划任务执行过程的日志。

    • /var/log/dmesg: 开机时内核引导产生的各种日志信息。

    • /var/log/lastlog: 所有账户最近一次的登录信息。

    • /var/log/maillog: 记录邮件相关日志信息,主要是STMP协议和POP3协议所产生的信息。

    • /var/log/wtmp、/var/log/btmp: 这两个日志文件主要记录用户正常登录和登录失败时的相关信息,是二进制格式文件,可以使用last和lastb命令进行查看。

    • /var/log/messages: 几乎系统发生的所有错误(重要)信息都会记录在这个文件中。

    • /var/log/secure: 涉及到需要输入账号密码的软件,只要有账户登录,都会被记录在这个文件中,包括login,ssh,sudo等等程序,应该经常查看分析。

    日志文件的一般格式

    日期和时间 主机名 服务名称 具体内容

    [root@centos82 ~]#tail /var/log/messages
    Oct 30 19:19:10 centos82 systemd[1]: Starting dnf makecache...
    Oct 30 19:19:11 centos82 dnf[22438]: Metadata cache refreshed recently.
    Oct 30 19:19:11 centos82 systemd[1]: Started dnf makecache.
    Oct 30 19:52:06 centos82 systemd[1]: proc-sys-fs-binfmt_misc.automount: Got automount request for /proc/sys/fs/binfmt_misc, triggered by 22513 (grep)
    Oct 30 19:52:06 centos82 systemd[1]: Mounting Arbitrary Executable File Formats File System...
    Oct 30 19:52:06 centos82 systemd[1]: Mounted Arbitrary Executable File Formats File System.
    Oct 30 19:52:06 centos82 kernel: ICMPv6: process `grep' is using deprecated sysctl (syscall) net.ipv6.neigh.default.base_reachable_time - use net.ipv6.neigh.default.base_reachable_time_ms instead
    Oct 30 20:20:10 centos82 systemd[1]: Starting dnf makecache...
    Oct 30 20:20:11 centos82 dnf[22536]: Metadata cache refreshed recently.
    Oct 30 20:20:11 centos82 systemd[1]: Started dnf makecache.
    

    日志类别和等级

    • facility:服务,设施,根据功能分为不同的日志

      auth, authpriv, cron, daemon, ftp, kern, lpr, mail, news, security(auth), user, uucp, syslog
      
    • priority:日志等级,从低到高排序

      debug, info, notice, warn, err, crit, alert, emerg
      

    配置文件

    /etc/rsyslog.conf:

    • modules:模块设置
    • global directives: 全局配置
    • rules: 日志记录规则

    rules配置格式

    服务名称[.=!]日志等级						日志存放路径或设备或主机
    facility.priority;facility.priority...		                        target
    facility.*								target
    facility.=priority							target
    facility.!priority							target
    facility.none								target
    
    # .*表示所有等级日志都要记录下来
    # .priority表示当前等级及之后等级的日志都记录下来
    # .=priority表示只记录当前等级的日志
    # .!priority表示除了该等级之外的其他等级
    # .none表示不记录此服务日志
    

    服务端配置网络日志

    • centos8
    [root@centos82 ~]#vim /etc/rsyslog.conf
    #### MODULES ####
    ...省略...
    # Provides UDP syslog reception
    # for parameters see http://www.rsyslog.com/doc/imudp.html
    module(load="imudp") # needs to be done just once
    input(type="imudp" port="514")
    
    # Provides TCP syslog reception
    # for parameters see http://www.rsyslog.com/doc/imtcp.html
    #module(load="imtcp") # needs to be done just once
    #input(type="imtcp" port="514")
    
    • centos 7
    [root@centos7 ~]#vim /etc/rsyslog.conf
    #### MODULES ####
    ...省略...
    # Provides UDP syslog reception
    $ModLoad imudp
    $UDPServerRun 514
    
    # Provides TCP syslog reception
    #$ModLoad imtcp
    #$InputTCPServerRun 514
    

    客户端启用网络日志

    # 在客户端指定将日志发送到远程的UDP,TCP日志服务器
    [root@centos7 ~]#vim /etc/rsyslog.conf
    *.info;mail.none;authpriv.none;cron.none		@10.0.0.81      #UDP
    *.info;mail.none;authpriv.none;cron.none		@@10.0.0.81	#TCP
    

    日志管理工具journalctl

    systemd-journald是使用内存的日志记录方式,所以只能用来管理和查询本次启动之后的日志信息。

    虽然systemd-journald的数据信息是基于内存的,但系统在/run/log/目录下还是以文件的方式记录了一份下来。不过只要一重启,/run/log/下的数据还是会被刷新。

    journalctl使用

    # 查看所有日志(本次登录)
    [root@centos82 ~]#journalctl
    
    # 按时间查看日志
    [root@centos82 ~]#journalctl --since "2020-10-29 00:00:00" --until "2020-10-31 00:00:00"
    [root@centos82 ~]#journalctl --since yesterday --until today
    [root@centos82 ~]#journalctl --since journalctl --since "12:00" --until now
    
    # 查看某个服务的日志
    [root@centos82 ~]#journalctl -u nginx.service
    
    # 查看指定进程的日志
    [root@centos82 ~]#journalctl _PID=666
    
    # 查看指定用户的日志
    [root@centos82 ~]#journalctl _UID=77
    
    # 查看指定日志等级的日志
    [root@centos82 ~]#journalctl -p warning
    

    日志轮循(logrotate)

    logrotate可以将旧的日志修改名称存储下来,并重新建立同名的空日志文件继续存储日志。

    logrotate利用cron定时任务来完成日志的轮循工作。

    logrotate配置

    [root@centos8 log]#rpm -ql logrotate
    /etc/cron.daily
    /etc/cron.daily/logrotate
    /etc/logrotate.conf
    /etc/logrotate.d
    /etc/logrotate.d/btmp
    /etc/logrotate.d/wtmp
    /etc/rwtab.d/logrotate
    /usr/lib/.build-id
    /usr/lib/.build-id/c5
    /usr/lib/.build-id/c5/48d4f89fab16e46d486dd02cccee4ec98f218f
    /usr/sbin/logrotate
    /usr/share/doc/logrotate
    /usr/share/doc/logrotate/ChangeLog.md
    /usr/share/licenses/logrotate
    /usr/share/licenses/logrotate/COPYING
    /usr/share/man/man5/logrotate.conf.5.gz
    /usr/share/man/man8/logrotate.8.gz
    /var/lib/logrotate
    /var/lib/logrotate/logrotate.status
    
    [root@centos8 log]#vim /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			<<== 创建新的日志文件
    
    # 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
    
    # system-specific logs may be also be configured here.
    

    /etc/logrotate.conf:主配置文件

    /etc/logrotate.d:这个目录被主配置文件调用,里面可以放置自己配置文件,用来覆盖Logrotate的默认值

    使用说明

    [root@centos8 log]#cat /etc/logrotate.d/syslog
    /var/log/cron
    /var/log/maillog
    /var/log/messages
    /var/log/secure
    /var/log/spooler
    {
        missingok			<<== 如果没有文件就忽略跳过
        sharedscripts		
        postrotate
            /usr/bin/systemctl kill -s HUP rsyslog.service >/dev/null 2>&1 || true
        endscript
    }
    
    • 文件名:需要被处理的日志文件,多个文件以空格或者换行隔开。
    • 参数:{}中间写需要进行轮循的具体操作参数,如果没写沿用主配置文件的值,主配置也没写使用默认值。
    • 脚本:调用shell命令来进行额外的操作。上面/usr/bin/systemctl kill -s HUP rsyslog.service的意思是执行完毕后,发送HUP信号重启rsylogd服务
      • prerotate:启动logrotate之前的命令。
      • postrotate:执行完logrotate之后的命令。
      • endscript:和上面两项成对出现,表面脚本的结尾。
      • sharedscripts:指对所有日志文件执行脚本,如果不写,脚本只会执行一次。

    自定义日志logrotate

    /var/log/test.log {
        daily				<<== 每天执行一次
        rotate 5				<<== 保留5天
        compress				<<== 压缩日志
        delaycompress			<<== 延迟压缩,当生成第二个轮循日志的时候将第一个轮循日志压缩
        missingok				<<== 如果没有文件就忽略跳过
        size 100M				<<== 文件如果大于100M就进行轮循
        notifempty				<<== 如果日志是空的就跳过
        create 640 root root	        <<== 创建新日志,并修改权限和uu'v [shu'zhi]
        postrotate
        	echo `date +%F_%T` >> /data/test.log
        endscript
    }
    
    更多其他信息可以查看帮助手册:man logrotate
  • 相关阅读:
    2w字 + 40张图带你参透并发编程!
    完了,这个硬件成精了,它竟然绕过了 CPU...
    一文详解 Java 并发模型
    详解匈牙利算法与二分图匹配
    机器学习 | 详解GBDT在分类场景中的应用原理与公式推导
    Python | 浅谈并发锁与死锁问题
    LeetCode 91,点赞和反对五五开,这题是好是坏由你来评判
    LeetCode 90 | 经典递归问题,求出所有不重复的子集II
    【Azure DevOps系列】什么是Azure DevOps
    MSIL入门(四)之委托delegate
  • 原文地址:https://www.cnblogs.com/wuvikr/p/13906662.html
Copyright © 2011-2022 走看看