简体   繁体   中英

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)

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" }
  }  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM