无法对 bash 上的 nagios 插件输出应用否定先行断言

Unable to apply negative lookahead assertion to nagios plugin output on bash

以下正则表达式(通过 regex101.com 尝试 PCRE/PHP 正则表达式的风格)匹配给定日志文件中不包含代码 201 和 204 的行 -

正则表达式

/^(?!.*HTTP\/1.1\"\s*(201|204)).*$/gm

示例日志文件

127.0.0.1 - - [20/Oct/2016:11:35:08 +0000]  "GET //fam/shared_generate_LDA_compliant.php HTTP/1.1" 200 171 "-" "curl/7.40.0" - 0.002
127.0.0.1 - - [20/Oct/2016:11:35:34 +0000]  "GET /fam/update_daily_cap_in_memcache_contents.php HTTP/1.1" 200 64 "-" "curl/7.40.0" - 2.032
127.0.0.1 - - [20/Oct/2016:11:36:01 +0000]  "GET //fam/audience_pixel_cache_generator.php HTTP/1.1" 200 229001 "-" "curl/7.40.0" - 0.063
127.0.0.1 - - [20/Oct/2016:11:36:01 +0000]  "GET //fam/shared_generate_banner_campaign_assoc.php HTTP/1.1" 200 302 "-" "curl/7.40.0" - 0.406
127.0.0.1 - - [20/Oct/2016:11:36:02 +0000]  "GET /fam/update_daily_cap_in_memcache_contents.php HTTP/1.1" 200 64 "-" "curl/7.40.0" - 0.888
127.0.0.1 - - [20/Oct/2016:11:36:32 +0000]  "GET /fam/update_daily_cap_in_memcache_contents.php HTTP/1.1" 200 64 "-" "curl/7.40.0" - 0.965
127.0.0.1 - - [20/Oct/2016:11:37:01 +0000]  "GET //fam/audience_pixel_cache_generator.php HTTP/1.1" 200 229001 "-" "curl/7.40.0" - 0.021

我想在此 nagios 插件命令上应用相同的命令,但它不起作用 -

/usr/local/nagios/libexec/check_logwarn -d /tmp/logwarn_nginx -p /mnt/log/nginx/access_`(date +'%Y%m%d')`_`(date +'%H')`.log "^(?!.*HTTP\/1.1\"\s*(201|204)).*$"

抛出错误 -

bash: !.*HTTP\/1.1\"\s*: event not found

以下正则表达式与 nagios 插件命令一起使用,它找出包含代码 200 或 201 的行 -

/usr/local/nagios/libexec/check_logwarn -d /tmp/logwarn_nginx -p /mnt/log/nginx/access_`(date +'%Y%m%d')`_`(date +'%H')`.log ".*HTTP/1.1\"\s*(200|201)"

开盘赏金

如何在 POSIX 正则表达式风格中不使用前瞻来匹配不包含代码 201 和 204 的行?

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

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

/usr/local/nagios/libexec/check_logwarn -d /tmp/logwarn_nginx /mnt/log/nginx/access_(date +'%Y%m%d')_(date +'%H').log '!.*HTTP/1.1\"\s*(205|201)'

更新 - 根据下面的评论更正了上面的命令。