linux 系統日誌的分析大全

來源:互聯網
上載者:User

轉自:http://www.itokit.com/2012/0602/74289.html

日誌分類:

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. 系統和服務日誌

  系統Log Service是由一個名為syslog的服務管理的,如一下記錄檔都是由syslogLog Service驅動的:

  /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

  …...

LinuxLog Service介紹

1. 在Linux系統,大部分日誌都是由syslogLog Service驅動和管理的
      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錯誤層級(危險狀態)的所有訊息類型發給所有使用者

LinuxLog Service器配置

  此伺服器的配置非常簡單,只是修改一個檔案的一個地方,然後重啟服務即可:

  [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這台Log Service器,把一台ubuntu系統的所有

  info層級的auth資訊發給Log Service器,那麼對於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廣泛使用的日誌系統。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.