天天看点

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