• Linux:日志管理rsyslogd和logrotate


    系统中常见的日志文件

    日志文件 说明
    /var/log/cron 记录了系统定时任务相关的日志。
    /var/log/cups/ 记录打印信息的日志
    /var/log/dmesg 记录了系统在开机时内核自检的信息。也可以使用 dmesg 命令直接查看内核自检信息。
    /var/log/btmp 记录错误登录的日志。这个文件是二进制文件,不能直接 vi 查看,而要使用 lastb 命令查看
    /var/log/lastlog 记录系统中所有用户最后一次的登录时间的日志。这个文件也是二进制文件,不能直接 vi,而要使用 lastlog 命令查看。
    /var/log/mailog 记录邮件信息。
    /var/log/messages 记录系统重要信息的日志。这个日志文件中会记录 Linux 系统的绝大多数重要信息,如果系统出现问题时,首先要检查的就应该是这个日志文件。
    /var/log/secure 记录验证和授权方面的信息,只要涉及账户和密码的程序都会记录。比如说系统的登录,ssh 的登录,su 切换用户,sudo 授权,甚至添加用户和修改用户密码都会记录在这个日志文件中。
    /var/log/wtmp 永久记录所有用户的登录、注销信息,同时记录系统的启动、重启、关机事件。同样这个文件也是一个二进制文件,不能直接 vi,而需要使用 last 命令来查看。
    /var/run/utmp 记录当前已经登录的用户的信息。这个文件会随着用户的登录和注销而不断变化,只记录当前登录用户的信息。同样这个文件不能直接vi,而要使用 w,who,users 等命令来查询。

    除了系统默认的日志之外,采用 RPM 方式安装的系统服务也会默认把日志记录在/var/log/目录中(源码包安装的服务日志是在源码包指定目录中)。不过这些日志不是由 rsyslogd 服务来记录和管理的,而是各个服务使用自己的日志管理文档来记录自身日志。

    日志文件 说 明
    /var/log/httpd/ RPM 包安装的 apache 服务的默认日志目录
    /var/log/mail/ RPM 包安装的邮件服务的额外日志目录
    /var/log/samba/ RPM 包安装的 samba 服务的日志目录
    /var/log/sssd/ 守护进程安全服务目录

    日志服务 rsyslogd

    日志文件格式

    只要是由日志服务 rsyslogd 记录的日志文件,他们的格式是一样的。基本日志格式包含以下四列:

    • 事件产生的时间;
    • 发生事件的服务器的主机名;
    • 产生事件的服务名或程序名;
    • 事件的具体信息

    rsyslogd 服务的配置文件

    /etc/rsyslog.conf 配置文件格式:

    authpriv.* /var/log/secure

    服务名称[连接符号]日志等级 日志记录位置

    认证相关服务.所有日志等级 记录在/var/log/secure 日志中

    连接符号:

    • .”代表只要比后面的等级高的(包含该等级)日志都记录下来。比如:“cron.info”代表 cron 服务产生的日志,只要日志等级大于等于 info 级别,就记录
    • “.=”代表只记录所需等级的日志,其他等级的都不记录。比如:“*.=emerg”代表任何日志服务产生的日志,只要等级是 emerg 等级就记录。这种用法及少见,了解就好
    • “.!”代表不等于,也就是除了该等级的日志外,其他等级的日志都记录。

    日志等级:

    等 级 名 称 说 明
    debug(LOG_DEBUG) 一般的调试信息说明
    info(LOG_INFO) 基本的通知信息
    notice(LOG_NOTICE) 普通信息,但是有一定的重要性
    warning(LOG_WARNING) 警告信息,但是还不回影响到服务或系统的运行
    err(LOG_ERR) 错误信息,一般达到 err 等级的信息以及可以影响到服务或系统的运行了。
    crit(LOG_CRIT) 临界状况信息,比 err 等级还要严重
    alert(LOG_ALERT) 警告状态信息,比 crit 还要严重。必须立即采取行动
    emerg(LOG_EMERG) 疼痛等级信息,系统已经无法使用了
    * 代表所有日志等级,比如:“authpriv.*”代表 authpriv 认证信息服务产生的日志,所有的日志等级都记录

    日志等级这里还可以识别“none”,如果日志等级是 none,就说明忽略这个日志服务,该服务的所有日志都不再记录。

    日志记录位置:

    日志记录位置就是当前日志输出到哪个日志文件中保存,当然也可以把日志输出到打印机打印,或者输出到远程日志服务器上(当然日志服务器要允许接收才行)。日志的记录位置也是固定的,我们来学习下:

    • 日志文件的绝对路径。这是最常见的日志保存方法,如“/var/log/secure”就是保存系统验证和授权信息日志的。
    • 系统设备文件。如“/dev/lp0”代表第一台打印机,如果日志保存位置是打印机设备的话,当有日志时就会在打印机打印
    • 转发给远程主机。如使用“@192.168.0.210:514”,就会把日志内容使用 UDP 协议发送到192.168.0.210 的 UDP 514 端口上;如果使用“@@192.168.0.210:514”就会把日志内容使用 TCP 协议发送到 192.168.0.210 的 TCP 514 端口上,其中 514 是日志服务默认端口。
    • 用户名。如“root”,就会把日志发送给 root 用户,当然 root 要在在线,否则就收不到日志信息了。可以使用“*”代表发送给所有在线用户.

    忽略或丢弃日志。如果接受日志的对象是“”,代表这个日志不会记录,而被直接丢弃。如“local3.*”代表忽略 local3 服务类型所有的日志都不记录.

    日志轮替

    日志文件的命名规则

    日志轮替最主要的作用就是把旧的日志文件移动并改名,同时建立新的空日志文件,当旧日志文件超出保存的范围之后,就会进行删除。那么旧的日志文件改名之后,如何命名呢?主要依靠/etc/logrotate.conf 配置文件中“dateext”参数:

    • 如果配置文件中拥有“dateext”参数,那么日志会用日期来作为日志文件的后缀,例如“secure-20180605”。这样的话日志文件名不会重叠,所以也就不需要日志文件的改名,只需要保存指定的日志个数,删除多余的日志文件即可。
    • 如果配置文件中没有“dateext”参数,那么日志文件就需要进行改名了。当第一次进行日志轮替时,当前的“secure”日志会自动改名为“secure.1”,然后新建“secure”日志,用来保存新的日志。当第二次进行日志轮替时,“secure.1”会自动改名为“secure.2”,当前的“secure”日志会自动改名为“secure.1”,然后也会新建“secure”日志,用来保存新的日志,以此类推。

    logrotate 配置文件

    /etc/logrotate.conf

    # see "man logrotate" for details
    # rotate log files weekly
    #每周对日志文件进行一次轮替
    weekly
    
    # keep 4 weeks worth of backlogs
    #保存 4 个日志文件,也就是说如果进行了 5 次日志轮替,就会删除第一个备份日志
    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
    #包含/etc/logrotate.d/目录中所有的子配置文件。也就是说会把这个目录中所有子配置文件读取进来,进行日志轮替。
    include /etc/logrotate.d
    
    # no packages own wtmp and btmp -- we'll rotate them here
    #以下两个轮替日志有自己的独立参数,如果和默认的参数冲突,则独立参数生效。
    #以下参数仅对此目录有效
    /var/log/wtmp {
        #每月对日志文件进行一次轮替
        monthly
        #建立的新日志文件,权限是 0664,所有者是 root,所属组是 utmp 组
        create 0664 root utmp
            #日志文件最小轮替大小是 1MB。也就是日志一定要超过 1MB 才会轮替,否则就算时间达到一个月,也不进行日志转储
            minsize 1M
        #仅保留一个日志备份。也就是只有 wtmp 和 wtmp.1 日志保留而已    
        rotate 1
    }
    
    /var/log/btmp {
        #如果日志不存在,则忽略该日志的警告信息
        missingok
        monthly
        create 0600 root utmp
        rotate 1
    }
    
    # system-specific logs may be also be configured here.
    

    logrotate 配置文件的主要参数:

    • daily 日志的轮替周期是每天
    • weekly 日志的轮替周期是每周
    • monthly 日志的轮替周期是每月
    • rotate 数字 保留的日志文件的个数。0 指没有备份
    • compress 日志轮替时,旧的日志进行压缩
    • create mode owner group 建立新日志,同时指定新日志的权限与所有者和所属组。如create 0600 root utmp
    • mail address 当日志轮替时,输出内容通过邮件发送到指定的邮件地址。如mail shenc@lamp.net
    • missingok 如果日志不存在,则忽略该日志的警告信息
    • notifempty 如果日志为空文件,则不进行日志轮替
    • minsize 大小 日志轮替的最小值。也就是日志一定要达到这个最小值才会轮替,否则就算时间达到也不轮替
    • size 大小 日志只有大于指定大小才进行日志轮替,而不是按照时间轮替。如 size 100k
    • dateext 使用日期作为日志轮替文件的后缀。如 secure-20180605
    • sharedscripts 在此关键字之后的脚本只执行一次
    • prerotate/endscript 在日志轮替之前执行脚本命令。endscript 标示 prerotate 脚本结束。
    • postrotate/endscript 在日志轮替之后执行脚本命令。endscript 标示 postrotate 脚本结束

    这些参数中较为不好理解的应该就是 prerotate/endscript 和 postrotate/endscript 参数了,我们利用“man logrotate”中的列子来解释下这两个参数。例如:

    "/var/log/httpd/access.log" /var/log/httpd/error.log {
        #日志轮替的是/var/log/httpd/中 RPM 包安装的 apache 正确访问日志和错误日志
        rotate 5
        #轮替 5 次
        mail www@my.org
        #信息发送到指定邮箱
        size 100k
        #日志大于 100KB 时才进行日志轮替,不再按照时间轮替
        sharedscripts
        #以下脚本只执行一次
        postrotate
        #在日志轮替结束之后,执行以下脚本
            /usr/bin/killall -HUP httpd
            #重启 apache 服务
        endscript
        #脚本结束
    }
    

    prerotate 和 postrotate 主要用于在日志轮替的同时,执行指定的脚本,一般用于日志轮替之后重启服务。这里强调,如果你的日志是写入 rsyslog 服务的配置文件的,那么把新日志加入 logrotate后,一定要重启 rsyslog 服务,否则你会发现虽然新日志建立了,但是数据还是写入了旧的日志当中。那是因为虽然 logrotate 知道日志轮替了,但是 rsyslog 服务却并不知道。同理,如果你的日志不是被 rsyslog 管理,如源码包安装的 Apache、Nginx 等服务,则需要重启 Apache 或 Nginx 服务,否则日志也不能正常轮替。

    把自己的日志加入日志轮替

    这里有两个方法:第一种方法是直接在/etc/logrotate.conf 配置文件中写入该日志的轮替策略,从而把日志加入轮替;第二种方法是在/etc/logrotate.d/目录中新建立该日志的轮替文件,在该轮替文件中写入正确的轮替策略,因为该目录中的文件都会被“include”到主配置文件中,所以也可以把日志加入轮替。我们这里推荐第二种方法,因为系统中需要轮替的日志非常多,如果全都直接写入/etc/logrotate.conf 配置文件,那么这个文件的可管理性就会非常差,不利于此文件的维护。

    举例:当我们使用yum安装nginx后,自动会把nginx的日志加入到日志轮替中:

    image-20220121142037185

    配置都在/etc/logrotate.d/nginx中,当然我们也可以自己配置:

    /var/log/nginx/*log {
        create 0664 nginx root
        daily
        rotate 10
        missingok
        notifempty
        compress
        sharedscripts
        postrotate
            /bin/kill -USR1 `cat /run/nginx.pid 2>/dev/null` 2>/dev/null || true
        endscript
    }
    

    logrotate 命令

    我们日志轮替之所以可以在指定的时间备份日志,其实也要依赖系统定时任务。如果大家还记得/etc/cron.daily/目录,就会发现这个目录中是有 logrotate 文件,logrotate 通过这个文件依赖定
    时任务执行的。

    logrotate 命令格式:

    logrotate [选项] 配置文件名
    选项:
    如果此命令没有选项,则会按照配置文件中的条件进行日志轮替
    -v: 显示日志轮替过程。加了-v 选项,会显示日志的轮替的过程
    -f: 强制进行日志轮替。不管日志轮替的条件是否已经符合,强制配置文件中所有
    的日志进行轮替

    我们执行 logrotate 命令,并查看下执行过程(这里我是在我的一台腾讯云主机上执行的):

    [root@VM-16-12-centos log]# logrotate -v /etc/logrotate.conf
    reading config file /etc/logrotate.conf
    including /etc/logrotate.d
    Ignoring 2019-11-05 because it's not a regular file.
    Ignoring 2020-08-05 because it's not a regular file.
    reading config file bootlog
    reading config file chrony
    reading config file firewalld
    reading config file iscsiuiolog
    reading config file opensm
    reading config file subscription-manager
    reading config file syslog
    reading config file wpa_supplicant
    reading config file yum
    Allocating hash table for state file, size 15360 B
    
    Handling 11 logs
    
    rotating pattern: /var/log/boot.log
     after 1 days (7 rotations)
    empty log files are not rotated, old logs are removed
    considering log /var/log/boot.log
      log does not need rotating (log is empty)
    rotating pattern: /var/log/chrony/*.log  weekly (4 rotations)
    empty log files are rotated, old logs are removed
    considering log /var/log/chrony/*.log
      log /var/log/chrony/*.log does not exist -- skipping
    not running postrotate script, since no logs were rotated
    
    rotating pattern: /var/log/firewalld  weekly (4 rotations)
    empty log files are rotated, only log files >= 1048576 bytes are rotated, old logs are removed
    considering log /var/log/firewalld
      log /var/log/firewalld does not exist -- skipping
    
    rotating pattern: /var/log/iscsiuio.log  weekly (4 rotations)
    empty log files are not rotated, old logs are removed
    considering log /var/log/iscsiuio.log
      log /var/log/iscsiuio.log does not exist -- skipping
    not running postrotate script, since no logs were rotated
    
    rotating pattern: /var/log/opensm.log  weekly (4 rotations)
    empty log files are not rotated, old logs are removed
    considering log /var/log/opensm.log
      log /var/log/opensm.log does not exist -- skipping
    
    rotating pattern: /var/log/rhsm/*.log  weekly (4 rotations)
    empty log files are not rotated, old logs are removed
    considering log /var/log/rhsm/rhsm.log
      log does not need rotating (log has been rotated at 2022-1-16 3:24, that is not week ago yet)
    
    rotating pattern: /var/log/cron
    /var/log/maillog
    /var/log/messages
    /var/log/secure
    /var/log/spooler
     41943040 bytes (25 rotations)
    empty log files are rotated, old logs are removed
    considering log /var/log/cron
      log does not need rotating (log size is below the 'size' threshold)
    considering log /var/log/maillog
      log does not need rotating (log size is below the 'size' threshold)
    considering log /var/log/messages
      log does not need rotating (log size is below the 'size' threshold)
    considering log /var/log/secure
      log does not need rotating (log size is below the 'size' threshold)
    considering log /var/log/spooler
      log does not need rotating (log size is below the 'size' threshold)
    not running postrotate script, since no logs were rotated
    
    rotating pattern: /var/log/wpa_supplicant.log  30720 bytes (4 rotations)
    empty log files are not rotated, old logs are removed
    considering log /var/log/wpa_supplicant.log
      log /var/log/wpa_supplicant.log does not exist -- skipping
    
    rotating pattern: /var/log/yum.log  yearly (4 rotations)
    empty log files are not rotated, log files >= 30720 are rotated earlier, old logs are removed
    considering log /var/log/yum.log
      log does not need rotating (log has been rotated at 2022-1-1 3:48, that is not year ago yet)
    
    rotating pattern: /var/log/wtmp  monthly (1 rotations)
    empty log files are rotated, only log files >= 1048576 bytes are rotated, old logs are removed
    considering log /var/log/wtmp
      log does not need rotating ('misinze' directive is used and the log size is smaller than the minsize value
    rotating pattern: /var/log/btmp  monthly (1 rotations)
    empty log files are rotated, old logs are removed
    considering log /var/log/btmp
      log does not need rotating (log has been rotated at 2022-1-1 3:48, that is not month ago yet)
    
  • 相关阅读:
    王晶:华为云OCR文字识别服务技术实践、底层框架及应用场景 | AI ProCon 2019【华为云技术分享】
    华为云实战开发】5.如何快速创建免费Git代码仓库【华为云技术分享】
    【华为云实战开发】9.如何进行PHP项目的快速搭建并实现CICD?【华为云技术分享】
    Linux入侵痕迹检测方案【华为云技术分享】
    【Python3网络爬虫开发实战】6.4-分析Ajax爬取今日头条街拍美图【华为云技术分享】
    Python如何爬取实时变化的WebSocket数据【华为云技术分享】
    遍历json
    选中文字弹出提示
    基本动画函数
    动画的基本原理
  • 原文地址:https://www.cnblogs.com/wwjj4811/p/15830203.html
Copyright © 2020-2023  润新知