nagios+check_logifiles实现日志监控

nagios+check_logifiles实现日志监控

日志检查时我们平时用的非常多的一种监控方式,检查日志我们需要使用nagios插件,比如nagios自带的check_logfile,功能比较有限;我们使用ConSol Labs出品的check_logfiles,它能够处理截断日志,支持宏定义,支持正则等功能,使我们的监控更加灵活。

一.安装

1.安装check_logfiles

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. tar -zxvf check_logfiles-3.6.3.tar.gz   
  2. cd /usr/local/src/ check_logfiles-3.6.3  
  3. ./configure --prefix=/usr/local/nagios/ --with-nagios-user=nagios --with-nagios-group=nagios --with-seekfiles-dir=/usr/local/nagios/var/tmp --with-protocols-dir=/usr/local/nagios/var/tmp --with-perl=/usr/bin/perl --with-gzip=/bin/gzip  
  4. make  
此时可能会报错:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. CDPATH="${ZSH_VERSION+.}:" && cd . && /bin/sh /usr/local/src/check_logfiles-3.6.3/missing autoconf  
  2. aclocal.m4:21: warning: this file was generated for autoconf 2.69.  
  3. You have another version of autoconf.  It may work, but is not guaranteed to.  
  4. If you have problems, you may need to regenerate the build system entirely.  
  5. To do so, use the procedure documented by the package, typically 'autoreconf'.  
  6. configure.ac:4: error: Autoconf version 2.65 or higher is required  
  7. aclocal.m4:278: AM_INIT_AUTOMAKE is expanded from...  
  8. configure.ac:4: the top level  
  9. autom4te: /usr/bin/m4 failed with exit status: 63  
  10. WARNING: 'autoconf' is probably too old.  
  11.          You should only need it if you modified 'configure.ac',  
  12.          or m4 files included by it.  
  13.          The 'autoconf' program is part of the GNU Autoconf package:  
  14.          <http://www.gnu.org/software/autoconf/>  
  15.          It also requires GNU m4 and Perl in order to run:  
  16.          <http://www.gnu.org/software/m4/>  
  17.          <http://www.perl.org/>  
  18. make: *** [configure] 错误 63  
这是由于服务器的autoconf版本问题导致,正如提示说“aclocal.m4:21: warning: this file was generated for autoconf 2.69.” 编译需要autoconf的版本为2.6.9,而我们的版本为

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. [root@nagios monitors]# /usr/bin/autoconf -V  
  2. autoconf (GNU Autoconf) 2.63  
  3. Copyright (C) 2008 Free Software Foundation, Inc.  
  4. License GPLv2+: GNU GPL version 2 or later  
  5. <http://gnu.org/licenses/old-licenses/gpl-2.0.html>  
  6. This is free software: you are free to change and redistribute it.  
  7. There is NO WARRANTY, to the extent permitted by law.  
  8.   
  9. Written by David J. MacKenzie and Akim Demaille.  
因此我们需要升级将autoconf版本升级为2.69.

2.安装autoconf

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. [root@test src]# wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz  
  2. [root@test src]# cd autoconf-2.69  
  3. [root@test src]# ./configure --prefix=/usr  
  4. [root@test src]# make && make install  

注意:我们一定要将其安装到/usr下,否则编译check_logfiles时不会使用新版的autoconf

3.编译安装check_logfiles

make && make install

安装完成后check_logfiles插件将安装到/usr/local/nagios/libexec下,我们需要配置下权限

chown nagios.nagios /usr/local/nagios/libexec/check_logfiles

另外,由于我们检查下是否有/usr/local/nagios/var/tmp这个目录,如果没有的话还要新建,因为我们之前将seekfile及protocols目录安装在此。

至此,安装完毕。

二.配置

首先我们来看下check_logfiles自带的帮助信息

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. [root@nagios src]# /usr/local/nagios/libexec/check_logfiles -h  
  2. This Nagios Plugin comes with absolutely NO WARRANTY. You may use  
  3. it on your own risk!  
  4. Copyright by ConSol Software GmbH, Gerhard Lausser.  
  5.   
  6. This plugin looks for patterns in logfiles, even in those who were rotated  
  7. since the last run of this plugin.  
  8.   
  9. You can find the complete documentation at   
  10. http://labs.consol.de/nagios/check_logfiles/  
  11.   
  12. Usage: check_logfiles [-t timeout] -f <configfile>  
  13.   
  14. The configfile looks like this:  
  15.   
  16. $seekfilesdir = '/opt/nagios/var/tmp';      写状态信息的目录,这里面记录已经检查过的日志内容,相当于历史记录  
  17. # where the state information will be saved.  
  18.   
  19. $protocolsdir = '/opt/nagios/var/tmp';                  写协议信息的目录,这里面记录日志检查的匹配信息  
  20. # where protocols with found patterns will be stored.  
  21.   
  22. $scriptpath = '/opt/nagios/var/tmp';                可调用的脚本或程序  
  23. # where scripts will be searched for.  
  24.   
  25. $MACROS = { CL_DISK01 => "/dev/dsk/c0d1", CL_DISK02 => "/dev/dsk/c0d2" };定义宏,我们可以调用的变量  
  26.   
  27. @searches = (此处为配置文件的内容,我们可以通过配置文件来执行程序,也可以通过在命令行中直接定义。通过配置文件更方便  
  28.   {  
  29.     tag => 'temperature',<span style="white-space:pre">    </span>tag可以理解为一个自定义的标志,它将在生成状态信息或协议信息中作为名字中的一部分使用,并没有实际的意义  
  30.     logfile => '/var/adm/syslog/syslog.log',<span style="white-space:pre"> </span>logfile为所要监控的日志文件  
  31.     rotation => 'bmwhpux',<span style="white-space:pre">   </span>rotation如果有截断日志的话用来定义如何匹配截断日志  
  32.     criticalpatterns => ['OVERTEMP_EMERG', 'Power supply failed'],<span style="white-space:pre">   </span>严重错误,可以匹配一个或多个正则表达式  
  33.     warningpatterns => ['OVERTEMP_CRIT', 'Corrected ECC Error'],<span style="white-space:pre"> </span>警告错误,可以匹配一个或多个正则表达式  
  34.     options => 'script,protocol,nocount',<span style="white-space:pre">    </span>选项列表,我们可以选择启动脚本,写协议,不计数等操作  
  35.     script => 'sendnsca_cmd'<span style="white-space:pre"> </span>脚本的名字  
  36.   },  
  37.   {  
  38.     tag => 'scsi',  
  39.     logfile => '/var/adm/messages',  
  40.     rotation => 'solaris',  
  41.     criticalpatterns => 'Sense Key: Not Ready',  
  42.     criticalexceptions => 'Sense Key: Not Ready /dev/testdisk',  
  43.     options => 'noprotocol'  
  44.   },  
  45.   {  
  46.     tag => 'logins',  
  47.     logfile => '/var/adm/messages',  
  48.     rotation => 'solaris',  
  49.     criticalpatterns => ['illegal key', 'read error.*$CL_DISK01$'],  
  50.     criticalthreshold => 4  
  51.     warningpatterns => ['read error.*$CL_DISK02$'],  
  52.   }  
  53. );  

以上将各个项目统一写到配置文件中,当然也可以将其放入命令行中调用,两种调用方式如下:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. [root@nagios src]# /usr/local/nagios/libexec/check_logfiles  
  2. Usage: check_logfiles [-t timeout] -f <configfile> [--searches=tag1,tag2,...]  
  3.        check_logfiles [-t timeout] --logfile=<logfile> --tag=<tag> --rotation=<rotation>  
  4.                       --criticalpattern=<regexp> --warningpattern=<regexp>  

三.应用

1.我们在被监控端编辑一个配置文件,如:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. [root@usvr-218 var]# vim /usr/local/nagios/var/log.cfg  
  2. @searches = (  
  3.     {  
  4.         tag => 'web_monitor',  
  5.         logfile => '/var/log/web_monitor.log',  
  6.         criticalpatterns => ['nginx has restart','nginx is down'],  
  7.         warningpatterns => ['500','302','502']  
  8.         #options => 'noprotocol'  
  9.     }  
  10. );  
我们定义了一个标志web_monitor,检查的日志文件为/var/log/web_monitor.log,当日志信息中匹配ciriticalpattern中的内容时会报严重错误,当匹配warningcriticals中的内容时会报警告错误;状态信息和协议信息会写入到/usr/local/nagios/var/tmp中,如

log._var_log_web_monitor.log.web_monitor,其中web_monitor就是我们配置中的tag

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. [root@usvr-218 tmp]# cat log._var_log_web_monitor.log.web_monitor   
  2. $state = {  
  3.            'runcount' => 17,  
  4.            'serviceoutput' => '',  
  5.            'logoffset' => 642985,  
  6.            'runtime' => 1431504819,  
  7.            'devino' => '64768:1178440',  
  8.            'privatestate' => {  
  9.                                'runcount' => 17,  
  10.                                'lastruntime' => 1431504220,  
  11.                                'logfile' => '/var/log/web_monitor.log'  
  12.                              },  
  13.            'logtime' => 1431504602,  
  14.            'servicestateid' => 0,  
  15.            'tag' => 'web_monitor'  
  16.          };  
  17.   
  18.   
  19. 1;  
被监控端的check_logfiles配置好了后,我们还需在nrpe.cfg中添加命令

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. command[check_logfile]=/usr/local/nagios/libexec/check_logfiles -f /usr/local/nagios/var/log.cfg  
  2.   
  3. service xinetd reload  

2.被监控端端我们再来看下监控端

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. define service{  
  2.     use                     nrpe-service         ; Name of service template to use  
  3.     host_name               test  
  4.     service_description     web_monitor  
  5.     check_command           check_nrpe!check_logfile  
  6.     check_interval          10    
  7.     notifications_enabled   1     
  8.     service_groups          logfile_check  
  9.     contact_groups          test  
  10.     }    

重启后,就可以看到我们的监控项了



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值