【问题标题】:logstash : exclude bots from resultlogstash :从结果中排除机器人
【发布时间】:2017-01-20 04:03:39
【问题描述】:

我使用 logstash 将我的 Web 服务器日志存储到弹性搜索引擎中。在我的 logstash 配置文件中,我还使用“useragent”插件来获取漂亮的用户代理信息。所以像这样记录到 ES 中:

    "message": "157.55.XXX.XXX - - [10/Oct/2016:02:24:27 +0200] "GET /handle/boreal:5621?site_name=BOREAL HTTP/1.1" 301 373 "-" "Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)"",
    ...
    "agent": ""Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)""
    "useragent": {
        "name": "bingbot",
        "os": "Other",
        "os_name": "Other",
        "device": "Spider",
        "major": "2",
        "minor": "0"
    } 

如您所见,此请求来自 Microsoft BingBot 机器人。我检查了所有记录,发现很多机器人访问我的网站:bingbot、googlebot、BaiduSpider、Yahoo!啜饮,...

我现在正试图从我的 ES 响应中排除这些请求。但我没有找到一个优雅的解决方案。由于我是 ES 查询的初学者,您能帮我改进我的请求吗?

{                                               
  "size": 0,                                    
  "query": {                                    
    "filtered": {                               
      "query": {                                
        "match_all": {}                         
      },                                        
      "filter": {                               
        "bool": {                               
          "must_not": [                         
            {                                   
              "regexp": {                       
                "useragent.name": ".*bot.*"     
              }                                 
            }                                   
          ]                                     
        }                                       
      }                                         
    }                                           
  },                                            
  "aggs": {                                     
    "agent": {                                  
      "terms": {                                
        "field": "useragent.name.raw"           
      }                                         
    }                                           
  }                                             
}                                               

使用此请求,来自 bingbot、googlebot 或任何其他“stuffbot”的所有请求都将被排除,但“Yahoo! Slurp”、“BaiduSpider”、... 我尝试使用更复杂的正则表达式“(.*bot.*|BaiduSpider|Yahoo! Slurp)”,但百度和雅虎的结果仍然出现在 ES 响应中。

【问题讨论】:

    标签: elasticsearch logstash bots


    【解决方案1】:

    如果您已经在使用useragent logstash filter,您可以从出色且维护良好的ua-parser 中受益。它可以识别很多机器人并将它们标记为“useragent.device:Spider”。 一个适合 elasticsearch 的查询可能是:

    {
      "query": {
        "bool": {
          "must_not": {
            "term": { "useragent.device": "Spider" }
          }
        }
      }
    }
    

    但是,您可能需要定期更新过滤器插件,例如使用:

    logstash-plugin update logstash-filter-useragent
    

    【讨论】:

      【解决方案2】:

      must_not 中使用多个对象怎么样,这是一个数组。每个机器人从结果中排除一个。

      来自文档:

      must_not:所有这些子句都不能匹配。等价于 NOT。

      类似这样的:

      "filter": {                               
        "bool": {                               
          "must_not": [
            {
              "regexp": { "useragent.name": "regex for bing bot" }                                 
            },
            {
              "regexp": { "useragent.name": "regex for google bot" }                                 
            },
            ...
          ]
        }
      }
      

      如果用户代理是静态的,您可以完全避免使用regexpterm

      {
        "term" : { "useragent.name": "bing bot agent name" } 
      }
      

      【讨论】:

        【解决方案3】:

        一旦你有了必要的正则表达式,你就可以把它们放入logstash并让它标记事件。这将使您的查询更短、更易于阅读且速度更快。

        【讨论】:

        • 如果出现新机器人,则使用此解决方案,我需要更新 logstash 配置并重新解析文件。这就是我更喜欢 ElasticSearch 解决方案的原因。
        • 通过查询,您仍然必须意识到新机器人的存在,更新您的查询,并重新发布基于旧查询的数据/报告。使用 logstash,您无需使用 logstash 重新加载整个文件,但可以通过查询进行更新或使用 logstash 查询 elasticsearch 并使用新代理标记任何文档。您的查询会更清晰,并且您将有一个地方来维护代理列表。到处都有取舍。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-18
        • 1970-01-01
        • 2019-08-09
        • 2012-04-28
        相关资源
        最近更新 更多