linux 日誌管理

  • 2019 年 10 月 3 日
  • 筆記

系統日誌存放的位置

linux中的日誌文件存儲在 /var/log*

不同模組的日誌會記錄到不同的模組中去

模組 位置
內核及公共消息日誌 /var/log/messages
計劃任務日誌 /var/log/cron
系統引導日誌 /var/log/dmesg
郵件系統日誌 /var/log/maillog

查看內核日誌
tail -f /var/log/message

[root@ecs-t6-large-2-linux-20190824103606 ~]# tail -f /var/log/messages  Sep 15 07:15:30 ecs-t6-large-2-linux-20190824103606 nm-dispatcher: req:1 'dhcp4-change' [eth0]: new request (2 scripts)  Sep 15 07:15:30 ecs-t6-large-2-linux-20190824103606 nm-dispatcher: req:1 'dhcp4-change' [eth0]: start running ordered scripts...  Sep 15 08:00:26 ecs-t6-large-2-linux-20190824103606 systemd: Starting Docker Cleanup...  Sep 15 08:00:26 ecs-t6-large-2-linux-20190824103606 systemd: Started Docker Cleanup.  Sep 15 08:01:01 ecs-t6-large-2-linux-20190824103606 systemd: Started Session 312 of user root.  Sep 15 09:00:04 ecs-t6-large-2-linux-20190824103606 systemd: Starting Docker Cleanup...  Sep 15 09:00:04 ecs-t6-large-2-linux-20190824103606 systemd: Started Docker Cleanup.  Sep 15 09:01:01 ecs-t6-large-2-linux-20190824103606 systemd: Started Session 313 of user root.  Sep 15 09:03:59 ecs-t6-large-2-linux-20190824103606 systemd: Started Session 314 of user root.  Sep 15 09:03:59 ecs-t6-large-2-linux-20190824103606 systemd-logind: New session 314 of user root.

日誌記錄的一般格式如下

Sep 15 09:03:59 ecs-t6-large-2-linux-20190824103606 systemd-logind: New session 314 of user root.      時間            主機名                          子系統名            消息欄位

日誌的分類

內核及系統日誌

系統日誌由 rsyslog統一管理,日誌的格式也基本相同

查看系統上rsyslog的版本

[root@ecs-t6-large-2-linux-20190824103606 ~]# rpm -q rsyslog  rsyslog-8.24.0-34.el7.x86_64

查看rsyslog的位置

[root@ecs-t6-large-2-linux-20190824103606 ~]# whereis rsyslog  rsyslog: /usr/lib64/rsyslog /etc/rsyslog.d /etc/rsyslog.conf
  • 主程式 /etc/rsyslog.d
  • 配置文件 /etc/rsyslog.conf

配置文件中可以配置日誌的級別/記錄方式等

/etc/rsyslog.conf的配置規則

查看配置文件 cat /etc/rsyslog.conf 然後截取RULE部分

#### RULES ####  ##########  日誌設備.日誌級別  ##################          #########  消息發送位置  ###############  # Log all kernel messages to the console.  # Logging much else clutters up the screen.  #kern.*                                                 /dev/console    # Log anything (except mail) of level info or higher.  # Don't log private authentication messages!  *.info;mail.none;authpriv.none;cron.none                /var/log/messages    # The authpriv file has restricted access.  authpriv.*                                              /var/log/secure    # Log all the mail messages in one place.  mail.*                                                  -/var/log/maillog      # Log cron stuff  cron.*                                                  /var/log/cron    # Everybody gets emergency messages  *.emerg                                                 :omusrmsg:*    # Save news errors of level crit and higher in a special file.  uucp,news.crit                                          /var/log/spooler    # Save boot messages also to boot.log  local7.*                                                /var/log/boot.log  local2.info                     /tmp/test.log

記錄日誌時,會從上到下讀取配置資訊,只要匹配上就記錄資訊,每命中一個配置,就記錄一下

日誌設備

日誌設備.級別 說明
auth -pam(linux中的認證機制) 產生的日誌
authpriv -ssh,ftp 等登錄資訊的驗證資訊
cron 時間任務相關
kern -內核相關
lpr -列印
mail -郵件
mark(syslog) -rsyslog服務內部的資訊,時間標識
news -新聞組
user -用戶程式產生的相關資訊
uucp -unix to unix copy unix主機之間的相關通訊
local 1~7 自定義日誌設備文件

用戶可以使用local 1~7 自定義設置的設備,及其輸出方式

# 訂製 , info級別以上的local2 日誌,全部記錄到  /tmp/test.log  local2.info                                              /tmp/test.log

修改了日誌文件需要重啟

systemctl restart rsyslog

如果依然無效, vim /etc/selinux/config,將 selinux = disable ,然後重啟

九種日誌級別,自上而下級別由低到高

級別 說明
NONE 什麼都不記錄
EMERG (緊急) 導致主機系統不可用的情況
ALERT(警告) 必須馬上採取解決措施
CRIT (嚴重) 比較嚴重的情況
ERR 運行時的錯誤
WARNING (提醒) 可能影響系統功能的事件
NOTICE(注意) 不會影響系統功能,但是值得注意
INFO 一般資訊
DEBUG 調試資訊

自下而上,資訊記錄的越來越少

三種連接符號

符號 說明
. 記錄大於等於後面級別的日誌
.= 只記錄等於後面級別的日誌
.!= 只記錄不等於後面級別的日誌

日誌的處理方式

處理方式 說明
本地文件 通常就是文件的絕對路徑
印表機 如: /dev/lp0
用戶名稱 顯示給指定的用戶
遠程主機 @XX.XXX.XX.X
* 顯示給所有在線的用戶

用戶日誌

記錄了系統上用戶登錄/登出的相關資訊

用戶日誌 說明
/var/log/lastlog 最近的用戶登錄事件
/var/log/wtmp 用戶的登錄/註銷/及系統的開關機事件
/var/log/utmp 當前登錄的每個用戶的詳細資訊
/var/log/secure 和用戶驗證相關的安全性事件

/var/log 中存放的是系統上各式各樣的日誌文件, text類型的可以直接查看, 還有一些
是data類型的文件,這種文件是被系統上的命令使用的文件, 如 w who 命令

[root@ecs-t6-large-2-linux-20190824103606 ~]# file /var/log/utmp  /var/log/utmp: cannot open (No such file or directory)  [root@ecs-t6-large-2-linux-20190824103606 ~]# file /var/log/wtmp  /var/log/wtmp: data  [root@ecs-t6-large-2-linux-20190824103606 ~]# file /var/log/secure  /var/log/secure: ASCII text  [root@ecs-t6-large-2-linux-20190824103606 ~]# file /var/log/lastlog  /var/log/lastlog: data

用戶登錄分析

who、w、users、last、lastlog

手動記錄日誌

命令: logger

作用: 從命令行直接向系統日誌文件寫入一行資訊

echo hahaha | logger -it "logger test" -p user.info    -p: 指定日誌設備.日誌規則  -it : 描述

查看

[root@ecs-t6-large-2-linux-20190824103606 ~]# tail -1 /var/log/messages  Sep 15 10:19:30 ecs-t6-large-2-linux-20190824103606 journal: logger test[25291]: hahaha  

程式日誌

由各種應用程式獨立控制的日誌,格式不統一

日誌轉儲 logrotate

系統上時時刻刻產生日誌,為了防止它填滿硬碟,需要對它進行轉儲,說白了,就是刪除舊的日誌文件,創建新的日誌文件

查看他的配置文件

cat /etc/logrotate.conf

[root@ecs-t6-large-2-linux-20190824103606 ~]# cat /etc/logrotate.conf  # see "man logrotate" for details  # 每周備份一次  mv  weekly    # 保留4份舊的日誌文件  rotate 4    # 每次 輪轉備份 時,都使用新創建的日誌文件  create    # 新創建的日誌文件,使用日期當後綴  dateext    # 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 and btmp -- we'll rotate them here    # 文件的轉儲規則  /var/log/wtmp {      monthly      create 0664 root utmp      minsize 1M      rotate 1  }    /var/log/btmp {      missingok      monthly      create 0600 root utmp      rotate 1  }  

查看系統中次要的配置文件

[root@ecs-t6-large-2-linux-20190824103606 ~]# ll /etc/logrotate.d/  total 24  -rw-r--r--  1 root root  91 Apr 11  2018 bootlog  -rw-r--r--  1 root root 137 Sep 12 17:14 log_test  -rw-r--r--. 1 root root 136 Jun 10  2014 ppp  -rw-r--r--  1 root root 224 Oct 30  2018 syslog  -rw-r--r--  1 root root 100 Oct 31  2018 wpa_supplicant  -rw-r--r--  1 root root 103 Nov  5  2018 yum

可以仿照/etc/logrotate.d/ 配置自己的日誌文件的轉儲規則

看一下 syslog的規則如下:

[root@ecs-t6-large-2-linux-20190824103606 ~]# cat /etc/logrotate.d/syslog  /var/log/cron  /var/log/maillog  /var/log/messages  /var/log/secure  /var/log/spooler  {      missingok  # 日誌在轉儲期間,出現任何錯誤都ok      sharedscripts # sharedscripts的作用是在所有的日誌文件都輪轉完畢後統一執行一次腳本。如果沒有配置這條指令,那麼每個日誌文件輪轉完畢後都會執行一次腳本。      # 下面的執行的shell 腳本      postrotate      /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true      endscript  }

可以看到了,它裡面配置了 /var/log/ cron定時任務,maillog郵件,message消息,secure用戶登錄登出 及列印服務spooler的日誌轉儲規則

logrotate的參數

參數 說明
compress 通過gzip 壓縮轉儲以後的日誌
nocompress 不需要壓縮時,用這個參數
copytruncate 用於還在打開中的日誌文件,把當前日誌備份並截斷
nocopytruncate 備份日誌文件但是不截斷
create mode owner group 轉儲文件,使用指定的文件模式創建新 的日誌文件
nocreate 不建立新的日誌文件
delaycompress 和 compress 一起使用時,轉儲的日誌文件到 下一次轉儲時才壓縮
nodelaycompress 覆蓋 delaycompress 選項,轉儲同時壓縮
errors address 專儲時的錯誤資訊發送到指定的Email 地址
ifempty 即使是空文件也轉儲,這個是 logrotate 的預設選項。
notifempty 如果是空文件的話,不轉儲
mail address 把轉儲的日誌文件發送到指定的E-mail 地址
nomail 轉儲時不發送日誌文件
olddir directory 轉儲後的日誌文件放入指定的目錄,必須和當前 日誌文件在同一個文件系統
noolddir 轉儲後的日誌文件和當前日誌文件放在同一個目錄下轉儲後的日誌文件和當前日誌文件放在同一個目錄下
prerotate/endscript 在轉儲以前需要執行的命令可以放入這個對之間,這兩個關鍵字必須單獨成行
daily 指定轉儲周期為每天
weekly 指定轉儲周期為每周
monthly 指定轉儲周期為每月
rotate count 指定日誌文件刪除之前轉儲的次數,0 指沒有備 份,5 指保留5 個備份
ootext [+] list 不轉儲指定擴展名的文件,預設的擴展名是:.rpm-orig .rpmsave
size 當日誌文件到達指定的大小時才轉儲,可以指定bytes(預設)以及KB(sizek)或者MB (sizem)