Search code examples
regexlogstashelastic-stacklogstash-grok

Logstash filter: compare time using regex


I use this code in logstash filter to compare time but don't work.

if [timecheck] =~ /.*((\[0\]\[0-6\]):\[0-5\]\[0-9\]:\[0-5\]\[0-9\])|((\[1\]\[2-9\]|2\[0-3\]):\[0-5\]\[0-9\]:\[0-5\]\[0-9\]).*/ {
  mutate {
    add_tag => "OVERTIME"
  }
}
else if [timecheck] =~ /.+/ {
  mutate {
    add_tag => "WORKING-HOURS"
  }
}
else {
  mutate { add_tag => "NO-TIMECHECK-MATCH" }
}

logstash work but regex not match. Always enter in WORKING-HOURS because is not empty

(I try regex on regexr.com and work well)


Solution

  • Don't escape the square brackets.

      if [timecheck] =~ /(([0][0-6]):[0-5][0-9]:[0-5][0-9])|(([1][8-9]|2[0-3]):[0-5][0-9]:[0-5][0-9])/ {
        mutate {
          add_tag => "OVERTIME"
          add_field => { "time-work" => "OVERTIME" }
        }
      }
      else if [timecheck] =~ /.+/ {
        mutate {
          add_tag => "WORKING-HOURS"
          add_field => { "time-work" => "WORKING-HOURS" }
        }
      }
      else {
        mutate { add_tag => "NO-TIMECHECK-MATCH" }
      }