无法通过通过Jenkins触发的PHP exec()调用的nagios check_logwarn插件命令来捕获对日志文件的更改 [英] Unable to capture changes to log file via nagios check_logwarn plugin command invoked via PHP exec() triggered via Jenkins

查看:120
本文介绍了无法通过通过Jenkins触发的PHP exec()调用的nagios check_logwarn插件命令来捕获对日志文件的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 nagios check_logwarn 来捕获对日志文件.

I am using nagios check_logwarn to capture changes to log files.

为了测试我的设置,我一直在以下日志行中手动添加有关的日志文件-

In order to test my setup, I have been manually adding the following log line to the concerned log file -

[Mon Mar 20 14:24:31 2017] [hphp] [12082:7f238d3ff700:32:000001] []
\nFatal error: entire web request took longer than 10 seconds and timed out in /var/cake_1.2.0.6311-beta
app/webroot/openx/www/delivery/postGetAd.php on line 483

上面的内容应该被以下nagios命令捕获,因为它包含关键字"Fatal"

The above should get caught by the following nagios command, because it contains the keyword "Fatal"

/usr/local/nagios/libexec/check_logwarn -d /tmp/logwarn_hiphop_error -p /mnt/log/hiphop/error_`(date +'%Y%m%d')`.log "^.*Fatal*"

输出(按预期)-

Log errors: \nFatal error: entire web request took longer than 10 seconds and timed out in /var/cake_1.2.
0.6311-beta
\nFatal error: entire web request took longer than 10 seconds and timed out in /var/cake_1.2.0.6311-beta

直接运行此命令即可(情况1 ),但似乎通过Jenkins项目触发的PHP exec调用该命令却无法捕获相同的内容(情况2 ).

Running this command directly works (case 1), but it seems invoking the same via a PHP exec which is triggered via a Jenkins project is not catching the same (case 2).

以下是案例2的PHP代码-

Following is the PHP code of case 2 -

$errorLogCommand = '/usr/local/nagios/libexec/check_logwarn -d /tmp/logwarn_hiphop_error -p /mnt/log/hiphop/error_'.$date.'.log "^.*Fatal*"';
$output = exec($errorLogCommand);
file_put_contents('/var/cake_1.2.0.6311-beta/deployment/deployment.log', "\n ".date("Y-m-d H:i:s")." Checked error key words in error_".$date.".log. command -> ".$errorLogCommand, FILE_APPEND);
if($output!="OK: No log errors found")
{
    file_put_contents('/var/cake_1.2.0.6311-beta/deployment/deployment.log', "\n ".date("Y-m-d H:i:s")." - Hiphop errors -> ".$output, FILE_APPEND);

    $failure=true;
    break;
}
else
{
    file_put_contents('/var/cake_1.2.0.6311-beta/deployment/deployment.log', "\n ".date("Y-m-d H:i:s")." - No Error found -> ".$output, FILE_APPEND);
}

以下是输出-

 2017-03-20 14:16:45 Checked error key words in error_20170320.log. command -> /usr/local/nagios/libexec/
check_logwarn -d /tmp/logwarn_hiphop_error -p /mnt/log/hiphop/error_20170320.log "Fatal"
 2017-03-20 14:16:45 - No Error found -> OK: No log errors found

请注意,使用与情况1相同的nagios命令(/usr/local/nagios/libexec/check_logwarn),在这种情况下意外地未检测到日志错误.

Note that with the same nagios command (/usr/local/nagios/libexec/check_logwarn) as in case 1, log error is not detected in this case, unexpectedly.

以下是我对nagios生成的内部跟踪文件的内容的观察-/tmp/logwarn_hiphop_error/mnt_log_hiphop_error_20170320.log-

Following are my observations of the contents of the internal tracker file which nagios generates - /tmp/logwarn_hiphop_error/mnt_log_hiphop_error_20170320.log -

在情况1中检测到错误时,以下是文件中的更改-

When error is detected in case 1, following are the changes in the file -

在运行命令之前

# logwarn 1.0.10 state for "/mnt/log/hiphop/error_20170320.log"
INODENUM="1208110246"
LINENUM="110"
POSITION="111627"
MATCHING="true"

运行命令后

# logwarn 1.0.10 state for "/mnt/log/hiphop/error_20170320.log"
INODENUM="1208110246"
LINENUM="116"
POSITION="112087"
MATCHING="false"

此外,以下是第2种情况下对同一文件的更改-

Also, following are the changes to the same file in case 2 -

在运行php文件之前

# logwarn 1.0.10 state for "/mnt/log/hiphop/error_20170320.log"
INODENUM="1208110246"
LINENUM="102"
POSITION="109329"
MATCHING="true"

之后

# logwarn 1.0.10 state for "/mnt/log/hiphop/error_20170320.log"
INODENUM="1208110246"
LINENUM="110"
POSITION="111627"
MATCHING="true"

我不确定为什么在情况2中MATCHING参数为true,而在情况1中为false.实际上,错误匹配发生在情况1中.

I am not sure why the MATCHING parameter is true in the case 2, whereas in case 1 it is false. In fact, the error matching happened in case 1.

更新

我尝试将命令包装在escapeshellcmd中,以确保不删除正则表达式-

I tried wrapping the command in an escapeshellcmd, to ensure that the regex is not being stripped out -

$output = exec(escapeshellcmd($errorLogCommand)); 

但输出仍然没有变化.

更新2

发现我在手动添加的日志行中有换行符.对于从命令行运行PHP文件的情况,始终删除那些已修复的问题.但是,对于情况2,该问题仍然可以重现,在这种情况下,我通过Jenkins触发了项目,并且该文件在AWS代码部署的一个挂钩中被调用.

Found that I had line breaks in the log line I was manually adding. Removing those fixed it consistently for the case of running the PHP file from command line. However, the problem is still reproducible consistently for the case 2, where I am triggering the project via Jenkins and this file gets called in one of the hooks of AWS code deploy.

好吧,看来这很难解决.对于手动调用PHP文件,该问题已解决,但是在通过Jenkins进行调用时,我仍然始终遇到相同的问题.

Well, it seems this is not going to get solved so easily. The problem got fixed for manual invocation of the PHP file, but on invocation via Jenkins, I am still getting the same problem consistently.

推荐答案

logwarn 文档提到了对否定检查表达式的支持.

The logwarn documentation mentions support for a negative checking expression.

请尝试在模式字符串之前添加一个感叹号(!),以排除而不是包含这些匹配项

Please try pre-pending an exclamation mark (!) before the pattern string to exclude rather than include these matches

这篇关于无法通过通过Jenkins触发的PHP exec()调用的nagios check_logwarn插件命令来捕获对日志文件的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆