logwatch配置筆記

參考文章:

    https://segmentfault.com/a/1190000002537665

    http://hxl2009.blog.51cto.com/779549/703971


logwatchperl語言寫的。能夠對Linux 的日誌文件進行分析,並自動發送mail給相關處理人員。

Logwatchmail功能是藉助宿主系統自帶的mail server 發郵件的,所以系統需安裝mail server , sendmailpostfix,Qmail

 

環境:CentOS6.7

【說明:在CentOS7上測試時候,接收到的郵件內容自動變成了附件形式,按照網上的方法http://www.shangxueba.com/jingyan/1642876.html也沒解決掉】

 

安裝配置logwatch

yum install logwatch安裝即可。

主要釋放的文件如下:

wKiom1dmqPjCC6I3AAAo1SPK31E706.png


# cd /etc/logwatch/conf/

# cp /usr/share/logwatch/default.conf/logwatch.conflogwatch.conf

# vim logwatch.conf 內容如下:

LogDir = /var/log    # logwatch 會去 /var/log 找你定義的 log 文件
TmpDir = /var/cache/logwatch
 
MailTo = [email protected]    # 多收件人直接用逗號隔開
MailFrom = [email protected]
 
Print = No        #可選項, Yes 會被打印到系統標準輸出, 並且不會以郵件的形式發送到 MailTo設                   # 定的郵箱裏 , No 選項則會發到郵箱中
Range = Yesterday         # 處理什麼時候的日誌,可選All、Yesterday、Today
Detail = Low         # 日誌詳細度, 可選項 Low , Med , High , 或是 0-10數字
 
Output = mail        #可以選擇mail, html or unformatted
 
Service = All        # 監控所有服務 all
Service = "-exim"         # 不監控的服務前面加-, 如 -exim, 即不監控exim 服務,可以寫多條
 
mailer = "mailx -t"  # 我服務器上用的是mailx,所有默認的sendmail -t 被我改掉了。
 
DailyReport = No


爲了發送郵件,還得安裝郵件發送客戶端,如下

yum install mailx

vim /etc/mail.rc 在最後添加個能發送郵件的賬號

set [email protected] smtp=smtp.sina.com
[email protected] smtp-auth-password=xxxx
set smtp-auth=login

 

 

添加後,我們執行/usr/bin/perl/usr/share/logwatch/scripts/logwatch.pl 發送郵件測試。

 

郵件發送沒問題的話,可以添加一條計劃任務讓系統自動發每天早上發郵件。

echo '30 6 * * * /usr/bin/perl/usr/share/logwatch/scripts/logwatch.pl > /dev/null 2>&1' >>/var/spool/cron/root

 

此外,默認在安裝logwatch的時候,在/etc/cron.daily/會自動生成個0logwatch文件。

我們可以刪掉這個/etc/cron.daily/0logwatch文件,反正上面操作中也配置了cron計劃任務。

 


其它:

Logwatch 也可以在命令行上使用:

logwatch -h 即可列出全部的參數

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>: 報告的詳細度,可選項: High, Med, Low , 數字 0-10
--logfile <name>:    日誌文件名
--logdir <name>:  日誌路徑
--service <name>:    服務名稱
--print:             結果輸出到屏幕
--mailto <addr>:  結果通過郵件發送
--archives:          以歸檔格式發送
--save <filename>:   結果保存到本地文件中
--range <range>:  蒐集的日誌時間,可以爲: Yesterday,Today,All,接參數help還可以看到更詳細的參數
--numeric:           不進行DNS反解析,只顯示數字格式的IP
--debug <level>:  Debug級別,High, Med, Low or any #.
--splithosts:        爲每個主機創建一份報告
--multiemail:     將報告發送給多個郵件地址,沒有使用--splithosts參數時,這個選項自動忽略
--output <output type>: 報告的輸出格式,可以是mail、html、unformatted#.
--encode:            使用base64編碼 on output mail.
--no-oldfiles-log: Suppress the logwatch log,which informs about the
                   old files in logwatchtmpdir.
--version: Displays current version.


 

例如:

logwatch --detail High --Service All --rangeAll --print    # 顯示所有日誌
logwatch --service cron --detail High             # 查看 sshd 日誌


 

 

故障排除

 如果沒有收到 Logwatch日誌報告郵件 , 請檢察本地 sendmail 服務是否正常

 

# logwatch --range today    直接將結果發送郵件
# logwatch --range today --print 當天不發送郵件
# logwatch --range today --print --mailto [email protected]  當天並且發送郵件
 
# /usr/share/logwatch/scripts/logwatch.pl --print
# /usr/share/logwatch/scripts/logwatch.pl--mailto [email protected]


 

 

 

摘錄:定製自己要監控的日誌

用一個簡單的例子介紹自定義logwatch的配置方法。

 

首先創建logwatch日誌文件組
/etc/logwatch/conf/logfiles/test.conf

LogFile = /path/to/your/logfile

LogFile = /path/to/your/second/logfile

 

然後創建logwatch服務配置文件
/etc/logwatch/conf/services/test.conf

Title = testtitle     # 日誌文件裏的標題

LogFile = test   # logwatch日誌文件組的名字,通常是對應的配置文件的文件名部分

 

創建logwatch服務過濾器腳本
/etc/logwatch/scripts/services/test

#!/bin/bash

grep -i ERROR

 

上面的腳本會從日誌文件裏過濾出包含ERROR的行。最後,爲新建的腳本添加執行權限:

全選複製放進筆記

chmod+x /etc/logwatch/scripts/services/test

 



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章