天天看點

Logwatch日志分析工具

介紹:

Logwatch是使用 Perl 開發的一個日志分析工具。

Logwatch能夠對Linux 的日志檔案進行分析,并自動發送mail給相關處理人員,可定制需求。

Logwatch的mail功能是借助宿主系統自帶的mail server 發郵件的,是以系統需安裝mail server , 如sendmail,postfix,Qmail等

安裝:

<code>redhat,centOS: yum -y </code><code>install</code> <code>logwatch</code>

<code>debian,ubuntu: apt-get </code><code>install</code> <code>logwatch</code>

目錄結構:

<code>/usr/share/logwatch</code>

<code>    </code><code>default.conf/     </code><code># 配置目錄</code>

<code>        </code><code>logwatch.conf   </code><code># 主配置檔案,收件人,級别等</code>

<code>        </code><code>logfiles/       </code><code># 定義待分析服務的日志檔案組路徑,相對于/var/log(*.conf)</code>

<code>        </code><code>services/       </code><code># 自定義需分析日志的Service目錄(*.conf)</code>

<code>    </code><code>scripts/          </code><code># 可執行腳本</code>

<code>        </code><code>logwatch.pl     </code><code># 啟動分析的perl腳本,/usr/sbin/logwatch的源連結</code>

<code>        </code><code>logfiles/       </code><code># 可包含多個logwatch日志檔案組的子目錄,對應的日志服務運作的時候,子目錄下的腳本會自動被調用</code>

<code>        </code><code>services/       </code><code># logwatch日志服務的過濾腳本,一一對應</code>

<code>        </code><code>shared/         </code><code># 可被多個logwatch日志服務引用的腳本</code>

<code>    </code><code>dist.conf/</code>

<code>        </code><code>logfiles/</code>

<code>        </code><code>services/</code>

<code>    </code><code>lib/</code>

預設情況下使用的是/usr/share/logwatch/default.conf/logwatch.conf作為主配置檔案,但在/etc/logwatch/conf/logwatch.conf中的存在配置選項會覆寫前一個(/usr/share/logwatch下的logwatch.conf還是會起作用,比如在/etc/logwatch的logwatch.conf中沒有的選項)。但優先級最高的是在執行指令行中指定的選項。

在/etc/logwatch下也存在一個與/usr/share/logwatch類似的目錄結構,可以在這裡添加自定義的監控日志資訊。

配置結構:

這三個目錄的内容是/usr/share/logwatch/default.conf,/usr/share/logwatch/dist.conf和/etc/logwatch/conf,都有相同結構:

services:此子目錄包含配置特定于每個服務的檔案。Logwatch确定哪些服務可用檢查此目錄的内容。每個服務配置檔案由其服務名稱帶有“.conf”字尾。

logfiles:此子目錄包含日志檔案組配置檔案。每個日志檔案組配置檔案包含有關資訊一個或多個具有相同格式的日志檔案。幾個服務可能使用相同的日志檔案組配置檔案。每一個配置檔案由組命名名稱與“.conf”字尾。許多的組名取自該名稱的系統日志檔案(如消息,郵件日志,安全等),但不總是。

logwatch.conf:此檔案包含預設值整體執行Logwatch,并影響全部的服務。它的許多參數都可以當指令行開關被覆寫調用Logwatch可執行檔案。

ignore.conf:該檔案指定正規表達式,當與logwatch的輸出比對時,會抑制比對線,無論哪一個正在執行服務。

/etc/logwatch/conf目錄也可能包含檔案'override.conf'

可執行結構:

兩個目錄的内容/usr/share/logwatch/scripts和/etc/logwatch/scripts具有相同的結構:

services:此子目錄包含可執行檔案為每項服務。除非另有規定在配置服務檔案(見上文)中,可執行檔案是用perl語言編寫的。

shared:此子目錄包含可執行檔案由多個配置服務調用檔案。

logfiles:此子目錄可能包含子目錄日志檔案組名。每個可執行檔案這些子目錄自動被調用運作使用相應的服務日志檔案組名。

編輯配置:

cp /usr/share/logwatch/default.conf/logwatch.conf /etc/logwatch/conf/logwatch.conf

<code>vim </code><code>/etc/logwatch/conf/logwatch</code><code>.conf   </code><code>#配置檔案中不區分大小寫</code>

<code>MailTo = root       </code><code>#指定電子郵件位址,每日發送報告,多郵箱逗号隔開</code>

<code>LogDir = </code><code>/var/log</code>   <code>#定義日志檔案位置</code>

<code>TmpDir = </code><code>/var/cache/logwatch</code>    <code>#緩存</code>

<code>MailFrom = Logwatch  </code><code>#郵件發送來源</code>

<code>Print =            </code><code>#選項yes會列印到系統輸出,不會發送到設定郵箱,NO則會發送到郵箱</code>

<code>Range = yesterday  </code><code>#接受範圍,如yesterday昨天或today今天或all所有</code>

<code>#Range = all   #當配置所有日志時,必須配上archive = yes 将會處理所有的包括已存檔的日志</code>

<code>#Archive = yes</code>

<code>Detail = High    </code><code>#等級,低:Low=0,中:Med=5,高:High = 10  字母或數字都可以</code>

<code>Service = All    </code><code>#所有服務,或指定某個服務</code>

<code>Service = </code><code>"-zz-network"</code>    <code>#在前面加“-”表示不監控服務</code>

<code>Service = </code><code>"-zz-sys"</code>         <code>#Service選項指定想要監控的一個或多個服務。在/usr/share/logwatch/scripts/services目錄下列出的服務都能被監控</code>

<code>Service = </code><code>"-eximstats"</code>      

<code>mailer = </code><code>"sendmail -t"</code>  <code>#指定發送郵件的用戶端</code>

<code>#DailyReport = No  #禁止每日執行任務</code>

<code>#Output = &lt;mail,html,unformattted&gt;  #指定輸出格式</code>

<code>#Save = /tmp/logwatch   #将會把輸出報錯到檔案中</code>

使用:

檢測指令

/usr/bin/perl /usr/share/logwatch/scripts/logwatch.p

選項:

--detail&lt;報告詳細程度&gt;: 指定日志報告的詳細程度; 

--logfile&lt;日志檔案&gt;: 僅處理指定的日志檔案; 

--service&lt;服務名&gt;: 僅處理指定服務的日志檔案; 

--print: 列印結果到标準輸出; 

--mailto&lt;郵件位址&gt;: 将結果發送到指定郵箱; 

--range&lt;日期範圍&gt;: 指定處理日志的日期範圍; 

--archives: 處理歸檔日志檔案; 

--debug&lt;調試等級&gt;: 調試模式; 

--save&lt;檔案名&gt;: 将結果儲存到指定檔案中,而不顯示或者發送到指定郵箱; 

--logdir&lt;目錄&gt;: 指定查找日志檔案的目錄,而不使用預設的日志目錄; 

--hostname&lt;主機名&gt;: 指定在日志報告中使用的主機名,不使用系統預設的主機名; 

--numeric: 在報告中顯示ip位址而不是主機名; 

--help: 顯示指令的幫助資訊。

執行個體:

<code>logwatch --detail Low --mailto email@address --service http --range today   </code><code>#将今天的http LOW日志發送到郵箱</code>

<code>logwatch --detail High --service all --range all --print    列印所有服務日志</code>

<code>logwatch --service sshd --detail high --print   列印SSHD錯誤日志</code>

定制自己要監控的日志

首先建立logwatch日志檔案組:

<code>vim </code><code>/etc/logwatch/conf/logfiles/test</code><code>.conf</code>

<code>LogFile = </code><code>/usr/local/nginx/logs/nginx</code><code>.log</code>

然後建立logwatch服務配置檔案:

<code>vim </code><code>/etc/logwatch/conf/services/test</code><code>.conf</code>

<code>Title = </code><code>test</code> <code>title   </code><code>#日志檔案的标題</code>

<code>LogFile = </code><code>test</code>    <code>#日志檔案組的名字,即上面建立的日志檔案組test.conf</code>

建立logwatch服務過濾器腳本:

<code>vim </code><code>/etc/logwatch/scripts/services/test</code>

<code>cat</code> <code>/etc/logwatch/scripts/services/test</code> 

<code>#!/bin/sh</code>

<code>grep</code> <code>-i ERROR | </code><code>wc</code> <code>-l</code>

#此處實驗隻是簡單的寫了腳本,工作中可根據需要編寫

給腳本檔案可執行權限:

<code>chmod</code> <code>+x </code><code>/etc/logwatch/scripts/services/test</code>

測試輸出效果:

<code>logwatch --service </code><code>test</code> <code>--print    </code><code>#test為服務名</code>

<code> </code><code>################### Logwatch 7.3.6 (05/19/07) #################### </code>

<code>        </code><code>Processing Initiated: Mon May 22 14:09:22 2017</code>

<code>        </code><code>Date Range Processed: yesterday</code>

<code>                              </code><code>( 2017-May-21 )</code>

<code>                              </code><code>Period is day.</code>

<code>      </code><code>Detail Level of Output: 10</code>

<code>              </code><code>Type of Output: unformatted</code>

<code>           </code><code>Logfiles </code><code>for</code> <code>Host: zabbix</code>

<code>  </code><code>################################################################## </code>

<code> </code> 

<code> </code><code>--------------------- </code><code>test</code> <code>title Begin ------------------------ </code>

<code> </code><code>1022</code>

<code> </code><code>---------------------- </code><code>test</code> <code>title End ------------------------- </code>

<code> </code><code>###################### Logwatch End #########################</code>

版權聲明:原創作品,如需轉載,請注明出處。否則将追究法律責任

本文轉自 80後小菜鳥 51CTO部落格,原文連結:http://blog.51cto.com/zhangxinqi/1928208