歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> LogWatch介紹

LogWatch介紹

日期:2017/2/27 15:58:48   编辑:Linux教程
在維護Linux服務器時,經常需要查看系統中各種服務的日志,以檢查服務器的運行狀態。如登陸歷史、郵件、軟件安裝等日志。系統管理員一個個去檢查會十分不方便;且大多時候,這會是一種被動的檢查,即只有在發現系統運行異常時才會想到去查看日志以獲取異常的信息。那麼如何主動、集中的分析這些日志,並生產報告,定時發送給管理員就會顯得十分重要。LogWatch即提供了這樣的功能。

本文即介紹LogWatch的簡單使用,更詳細的用法請參見Logwatch — a syslog analyzer written in Perl。
安裝與配置
os kernel
[root@dev ~]# uname -a
Linux dev.egolife.com 2.6.32-358.el6.x86_64 #1 SMP Fri Feb 22 13:35:02 PST 2013 x86_64 x86_64 x86_64 GNU/Linux
yum install
[root@dev ~]# yum install -y logwatch
... ...
================================================================
Installing:
 logwatch            noarch   7.3.6-49.el6     base       297 k
Installing for dependencies:
 perl-Date-Manip     noarch   6.24-1.el6       base       1.3 M
 perl-YAML-Syck      x86_64   1.07-4.el6       base       75 k
... ...
在安裝logwatch時,會同時安裝依賴包perl-Date-Manip和perl-YAML-Syck。

初次使用
1.命令幫助
[root@dev ~]# logwatch --help

Usage: /usr/sbin/logwatch [--detail <level>] [--logfile <name>]
   [--print] [--mailto <addr>] [--archives] [--range <range>] [--debug <level>]
   [--save <filename>] [--help] [--version] [--service <name>]
   [--numeric] [--output <output_type>]
   [--splithosts] [--multiemail] [--no-oldfiles-log]

--detail <level>: Report Detail Level - High, Med, Low or any #.
--logfile <name>: *Name of a logfile definition to report on.
--logdir <name>: Name of default directory where logs are stored.
--service <name>: *Name of a service definition to report on.
--print: Display report to stdout.
--mailto <addr>: Mail report to <addr>.
--archives: Use archived log files too.
--save <filename>: Save to <filename>.
--range <range>: Date range: Yesterday, Today, All, Help
                             where help will describe additional options
--numeric: Display addresses numerically rather than symbolically and numerically
           (saves  a  nameserver address-to-name lookup).
--debug <level>: Debug Level - High, Med, Low or any #.
--splithosts: Create a report for each host in syslog.
--multiemail: Send each host report in a separate email.  Ignored if 
              not using --splithosts.
--output <output type>: Report Format - mail, html or unformatted#.
--encode: Use base64 encoding on output mail.
--no-oldfiles-log: Suppress the logwatch log, which informs about the
                   old files in logwatch tmpdir.
--version: Displays current version.
--help: This message.
* = Switch can be specified multiple times...
從以上幫助,可以看出,LogWatch整個原理就是,LogWatch 首先要知道針對哪一個服務, 從這個服務中得到需要處理的 Log 文件信息, 然後這個文件送給過濾腳本處理, 之後把處理後格式化的信息展現出。

2.顯示SSH登陸歷史
[root@dev ~]# logwatch --service sshd --print

 ################### Logwatch 7.3.6 (05/19/07) #################### 
        Processing Initiated: Fri Jun 21 08:47:19 2013
        Date Range Processed: yesterday
                              ( 2013-Jun-20 )
                              Period is day.
      Detail Level of Output: 0
              Type of Output: unformatted
           Logfiles for Host: dev.egolife.com
  ################################################################## 

 --------------------- SSHD Begin ------------------------ 

 Failed logins from:
    172.29.73.125: 2 times


 Received disconnect:
    13: Authentication cancelled by user. : 2 Time(s)

 **Unmatched Entries**
 userauth_pubkey: unsupported public key algorithm: x509v3-sign-rsa : 2 time(s)

 ---------------------- SSHD End ------------------------- 


 ###################### Logwatch End #########################
配置
查看logwatch package的主要文件
[root@dev ~]# rpm -ql logwatch
/etc/cron.daily/0logwatch                   
/etc/logwatch                               
/etc/logwatch/conf
/etc/logwatch/conf/ignore.conf
/etc/logwatch/conf/logfiles
/etc/logwatch/conf/logwatch.conf
/etc/logwatch/conf/override.conf
/etc/logwatch/conf/services
/etc/logwatch/scripts
/etc/logwatch/scripts/services
/usr/sbin/logwatch
/usr/share/doc/logwatch-7.3.6
... ...
/var/cache/logwatch
從以上輸出,可以看出logwatch是以cron job的方式定時運行的,默認在/etc/cron.daily目錄下,即每天運行一次。
[root@dev ~]# cat /etc/cron.daily/0logwatch 
#!/bin/bash

DailyReport=`grep -e "^[[:space:]]*DailyReport[[:space:]]*=[[:space:]]*" /usr/share/logwatch/default.conf/logwatch.conf | head -n1 | sed -e "s|^\s*DailyReport\s*=\s*||"`

if [ "$DailyReport" != "No" ] && [ "$DailyReport" != "no" ]
then
    logwatch
fi
主要配置文件
[root@dev ~]# tree /etc/logwatch/
/etc/logwatch/
├── conf
│   ├── ignore.conf
│   ├── logfiles
│   ├── logwatch.conf     
│   ├── override.conf
│   └── services
└── scripts
    └── services

5 directories, 3 files
  • logwatch.conf 自定義LogWatch主配置,如報告分析時間,級別,收件人等,默認設置在/usr/share/logwatch/default.conf/logwatch.conf文件中。
  • ignore.conf 過濾配置,定義正則表達式,過濾輸出報告內容。
  • override.conf 覆蓋或者重寫配置,針對/etc/logwatch/conf/services下自定義的服務
  • conf/services 自定義需分析日志的Service目錄,默認支持的Service在/usr/share/logwatch/default.conf/services下。
  • logfiles 定義待分析服務的日志路徑,默認配置在/usr/share/logwatch/default.conf/logfiles/下。
  • scripts/services 定義Service的執行腳本。

郵件通知

[root@dev ~]# less /usr/share/logwatch/default.conf/logwatch.conf
# Default person to mail reports to.  Can be a local account or a
# complete email address.  Variable Print should be set to No to
# enable mail feature.
MailTo = root
LogWatch默認將分析的日志報告發送給本機的root用戶,此時要查看則需登陸到服務器上,使用mail指令查看。

另外,也可以將報告發送到外部郵箱,如[email protected],此時需在服務器上配置簡單的郵件服務,如Postfix,Sendmail,編輯/etc/logwatch/conf/logwatch.conf覆蓋MailTo配置,或者在/etc/aliase中定義賬戶別名,使root為[email protected]別名,則LogWatch會將日志報告發送給[email protected],這樣不用登陸到服務器就可以查看日志報告了。

小結
LogWatch安裝後,基本不用配置即可使用,即可達到主動、集中的分析系統日志,並生產報告,定時發送給管理員的目的。
Copyright © Linux教程網 All Rights Reserved