grok模式从Logstash中的日志文件中获取具有特定单词的行

时间:2016-09-01 12:25:07

标签: logstash logstash-grok logstash-forwarder

我正在尝试使用Logstash来记录很少的自定义文件,为此我必须得到包含单词的行:“out of swap memory”。以下是/etc/logstash/conf.d/10-syslog.conf文件的内容,其中我为自定义日志文件创建了custom_error类型过滤器,我需要记录该文件以获取所需的行:

 filter {
  if [type] == "syslog" {
    grok {
      match => { "message" => "%{SYSLOGTIMESTAMP:syslog_timestamp} %{SYSLOGHOST:syslog_hostname} %{DATA:syslog_program}(?:\[%{POSINT:syslog_pid}\])?: %{GREEDYDATA:syslog_message}" }
      add_field => [ "received_at", "%{@timestamp}" ]
      add_field => [ "received_from", "%{host}" ]
    }
    syslog_pri { }
    date {
      match => [ "syslog_timestamp", "MMM  d HH:mm:ss", "MMM dd HH:mm:ss" ]
    }
  }
  if [type] == "custom_error" {
    grok {
      match => { "message" => "Exception java.lang.OutOfMemoryError: requested %{NUMBER:int} bytes for promotion. Out of swap space?" }
      add_field => [ "received_at", "%{@timestamp}" ]
      add_field => [ "received_from", "%{host}" ]
    }
  date {
    match => [ "syslog_timestamp", "MMM  d HH:mm:ss", "MMM dd HH:mm:ss" ]
  }
 }
}

但是我仍然没有得到理想的结果,许多其他消息以及所需的行也出现了,这使得日志消息过于冗长。 如何从日志文件中获得所需的行?

1 个答案:

答案 0 :(得分:1)

如果我理解您的问题,您可以在logstash配置中使用条件 文档中解释了here

因此,如果您想要专门过滤包含单词list2的行,您可以使用:

public static void smartCombine(ArrayList<Integer> list1, ArrayList<Integer> list2) {
    for(Integer i : list1) {
        if(list2.contains(i)) {
            list2.remove(i);
        } else {
            list2.add(i);
        }
    }
    // if you don't need the union, don't add the line below and
    // list1 will contain [4, 3] otherwise it will contain [4, 3, 5, 10, 7]
    list1.addAll(list2);
}

public static void main(String[] args) {
    ArrayList<Integer> list1 = new ArrayList<Integer>();
    ArrayList<Integer> list2 = new ArrayList<Integer>();

    Collections.addAll(list1, 4, 3);
    Collections.addAll(list2, 5, 10, 4, 3, 7);

    // Remove comment when method ready
    smartCombine(list1, list2);
    System.out.println(list1);
    System.out.println(list2);

    // list1 will the contain the union of both lists [4, 3, 5, 10, 7]
    // list2 will have symmetric difference [5, 10, 7]
}