深入分析linux 記錄檔

來源:互聯網
上載者:User


概述

記錄檔的概念很好理解,就是什麼時間,誰,在哪裡裡,做了什麼事。專門有一個叫做syslog(ubuntu採用的拓展的rsyslog)的程式來負責收集系統產生的這些資訊,然後按照規則把他們寫道特定的地方,這些地方包括檔案,控制台,印表機,遠程主機等。由於記錄檔記錄資訊逐漸增多,就需要有程式管理記錄檔,這就是logrotate。對於這兩個程式的認識主要對設定檔的瞭解。但是最重要的不是這些,而是要能看懂日誌資訊,這就要對系統,對服務等有個全面的認識,這樣才能知道記錄檔裡說的是怎麼一回事。

syslog配置

文法

文法:服務名稱 [運算子] 資訊等級         記錄資訊的位置

服務名稱

服務名稱:該服務產生的訊息會被紀錄的意 思。syslog 認識的服務主要有底下這些:

auth, authpriv:主要與認證有關的機制,例如telnet, login, ssh 等需要認證的服務都是使用此一機制;
cron:例行性命令 cron/at 等產生訊息記錄的地方;
daemon:與各個 daemon 有關的訊息;
kern:核心 (kernel) 產生訊息的地方;
lpr:列印相關的訊息!
mail:只要與郵件收發有關的訊息紀錄都屬於這個;
news:與新聞群組伺服器有關的東西;
syslog:syslogd 這支程式本身產生的資訊啊!
user, uucp, local0 \~ local7:與 Unix like 機器本身有關的一些訊息。
運算子

運算子限制了記錄資訊的範圍

. :代表比後面還要高的等級(含該等級)都被記錄下來的意思, 例如:mail.info 代表只要是 mail 的資訊,而且該資訊等級高於 info (含info )時,就會被記錄下來;
.=:代表所需要的等級就是後面接的等級而已;
.!:代表不等於。
資訊等級

資訊等級:總共分成下列幾種等級:

info : 提示一些訊息資料;
notice : 注意!需要比較留意的訊息;
waring 或 warn : 警示的訊息,以上三個訊息都還是沒有錯誤的情況,雖然是需要留意,但是還沒有到錯誤的情況;
err 或 error : 呀!錯誤訊息出現了!該要檢驗錯誤的問題發生原因了;
crit : 臨界點了!再不處理可就傷腦筋了!
alert : 錯誤訊息一再地警警示告!你將要完蛋了!
emerg 或 panic : 阿!系統已經進入混亂的階段!真的是完蛋了~~
特殊等級:例如 debug (顯示較多的資訊!)及 none (不要記錄該服務的內容!)等
記錄日誌位置

記錄資訊的位置

檔案的絕對路徑:通常就是放在 /var/log 裡頭的檔案啦!如果絕對路徑後跟“-”表示非同步寫入,這樣能提升效能。
印表機或其它裝置:例如 /dev/lp0 這個印表機裝置,/dev/console指控制台
使用者名稱:如root,顯示給使用者??×磽餿綣??就是告訴所有使用者
遠程主機:例如 @test.adsldns.org
設定檔樣本

# 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                                                 *

# 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
logrotate配置

/etc/logrotate.conf和/etc/logrotate.d/*

這是一個預設設定檔,也就是如果具體的服務沒有具體設定就採用此預設項

# see "man logrotate" for details
# rotate log files weekly
weekly
# keep 4 weeks worth of backlogs
rotate 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 {
monthly
minsize 1M
create 0664 root utmp
rotate 1
}
# system-specific logs may be also be configured here.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.